Shortcode

GitHub Workflow Status GitHub issues License Hex.pm Hex.pm

An Ecto type for UUIDs and ID displayed as shortcode with support of prefix 'à la Stripe'.

Installation

Shortcode is published on Hex. The package can be installed by adding shortcode to your list of dependencies in mix.exs:

def deps do
  [
    {:shortcode, "~> 0.7.0"}
  ]
end

The docs can be found at https://hexdocs.pm/shortcode.

Usage

You can use Shortcode UUID or Shortcode id for regular or primary key fields.

defmodule MyApp.UserWithUUID do
  use Ecto.Schema

  @primary_key {:id, Shortcode.Ecto.UUID, prefix: "usr", autogenerate: true}
  schema "users" do
  end
end

defmodule MyApp.UserWithID do
  use Ecto.Schema

  @primary_key {:id, Shortcode.Ecto.ID, prefix: "usr", autogenerate: true}
  schema "users" do
  end
end
defmodule RequestUUID do
  use Ecto.Schema

  schema "request" do
    field :uuid, Shortcode.Ecto.UUID
    field :uuid_with_prefix, Shortcode.Ecto.UUID, prefix: "req"
    field :autogenerated_uuid, Shortcode.Ecto.UUID, autogenerate: true
    field :autogenerated_uuid_with_prefix, Shortcode.Ecto.UUID, autogenerate: true, prefix: "req"
  end
end

defmodule RequestID do
  use Ecto.Schema

  schema "request" do
    field :request_id, Shortcode.Ecto.ID
    field :request_id_with_prefix, Shortcode.Ecto.ID, prefix: "req"
    field :autogenerated_request_id, Shortcode.Ecto.UUID, autogenerate: true
    field :autogenerated_request_id_with_prefix, Shortcode.Ecto.UUID, autogenerate: true, prefix: "req"
  end
end