Repodb.Multi.StringMap
repodb · API reference
Transactional composition of named database operations.
A multi is an ordered set of named operations executed inside one transaction. Each operation can read results produced by earlier operations, which makes it useful for workflows such as "insert a user, then insert an audit event with that user id".
let create_user_and_log email =
Multi.empty
|> Multi.insert_returning "user" ~table:User.table
~columns:[ "email" ]
~values:[ Driver.Value.text email ]
|> Multi.insert_fn "audit" (fun results ->
let row = Multi.get_row_raw_exn results "user" in
let user_id = Driver.row_int64 row "id" in
( Audit.table,
[ "user_id"; "event" ],
[ Driver.Value.int64 user_id; Driver.Value.text "created" ] ))Make(D).execute conn multi returns either all named results or a multi_error containing the failing operation name, normalized database error, and results completed before the failure. The surrounding transaction is rolled back on failure, so completed is diagnostic information, not a list of committed writes.
Use validate_multi in tests or CLIs to catch duplicate operation names before execution. Prefer descriptive names because they are the keys used to retrieve results and the names reported in errors.
type key = String.ttype !'a tval empty : 'a tval add : key -> 'a -> 'a t -> 'a tval add_to_list : key -> 'a -> 'a list t -> 'a list tval update : key -> ('a option -> 'a option) -> 'a t -> 'a tval singleton : key -> 'a -> 'a tval remove : key -> 'a t -> 'a tval merge :
(key -> 'a option -> 'b option -> 'c option) ->
'a t ->
'b t ->
'c tval union : (key -> 'a -> 'a -> 'a option) -> 'a t -> 'a t -> 'a tval cardinal : 'a t -> intval bindings : 'a t -> (key * 'a) listval min_binding : 'a t -> key * 'aval min_binding_opt : 'a t -> (key * 'a) optionval max_binding : 'a t -> key * 'aval max_binding_opt : 'a t -> (key * 'a) optionval choose : 'a t -> key * 'aval choose_opt : 'a t -> (key * 'a) optionval find : key -> 'a t -> 'aval find_opt : key -> 'a t -> 'a optionval find_first : (key -> bool) -> 'a t -> key * 'aval find_first_opt : (key -> bool) -> 'a t -> (key * 'a) optionval find_last : (key -> bool) -> 'a t -> key * 'aval find_last_opt : (key -> bool) -> 'a t -> (key * 'a) optionval iter : (key -> 'a -> unit) -> 'a t -> unitval fold : (key -> 'a -> 'acc -> 'acc) -> 'a t -> 'acc -> 'accval map : ('a -> 'b) -> 'a t -> 'b tval mapi : (key -> 'a -> 'b) -> 'a t -> 'b tval filter : (key -> 'a -> bool) -> 'a t -> 'a tval filter_map : (key -> 'a -> 'b option) -> 'a t -> 'b tval partition : (key -> 'a -> bool) -> 'a t -> 'a t * 'a tval split : key -> 'a t -> 'a t * 'a option * 'a tval is_empty : 'a t -> boolval mem : key -> 'a t -> boolval equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> boolval compare : ('a -> 'a -> int) -> 'a t -> 'a t -> intval for_all : (key -> 'a -> bool) -> 'a t -> boolval exists : (key -> 'a -> bool) -> 'a t -> boolval to_list : 'a t -> (key * 'a) listval of_list : (key * 'a) list -> 'a tval to_seq : 'a t -> (key * 'a) Seq.tval to_rev_seq : 'a t -> (key * 'a) Seq.tval to_seq_from : key -> 'a t -> (key * 'a) Seq.tval add_seq : (key * 'a) Seq.t -> 'a t -> 'a tval of_seq : (key * 'a) Seq.t -> 'a t