UXID (UXID v0.2.1) 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.

Link to this section 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.

Callback implementation for Ecto.ParameterizedType.embed_as/2.

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.

Link to this section Types

Specs

error_string() :: String.t()

An error string returned by the library if generation fails

Specs

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

Options for generating a UXID

Specs

options() :: [option()]

Specs

t() :: %UXID{
  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

Specs

uxid_string() :: String.t()

A UXID represented as a String

Link to this section Functions

Generates a loaded version of the UXID.

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

Link to this function

dump(data, dumper, params)

View Source

Dumps the given term into an Ecto native type.

Callback implementation for Ecto.ParameterizedType.embed_as/2.

Link to this function

equal?(term1, term2, params)

View Source

Callback implementation for Ecto.ParameterizedType.equal?/3.

Specs

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

Returns an encoded UXID string along with response status.

Specs

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

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.

Link to this function

load(data, loader, params)

View Source

Loads the given term into a UXID.

Specs

new(opts :: options()) :: {:ok, t()} | {:error, error_string()}

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

Returns the underlying schema type for a UXID.