UXID (UXID v2.1.0)

View Source

Generates UXIDs and acts as an Ecto ParameterizedType

User eXperience focused IDentifiers (UXIDs) are identifiers which:

  • Describe the resource (aid in debugging and investigation)
  • Work well with copy and paste (double clicking selects the entire ID)
  • Can be shortened for low cardinality resources
  • Are secure against enumeration attacks
  • Can be generated by application code (not tied to the datastore)
  • Are K-sortable (lexicographically sortable by time - works well with datastore indexing)
  • Do not require any coordination (human or automated) at startup, or generation
  • Are very unlikely to collide (more likely with less randomness)
  • Are easily and accurately transmitted to another human using a telephone

Many of the concepts of Stripe IDs have been used in this library.

Summary

Types

Options for generating a UXID

t()

A UXID represented as a String

Functions

Generates a loaded version of the UXID.

Casts the given input to the UXID ParameterizedType with the given parameters.

Dumps the given term into an Ecto native type.

Dictates how the type should be treated if embedded. For UXIDs, we use :self since they're already strings.

Checks if two UXIDs are equal.

Returns an encoded UXID string along with response status.

Returns an unwrapped encoded UXID string.

Converts the options specified in the field macro into parameters to be used in other callbacks.

Loads the given term into a UXID.

Returns a new UXID.Codec struct. This is useful for development.

Returns the underlying schema type for a UXID.

Types

option()

@type option() ::
  {:case, atom()}
  | {:time, integer()}
  | {:size, atom()}
  | {:rand_size, integer()}
  | {:prefix, String.t()}
  | {:delimiter, String.t()}

Options for generating a UXID

options()

@type options() :: [option()]

t()

@type t() :: String.t()

A UXID represented as a String

Functions

autogenerate(opts)

Generates a loaded version of the UXID.

cast(data, params)

Casts the given input to the UXID ParameterizedType with the given parameters.

dump(data, dumper, params)

Dumps the given term into an Ecto native type.

embed_as(format, params)

Dictates how the type should be treated if embedded. For UXIDs, we use :self since they're already strings.

encode_case()

equal?(left, right, params)

Checks if two UXIDs are equal.

generate(opts \\ [])

@spec generate(opts :: options()) :: {:ok, t()}

Returns an encoded UXID string along with response status.

generate!(opts \\ [])

@spec generate!(opts :: options()) :: t()

Returns an unwrapped encoded UXID string.

init(opts)

Converts the options specified in the field macro into parameters to be used in other callbacks.

load(data, loader, params)

Loads the given term into a UXID.

new(opts \\ [])

@spec new(opts :: options()) :: {:ok, UXID.Codec.t()}

Returns a new UXID.Codec struct. This is useful for development.

type(opts)

Returns the underlying schema type for a UXID.