kura_types (kura v2.17.1)

View Source

Type system for casting, dumping, and loading values between Erlang and PostgreSQL.

Supported types: id, integer, float, string, text, boolean, date, utc_datetime, uuid, jsonb, {array, Type}.

  • cast/2 - coerce external input to Erlang terms
  • dump/2 - convert Erlang terms to pgo-compatible values
  • load/2 - convert pgo results back to Erlang terms

Summary

Functions

Coerce external input to an Erlang term for the given type.

Convert an Erlang term to a pgo-compatible value for storage.

Map a PostgreSQL catalog type name (udt_name, e.g. ~"int4") to a kura field type - the inverse of to_pg_type/1. Used by schema introspection (rebar3 kura gen_schemas) to bootstrap schemas from an existing database.

Convert a pgo result value back to an Erlang term.

Return the PostgreSQL DDL type string for a kura type.

Types

kura_type()

-type kura_type() ::
          id | integer | bigint | smallint | float | decimal | string | text | binary | boolean | date |
          time | utc_datetime | naive_datetime | uuid | jsonb |
          {enum, [atom()]} |
          {array, kura_type()} |
          {encrypted, kura_type()} |
          vector |
          {vector, pos_integer()} |
          {embed, embeds_one | embeds_many, module()} |
          {custom, module()}.

Functions

cast/2

-spec cast(kura_type(), term()) -> {ok, term()} | {error, binary()}.

Coerce external input to an Erlang term for the given type.

dump/2

-spec dump(kura_type(), term()) -> {ok, term()} | {error, binary()}.

Convert an Erlang term to a pgo-compatible value for storage.

from_pg_type/2

-spec from_pg_type(binary(), #{serial => boolean(), char_max_length => non_neg_integer()}) ->
                      kura_type() | {unsupported, binary()}.

Map a PostgreSQL catalog type name (udt_name, e.g. ~"int4") to a kura field type - the inverse of to_pg_type/1. Used by schema introspection (rebar3 kura gen_schemas) to bootstrap schemas from an existing database.

Opts supplies context the type name alone does not carry:

  • char_max_length - distinguishes ~"varchar" at 255 (string) from any other length (text).
  • serial - an ~"int8" column with a nextval default is kura's id (BIGSERIAL); a plain ~"int8" is bigint.

float4 (REAL) widens to float since kura models a single float type (DOUBLE PRECISION). Returns {unsupported, TypeName} for a PG type kura does not model (including native CREATE TYPE ... AS ENUM types), so the caller can skip the field and warn rather than guessing.

load/2

-spec load(kura_type(), term()) -> {ok, term()} | {error, binary()}.

Convert a pgo result value back to an Erlang term.

to_pg_type/1

-spec to_pg_type(kura_type()) -> binary().

Return the PostgreSQL DDL type string for a kura type.