summaryrefslogtreecommitdiff
path: root/lib/value/value.mli
blob: 5a8984d1b1d858b4d6ab348868c035e4b8ba0f82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(** Operations on the values stored in the database (fetching, updating, …).

    A value is one row of a content or list table in the database. *)

type value = Model.target Model.Data.any
(** The data of a field of a content or a list, whatever the type of the data
    held: an existential pack over {!Model.Data.data} *)

val none : value
(** [none]: the field is empty (or left as is, on update) *)

val single : ('db, 'ocaml, Model.target) Model.Typ.t * 'ocaml -> value
(** [single (typ, data)]: the value for the field, in its code representation
    ['ocaml] — a single value, or an [int64 Seq.t] for a multivalued reference —
    together with the type [typ] of the field *)

type 'a t
(** A value: a row of the table of the content or list ['a], identified by its
    [id].

    The parameter ['a] is the content or the static list whose table holds the
    row: [Model.content] for contents ([Model.content Value.t]) and [Model.list]
    for lists ([Model.list Value.t]).

    The type is abstract: a value is built with [v] and inspected with [content]
    and [id]. *)

val v : 'a -> int64 -> 'a t
(** Build a value from its content or list and its row id *)

val content : 'a t -> 'a
(** The content or list the value belongs to *)

val id : 'a t -> int64
[@@deprecated
  "Use [Value.get v Value.id_ref] to read the primary key as typed data"]
(** The primary key of the value in its table *)

val id_ref : Model.target Model.Typ.monoreference Model.Typ.named
(** The pre-built {!Model.Typ.named} handle of the reserved field ["id"]
    ({!Model.Identifier.id}) — an integer field present in every row, content or
    list. This is the key to read the primary key of any value. *)

val get :
  'a t -> ('db, 'ocaml, Model.target) Model.Typ.t Model.Typ.named -> 'ocaml
(** Read a field of the value and return its data directly in its code
    representation ['ocaml] — e.g. [get v id_ref : int64 option] is the primary
    key of [v], and a multivalued reference field would yield an [int64 Seq.t].

    The match between the requested named type and the readable field is proved
    by {!Model.Typ.provably_equal} on name and type. The reserved field [id_ref]
    is always readable; the other fields are read from the value's dictionary
    (see {!add_field}) and raise [Not_found] when absent. *)

val load : Caqti_lwt.connection -> Model.content t -> unit Lwt.t
(** [load db v] reads the row of [v] in the database (one query for the content
    table, plus one per multivalued reference field) and stores each field in
    [v]'s dictionary with {!add_field}, so that {!get} can then read every field
    as typed data.

    The value is modified in place — the dictionary is part of its state — so
    the function returns [unit]. The dictionary is empty when the value is built
    with {!v}: [load] is what populates it. The primary key needs no loading —
    {!get} with {!id_ref} reads it directly from the value. *)

val pp : Format.formatter -> 'a t -> unit
(** Pretty-print a value as [{ id = …; field = …; … }]: the primary key and the
    fields currently held in the value's dictionary, sorted by field name (the
    dictionary is a hashtable, sorting keeps the output stable).

    The content or list the value belongs to is not printed — its type ['a] is
    abstract here — and neither are the fields not loaded yet: only what {!load}
    (or {!add_field}) put in the dictionary is shown. *)