EctoUnnest.Types (ecto_unnest v0.3.0)

Copy Markdown View Source

Ecto type inference from the schema and mapping to a PostgreSQL type.

The type is read from schema.__schema__(:type, field) and mapped to the PG type used in ::type[] casts. The :types option (override) takes precedence and is the only path for binary sources ("table"), which have no schema.

Summary

Functions

PG type names always allowed for a :types override (see EctoUnnest's :allowed_types).

Returns the Ecto type for a field. For a binary source it requires an override from :types, but even then returns only a type stand-in — the real PG type comes from the override.

True when an Ecto type is stored as jsonb (a per-row value that must go in as pre-encoded JSON text with a ::jsonb cast in the projection — see EctoUnnest's JSON mode). Covers :map, {:map, _}, {:array, :map}, {:array, {:map, _}} and parameterized types whose primitive is one of those.

{:ok, "pg_type"} or {:error, :array_unsupported}.

Functions

default_pg_types()

PG type names always allowed for a :types override (see EctoUnnest's :allowed_types).

ecto_type!(schema, field, override)

Returns the Ecto type for a field. For a binary source it requires an override from :types, but even then returns only a type stand-in — the real PG type comes from the override.

jsonb?(t)

True when an Ecto type is stored as jsonb (a per-row value that must go in as pre-encoded JSON text with a ::jsonb cast in the projection — see EctoUnnest's JSON mode). Covers :map, {:map, _}, {:array, :map}, {:array, {:map, _}} and parameterized types whose primitive is one of those.

resolve_pg_type(ecto_type, override, kind \\ :array)

{:ok, "pg_type"} or {:error, :array_unsupported}.

override (from :types) takes precedence. kind decides about arrays:

  • :array (an unnest column) + array type -> {:error, :array_unsupported} (unnest would flatten a multi-dimensional array),
  • :scalar (a placeholder) + array type -> OK, e.g. "bigint[]" — it is an ordinary scalar parameter, so a constant array value lands in the column fine.