summaryrefslogtreecommitdiff
path: root/lib/model/model.mli
blob: f51386c74bb51b2531c5cd73647bf879ba35cdf7 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
(** {1 The model package}

    This package describes the content model of the application, as declared in
    the configuration file.

    The model is a tree of {b contents}. Each {!content} has an identifier, a
    list of {!section}s, and optionally sub-contents (a content with a parent is
    a subcontent). Each section groups a list of {!field}s. A field has a name,
    a label translated in several languages, a {!typ} — text, integer, or a
    reference to another content or to a static {!DataList.t} (single or
    multivalued) — and optional validation rules expressed in a small expression
    language (see {!Dsl}).

    For example, this configuration:

    {[
      { "content": [ {
          "id": "entity",
          "section": [ { "field": [
              { "name": "name",
                "label": { "en": "Name", "fr": "Nom" },
                "type": "text",
                "required": true },
              { "name": "user_type",
                "label": {},
                "type": { "content": "user_type_list", "multivaluated": true } }
          ] } ]
        },
        { "id": "user_type_list" }
      ] }
    ]}

    defines two contents: ["entity"], with a required text field ["name"] and a
    multivalued reference field ["user_type"] pointing to the content
    ["user_type_list"].

    {2:modules Modules}

    - {!Identifier} — the names of contents and fields, validated so that they
      are always safe to embed as SQL identifiers.
    - {!Content}, {!Section}, {!Field} — the structure of the model.
    - {!Typ} — the field types, and the values carried by fields.
    - {!Reference} — a link from a reference field to its target content.
    - {!DataList} — a static list declared in the configuration: referenceable,
      but not editable by the user.
    - {!Dsl} — the expression language used in validation rules and display
      criteria.

    {2:aliases Resolved types}

    The aliases at the end of this file are the model types with references
    resolved to {!target}. They are the types used everywhere outside this
    package: {!content} is what the configuration loader produces and what the
    query layer and the HTML renderer operate on. *)

module Identifier = Identifier
(** Names of contents, fields and lists.

    The type is abstract: the only way to build one is {!Identifier.of_string},
    which accepts a non-empty sequence of letters, digits, underscores and
    hyphens (rejecting in particular the double quote and the dot), so an
    identifier is always safe to embed in a quoted SQL identifier
    ({!Identifier.quote}). The fixed columns of the generated schema are
    predefined: {!Identifier.id}, {!Identifier.parent}, {!Identifier.target}.
    {!Identifier.dot} builds the composite name of the dedicated table of a
    multivalued field, e.g. ["entity.user_type"]. *)

module Content = Content
(** A content: the unit of the model, editable by the user. It has an
    {!Identifier.t} [id], a list of {!Section.t}s, a list of sub-contents, and a
    weak link to its parent. Build one with {!Content.v}; navigate with
    {!Content.parent} / {!Content.root}; iterate over all its fields with
    {!Content.fields}. *)

type target = Reference.target
(** The target of a reference field, resolved: either a lazy pointer to a
    content ({!Reference.Content}) or a static {!DataList.t}
    ({!Reference.List}). *)

type list = target Data_list.t
(** A static list, resolved: its fields are typed with {!target} *)

type content = target Content.t
(** A content : the element produced by the configuration loader and manipulated
    by the query and rendering layers. *)

module Typ = Typ
(** The type of a field, and the values a field can carry.

    A field type is one of:
    - {!Typ.Text} — a string, stored as a nullable column;
    - {!Typ.Integer} — an integer, stored as a nullable column;
    - {!Typ.MonoReference} — a reference to a single target ({!target}), stored
      as a nullable foreign key for a content, or as a plain column for a static
      list;
    - {!Typ.MultiReference} — a reference to several targets of the same kind,
      stored in a dedicated table ["content.field"].

    A {!Typ.reference} record carries the target of a reference field (a
    {!target}) plus optional [constraints] restricting the allowed values. A
    {!Typ.value} is the data provided for a field when inserting/updating:
    {!Typ.None} (leave as is), {!Typ.V} (a single value) or {!Typ.Seq} (a
    sequence of references, for multivalued fields). *)

type 'db typ = ('db, target) Typ.t
(** A field type descriptor ; ['db] is the database representation of the field
    (e.g. [string option] for a text field, [int64] for a multivalued reference)
*)

module Reference = Reference
(** A link from a reference field to its target content.

    The target is a lazy pointer ({!Reference.v} / {!Reference.get}): it is
    created while the configuration is parsed, before the target content
    necessarily exists, and resolved once all contents are registered. This is
    what allows contents to reference each other, including cyclically. *)

type reference = target Typ.reference
(** A reference field target: a {!target}, plus optional constraints *)

module DataList = Data_list
(** A static list declared in the configuration: its entries are loaded by the
    data provider and the list can be referenced from fields, but it is not
    editable by the user. Lists have their own fields (no sections); a reference
    field in a list can only target another list. *)

module Path = Path
(** Evaluation of a path through fields: from a content or a list, follow the
    fields named by the path (intermediate segments must be reference fields)
    and get the final field. *)

module Section = Section
(** A group of fields displayed together, with a per-language [label] and an
    optional [criteria] controlling when the whole section is displayed. *)

type section = target Section.t
(** A section of {!content} *)

type value = target Typ.value
(** A value provided for a field when inserting/updating data *)

module Field = Field
(** A field of a content.

    A field has a {!Identifier.t} name, a [label] as a list of
    [(language, text)] pairs, a {!Typ.field_ex} type, a [required] flag, and
    optionally a [placeholder], [validation] rules (a {!Dsl} expression plus a
    per-language error message) and a [criteria] controlling when the field is
    displayed. Build one with {!Field.create}. *)

type field = target Field.t
(** A field of {!content} *)

type field_ex = target Field.field_ex
(** The type descriptor of {!field} *)

module Dsl = Dsl
(** A small expression language, used for validation rules (["rule"] key of a
    field) and display criteria (["criteria"] key of a field or section).

    An expression is built from literals, integers, paths to values
    ({!Dsl.ValuePath}) or to other contents ({!Dsl.ContentPath}), the usual
    comparison and arithmetic operators ([=], [<>], [<], [>], [<=], [>=], [+],
    [-], [/], [and], [or]), and functions: [ucase], [trim], [length], [concat],
    [join], [nvl], [if], [matches], [substring], [uniq]. For example the rule
    ["length(:) >= 2"] checks that the field value has at least two characters.
*)