# Shortcode
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/elielhaouzi/shortcode/CI?cacheSeconds=3600&style=flat-square)](https://github.com/elielhaouzi/shortcode/actions) [![GitHub issues](https://img.shields.io/github/issues-raw/elielhaouzi/shortcode?style=flat-square&cacheSeconds=3600)](https://github.com/elielhaouzi/shortcode/issues) [![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?cacheSeconds=3600?style=flat-square)](http://opensource.org/licenses/MIT) [![Hex.pm](https://img.shields.io/hexpm/v/shortcode?style=flat-square)](https://hex.pm/packages/shortcode) [![Hex.pm](https://img.shields.io/hexpm/dt/shortcode?style=flat-square)](https://hex.pm/packages/shortcode)
An Ecto type for UUIDs and ID displayed as shortcode with support of prefix 'à la Stripe'.
## Installation
Shortcode is published on [Hex](https://hex.pm/packages/shortcode). The package can be installed
by adding `shortcode` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:shortcode, "~> 0.7.0"}
]
end
```
The docs can be found at [https://hexdocs.pm/shortcode](https://hexdocs.pm/shortcode).
## Usage
You can use Shortcode UUID or Shortcode id for regular or primary key fields.
```elixir
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
```
```elixir
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
```