UXID (UXID v2.0.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

An error string returned by the library if generation fails

Options for generating a UXID

t()

A UXID struct

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.

Returns an encoded UXID string along with response status.

Returns an unwrapped encoded UXID string or raises on error.

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 struct. This is useful for development.

Returns the underlying schema type for a UXID.

Types

error_string()

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

An error string returned by the library if generation fails

option()

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

Options for generating a UXID

options()

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

t()

@type t() :: %UXID{
  case: atom() | nil,
  encoded: String.t() | nil,
  prefix: String.t() | nil,
  rand: binary() | nil,
  rand_encoded: String.t() | nil,
  rand_size: pos_integer() | nil,
  size: atom() | nil,
  string: String.t() | nil,
  time: pos_integer() | nil,
  time_encoded: String.t() | nil
}

A UXID struct

uxid_string()

@type uxid_string() :: 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.

encode_case()

generate(opts \\ [])

@spec generate(opts :: options()) :: {:ok, uxid_string()} | {:error, error_string()}

Returns an encoded UXID string along with response status.

generate!(opts \\ [])

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

Returns an unwrapped encoded UXID string or raises on error.

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, t()} | {:error, error_string()}

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

type(opts)

Returns the underlying schema type for a UXID.