kura_multi (kura v2.0.6)
View SourceTransaction pipelines that group multiple repo operations atomically.
Build a pipeline with new/0, add steps with insert/3, update/3,
delete/3, or run/3, then execute with kura_repo_worker:multi/2.
Multi = kura_multi:new(),
M1 = kura_multi:insert(Multi, create_user, UserCS),
M2 = kura_multi:insert(M1, create_profile, fun(#{create_user := User}) ->
kura_changeset:cast(profile, #{}, #{user_id => maps:get(id, User)}, [user_id])
end),
{ok, Results} = kura_repo_worker:multi(MyRepo, M2).
Summary
Functions
Append the operations of the second multi onto the first.
Add a delete step.
Add an insert step. Accepts a changeset or a fun(PreviousResults) -> Changeset.
Create a new empty multi pipeline.
Add an arbitrary function step that receives previous results.
Return the operations list in execution order.
Add an update step.
Functions
-spec append(#kura_multi{operations :: [{atom(), term()}]}, #kura_multi{operations :: [{atom(), term()}]}) -> #kura_multi{operations :: [{atom(), term()}]}.
Append the operations of the second multi onto the first.
-spec delete(#kura_multi{operations :: [{atom(), term()}]}, atom(), #kura_changeset{valid :: boolean(), schema :: module() | undefined, data :: map(), params :: map(), changes :: map(), errors :: [{atom(), binary()}], types :: #{atom() => kura_types:kura_type()}, required :: [atom()], action :: atom() | undefined, constraints :: [#kura_constraint{type :: unique | foreign_key | check | exclusion, constraint :: binary(), field :: atom(), message :: binary()}], assoc_changes :: #{atom() => #kura_changeset{} | [#kura_changeset{}]}, prepare :: [fun((#kura_changeset{}) -> #kura_changeset{})], optimistic_lock :: atom() | undefined} | fun((map()) -> #kura_changeset{valid :: boolean(), schema :: module() | undefined, data :: map(), params :: map(), changes :: map(), errors :: [{atom(), binary()}], types :: #{atom() => kura_types:kura_type()}, required :: [atom()], action :: atom() | undefined, constraints :: [#kura_constraint{type :: unique | foreign_key | check | exclusion, constraint :: binary(), field :: atom(), message :: binary()}], assoc_changes :: #{atom() => #kura_changeset{} | [#kura_changeset{}]}, prepare :: [fun((#kura_changeset{}) -> #kura_changeset{})], optimistic_lock :: atom() | undefined})) -> #kura_multi{operations :: [{atom(), term()}]}.
Add a delete step.
-spec insert(#kura_multi{operations :: [{atom(), term()}]}, atom(), #kura_changeset{valid :: boolean(), schema :: module() | undefined, data :: map(), params :: map(), changes :: map(), errors :: [{atom(), binary()}], types :: #{atom() => kura_types:kura_type()}, required :: [atom()], action :: atom() | undefined, constraints :: [#kura_constraint{type :: unique | foreign_key | check | exclusion, constraint :: binary(), field :: atom(), message :: binary()}], assoc_changes :: #{atom() => #kura_changeset{} | [#kura_changeset{}]}, prepare :: [fun((#kura_changeset{}) -> #kura_changeset{})], optimistic_lock :: atom() | undefined} | fun((map()) -> #kura_changeset{valid :: boolean(), schema :: module() | undefined, data :: map(), params :: map(), changes :: map(), errors :: [{atom(), binary()}], types :: #{atom() => kura_types:kura_type()}, required :: [atom()], action :: atom() | undefined, constraints :: [#kura_constraint{type :: unique | foreign_key | check | exclusion, constraint :: binary(), field :: atom(), message :: binary()}], assoc_changes :: #{atom() => #kura_changeset{} | [#kura_changeset{}]}, prepare :: [fun((#kura_changeset{}) -> #kura_changeset{})], optimistic_lock :: atom() | undefined})) -> #kura_multi{operations :: [{atom(), term()}]}.
Add an insert step. Accepts a changeset or a fun(PreviousResults) -> Changeset.
Create a new empty multi pipeline.
-spec run(#kura_multi{operations :: [{atom(), term()}]}, atom(), fun((map()) -> {ok, term()} | {error, term()})) -> #kura_multi{operations :: [{atom(), term()}]}.
Add an arbitrary function step that receives previous results.
Return the operations list in execution order.
-spec update(#kura_multi{operations :: [{atom(), term()}]}, atom(), #kura_changeset{valid :: boolean(), schema :: module() | undefined, data :: map(), params :: map(), changes :: map(), errors :: [{atom(), binary()}], types :: #{atom() => kura_types:kura_type()}, required :: [atom()], action :: atom() | undefined, constraints :: [#kura_constraint{type :: unique | foreign_key | check | exclusion, constraint :: binary(), field :: atom(), message :: binary()}], assoc_changes :: #{atom() => #kura_changeset{} | [#kura_changeset{}]}, prepare :: [fun((#kura_changeset{}) -> #kura_changeset{})], optimistic_lock :: atom() | undefined} | fun((map()) -> #kura_changeset{valid :: boolean(), schema :: module() | undefined, data :: map(), params :: map(), changes :: map(), errors :: [{atom(), binary()}], types :: #{atom() => kura_types:kura_type()}, required :: [atom()], action :: atom() | undefined, constraints :: [#kura_constraint{type :: unique | foreign_key | check | exclusion, constraint :: binary(), field :: atom(), message :: binary()}], assoc_changes :: #{atom() => #kura_changeset{} | [#kura_changeset{}]}, prepare :: [fun((#kura_changeset{}) -> #kura_changeset{})], optimistic_lock :: atom() | undefined})) -> #kura_multi{operations :: [{atom(), term()}]}.
Add an update step.