UUIDv7 for Elixir and (optionally) Ecto, using microseconds.
There are other UUID v7 packages, but I wanted the additional clock precision.
Uses the method described in Section 6.2, Method 3 from this IETF Draft to add additional clock precision using microseconds to a version 7 UUID.
The package can be installed by adding uuid_v7
to your list of dependencies in mix.exs
:
def deps do
[
{:uuid_v7, "~> 0.3.1"}
]
end
iex> UUIDv7.generate()
"018e90d8-06e8-7f9f-bfd7-6730ba98a51b"
iex> UUIDv7.bingenerate()
<<1, 142, 144, 216, 124, 16, 127, 196, 158, 92, 92, 74, 83, 46, 116, 173>>
Use this the same way you would use Ecto.UUID
. For example:
defmodule MyApp.Blog.Post do
use Ecto.Schema
@primary_key {:id, UUIDv7, autogenerate: true}
@foreign_key_type UUIDv7
schema "blog_posts" do
field :text, :string
# etc.
end
end