Jumar.Types.TypeId (Jumar v0.1.0)
View SourceAn Elixir implementation of typeid using Jumar.Types.UUIDv7
or another Ecto UUID module. This allows using UUIDs as the database
primary key, while transparently converting to a Stripe like ID
(user_2x4y6z8a0b1c2d3e4f5g6h7j8k
) by Ecto
.
A lot of the code here was taken from the amazing work done by sloanelybutsurely on the typeid-elixir package.
Summary
Types
The parameters for a type id. This only includes the prefix itself.
A lowercase ASCII string under 63 characters long. The part before the underscore of a Type Id.
A type id string.
A string representation of a UUID.
Functions
Casts a given input to a type id.
Base32 decodes a UUID with Crockford's lowercase alphabet.
Base32 encodes a UUID with Crockford's lowercase alphabet.
Dumps the given type id to an Ecto native type.
Checks if the two type ids are equal. This is a basic ==
equality
checker.
Generates a new type id.
Converts the given options to t:params
. Raises if the prefix is missing or
not binary.
Loads data from the database to a type id.
Returns the underlying schema type for a prefixed uuid. This is a basic UUID
under the hood and is stored the same way an Ecto.UUID
would be.
Types
Functions
Casts a given input to a type id.
Base32 decodes a UUID with Crockford's lowercase alphabet.
Examples
iex> "0123456789abcdefghjkmnpqrs"
...> |> crockford_decode32()
...> |> Jumar.Types.UUIDv7.load()
{:ok, "0110c853-1d09-52d8-d73e-1194e95b5f19"}
iex> "01h455vb4pex5vsknk084sn02q"
...> |> crockford_decode32()
...> |> Jumar.Types.UUIDv7.load()
{:ok, "01890a5d-ac96-774b-bcce-b302099a8057"}
iex> crockford_decode32("invalid type id")
:error
Base32 encodes a UUID with Crockford's lowercase alphabet.
Examples
iex> with {:ok, dumped_uuid} <- Jumar.Types.UUIDv7.dump("0110c853-1d09-52d8-d73e-1194e95b5f19") do
...> crockford_encode32(dumped_uuid)
...> end
"0123456789abcdefghjkmnpqrs"
iex> with {:ok, dumped_uuid} <- Jumar.Types.UUIDv7.dump("01890a5d-ac96-774b-bcce-b302099a8057") do
...> crockford_encode32(dumped_uuid)
...> end
"01h455vb4pex5vsknk084sn02q"
iex> crockford_encode32("invalid uuid")
:error
Dumps the given type id to an Ecto native type.
Checks if the two type ids are equal. This is a basic ==
equality
checker.
Generates a new type id.
@spec init(Ecto.ParameterizedType.opts()) :: params()
Converts the given options to t:params
. Raises if the prefix is missing or
not binary.
Loads data from the database to a type id.
@spec type(params()) :: :uuid
Returns the underlying schema type for a prefixed uuid. This is a basic UUID
under the hood and is stored the same way an Ecto.UUID
would be.