Custom field types

Given the flexibility that Ecto aims for, it should come as no surprise if we tell you that, besides the field types supported out of the box such as :string, :integer, and :map, Ecto also allows you to define your own custom types.

To create a custom type module, you need to implement the Ecto.Type behaviour. We will create a custom Ksuid Ecto type to use as the primary key of all our schemas. KSUIDs, or K-Sortable Unique IDentifiers, are as safe and random as UUIDs, but have the cool property of being lexicographically sortable. These identifiers are 20 bytes long and their string representation is always 27 characters long.

We just need to add the ksuid library to our ElixirDrip dependencies declared on the elixir_drip ...

Get Mastering Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.