diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-04-30 09:16:03 +0200 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-05-06 16:45:25 +0200 |
| commit | 960d5b8f2728f71dd9ce038d527844d82ca2f0f2 (patch) | |
| tree | 9a15dc43f91a0b58a3aaa10223bba35abf7ef5f2 /test/update.ml | |
| parent | a5efd7d15b4d049a4ed539ed5d9c2f1037d00973 (diff) | |
Allow updates for existing contents
Diffstat (limited to 'test/update.ml')
| -rw-r--r-- | test/update.ml | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/test/update.ml b/test/update.ml new file mode 100644 index 0000000..1e94fc2 --- /dev/null +++ b/test/update.ml @@ -0,0 +1,207 @@ +let name = __FILE__ +let description = "Update a content" + +type actions = + | Commit + | Unset of string + | Text of string * string + | Integer of string * int + | Reference of Model.Reference.t Model.Typ.reference * string * int + | References of Model.Reference.t Model.Typ.reference * string * int64 Seq.t + +let actions : actions list ref = ref [] + +type t = string list + +let pp : Format.formatter -> t -> unit = Format.(pp_print_list pp_print_string) + +let configuration = + lazy + (let model = + {| + { + "content": [ + { + "id": "entity", + "section": [ + { + "field": [ + { + "name": "references", + "label": {}, + "type": { + "content": "entity", + "multivaluated": true + } + }, + { + "name": "reference", + "label": {}, + "type": { + "content": "entity", + "multivaluated": false + } + }, + { + "name": "integer", + "label": {}, + "type": "integer" + }, + { + "name": "text", + "label": {}, + "type": "text" + }, + ] + } + ] + } + ] + }|} + in + Configuration.Read_conf.read_string_conf model + |> Result.map_error (fun e -> + Croquette_common.Format.format_string (fun fmt -> + Configuration.Read_conf.pp_error fmt e)) + |> Result.get_ok') + +let speclist () = + let (lazy configuration) = configuration in + let content = Configuration.Ast.get_content configuration "entity" in + let fields = Model.Content.fields (Option.get content) in + Seq.fold_left + (fun acc feature -> + let key = "--" ^ Model.Field.name feature in + let (Model.Field.T type_) = Model.Field.type_ feature in + let argument = + match type_ with + | Model.Typ.Text -> + Some + ( key, + Arg.String + (fun value -> + actions := + Text (Model.Field.name feature, value) :: !actions), + "<v> Set field with the value <v>" ) + | Model.Typ.Integer -> + Some + ( key, + Arg.Int + (fun value -> + actions := + Integer (Model.Field.name feature, value) :: !actions), + "<n> Set field with the value <n>" ) + | Model.Typ.MonoReference target -> + Some + ( key, + Arg.Int + (fun value -> + actions := + Reference (target, Model.Field.name feature, value) + :: !actions), + "<id> Set field with the id" ) + | Model.Typ.MultiReference target -> + Some + ( key, + Arg.String + (fun value -> + let ids = + String.split_on_char ',' value + |> List.map (fun v -> Int64.of_string (String.trim v)) + |> List.to_seq + in + + actions := + References (target, Model.Field.name feature, ids) + :: !actions), + "<id,..> Set this list of values" ) + in + + match argument with None -> acc | Some v -> v :: acc) + [ + ( "--commit", + Arg.Unit (fun () -> actions := Commit :: !actions), + "\tCommit this group of update" ); + ( "--clear", + Arg.String (fun feature -> actions := Unset feature :: !actions), + "<f> Clear the field <f>" ); + ] + fields + |> Arg.align + +let exec : string list -> t = + fun _ -> + let (lazy configuration) = configuration in + let content = + Configuration.Ast.get_content configuration "entity" |> Option.get + in + + Lwt_main.run + (let ( let* ) = Lwt.bind in + let* db = Common.connect configuration in + let module Db = (val db) in + (* Insert a content without any value *) + let* id = Query.insert db content (fun _ -> Model.Typ.None) in + + let group_commit : actions Seq.t Seq.t = + List.rev !actions |> List.to_seq + |> Seq.group (fun a b -> + match (a, b) with _, Commit -> false | _ -> true) + in + (* Update the values from the argument in the command line *) + let* printers = + Seq.fold_left + (fun acc actions -> + let* acc = acc in + let* () = + Query.update db id content (fun key -> + let value = + Seq.find_map + (function + | Unset k when String.equal k key -> + let field = + Model.Content.fields content + |> Seq.find (fun f -> + String.equal key (Model.Field.name f)) + |> Option.get + in + let (Model.Field.T t) = Model.Field.type_ field in + begin match t with + | Model.Typ.MultiReference _ as e -> + Some Model.Typ.(Seq (e, Seq.empty)) + | Integer as e -> Some Model.Typ.(V (e, None)) + | Text as e -> Some Model.Typ.(V (e, None)) + | MonoReference _ as e -> + Some Model.Typ.(V (e, None)) + end + | Text (k, v) when String.equal k key -> + Some Model.Typ.(V (Text, Some v)) + | Integer (k, v) when String.equal k key -> + Some Model.Typ.(V (Integer, Some (Int64.of_int v))) + | Reference (target, k, v) when String.equal k key -> + Some + Model.Typ.( + V (MonoReference target, Some (Int64.of_int v))) + | References (target, k, v) when String.equal k key -> + Some Model.Typ.(Seq (MultiReference target, v)) + | _ -> None) + actions + in + match value with Some v -> v | None -> Model.Typ.None) + in + (* Get the values from the content registered in the database *) + let* json_result = Db.find (Common.content_as_json content) id in + let* json = Caqti_lwt.or_fail json_result in + + (* Also query for the referenoces *) + let* refs = Common.collect_references db content id in + let acc = + List.fold_left + (fun acc (_, _, ref) -> ref :: acc) + (json :: acc) (List.rev refs) + in + + Lwt.return acc) + (Lwt.return []) group_commit + in + Lwt.return (List.rev printers)) |
