The data model
This page describes the data that a plugin works with: the library, its outlines and rows, and facets, which are how a plugin stores additional information of its own.
The library, outlines and rows
Section titled “The library, outlines and rows”The library contains everything that belongs to the person: areas, outlines, rows, saved searches and settings. An outline is a document, and a row is one line of an outline. A row can have other rows below it, which are called its children.
The fields of a row and of an outline are the following:
Row { id, outline, parent, position, title, note, marker, created, facets }Outline { id, kind, title, note, area, position, starred, archived, created, updated, share, facets }The id is a text that the app generates, and a plugin never chooses it. The outline of a row is the outline that contains it, and its parent is the row above it in the tree, or null for a row at the top level. Its position is a text that orders the row among the other rows at the same level. The marker determines how the row is drawn: bullet, numbered, or task for a to-do.
The kind of an outline is standard, inbox or journal. Its area is the area that contains it, or null, and its share is null unless the outline is shared with friends. The facets field holds the facets, which the next section describes.
Facets
Section titled “Facets”A facet is a piece of extra information that a plugin attaches to a row, to an outline or to the library as a whole. A due date, a list of tags and the number of work sessions completed on a row are all examples.
The following code defines a facet that counts the work sessions of a row, and then uses it:
const sessions = ctx.facets.define<number>({ key: 'com.example.pomodoro/sessions', // the name, written <plugin id>/<name> on: 'row', // what it is attached to: 'row', 'outline' or 'library' merge: 'counter', // what happens when two devices change it at once schema: { type: 'integer', minimum: 0 }, // the allowed values: whole numbers, zero or more});
sessions.of(row); // reads the value on a row, or nothingawait ctx.dispatch(sessions.set(row.id, 3)); // writes the valueawait ctx.dispatch(sessions.clear(row.id)); // removes the valuectx.observe(sessions.on(row.id), { new: true }, change => { /* the value changed on this row */ });The object that define returns, here sessions, is called the handle. It reads and writes the value, so the key never has to be typed again. Writing a value requires the permission write:rows.
A facet is defined by the following properties:
- a key, which is its name;
- the kind of thing it is attached to: a row, an outline or the library;
- a schema, which is a description of the values that are allowed. The app checks every value that is written against it, and it refuses a value that does not conform;
- a merge kind, which determines what happens when two devices change the value at the same time;
- a setting,
sync, that states whether the value is shared with the other devices of the person. It istrueunless the definition sayssync: false, in which case the value stays on the device where it was written.
The definition accepts three optional settings in addition: paths, which lets a search find rows by this value quickly, alias, which lets a search use the form pomodoro>3, and validate, which is a function that makes an additional check after the schema check.
The four merge kinds
Section titled “The four merge kinds”lww, which stands for “last write wins”, keeps the change that was made last.setis for values that are sets. When two devices add different elements, the result contains both additions.textis for values that are texts. The changes made on the two devices are merged into a single text.counteris for values that are numbers. The result adds up what each device added.
Rules that apply to the data of a plugin
Section titled “Rules that apply to the data of a plugin”Names. The id of a plugin is written like a web address in reverse order, for example com.example.pomodoro. The key of a facet and the id of a command both have the form <plugin id>/<name>. The features that ship with the app use tessera.<name>.
Computed information. Some information is not stored but computed from the data: lists of tags, counts, numbering, links that point back to a row, and the Today and Upcoming lists. The app updates it after every change.
Keeping data consistent. When the data of a plugin can become inconsistent, the plugin registers a function with ctx.normalize. After each change, the app calls the function and applies the repairs that it returns in the same step as the change. Calling the function a second time on the same data must not change anything.
Uninstalling a plugin. When a plugin is uninstalled, the values of its facets remain in the library and continue to be shared with the other devices. They become usable again if the plugin is installed again.
Facets that ship with the app
Section titled “Facets that ship with the app”The features that ship with the app store their data in facets as well, for example the status of a to-do, the tags of a row and a due date. A plugin can read these facets and change them. All of them are available under ctx.facets.core.
The following code reads the status of a row, and then marks the row as done, which requires the permission write:rows:
const status = ctx.facets.core.tasks.status.of(row);// for example { state: 'done' }, or undefined when the row has no status
await ctx.dispatch(ctx.facets.core.tasks.status.set(row.id, { state: 'done' }));The first column of the following table is the key under which each value is stored. A plugin does not type the key: it uses the corresponding object under ctx.facets.core, as in the code above.
| Key | Attached to | Value | When two devices change it |
|---|---|---|---|
tessera.tasks/status |
row | the state of a to-do (open, done or cancelled) and the time it was closed |
the later change wins |
tessera.tasks/priority |
row | high or low |
the later change wins |
tessera.schedule/when, tessera.schedule/due |
row | a date and, optionally, a time: the start date and the deadline | the later change wins |
tessera.schedule/repeat, tessera.schedule/alert |
row | a rule for repeating, and the number of minutes before which to alert | the later change wins |
tessera.tags/tags |
row | a set of tags | the two sets are combined |
tessera.links/targets |
row | for each link text, the outline it points to | the later change wins, link by link |
tessera.views/layout, tessera.views/cells |
outline, row | how an outline is displayed (rows, table or cards), its columns and its cells |
the later change wins |
tessera.media/attachments |
row | a list of attachments | the later change wins |
tessera.journal/day |
row | a day, written YYYY-MM-DD |
the later change wins |
tessera.sharing/assignee |
row | the id of a person | the later change wins |
