View Source TypeID (TypeID Elixir v1.1.0)

TypeID Elixir

CI Hex.pm Documentation

a-type-safe-k-sortable-globally-unique-identifier-inspired-by-stripe-ids

A type-safe, K-sortable, globally unique identifier inspired by Stripe IDs

TypeIDs are a modern, type-safe, globally unique identifier based on the upcoming UUIDv7 standard. They provide a ton of nice properties that make them a great choice as the primary identifiers for your data in a database, APIs, and distributed systems. Read more about TypeIDs in their spec.

installation

Installation

The package can be installed from hex by adding typeid_elixir to your list of dependencies in mix.exs:

def deps do
  [
    {:typeid_elixir, "~> 1.0"}
  ]
end

spec

Spec

The original TypeID spec is defined here.

usage-with-ecto

Usage with Ecto

TypeID implements the Ecto.ParameterizedType behaviour so you can use TypeIDs as fields in your Ecto schemas.

defmodule MyApp.Accounts.User do
  use Ecto.Schema

  @primary_key {:id, TypeID, autogenerate: true, prefix: "acct", type: :uuid}
  @foreign_key_type TypeID

  # ...
end

underlying-types

Underlying types

TypeIDs can be stored as either :string or :uuid. :string will store the entire TypeID including the prefix. :uuid stores only the UUID portion and requires a :uuid or :uuid column.

Default type

The type used can be set globally in the application config.

config :typeid_elixir,
  default_type: :uuid

Link to this section Summary

Types

t()

An internal struct representing a TypeID.

Functions

Parses a t/0 from a prefix and suffix.

Like from/2 but raises an error if the prefix or suffix are invalid.

Parses a t/0 from a string.

Like from_string/1 but raises an error if the string is invalid.

Parses a t/0 from a prefix and a string representation of a uuid.

Like from_uuid/2 but raises an error if the prefix or uuid are invalid.

Parses a t/0 from a prefix and a raw binary uuid.

Like from_uuid_bytes/2 but raises an error if the prefix or uuid_bytes are invalid.

Generates a new t/0 with the given prefix.

Returns the prefix of the given t/0.

Returns the base 32 encoded suffix of the given t/0

Returns an iodata/0 representation of the given t/0.

Returns a string representation of the given t/0

Returns t/0's UUID as a string.

Returns the raw binary representation of the t/0's UUID.

Link to this section Types

@opaque t()

An internal struct representing a TypeID.

Link to this section Functions

@spec from(prefix :: String.t(), suffix :: String.t()) :: {:ok, t()} | :error

Parses a t/0 from a prefix and suffix.

example

Example

iex> {:ok, tid} = TypeID.from("invoice", "01h45ydzqkemsb9x8gq2q7vpvb")
iex> tid
#TypeID<"invoice_01h45ydzqkemsb9x8gq2q7vpvb">
@spec from!(prefix :: String.t(), suffix :: String.t()) :: t() | no_return()

Like from/2 but raises an error if the prefix or suffix are invalid.

@spec from_string(String.t()) :: {:ok, t()} | :error

Parses a t/0 from a string.

example

Example

iex> {:ok, tid} = TypeID.from_string("game_01h45yhtgqfhxbcrsfbhxdsdvy")
iex> tid
#TypeID<"game_01h45yhtgqfhxbcrsfbhxdsdvy">
@spec from_string!(String.t()) :: t() | no_return()

Like from_string/1 but raises an error if the string is invalid.

@spec from_uuid(prefix :: String.t(), uuid :: String.t()) :: {:ok, t()} | :error

Parses a t/0 from a prefix and a string representation of a uuid.

example

Example

iex> {:ok, tid} = TypeID.from_uuid("device", "01890be9-b248-777e-964e-af1d244f997d")
iex> tid
#TypeID<"device_01h45ykcj8exz9cknf3mj4z6bx">
Link to this function

from_uuid!(prefix, uuid)

View Source
@spec from_uuid!(prefix :: String.t(), uuid :: String.t()) :: t() | no_return()

Like from_uuid/2 but raises an error if the prefix or uuid are invalid.

Link to this function

from_uuid_bytes(prefix, uuid_bytes)

View Source
@spec from_uuid_bytes(prefix :: String.t(), uuid_bytes :: binary()) ::
  {:ok, t()} | :error

Parses a t/0 from a prefix and a raw binary uuid.

example

Example

iex> {:ok, tid} = TypeID.from_uuid_bytes("policy", <<1, 137, 11, 235, 83, 221, 116, 212, 161, 42, 205, 139, 182, 243, 175, 110>>)
iex> tid
#TypeID<"policy_01h45ypmyxekaa2apdhevf7bve">
Link to this function

from_uuid_bytes!(prefix, arg)

View Source
@spec from_uuid_bytes!(prefix :: String.t(), uuid_bytes :: binary()) ::
  t() | no_return()

Like from_uuid_bytes/2 but raises an error if the prefix or uuid_bytes are invalid.

@spec new(prefix :: String.t(), Keyword.t()) :: t()

Generates a new t/0 with the given prefix.

Optional: Specify the time of the UUID v7 by passing time: unix_millisecond_time as the second argument.

example

Example

iex> TypeID.new("acct")
#TypeID<"acct_01h45y0sxkfmntta78gqs1vsw6">
@spec prefix(tid :: t()) :: String.t()

Returns the prefix of the given t/0.

example

Example

iex> tid = TypeID.new("doc")
iex> TypeID.prefix(tid)
"doc"
@spec suffix(tid :: t()) :: String.t()

Returns the base 32 encoded suffix of the given t/0

example

Example

iex> tid = TypeID.from_string!("invite_01h45y3ps9e18adjv9zvx743s2")
iex> TypeID.suffix(tid)
"01h45y3ps9e18adjv9zvx743s2"
@spec to_iodata(tid :: t()) :: iodata()

Returns an iodata/0 representation of the given t/0.

examples

Examples

iex> tid = TypeID.from_string!("player_01h4rn40ybeqws3gfp073jt81b")
iex> TypeID.to_iodata(tid)
["player", ?_, "01h4rn40ybeqws3gfp073jt81b"]


iex> tid = TypeID.from_string!("01h4rn40ybeqws3gfp073jt81b")
iex> TypeID.to_iodata(tid)
"01h4rn40ybeqws3gfp073jt81b"
@spec to_string(tid :: t()) :: String.t()

Returns a string representation of the given t/0

example

Example

iex> tid = TypeID.from_string!("user_01h45y6thxeyg95gnpgqqefgpa")
iex> TypeID.to_string(tid)
"user_01h45y6thxeyg95gnpgqqefgpa"
@spec uuid(tid :: t()) :: String.t()

Returns t/0's UUID as a string.

example

Example

iex> tid = TypeID.from_string!("item_01h45ybmy7fj7b4r9vvp74ms6k")
iex> TypeID.uuid(tid)
"01890be5-d3c7-7c8e-b261-3bdd8e4a64d3"
@spec uuid_bytes(tid :: t()) :: binary()

Returns the raw binary representation of the t/0's UUID.

example

Example

iex> tid = TypeID.from_string!("order_01h45y849qfqvbeayxmwkxg5x9")
iex> TypeID.uuid_bytes(tid)
<<1, 137, 11, 228, 17, 55, 125, 246, 183, 43, 221, 167, 39, 216, 23, 169>>
Link to this function

validate_prefix!(prefix)

View Source