UUIDv7.Boxed (UUUIDv7 v0.3.0)

Copy Markdown View Source

Ecto type that autogenerates %UUID{} structs holding raw UUIDv7 bytes.

Why

The default Ecto.UUID (and UUIDv7 by delegation) stores UUIDs as 36-char hex strings in your schemas. Every Repo.insert/1 pays a hex → binary decode in dump/1, and every autogenerate/0 pays a binary → hex encode it will undo a moment later.

UUIDv7.Boxed cuts the round-trip: it autogenerates %UUID{} structs carrying the raw 16 bytes, and dump/1 returns those bytes directly. Hex encoding is deferred to to_string/1, inspect/1, and JSON encoding — paid once at display time, not on every write.

Use as a primary key

schema "users" do
  @primary_key {:id, UUIDv7.Boxed, autogenerate: true}
  ...
end

Values come back as %UUID{bytes: <<_::128>>}. cast/1 accepts the struct, a 16-byte raw binary, or a 36-char hex string.

Summary

Functions

Callback implementation for Ecto.Type.embed_as/1.

Callback implementation for Ecto.Type.equal?/2.

Bulk-generates n boxed UUIDv7 values sharing the same millisecond.

Functions

embed_as(_)

Callback implementation for Ecto.Type.embed_as/1.

equal?(term1, term2)

Callback implementation for Ecto.Type.equal?/2.

generate_many(n)

@spec generate_many(pos_integer()) :: [UUID.t()]

Bulk-generates n boxed UUIDv7 values sharing the same millisecond.

Delegates to UUIDv7.bingenerate_many/1; see that function for the ordering and counter semantics.

Example

iex> uuids = UUIDv7.Boxed.generate_many(4)
iex> length(uuids)
4
iex> Enum.all?(uuids, &match?(%UUID{}, &1))
true