Embedded Schemas
View SourceEmbedded schemas let you nest structured data inside a parent schema, stored as JSONB in PostgreSQL. They're useful for semi-structured data that doesn't need its own table - addresses, settings, metadata, tags, etc.
Kura supports two embed types:
embeds_one- a single nested mapembeds_many- a list of nested maps
Defining Embedded Schemas
An embedded schema is a module that exports fields/0. It has no table of
its own, so it exports no table/0 and does not declare
-behaviour(kura_schema): tools that look for that attribute, such as the
rebar3_kura migration generator, treat every module that declares it as a
table to create.
-module(address).
-include_lib("kura/include/kura.hrl").
-export([fields/0]).
fields() ->
[
#kura_field{name = street, type = string},
#kura_field{name = city, type = string},
#kura_field{name = zip, type = string}
].-module(tag).
-include_lib("kura/include/kura.hrl").
-export([fields/0]).
fields() ->
[
#kura_field{name = label, type = string},
#kura_field{name = weight, type = integer}
].Adding Embeds to a Parent Schema
Export an embeds/0 callback from the parent schema returning a list of #kura_embed{} records:
-module(profile).
-behaviour(kura_schema).
-include_lib("kura/include/kura.hrl").
-export([table/0, fields/0, embeds/0]).
table() -> ~"profiles".
fields() ->
[
#kura_field{name = id, type = id},
#kura_field{name = name, type = string},
#kura_field{name = bio, type = text}
].
embeds() ->
[
#kura_embed{name = address, type = embeds_one, schema = address},
#kura_embed{name = tags, type = embeds_many, schema = tag}
].The corresponding migration defines the embed columns as JSONB:
up() ->
[{create_table, ~"profiles", [
#kura_column{name = id, type = id, primary_key = true},
#kura_column{name = name, type = string, nullable = false},
#kura_column{name = bio, type = text},
#kura_column{name = address, type = jsonb},
#kura_column{name = tags, type = jsonb, default = []},
#kura_column{name = inserted_at, type = utc_datetime, nullable = false},
#kura_column{name = updated_at, type = utc_datetime, nullable = false}
]}].Casting Embeds
Use kura_changeset:cast_embed/2 to cast nested params through the embedded schema's fields. Validation errors from the embedded schema bubble up to the parent changeset.
embeds_one
Pass a map of params for the embedded field:
Params = #{
name => ~"Alice",
bio => ~"Developer",
address => #{street => ~"123 Main St", city => ~"Portland", zip => ~"97201"}
},
CS = kura_changeset:cast(profile, #{}, Params, [name, bio]),
CS1 = kura_changeset:cast_embed(CS, address),
%% CS1#kura_changeset.changes contains:
%% #{name => ~"Alice",
%% bio => ~"Developer",
%% address => #{street => ~"123 Main St", city => ~"Portland", zip => ~"97201"}}embeds_many
Pass a list of maps:
Params = #{
name => ~"Alice",
tags => [
#{label => ~"important", weight => 10},
#{label => ~"urgent", weight => 5}
]
},
CS = kura_changeset:cast(profile, #{}, Params, [name]),
CS1 = kura_changeset:cast_embed(CS, tags),
%% CS1#kura_changeset.changes contains:
%% #{name => ~"Alice",
%% tags => [
%% #{label => ~"important", weight => 10},
%% #{label => ~"urgent", weight => 5}
%% ]}Custom Validation with with
By default, cast_embed casts all non-virtual fields of the embedded schema. Embedded params usually arrive in a request, so say which fields they may set. #{cast => [street, city]} lists them. To validate as well, pass a with option - {Module, Function} or a function that takes existing data and params, and returns a changeset:
WithFun = fun(_Data, EmbedParams) ->
ChildCS = kura_changeset:cast(address, #{}, EmbedParams, [street, city]),
kura_changeset:validate_required(ChildCS, [street, city])
end,
CS1 = kura_changeset:cast_embed(CS, address, #{with => WithFun}){kura, [{strict_assoc_cast, true}]} makes one of the two options mandatory; see Nested Changesets.
This is useful when you want to:
- Require specific fields on the embed
- Only allow a subset of fields to be cast
- Add custom validations (length, format, etc.)
Error Handling
Type mismatches are caught automatically:
%% Passing a list where a map is expected
Params = #{name => ~"Alice", address => [1, 2, 3]},
CS = kura_changeset:cast(profile, #{}, Params, [name]),
CS1 = kura_changeset:cast_embed(CS, address),
%% CS1#kura_changeset.valid =:= false
%% CS1#kura_changeset.errors =:= [{address, ~"expected a map"}]Validation errors from embedded schemas are merged into the parent:
WithFun = fun(_Data, EmbedParams) ->
ChildCS = kura_changeset:cast(address, #{}, EmbedParams, [street, city, zip]),
kura_changeset:validate_required(ChildCS, [street, city, zip])
end,
Params = #{name => ~"Alice", address => #{street => ~"123 Main St"}},
CS = kura_changeset:cast(profile, #{}, Params, [name]),
CS1 = kura_changeset:cast_embed(CS, address, #{with => WithFun}),
%% CS1#kura_changeset.valid =:= false
%% CS1#kura_changeset.errors =:= [{city, ~"can't be blank"}, {zip, ~"can't be blank"}]The embedded changeset's errors are added to the parent's as they are,
keyed by the embedded field's name, not nested under the embed's name. A
parent field and an embedded field with the same name are therefore not
told apart in errors; give embedded fields names the parent does not use
when a client needs to tell them apart.
Storage
Embedded schemas are stored as JSONB in PostgreSQL. Kura handles serialization automatically:
- On insert/update: Erlang maps are JSON-encoded before writing
- On load: JSONB values are decoded back into Erlang maps with atom keys
This means you can query embedded data using PostgreSQL's JSONB operators in raw SQL, while working with regular Erlang maps in application code.
When to Use Embedded Schemas
Good fit:
- Data that always belongs to the parent (addresses, settings, metadata)
- Semi-structured or variable data (form responses, config)
- Data that doesn't need independent querying or foreign keys
Consider a separate table instead when:
- The data needs its own associations
- You need to query or index the nested data frequently
- The data is shared across multiple parents