Ash.ULID
Ash.Type implementation for ULID.
Consists of three modules:
Ash.ULID- utility functions to generate ULIDs.Ash.Type.ULID-Ash.Typeimplementation.Ash.ULID.Extension-ulid_primary_keyshortcut.
Installation
Add to the deps, get deps (mix deps.get), compile them (mix deps.compile).
def deps do
[
{:ash_uuid, "~> 0.1"},
]
endUsage
Primary key
To use as a primary key in Ash.Resource it is recommended to add Ash.ULID.Extension:
defmodule Example.Resource do
use Ash.Resource,
extensions: [Ash.ULID.Extension]
attributes do
ulid_primary_key :id
end
endWhich is a shortcut for this:
uuid_primary_key :id, type: Ash.Type.ULID, default: &Ash.ULID.generate/0Attribute type
Ash.Type.ULID can be registered under ulid name in a config:
config :ash, custom_types: [ulid: Ash.Type.ULID]And then used like this:
defmodule Example.Another do
use Ash.Resource
attributes do
attribute :key, :ulid
end
relationships do
belongs_to :resource, Example.Resource, attribute_type: :ulid
end
end
Without an alias it is the same, just replace :ulid with Ash.Type.ULID.
Generate
To generate ULID call Ash.ULID.generate/0 or Ash.ULID.generate/1 with a specific timestamp.
References
ULID spec can be found here.
The work is mostly based on Ecto.ULID.