BoxTurtle.Schema.Ecto.Ksuid (BoxTurtle v0.1.0)

Copy Markdown View Source

An Ecto.Type for KSUID keys, stored as 27-character base62 strings. KSUIDs sort by creation time, which makes them convenient primary keys.

defmodule MyApp.Blog.Tag do
  use Ecto.Schema

  # Generated on insert; @foreign_key_type makes belongs_to fields KSUIDs too.
  @primary_key {:id, BoxTurtle.Schema.Ecto.Ksuid, autogenerate: true}
  @foreign_key_type BoxTurtle.Schema.Ecto.Ksuid

  schema "tags" do
    field :name, :string
  end
end

# The migration stores the key as a string column.
defmodule MyApp.Repo.Migrations.CreateTags do
  use Ecto.Migration

  def change do
    create table(:tags, primary_key: false) do
      add :id, :string, size: 27, primary_key: true
      add :name, :string, null: false
    end
  end
end

{:ok, tag} = MyApp.Repo.insert(%MyApp.Blog.Tag{name: "elixir"})
tag.id #=> "2Ux8k3WmS1mXJ9i1QeB6LcPq0Zf"

Summary

Functions

Callback implementation for Ecto.Type.embed_as/1.

Callback implementation for Ecto.Type.equal?/2.

True for a 27-character base62 string.

Functions

embed_as(_)

Callback implementation for Ecto.Type.embed_as/1.

equal?(term1, term2)

Callback implementation for Ecto.Type.equal?/2.

valid?(ksuid)

@spec valid?(term()) :: boolean()

True for a 27-character base62 string.