Crdt.Model_api
crdt · API reference
High-level editing API
High-level editing API.
Provides a user-friendly interface for editing CRDT documents with path-based access, transaction support, and automatic node creation.
Key features:
- Path-based navigation using JSON Pointer syntax
- Transaction support with flush/commit
- Automatic node creation for nested structures
- Type-safe node proxies for editing Usage:
let api = Model_api.create model in
(* Direct editing *)
Model_api.set api "/" (Value.String "hello");
(* String editing *)
let str_node = Model_api.str api "/text" in
Model_api.str_insert str_node ~pos:0 ~text:"hello";
(* Object editing *)
let obj_node = Model_api.obj api "/data" in
Model_api.obj_set obj_node ~key:"name" ~value:(Value.String "Alice");
(* Flush changes to patch *)
let patch = Model_api.flush apiTypes
type t = {
model : Model.t;
mutable builder : Patch_builder.t;
}An API handle for editing a model with transaction support
Creation
val create : Model.t -> tCreate an API handle for a model
val reset : t -> unitReset the transaction state, discarding any pending changes
Transaction Support
val flush : t -> Patch.t optionFlush pending operations as a patch. Returns None if no operations.
val commit : t -> Patch.t optionFlush and apply the patch to the model atomically. Returns the patch.
val has_pending : t -> boolCheck if there are pending operations
val pending_count : t -> intGet the number of pending operations
Clock Access
val current_time : t -> intGet the current logical time
val session_id : t -> intGet the session ID
val model : t -> Model.tGet the model
Path Resolution
type path_result =
| Found of Node.t (* Found the node *)
| Not_found (* Path doesn't exist *)
| Type_mismatch of string (* Wrong node type for path segment *)Result of resolving a path
val resolve_path : t -> string -> path_resultResolve a JSON Pointer path to a node
Node Access
val get : t -> string -> Node.t optionGet a node at a path, or None if not found
val get_value : t -> string -> Value.t optionGet the value at a path
val view : t -> string -> Value.t optionGet the fully resolved value at a path
Internal Helpers
val next_id : t -> Clock.timestampGet the next ID that will be assigned (before adding an op)
val add_const : t -> Value.t -> Clock.timestampAdd a new_con operation and return the ID that will be assigned
val set_root : t -> Value.t -> unitSet the document root to a value
Object Operations
type obj_proxy = {
obj_api : t;
obj_id : Clock.timestamp;
}Object proxy for editing
val obj : t -> string -> obj_proxy optionGet or create an object at a path. For an existing object, returns a proxy to edit it. For an unset val node, creates a new object and returns its proxy. The object won't exist in the model until commit is called.
val obj_set : obj_proxy -> key:string -> value:Value.t -> Clock.timestampSet a key in an object proxy. Returns the timestamp of the new value node.
val obj_delete : obj_proxy -> key:string -> unitDelete a key from an object. This sets the key to undefined.
val obj_get : obj_proxy -> key:string -> Node.t optionGet a value from the object (only works after commit)
val obj_keys : obj_proxy -> string listGet all keys in the object (only works after commit)
String Operations
type str_proxy = {
str_api : t;
str_id : Clock.timestamp;
mutable last_insert_end : Clock.timestamp option; (* End of last inserted text *)
}String proxy for editing. Tracks the last insert position for sequential appends.
val str : t -> string -> str_proxy optionGet or create a string at a path
val str_insert : str_proxy -> pos:int -> text:string -> unitInsert text at a position in a string.
parameter pos Character position (0 = start) parameter text Text to insert
val str_append : str_proxy -> text:string -> unitAppend text to a string
val str_delete : str_proxy -> pos:int -> len:int -> unitDelete text from a string.
parameter pos Start position parameter len Number of characters to delete
val str_value : str_proxy -> stringGet the current string value (only works after commit or for existing strings)
val str_length : str_proxy -> intGet the string length
Array Operations
type arr_proxy = {
arr_api : t;
arr_id : Clock.timestamp;
}Array proxy for editing
val arr : t -> string -> arr_proxy optionGet or create an array at a path
val arr_insert : arr_proxy -> pos:int -> value:Value.t -> unitInsert an element at a position in an array.
parameter pos Array index (0 = start) parameter value Value to insert
val arr_push : arr_proxy -> value:Value.t -> unitAppend an element to an array
val arr_delete : arr_proxy -> pos:int -> count:int -> unitDelete elements from an array.
parameter pos Start position parameter count Number of elements to delete
val arr_length : arr_proxy -> intGet the current array length
Binary Operations
type bin_proxy = {
bin_api : t;
bin_id : Clock.timestamp;
}Binary proxy for editing
val bin : t -> string -> bin_proxy optionGet or create a binary blob at a path
val bin_insert : bin_proxy -> pos:int -> data:bytes -> unitInsert bytes at a position
val bin_append : bin_proxy -> data:bytes -> unitAppend bytes
val bin_delete : bin_proxy -> pos:int -> len:int -> unitDelete bytes from binary.
parameter pos Start position parameter len Number of bytes to delete
val bin_value : bin_proxy -> bytesGet the current binary value
val bin_length : bin_proxy -> intGet the binary length
Vector Operations
type vec_proxy = {
vec_api : t;
vec_id : Clock.timestamp;
}Vector proxy for editing
val vec : t -> string -> vec_proxy optionGet or create a vector at a path
val vec_set : vec_proxy -> idx:int -> value:Value.t -> unitSet a slot in a vector (0-255)
val vec_get : vec_proxy -> idx:int -> Node.t optionGet a slot value from the vector
Document View
val view_document : t -> Value.tGet the complete document view
val pp_document : Format.formatter -> t -> unitPretty print the document