Jumar.Types.TypeId (Jumar v0.1.0)

View Source

An 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.

t()

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

params()

@type params() :: %{prefix: prefix(), uuid_module: module()}

The parameters for a type id. This only includes the prefix itself.

prefix()

@type prefix() :: binary()

A lowercase ASCII string under 63 characters long. The part before the underscore of a Type Id.

t()

@type t() :: binary()

A type id string.

uuid()

@type uuid() :: <<_::128>>

A string representation of a UUID.

Functions

cast(data, arg2)

@spec cast(term(), params()) :: {:ok, t()} | :error

Casts a given input to a type id.

crockford_decode32(arg1)

@spec crockford_decode32(binary()) :: binary() | :error

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

crockford_encode32(arg1)

@spec crockford_encode32(binary()) :: binary() | :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

dump(data, dumper, arg3)

@spec dump(any(), function(), params()) :: {:ok, any()} | :error

Dumps the given type id to an Ecto native type.

equal?(term1, term2, params)

@spec equal?(any(), any(), params()) :: boolean()

Checks if the two type ids are equal. This is a basic == equality checker.

generate(map)

@spec generate(params()) :: t()

Generates a new type id.

init(opts)

@spec init(Ecto.ParameterizedType.opts()) :: params()

Converts the given options to t:params. Raises if the prefix is missing or not binary.

load(uuid, loader, arg3)

@spec load(any(), function(), params()) :: {:ok, any()} | :error

Loads data from the database to a type id.

type(params)

@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.