Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** A cursor on a JSONB document: the bytes, held for reading and editing in place, and a focus on one element. Paths are SQLite's (json1.html, section 3.1): [$] the root, [.key] or [."key"] a member, [[N]] the element at index [N] and [[#-N]] the [N]th from the end, read as sqlite/src/json.c 3.54.1 reads them, as far as the document goes and no further. Searching reads the bytes and never builds a value, and an edit moves only the bytes after it, fixing the headers of the elements around it. *)
type t = Jsonb_core.t(** The type for cursors. *)
val of_string : string -> t(** [of_string s] is a cursor on a copy of the JSONB bytes [s], focused on the root. [s] is not checked: a malformed document fails where it is read. *)
val to_string : t -> string(** [to_string c] is the bytes of [c]'s document, its edits made. *)
val root : t -> unit(** [root c] focuses [c] on the root. *)
val seek : string -> t -> (bool, Jsonb_error.path) result(** [seek path c] focuses [c] on the element [path] names from the root: [Ok true] if there is one, [Ok false], the focus unchanged, if there is none, and [Error p] if [path] fails as [p]. *)
val to_path : t -> string(** [to_path c] is the path that focused [c], ["$"] at the root. *)
val pp : t Fmt.t(** [pp] formats a cursor as {!to_path} does. *)
val sort : t -> Jsonb_sort.t option(** [sort c] is the type of the focus, [None] for a reserved type. *)
val get : 'a Jsonb_codec.t -> t -> 'a(** [get codec c] is the focus read with [codec].
@raise Catalog.Error if the focus is malformed or not what [codec] reads. *)
val length : t -> int(** [length c] is the count of elements of the focus if it is an array, 0 otherwise (jsonbArrayCount). *)
(** {1:edit Edits}
An edit writes the value through its codec at the path, as the json1 function of its name does, and leaves the focus at the root. It is [Ok ()] whether or not it wrote anything, and [Error p] if the path fails as [p]. A path that names nothing creates the objects and arrays it goes through, where the edit inserts. *)
val insert : string -> 'a Jsonb_codec.t -> 'a -> t -> (unit, Jsonb_error.path) result(** [insert path codec v c] writes [v] where [path] names nothing (json_insert).*)
val replace : string -> 'a Jsonb_codec.t -> 'a -> t -> (unit, Jsonb_error.path) result(** [replace path codec v c] writes [v] over what [path] names (json_replace).*)
val set : string -> 'a Jsonb_codec.t -> 'a -> t -> (unit, Jsonb_error.path) result(** [set path codec v c] writes [v] at [path] either way (json_set). *)
val array_insert : string -> 'a Jsonb_codec.t -> 'a -> t -> (unit, Jsonb_error.path) result(** [array_insert path codec v c] inserts [v] into an array before the element [path] names, or at its end for [[#]] (json_array_insert). *)
val remove : string -> t -> (unit, Jsonb_error.path) result(** [remove path c] removes what [path] names (json_remove); [$] empties the document. *)