Repodb.Embedded.JSON

repodb · API reference

Embedded JSON documents with typed encode/decode schemas.

Embedded.Make(Json) adapts Repodb to any JSON library that satisfies the small JSON signature. The resulting module can decode an embedded JSON object into typed OCaml data, encode it back, and validate embedded payloads inside a Changeset.

Example with an application JSON module:

module Embedded = Repodb.Embedded.Make (App_json)

  type profile = { display_name : string; age : int option }

  let profile_schema =
    Embedded.schema ~name:"profile"
      ~fields:[ "display_name"; "age" ]
      ~decode:decode_profile_json
      ~encode:encode_profile_json
      ()

Use embeds_one_from_json for nullable embedded objects and embeds_many_from_json for JSON arrays. cast_embedded reads a JSON string parameter, decodes it with the schema, and stores the original JSON string in a changeset field. validate_embedded checks that an embedded field can be decoded and adds a validation error when it cannot.

Embedded values are stored as JSON text at the changeset boundary. Persist them in a Types.json field and use Json or Expr's JSON helpers when querying inside the document.

type t
val null : t
val to_string : t -> string
val of_string : string -> (t, string) result
val get_field : t -> string -> t option
val get_string : t -> string option
val get_int : t -> int option
val get_float : t -> float option
val get_bool : t -> bool option
val get_list : t -> t list option
val is_null : t -> bool
val make_object : (string * t) list -> t
val make_string : string -> t
val make_int : int -> t
val make_float : float -> t
val make_bool : bool -> t
val make_list : t list -> t