View Source Ecto.ShortUUID (ecto_shortuuid v0.4.0)

An Ecto type for Short UUID strings.

Summary

Types

A raw binary representation of a UUID.

A ShortUUID string.

A hex-encoded UUID string.

Functions

Generates a random ShortUUID string for use in primary keys.

Casts a value to a ShortUUID.

Dumps a ShortUUID or standard UUID to binary format for database storage.

Ecto v3 callback Dictates how the type should be treated inside embeds. https://hexdocs.pm/ecto/Ecto.Type.html#c:embed_as/1

Ecto v3 callback Checks if two terms are semantically equal. https://hexdocs.pm/ecto/Ecto.Type.html#c:equal?/2

Generates a ShortUUID string based on a version 4 (random) UUID.

Loads a binary UUID from the database and converts it to ShortUUID format.

Returns the underlying schema type for the custom type.

Types

raw()

@type raw() :: <<_::128>>

A raw binary representation of a UUID.

shortuuid()

@type shortuuid() :: binary()

A ShortUUID string.

uuid()

@type uuid() :: <<_::288>>

A hex-encoded UUID string.

Functions

autogenerate()

@spec autogenerate() :: shortuuid()

Generates a random ShortUUID string for use in primary keys.

This is called by Ecto when a record is inserted if the schema has @primary_key {:id, CustomShortUUIDType, autogenerate: true}.

cast(uuid)

@spec cast(uuid() | shortuuid() | any()) :: {:ok, shortuuid()} | :error

Casts a value to a ShortUUID.

Accepts:

  • Standard UUID strings (converts them to ShortUUID format)
  • ShortUUID strings (validates them)

Returns:

  • {:ok, shortuuid} on success
  • :error on failure

dump(uuid)

@spec dump(shortuuid() | uuid() | any()) :: {:ok, raw()} | :error

Dumps a ShortUUID or standard UUID to binary format for database storage.

Accepts:

  • ShortUUID strings
  • Standard UUID strings

Returns:

  • {:ok, binary_uuid} on success
  • :error on failure

embed_as(_)

@spec embed_as(term()) :: :self

Ecto v3 callback Dictates how the type should be treated inside embeds. https://hexdocs.pm/ecto/Ecto.Type.html#c:embed_as/1

equal?(term1, term2)

@spec equal?(term(), term()) :: boolean()

Ecto v3 callback Checks if two terms are semantically equal. https://hexdocs.pm/ecto/Ecto.Type.html#c:equal?/2

generate()

@spec generate() :: shortuuid()

Generates a ShortUUID string based on a version 4 (random) UUID.

load(uuid)

@spec load(raw() | any()) :: {:ok, shortuuid()} | :error

Loads a binary UUID from the database and converts it to ShortUUID format.

Returns:

  • {:ok, shortuuid} on success
  • :error on failure

Raises an ArgumentError if given a string UUID instead of binary.

type()

@spec type() :: :uuid

Returns the underlying schema type for the custom type.

For ShortUUID, this is always :uuid as the data is stored in UUID format in the database.