View Source TypeID (TypeID Elixir v1.1.0)
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 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
TypeID
s 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
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 a string representation 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.
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">
Returns the prefix of the given t/0
.
example
Example
iex> tid = TypeID.new("doc")
iex> TypeID.prefix(tid)
"doc"
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 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"
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 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>>