View Source TypeID (TypeID Elixir v0.0.1)
TypeID Elixir
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 by adding typeid
to your list of dependencies in
mix.exs
:
def deps do
[
{:typeid, "~> 0.1.0"}
]
end
spec
Spec
The original TypeID spec is defined here.
Link to this section Summary
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 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
Parses a t/0
from a prefix and suffix.
example
Example
iex> {:ok, tid} = TypeID.from("invoice", "01h45ydzqkemsb9x8gq2q7vpvb")
iex> tid
#TypeID<"invoice_01h45ydzqkemsb9x8gq2q7vpvb">
Like from/2
but raises an error if the prefix
or suffix
are invalid.
Parses a t/0
from a string.
example
Example
iex> {:ok, tid} = TypeID.from_string("game_01h45yhtgqfhxbcrsfbhxdsdvy")
iex> tid
#TypeID<"game_01h45yhtgqfhxbcrsfbhxdsdvy">
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.
example
Example
iex> {:ok, tid} = TypeID.from_uuid("device", "01890be9-b248-777e-964e-af1d244f997d")
iex> tid
#TypeID<"device_01h45ykcj8exz9cknf3mj4z6bx">
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.
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">
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.
example
Example
iex> TypeID.new("acct")
#TypeID<"acct_01h45y0sxkfmntta78gqs1vsw6">
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"
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"
Returns the type of the given t/0
.
example
Example
iex> tid = TypeID.new("doc")
iex> TypeID.type(tid)
"doc"
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"
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>>