Skip to content

Latest commit

 

History

History
92 lines (63 loc) · 2.25 KB

README.md

File metadata and controls

92 lines (63 loc) · 2.25 KB

AshUlid

Module Version Hex Docs License

Ash.Type implementation for ULID.

Consists of three modules:

  • AshUlid - utility functions to generate ULIDs.
  • AshUlid.Type - Ash.Type implementation.
  • AshUlid.Resource - resource extension with ulid_primary_key shortcut.

Installation

Add to the deps, get deps (mix deps.get), compile them (mix deps.compile).

def deps do
  [
    {:ash_ulid, "~> 1.0.0"},
  ]
end

Usage

Primary key

To use as a primary key in Ash.Resource it is recommended to add AshUlid.Resource extension that provides ulid_primary_key:

defmodule Example.Resource do
  use Ash.Resource,
    extensions: [AshUlid.Resource]

  attributes do
    ulid_primary_key :id
  end
end

Which is a shortcut for this:

uuid_primary_key :id, type: AshUlid.Type, default: &AshUlid.generate/0

To prevent formatter from adding parens to ulid_primary_key add :ash_ulid to import_deps in .formatter.exs.

If you plan to use ULID as a main type for primary keys it makes sense to set it as default_belongs_to_type in a config:

config :ash, default_belongs_to_type: AshUlid.Type

Attribute type

AshUlid.Type can be registered under ulid name in a config:

config :ash, custom_types: [ulid: AshUlid.Type]

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 AshUlid.Type.

If default_belongs_to_type is set then attribute_type: :ulid in this example is not needed.

Generate

To generate ULID call AshUlid.generate/0 or AshUlid.generate/1 with a specific timestamp.

References

ULID spec can be found here.

The work is mostly based on Ecto.ULID.