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 * string * int | References of Model.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 (Model.Identifier.of_string "entity") in let fields = Model.Content.fields (Option.get content) in Seq.fold_left (fun acc feature -> let key = "--" ^ Model.Identifier.to_string (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.Identifier.to_string (Model.Field.name feature), value) :: !actions), " Set field with the value " ) | Model.Typ.Integer -> Some ( key, Arg.Int (fun value -> actions := Integer (Model.Identifier.to_string (Model.Field.name feature), value) :: !actions), " Set field with the value " ) | Model.Typ.MonoReference target -> Some ( key, Arg.Int (fun value -> actions := Reference (target, Model.Identifier.to_string (Model.Field.name feature), value) :: !actions), " 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.Identifier.to_string (Model.Field.name feature), ids) :: !actions), " 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), " Clear the field " ); ] fields |> Arg.align let exec : string list -> t = fun _ -> let (lazy configuration) = configuration in let content = Configuration.Ast.get_content configuration (Model.Identifier.of_string "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.Identifier.to_string (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))