Encode UUIDs and Binaries as prefixed base64 strings similar to Stripe's prefixed IDs.
defmodule User do
use Ecto.Schema
@primary_key {:id, Ecto.PrefixID.Base64UUID, autogenerate: true, prefix: "usr"}
schema "users" do
# ...
end
end
Repo.insert!(%User{...}) => %User{id: "usr_l6atnMk5TnqnK8cL41nS2w"}
defmodule Client do
use Ecto.Schema
schema "clients" do
field :secret, Ecto.PrefixID.Binary, autogenerate: true, prefix: "cls", length: 32
end
end
Repo.insert!(%Client{...}) => %Client{secret: "cls_zbRihf31ZicYFS3k-1S17Nm4PVtXUKmCTNQGPGQ3fSc"}
If available in Hex, the package can be installed
by adding ecto_prefixed_id
to your list of dependencies in mix.exs
:
def deps do
[
{:ecto_prefixed_id, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/ecto_prefixed_id.