summaryrefslogtreecommitdiff
path: root/test/insert.ml
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-11-20 16:23:53 +0100
committerSébastien Dailly <sebastien@dailly.me>2026-11-20 16:23:53 +0100
commitb05e2d2be7be28ee6c577671002797cf9abaf177 (patch)
tree0474684d619721024f0a6cb132cf2f4d2e84c344 /test/insert.ml
parentc4ca180fd7c2085e3ce3f8e32aa0c2cb724f4c53 (diff)
New module for working with Values
Diffstat (limited to 'test/insert.ml')
-rw-r--r--test/insert.ml35
1 files changed, 18 insertions, 17 deletions
diff --git a/test/insert.ml b/test/insert.ml
index 217fc83..ae5748e 100644
--- a/test/insert.ml
+++ b/test/insert.ml
@@ -15,7 +15,7 @@ let pp : Format.formatter -> t -> unit =
Format.fprintf formatter "@[<v 2>%s:%Ld@;%s@]" content_id id content))
type content = Model.content
-type value = Model.value
+type value = Value.value
type properties = (string * value) list
(* A subcontent to insert, with one entry (properties) per row *)
@@ -31,17 +31,18 @@ type entry = properties * content_to_insert list
[content] is the type definition for the element we want to insert [entry]
contains all the values to insert in the database. *)
let insert_and_get_result :
- ?parent:int64 ->
+ ?parent:Model.content Value.t ->
Caqti_lwt.connection ->
content ->
properties ->
(string * int64 * string) Lwt.t =
fun ?parent db content entry ->
let ( let* ) = Lwt.bind in
- let* id =
+ let* inserted =
Query.insert ?parent db content (fun v ->
- match List.assoc_opt v entry with Some v -> v | None -> Model.Typ.None)
+ match List.assoc_opt v entry with Some v -> v | None -> Value.none)
in
+ let id = Value.id inserted in
let content_id = Model.Identifier.to_string (Model.Content.id content) in
let module Db = (val db) in
let* json_result = Db.find (Common.content_as_json content) id in
@@ -49,19 +50,19 @@ let insert_and_get_result :
Lwt.return (content_id, id, json)
(** Insert one row and collect the result line, together with the lines of its
- multivalued references. Return the accumulator and the id of the new row. *)
+ multivalued references. Return the accumulator and the inserted value. *)
let insert_one :
Caqti_lwt.connection ->
- ?parent:int64 ->
+ ?parent:Model.content Value.t ->
content ->
properties ->
(string * Int64.t * string) list ->
- ((string * Int64.t * string) list * Int64.t) Lwt.t =
+ ((string * Int64.t * string) list * Model.content Value.t) Lwt.t =
fun db ?parent content entry acc ->
let ( let* ) = Lwt.bind in
let* content_id, id, json = insert_and_get_result ?parent db content entry in
let* ext = Common.collect_references db content id in
- Lwt.return (List.append ext ((content_id, id, json) :: acc), id)
+ Lwt.return (List.append ext ((content_id, id, json) :: acc), Value.v content id)
module S = Configuration.Json5.Decode
@@ -96,14 +97,14 @@ let rec value :
( "numeric",
S.map
(fun v ->
- Lwt.return (key, Model.Typ.(V (Integer, Some (Int64.of_int v)))))
+ Lwt.return (key, Value.single (Model.Typ.Integer, Some (Int64.of_int v))))
S.int );
( "text",
- S.map (fun v -> Lwt.return (key, Model.Typ.(V (Text, Some v)))) S.string
+ S.map (fun v -> Lwt.return (key, Value.single (Model.Typ.Text, Some v))) S.string
);
("reference", reference_value db content key false);
("references", reference_value db content key true);
- ("other", S.succeed (Lwt.return (key, Model.Typ.None)));
+ ("other", S.succeed (Lwt.return (key, Value.none)));
]
(** Decode a reference field: single value or list of values, depending on
@@ -131,7 +132,7 @@ and reference_value :
(fun ids ->
Lwt.return
( key,
- Model.Typ.Seq
+ Value.multi
( Model.Typ.MultiReference reference,
List.to_seq (List.map ~f:Int64.of_int ids) ) ))
(S.list S.int)
@@ -140,7 +141,7 @@ and reference_value :
(fun v ->
Lwt.return
( key,
- Model.Typ.V
+ Value.single
(Model.Typ.MonoReference reference, Some (Int64.of_int v))
))
S.int
@@ -159,7 +160,7 @@ and reference_value :
in
Lwt.return
( key,
- Model.Typ.Seq
+ Value.multi
(Model.Typ.MultiReference reference, List.to_seq ids) ))
else
let** props = properties db target in
@@ -167,7 +168,7 @@ and reference_value :
(let* props = props in
let* _, id, _ = insert_and_get_result db target props in
Lwt.return
- (key, Model.Typ.V (Model.Typ.MonoReference reference, Some id)))
+ (key, Value.single (Model.Typ.MonoReference reference, Some id)))
)
and properties : Caqti_lwt.connection -> content -> properties Lwt.t S.decoder =
@@ -257,14 +258,14 @@ let exec : string list -> t =
Lwt_list.fold_left_s
(fun acc (entry, subcontents) ->
(* Insert the main content *)
- let* acc, id = insert_one db content entry acc in
+ let* acc, parent = insert_one db content entry acc in
(* Insert the subcontents associated *)
Lwt_list.fold_left_s
(fun acc (subcontent, subentries) ->
Lwt_list.fold_left_s
(fun acc entry ->
let* acc, _ =
- insert_one db ~parent:id subcontent entry acc
+ insert_one db ~parent subcontent entry acc
in
Lwt.return acc)
acc subentries)