minhajuddin / ecto_prefixed_id

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ecto.PrefixedID

Encode UUIDs and Binaries as prefixed base64 strings similar to Stripe's prefixed IDs.

Use it for a primary key

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"}

Use it for a binary blob of data

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"}

Installation

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.

About


Languages

Language:Elixir 100.0%