DDD.Type (vicander v0.1.1)

Copy Markdown View Source

The field types of entities, aggregates and value objects, and everything the framework does with them: checking a value, storing it in Postgres, and writing it as JSON.

Scalars

typeElixir valuePostgres column
:stringbinaryvarchar
:textbinarytext
:integerintegerbigint
:floatnumberfloat
:booleanbooleanboolean
:atomatomvarchar (as a string)
:uuidUUID stringuuid
:decimalDecimalnumeric
:dateDatedate
:timeTimetime(6)
:naive_datetimeNaiveDateTimetimestamp(6)
:datetimeDateTimetimestamp(0)
:datetime_usecDateTimetimestamp(6)
:binarybinarybytea
:mapmapjsonb
:jsonmap or listjsonb

Composite types

  • {:array, type}: a list of type (a scalar, enum, reference, value object or map), stored as a Postgres array, for example {:array, :string} for tags
  • {:enum, [:open, :done]}: one of the listed atoms, stored as a string
  • {:map, type}: a map with string keys and values of type, stored as jsonb
  • {:ref, Aggregate}: the identity of another aggregate. The aggregates refer to each other by id only. It has the type of that aggregate's identity
  • {:one, Module} / {:list, Module}: nested entities or value objects, stored as jsonb
  • :money: shorthand for {:one, DDD.Money}

A value that does not fit its type is a validation violation with the code :invalid_type. nil is always accepted by the type (use required: true to forbid it).

Summary

Functions

The column type a migration uses for a field (what the snapshot records).

A short description of a type, for error messages.

A field value as the value of its Ecto column.

The Ecto type the adapter uses for a field.

The type of an aggregate's identity (what {:ref, Aggregate} holds).

A value as JSON-ready data (strings, numbers, booleans, lists, maps).

A field value read from its Ecto column.

Check a declared type and expand its shorthands. Raises ArgumentError on an unknown type, so a typo fails at compile time.

True when value fits type (nil always does).

Types

t()

@type t() ::
  atom()
  | {:array, t()}
  | {:enum, [atom()]}
  | {:map, t()}
  | {:ref, module()}
  | {:one, module()}
  | {:list, module()}

Functions

column_type(type)

@spec column_type(t()) :: Ecto.Type.t()

The column type a migration uses for a field (what the snapshot records).

describe(type)

@spec describe(t()) :: String.t()

A short description of a type, for error messages.

dump(type, value)

@spec dump(t(), term()) :: term()

A field value as the value of its Ecto column.

ecto_type(type)

@spec ecto_type(t()) :: Ecto.Type.t()

The Ecto type the adapter uses for a field.

identity_type(module)

@spec identity_type(module()) :: t()

The type of an aggregate's identity (what {:ref, Aggregate} holds).

json_dump(type, value)

@spec json_dump(t(), term()) :: term()

A value as JSON-ready data (strings, numbers, booleans, lists, maps).

json_load(type, value)

@spec json_load(t(), term()) :: term()

The inverse of json_dump/2.

load(type, value)

@spec load(t(), term()) :: term()

A field value read from its Ecto column.

normalize!(type)

@spec normalize!(term()) :: t()

Check a declared type and expand its shorthands. Raises ArgumentError on an unknown type, so a typo fails at compile time.

iex> DDD.Type.normalize!(:money)
{:one, DDD.Money}
iex> DDD.Type.normalize!({:array, :string})
{:array, :string}

valid?(type, value)

@spec valid?(t(), term()) :: boolean()

True when value fits type (nil always does).

iex> DDD.Type.valid?({:enum, [:open, :done]}, :open)
true
iex> DDD.Type.valid?({:enum, [:open, :done]}, :weird)
false
iex> DDD.Type.valid?({:array, :integer}, [1, 2])
true
iex> DDD.Type.valid?(:integer, nil)
true