kura_multi (kura v2.0.6)

View Source

Transaction 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

append/2

-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.

delete(Multi, Name, CSOrFun)

-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.

insert(Multi, Name, CSOrFun)

-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.

new()

-spec new() -> #kura_multi{operations :: [{atom(), term()}]}.

Create a new empty multi pipeline.

run(Multi, Name, Fun)

-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.

to_list/1

-spec to_list(#kura_multi{operations :: [{atom(), term()}]}) -> [{atom(), term()}].

Return the operations list in execution order.

update(Multi, Name, CSOrFun)

-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.