View Source TypeID (TypeID Elixir v0.2.2)

TypeID Elixir

CI

Read the full documentation on hexdocs

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, "~> 0.2.1"}
  ]
end

spec

Spec

The original TypeID spec is defined here.

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 base 32 encoded suffix of the given t/0

Returns a string representation of the given t/0

Returns the type 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()) :: t()

Generates a new t/0 with the given prefix.

example

Example

iex> TypeID.new("acct")
#TypeID<"acct_01h45y0sxkfmntta78gqs1vsw6">
@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_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 type(tid :: t()) :: String.t()

Returns the type of the given t/0.

example

Example

iex> tid = TypeID.new("doc")
iex> TypeID.type(tid)
"doc"
@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>>