@yaks/journal 0.0.0-stage → 0.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +522 -2
- package/_dist/backed.d.ts +9 -0
- package/_dist/backed.d.ts.map +1 -0
- package/_dist/batch.d.ts +45 -0
- package/_dist/batch.d.ts.map +1 -0
- package/_dist/feed.d.ts +108 -0
- package/_dist/feed.d.ts.map +1 -0
- package/_dist/graph.d.ts +104 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/kept.d.ts +63 -0
- package/_dist/kept.d.ts.map +1 -0
- package/_dist/log.d.ts +142 -0
- package/_dist/log.d.ts.map +1 -0
- package/_dist/merge.d.ts +41 -0
- package/_dist/merge.d.ts.map +1 -0
- package/_dist/mod.d.ts +101 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/service.d.ts +68 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/tools.d.ts +5 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/undo.d.ts +48 -0
- package/_dist/undo.d.ts.map +1 -0
- package/_dist/value.d.ts +6 -0
- package/_dist/value.d.ts.map +1 -0
- package/_dist/views.d.ts +25 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +7 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/backed.js +225 -0
- package/backed.js.map +1 -0
- package/backed.ts +274 -0
- package/batch.js +12 -0
- package/batch.js.map +1 -0
- package/batch.ts +84 -0
- package/deno.json +1 -0
- package/feed.js +295 -0
- package/feed.js.map +1 -0
- package/feed.ts +368 -0
- package/graph.js +224 -0
- package/graph.js.map +1 -0
- package/graph.ts +329 -0
- package/held.js +25 -0
- package/held.js.map +1 -0
- package/held.ts +24 -0
- package/kept.js +61 -0
- package/kept.js.map +1 -0
- package/kept.ts +100 -0
- package/log.js +1161 -0
- package/log.js.map +1 -0
- package/log.ts +1299 -0
- package/merge.js +158 -0
- package/merge.js.map +1 -0
- package/merge.ts +185 -0
- package/mod.js +101 -0
- package/mod.js.map +1 -0
- package/mod.ts +102 -0
- package/names.js +206 -0
- package/names.js.map +1 -0
- package/names.ts +230 -0
- package/package.json +52 -4
- package/page.js +160 -0
- package/page.js.map +1 -0
- package/page.ts +233 -0
- package/service.js +118 -0
- package/service.js.map +1 -0
- package/service.ts +148 -0
- package/tools.js +110 -0
- package/tools.js.map +1 -0
- package/tools.ts +113 -0
- package/undo.js +187 -0
- package/undo.js.map +1 -0
- package/undo.ts +190 -0
- package/value.js +26 -0
- package/value.js.map +1 -0
- package/value.ts +35 -0
- package/views.js +85 -0
- package/views.js.map +1 -0
- package/views.ts +91 -0
- package/vocab.js +20 -0
- package/vocab.js.map +1 -0
- package/vocab.json +308 -0
- package/vocab.ts +22 -0
package/README.md
CHANGED
|
@@ -1,3 +1,523 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @yaks/journal
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Records graph transactions in SQL tables and provides entity history and undo,
|
|
4
|
+
and a feed that carries every commit to the other processes that opened the same
|
|
5
|
+
store. Install the journal plugin on the same database connection as the graph
|
|
6
|
+
so its records commit or roll back with the changes they describe.
|
|
7
|
+
|
|
8
|
+
A **bundle** is one entity's components as a JSON object. A **batch** is a list
|
|
9
|
+
of bundles applied in one transaction, normally one `graph.apply()` call. The
|
|
10
|
+
journal represents a recorded transaction as a `Batch`, identified by its
|
|
11
|
+
increasing `seq`. The **host** is the process that opened the graph, such as
|
|
12
|
+
`yak serve` or a CLI command.
|
|
13
|
+
|
|
14
|
+
See the [graph architecture](../graph/ARCHITECTURE.md) for the write phases.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
deno add jsr:@yaks/journal
|
|
20
|
+
# or: npx jsr add @yaks/journal
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## What it is for
|
|
24
|
+
|
|
25
|
+
Use the journal to inspect who changed an entity and its previous values,
|
|
26
|
+
reverse a recorded transaction, or consume committed changes incrementally.
|
|
27
|
+
History starts when recording is enabled; earlier state cannot be reconstructed
|
|
28
|
+
unless it was imported into the journal.
|
|
29
|
+
|
|
30
|
+
## What it records
|
|
31
|
+
|
|
32
|
+
| Table | Contents |
|
|
33
|
+
| ---------------- | ---------------------------------------------------------------- |
|
|
34
|
+
| `journal_tx` | Transaction sequence, timestamp, actor references, host and note |
|
|
35
|
+
| `journal_change` | Ordered component upserts/removals, or an entity deletion |
|
|
36
|
+
| `journal_field` | Ordered property values after each change |
|
|
37
|
+
| `journal_word` | The label of each name a row records, from the vocabulary |
|
|
38
|
+
|
|
39
|
+
An empty component still has a change row. Component removal records null values
|
|
40
|
+
for the fields known to the journal, keeping their history continuous across
|
|
41
|
+
removal and recreation. Previous values are reconstructed from the entity's own
|
|
42
|
+
indexed history rather than stored alongside each new value.
|
|
43
|
+
|
|
44
|
+
A change records its component by the component's identity, the `$id` its
|
|
45
|
+
vocabulary gives it (@yaks/vocab), and each value by its property's identity; a
|
|
46
|
+
word without one is recorded by its label. Every reader gets labels back, as the
|
|
47
|
+
vocabulary reads those identities now, so a renamed word's history reads under
|
|
48
|
+
its new label. Rows written before identities name each word by its label and
|
|
49
|
+
read the same way, until every store's journal is rewritten to identities. The
|
|
50
|
+
log is given the vocabulary for this: `log({ rows, vocab })`, or the host's
|
|
51
|
+
through `@yaks/journal/graph`. A query reads labels from `journal_word`, which
|
|
52
|
+
`ddl(vocab)` writes from the vocabulary each time the schema is installed, so a
|
|
53
|
+
statement over the journal stays short however large the vocabulary is.
|
|
54
|
+
|
|
55
|
+
Labels come back in the reader's namespace: a log or `backed()` given
|
|
56
|
+
`vocab.in(ns)` names each component as that namespace reads it, `cost` for
|
|
57
|
+
trip's own and `@yaks/model:cost` for the platform's (@yaks/vocab, "Reading in a
|
|
58
|
+
namespace"). `journal_word` holds the store's own labels, and a reading writes
|
|
59
|
+
into its statements only the few components it labels otherwise. A property
|
|
60
|
+
reads by one label in every namespace.
|
|
61
|
+
|
|
62
|
+
A property the store keeps in another form, such as the address @yaks/blob keeps
|
|
63
|
+
in place of a body, is recorded as stored and read back through the store's read
|
|
64
|
+
overrides (`log({ derived })`; `@yaks/journal/graph` binds the host's). History
|
|
65
|
+
and undo then hold the text a reader of the graph sees.
|
|
66
|
+
|
|
67
|
+
The tables hold no rows of the entity table and are not included in graph
|
|
68
|
+
snapshots. Normal writes append records; explicit redaction methods can modify
|
|
69
|
+
stored history. The graph reads the records as entities, below.
|
|
70
|
+
|
|
71
|
+
The plugin's `journal` hook runs inside the graph's transaction. It opens no
|
|
72
|
+
transaction and uses the synchronous `rows(statement)` callback supplied to
|
|
73
|
+
`log()`, which runs a @yaks/sql statement and returns its rows. Atomic recording
|
|
74
|
+
requires that callback to use the graph's active transaction on the same
|
|
75
|
+
connection. A separate unrelated database connection does not provide that
|
|
76
|
+
guarantee.
|
|
77
|
+
|
|
78
|
+
## What it leaves out
|
|
79
|
+
|
|
80
|
+
A component its vocabulary declares `journal: false` (@yaks/vocab) is not
|
|
81
|
+
recorded: a process's own bookkeeping rather than anyone's data, such as the
|
|
82
|
+
`created` and `updated` stamps (@yaks/provenance), which the transaction row
|
|
83
|
+
already records, an effect run or a lease (@yaks/effects). Such a write has no
|
|
84
|
+
history and no undo, though the feed still carries it to the other hosts on the
|
|
85
|
+
store (below). Deleting an entity that held only such components is not recorded
|
|
86
|
+
either, so it leaves no trace at all; the plugin's one read before a write, on
|
|
87
|
+
the `precondition` phase, is what each entity a bundle deletes holds. A
|
|
88
|
+
transaction with nothing left to record writes no row.
|
|
89
|
+
|
|
90
|
+
A store whose history is about a few of its components, beside others it writes
|
|
91
|
+
often, names them: `journal(log, vocab, { components: ['space'] })`, or
|
|
92
|
+
`{"use": "@yaks/journal", "with": {"components": ["space"]}}` in a config. Every
|
|
93
|
+
other component is left out as a `journal: false` one is.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { assertEquals } from '@std/assert'
|
|
97
|
+
import { graph } from '@yaks/graph'
|
|
98
|
+
import { statements, storage } from '@yaks/sqlite'
|
|
99
|
+
import { open } from '@yaks/sqlite/db'
|
|
100
|
+
import { loadVocab } from '@yaks/vocab'
|
|
101
|
+
import { ddl, journal } from '@yaks/journal'
|
|
102
|
+
import { logFor } from '@yaks/journal/graph'
|
|
103
|
+
|
|
104
|
+
let vocab = loadVocab([{
|
|
105
|
+
$defs: {
|
|
106
|
+
page: { component: true, properties: { title: { type: 'string' } } },
|
|
107
|
+
render: {
|
|
108
|
+
component: true,
|
|
109
|
+
journal: false,
|
|
110
|
+
properties: { state: { type: 'string' } },
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
}])
|
|
114
|
+
let driver = open(':memory:')
|
|
115
|
+
let store = storage(driver, vocab)
|
|
116
|
+
store.install()
|
|
117
|
+
for (let s of ddl(vocab)) driver.query(s)
|
|
118
|
+
let j = logFor({ storage: { statements: statements(driver) }, vocab })
|
|
119
|
+
let g = graph({ storage: store, vocab, plugins: [journal(j, vocab)] })
|
|
120
|
+
|
|
121
|
+
g.apply([
|
|
122
|
+
{ entity: { eid: 'p1' }, page: { title: 'Kickoff' } },
|
|
123
|
+
{ entity: { eid: 'r1' }, render: { state: 'owed' } },
|
|
124
|
+
])
|
|
125
|
+
g.apply([{ entity: { eid: 'r1' }, $delete: true }])
|
|
126
|
+
assertEquals(j.since(0).map((e) => e.patches.map((p) => p.target)), [['p1']])
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Read as entities
|
|
130
|
+
|
|
131
|
+
The vocabulary declares two computed components, read from these tables and
|
|
132
|
+
never written:
|
|
133
|
+
|
|
134
|
+
| Component | Properties | One per |
|
|
135
|
+
| --------- | -------------------------------------------- | -------------------------------------- |
|
|
136
|
+
| `_tx` | `seq`, `at`, `by`, `via`, `host`, `note` | row of `journal_tx`: a transaction |
|
|
137
|
+
| `_change` | `tx`, `target`, `comp`, `component`, `value` | row of `journal_change`: one operation |
|
|
138
|
+
|
|
139
|
+
`_change.value` is the operation's after-image, the properties it wrote as one
|
|
140
|
+
object, keyed by the properties' labels; it is absent where the operation
|
|
141
|
+
removed the component. `comp` refers to the `_comp` entity describing the
|
|
142
|
+
component (@yaks/vocab), the one whose eid is the component's identity, and a
|
|
143
|
+
deletion is the change whose `comp` is `entity`'s; where the graph describes no
|
|
144
|
+
such component (no @yaks/vocab, or a component since removed) a change has no
|
|
145
|
+
`comp`. `component` is the component's label whatever the graph describes,
|
|
146
|
+
`entity` for a deletion. `tx` refers to the `_tx` entity, and `target`, `by` and
|
|
147
|
+
`via` to entities of the graph.
|
|
148
|
+
|
|
149
|
+
A scoped read (a person's on yaks.app, or the API's on the box) sees a change
|
|
150
|
+
when its scope admits the change's target and the component it wrote, and a
|
|
151
|
+
transaction when it sees one of its changes. A deleted entity is judged as its
|
|
152
|
+
history last told it was (`_change`'s `held`, @yaks/sql `ghostly`): a scope sees
|
|
153
|
+
it, its history and its existence, when it would have seen it then. A deleted
|
|
154
|
+
memory's history reads for whoever saw the memory, and a deleted draft stays
|
|
155
|
+
hidden from everyone but its author.
|
|
156
|
+
|
|
157
|
+
So history is asked in the query grammar, at every door: `._change.target=T-5`
|
|
158
|
+
is an entity's history, oldest first, and `._change.target=T-5&.limit=20` its
|
|
159
|
+
latest twenty; `._change.comp=<its _comp>&.limit=20` is a component's latest
|
|
160
|
+
twenty writes, sought through the index on `component` however rarely it is
|
|
161
|
+
written; `._tx.via=S-7` is what one session wrote, and `._change.tx._tx.via=S-7`
|
|
162
|
+
the changes it made. Each record's eid is its row id written ahead of a tag for
|
|
163
|
+
the store and the component, so `yak graph show` and a link open one directly.
|
|
164
|
+
|
|
165
|
+
`@yaks/journal/graph`'s `schema()` is the journal's tables as statements, which
|
|
166
|
+
a writing host adds with the rest of its composed schema (@yaks/sqlite
|
|
167
|
+
`follow`). The host supplies `storage.statements`, the synchronous statement
|
|
168
|
+
capability from `@yaks/sql`; storage without that capability cannot bind a
|
|
169
|
+
journal. Journal statements participate in the graph's storage transaction, so a
|
|
170
|
+
rollback removes both the graph write and its history. Its `plugins(host)` only
|
|
171
|
+
binds transaction hooks.
|
|
172
|
+
|
|
173
|
+
`backed(vocab)`, exported by the root and by `@yaks/journal/vocab`, is what a
|
|
174
|
+
store reads them through: `storage(driver, vocab, { backed: backed(vocab) })`
|
|
175
|
+
(@yaks/sql `Backing`). A host composed from a config passes it. Nothing is
|
|
176
|
+
copied: a query reads the journal's tables through their indexes, and the
|
|
177
|
+
journal records no transaction of its own. A subscription over one is read again
|
|
178
|
+
after every commit.
|
|
179
|
+
|
|
180
|
+
## Who a recorded write is attributed to
|
|
181
|
+
|
|
182
|
+
The graph passes the resolved `$actor` to the journal: `by` identifies whom the
|
|
183
|
+
write acts for, and `via` identifies the session, connector or other entity
|
|
184
|
+
through which it was made. Both are stored as references into `entity`, then
|
|
185
|
+
read back as public eids. Create those entities before attributing writes to
|
|
186
|
+
them. Unspecified actors are recorded as null. A batch whose bundles name more
|
|
187
|
+
than one writer is recorded as one transaction per writer, each holding what its
|
|
188
|
+
entities' bundles wrote (@yaks/graph `writers`).
|
|
189
|
+
|
|
190
|
+
Authentication and actor selection belong to the API, CLI or tool caller; the
|
|
191
|
+
journal records what the graph passes to it.
|
|
192
|
+
|
|
193
|
+
The SQL `trace` column is an optional note. The graph plugin leaves it unset;
|
|
194
|
+
`log.write({ at, by, via, note }, patches)` can supply it directly.
|
|
195
|
+
|
|
196
|
+
Trusted maintenance writes can repair malformed historical identities with
|
|
197
|
+
`{ entity: { eid: destination }, $journal_from: 'legacy-writer' }`. The
|
|
198
|
+
destination is a generated eid, already present or created by the same batch;
|
|
199
|
+
the source is its exact stored, non-generated spelling. The journal updates its
|
|
200
|
+
actor, instrument and change-target references inside the graph transaction,
|
|
201
|
+
preserving transaction order, timestamps and recorded values. It leaves ordinary
|
|
202
|
+
graph references unchanged. A missing source is a no-op. `$prune` refuses while
|
|
203
|
+
journal references remain, so repair history before pruning the source.
|
|
204
|
+
|
|
205
|
+
## In a server
|
|
206
|
+
|
|
207
|
+
Add the package to a compatible `yak` plugin configuration:
|
|
208
|
+
|
|
209
|
+
```json
|
|
210
|
+
{ "plugins": ["@yaks/kernel", "@yaks/provenance", "@yaks/journal"] }
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
`@yaks/journal/graph` creates the SQL tables on the host's connection and
|
|
214
|
+
returns the journal plugin. `@yaks/journal/vocab` declares `_tx`, `_change`, the
|
|
215
|
+
`history` tool and the branch tools, and `@yaks/journal/tools` implements them.
|
|
216
|
+
|
|
217
|
+
```sh
|
|
218
|
+
yak history T-5 -n 10
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
The tool returns the entity's latest transactions first, each as the patch
|
|
222
|
+
applied to that entity, with `updated: { at, by, via }` metadata. Entity
|
|
223
|
+
deletion appears as `$delete`. A transaction that changed other entities
|
|
224
|
+
includes only the requested entity's changes in this response. The low-level
|
|
225
|
+
`history()` method instead returns transactions oldest first.
|
|
226
|
+
|
|
227
|
+
## Attribution and versions
|
|
228
|
+
|
|
229
|
+
Naming an aspect in the plugin's options turns its history to those aspects,
|
|
230
|
+
each covering every component not declared `journal: false`, and each on unless
|
|
231
|
+
set false:
|
|
232
|
+
|
|
233
|
+
```json
|
|
234
|
+
{ "use": "@yaks/journal", "with": { "attribution": true, "versions": true } }
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
- `attribution` keeps the `journal_tx` row of each transaction and a
|
|
238
|
+
`journal_change` row for each component it wrote or removed, and no values.
|
|
239
|
+
- `versions` has the store keep every version a write replaces
|
|
240
|
+
([@yaks/sqlite's history](../sqlite/README.md#history)), each naming its
|
|
241
|
+
`journal_tx` row's id. A store keeping versions requires the journal from then
|
|
242
|
+
on (`required`), and can be read as of a transaction or on a branch:
|
|
243
|
+
`YAK_AT=<id>` or `YAK_BRANCH=<id>` opens a `yak` command's graph that way.
|
|
244
|
+
`branch_new`, `branch_list` and `branch_delete` make, list and trash branches,
|
|
245
|
+
and `graph_query`, `graph_show` and `graph_apply` read and write on one given
|
|
246
|
+
its `branch`, which main never sees and no effect reaches:
|
|
247
|
+
|
|
248
|
+
```sh
|
|
249
|
+
yak branch new
|
|
250
|
+
yak graph apply --branch 1 --bundles '[{"entity":{"eid":"$d"},"doc":{"title":"draft"}}]'
|
|
251
|
+
yak graph query .doc.title=draft --branch 1
|
|
252
|
+
yak branch delete 1
|
|
253
|
+
yak branch list --trash
|
|
254
|
+
yak branch restore 1
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Trashing retains every version indefinitely. `branch_list --trash` finds
|
|
258
|
+
trashed branches and `branch_restore` returns them with their writes intact.
|
|
259
|
+
|
|
260
|
+
`branch_merge` writes what a branch changed since its fork to main as one
|
|
261
|
+
batch (./merge.ts). Each property merges against its value at the fork: one
|
|
262
|
+
only the branch changed takes the branch's value, one only main changed keeps
|
|
263
|
+
main's. One both changed to different values is a conflict, and nothing merges
|
|
264
|
+
by itself: while any conflict has no pick, nothing is written, and the answer
|
|
265
|
+
lists each conflict with its value at the fork, on the branch and on main side
|
|
266
|
+
by side. A removed component or a deleted entity against the other side's edit
|
|
267
|
+
is a conflict too. Stamped properties are main's to stamp on the merge's
|
|
268
|
+
write, and each property written carries main's value as its `$was`, so a main
|
|
269
|
+
that moved since refuses the merge whole. The branch is left as it is.
|
|
270
|
+
|
|
271
|
+
```sh
|
|
272
|
+
yak branch merge 1
|
|
273
|
+
yak branch merge 1 --pick '[{"eid":"<eid>","comp":"doc","prop":"title","take":"branch"}]'
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
Naming neither is the journal before versions: attribution, with every value as
|
|
277
|
+
a `journal_field` row.
|
|
278
|
+
|
|
279
|
+
## What it keeps
|
|
280
|
+
|
|
281
|
+
Once a UTC day, the duty `@yaks/journal/service` measures the store's file and
|
|
282
|
+
keeps the day in a `kept{day, at, bytes, free, tables, changes, file}` row: the
|
|
283
|
+
file's bytes, how many of them are free pages, each table's `[bytes, rows]` with
|
|
284
|
+
its indexes' bytes folded into its own, the changes the journal held per
|
|
285
|
+
component name (the log's `held()`, one pass over the index on `component`; a
|
|
286
|
+
deletion counts under `entity`), and the file it measured (@yaks/file's
|
|
287
|
+
`file{path, root}`, from the storage's `file`), so a machine's Disks tab marks
|
|
288
|
+
the graph's own file with its free pages. A store whose storage has no `measure`
|
|
289
|
+
(one that keeps no file of pages) is not measured, and a day's row measured
|
|
290
|
+
before it counted the changes is measured again. History is the journal's tables
|
|
291
|
+
(`journal_*`) and each component's versions (`<comp>$versions`).
|
|
292
|
+
|
|
293
|
+
The browsing app's sidebar offers the History page (`@yaks/journal/views`),
|
|
294
|
+
which reads only those rows, a month of them, and counts nothing itself: what
|
|
295
|
+
history keeps against the whole file, what it kept per change written and per
|
|
296
|
+
day between measurements, the changes the journal held for each component beside
|
|
297
|
+
what the component keeps in its own table and its versions, and every table,
|
|
298
|
+
largest first. A component opens its page, which lists its recent writes.
|
|
299
|
+
|
|
300
|
+
```ts
|
|
301
|
+
import { changes, history, perChange } from '@yaks/journal/kept'
|
|
302
|
+
import { equal } from '@yaks/testing'
|
|
303
|
+
|
|
304
|
+
let day = (at: string, journal: number, held: number) => ({
|
|
305
|
+
day: at.slice(0, 10),
|
|
306
|
+
at,
|
|
307
|
+
bytes: 1e9,
|
|
308
|
+
free: 0,
|
|
309
|
+
tables: { doc: [5e8, 1e6], journal_change: [journal, held] },
|
|
310
|
+
})
|
|
311
|
+
let monday = day('2026-10-19T04:00:00Z', 1_000_000, 10_000)
|
|
312
|
+
let tuesday = day('2026-10-20T04:00:00Z', 2_470_000, 20_000)
|
|
313
|
+
equal([history(tuesday), changes(tuesday)], [2_470_000, 20_000])
|
|
314
|
+
equal(perChange(monday, tuesday), 147)
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
## What it returns
|
|
318
|
+
|
|
319
|
+
Here the host lends the journal a statement capability over the same storage as
|
|
320
|
+
the graph. The SQLite adapter binds it to an in-memory database.
|
|
321
|
+
|
|
322
|
+
```ts
|
|
323
|
+
import { assertEquals } from '@std/assert'
|
|
324
|
+
import { graph } from '@yaks/graph'
|
|
325
|
+
import { statements, storage } from '@yaks/sqlite'
|
|
326
|
+
import { open } from '@yaks/sqlite/db'
|
|
327
|
+
import { loadVocab } from '@yaks/vocab'
|
|
328
|
+
import { ddl, journal, undo } from '@yaks/journal'
|
|
329
|
+
import { logFor } from '@yaks/journal/graph'
|
|
330
|
+
|
|
331
|
+
let title = { type: 'string' }
|
|
332
|
+
let vocab = loadVocab([{
|
|
333
|
+
$defs: { page: { component: true, properties: { title } } },
|
|
334
|
+
}])
|
|
335
|
+
let driver = open(':memory:')
|
|
336
|
+
let store = storage(driver, vocab)
|
|
337
|
+
store.install()
|
|
338
|
+
for (let s of ddl(vocab)) driver.query(s)
|
|
339
|
+
let j = logFor({ storage: { statements: statements(driver) }, vocab })
|
|
340
|
+
let g = graph({ storage: store, vocab, plugins: [journal(j, vocab)] })
|
|
341
|
+
|
|
342
|
+
g.apply([{ entity: { eid: 'p1' }, page: { title: 'Kickoff' } }])
|
|
343
|
+
g.apply([{ entity: { eid: 'p1' }, page: { title: 'Retro' } }])
|
|
344
|
+
|
|
345
|
+
let history = j.history('p1')
|
|
346
|
+
let changed = history.at(-1)!
|
|
347
|
+
undo(g, j)(changed.seq) // restores 'Kickoff' and records the undo
|
|
348
|
+
let entries = j.since(0) // Entry[], oldest first
|
|
349
|
+
assertEquals(store.read('.page')[0].page, { title: 'Kickoff' })
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
`Batch` carries `seq`, `at`, `by`, `via` and `deltas`. Each `Delta` identifies a
|
|
353
|
+
`target` eid, `comp`, optional `prop`, and `before`/`after` values. A null
|
|
354
|
+
`prop` describes a whole component's addition or removal.
|
|
355
|
+
|
|
356
|
+
`Entry` is the recorded operation format: `seq`, attribution, `note` and
|
|
357
|
+
`patches`. Each `Patch` contains `target`, `comp` and `value`, with null meaning
|
|
358
|
+
removal. `since()` returns entries, while `at()` and `history()` reconstruct
|
|
359
|
+
batches with before/after deltas.
|
|
360
|
+
|
|
361
|
+
| Method | Result |
|
|
362
|
+
| ------------------------------------------------------ | ------------------------------------------------------------ |
|
|
363
|
+
| `j.history(eid, n?)` | Latest `n` transactions for an entity, returned oldest first |
|
|
364
|
+
| `j.entries(eid, n?)` | Latest entries for an entity, newest first |
|
|
365
|
+
| `j.by(via, n?)` | Entries written through an instrument, newest first |
|
|
366
|
+
| `j.since(cursor?, n?)` | Entries strictly after the cursor, oldest first |
|
|
367
|
+
| `j.at(seq)` | One reconstructed `Batch`, or `undefined` |
|
|
368
|
+
| `j.patches(seq, target?)` | Recorded operations, optionally restricted to one entity |
|
|
369
|
+
| `j.before(eid, seq)` | Components just before a transaction |
|
|
370
|
+
| `j.latest(eid)`, `j.tip()` | Latest sequence for an entity or the whole log |
|
|
371
|
+
| `j.touchedSince(eid, seq)` | Whether the entity changed after a sequence |
|
|
372
|
+
| `j.wrote(comp, prop)` | Recorded values for a property across entities |
|
|
373
|
+
| `j.seek(text)` | Recorded values containing text |
|
|
374
|
+
| `j.scrubValue(field, value)`, `j.scrubRef(field, ref)` | Explicit history redaction |
|
|
375
|
+
| `j.holds(ref)` | Whether history still references stored content |
|
|
376
|
+
|
|
377
|
+
To turn an entry into graph bundles, load its batch with `j.at(entry.seq)` and
|
|
378
|
+
pass that to `applied(batch)`. `applied()` reconstructs the bundles without
|
|
379
|
+
reading current graph state. It does not repeat transaction attribution in the
|
|
380
|
+
returned bundles.
|
|
381
|
+
|
|
382
|
+
The consumer owns its cursor and delivery policy. Saving the cursor before doing
|
|
383
|
+
the work can lose work after a crash; saving it afterward can repeat work. The
|
|
384
|
+
journal alone does not guarantee exactly-once external effects.
|
|
385
|
+
|
|
386
|
+
## Other hosts' commits
|
|
387
|
+
|
|
388
|
+
A graph's `effect` phase runs only for the commits that graph made. Another
|
|
389
|
+
process or thread that opened the same store — a `yak` command beside
|
|
390
|
+
`yak serve`, a worker serving the effects role — commits where it cannot see.
|
|
391
|
+
The **feed** is how one host hears another: each commit writes one row to the
|
|
392
|
+
`journal_feed` table inside its own transaction, holding the host that made it
|
|
393
|
+
and what it wrote of each entity: the properties of each component it wrote,
|
|
394
|
+
`null` for one it removed, or `$delete` (`wrote(bundles)`). The values stay
|
|
395
|
+
where the graph keeps them. A follower pages through the feed from a cursor,
|
|
396
|
+
keeps what someone else wrote, and reads its values from the graph in one `get`
|
|
397
|
+
(`filled(heard, read)`):
|
|
398
|
+
|
|
399
|
+
```ts ignore
|
|
400
|
+
let f = feedOf({ rows }) // writes as a fresh host; feeder(f) is its plugin
|
|
401
|
+
let next = follow(f) // from the feed's tip
|
|
402
|
+
// …another host commits…
|
|
403
|
+
let heard = next() // [{ seq, wrote: [{ entity: { eid: 'p1' }, page: ['title'] }] }]
|
|
404
|
+
filled(heard.map((h) => h.wrote), (eids, comps) => store.get(eids, comps))
|
|
405
|
+
// [[{ entity: { eid: 'p1' }, page: { title: 'Hi' } }]]
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
A follower reads the values as the graph holds them when it looks, not as the
|
|
409
|
+
commit wrote them: two commits to a title heard in one look both carry the newer
|
|
410
|
+
one, and a component since removed reads as removed. The commit that changed it
|
|
411
|
+
is heard too, so a cache patched with the news ends where the graph is.
|
|
412
|
+
|
|
413
|
+
The feed is plumbing, not history. It carries every commit, journaled or not, so
|
|
414
|
+
a lease another process renews reaches this host's subscriptions as a task's new
|
|
415
|
+
title does. A row is kept for ten minutes (`KEEP`) and pruned by a later write,
|
|
416
|
+
so the table holds minutes of commits rather than growing with them. A follower
|
|
417
|
+
that was away longer than that is told how many commits it missed.
|
|
418
|
+
|
|
419
|
+
A host composes plugins in config order, and `@yaks/journal/graph`'s `plugins`
|
|
420
|
+
returns the journal's and the feed's, bound as the same host (`logFor`,
|
|
421
|
+
`feedFor`). Its `feed` is what a host offers as `host.feed(each)`: it looks
|
|
422
|
+
every 200ms (`{"use": "@yaks/journal", "with": {"every": 50}}` to change it),
|
|
423
|
+
however long `each` takes, and hands `each` the **news**: every commit heard
|
|
424
|
+
since its last call, oldest first, in one call (`{ applied, missed }`,
|
|
425
|
+
@yaks/graph `News`). A consumer still busy is handed more next time rather than
|
|
426
|
+
left behind what the feed keeps. News `each` fails on is offered again at the
|
|
427
|
+
next look, up to five times, before it is reported to the host and passed over.
|
|
428
|
+
When commits never reach the consumer, pruned or passed over, the next news says
|
|
429
|
+
`missed: true`, and the consumer reads again what it holds: @yaks/api's `/ws`
|
|
430
|
+
subscriptions reset every answer (`subs.hear`), and a web's duty coverage reads
|
|
431
|
+
the processes again.
|
|
432
|
+
|
|
433
|
+
A store an older journal made gains the `host` column and the feed's table the
|
|
434
|
+
next time a writing host opens it (`schema()`, @yaks/sqlite `grow`).
|
|
435
|
+
|
|
436
|
+
## Undo
|
|
437
|
+
|
|
438
|
+
`undo(g, j)(seq, actor?)` reconstructs an inverse and applies it through the
|
|
439
|
+
graph, with trusted access to server-owned properties. It restores previous
|
|
440
|
+
property values and removed components, removes components created by the
|
|
441
|
+
original transaction, brings back every entity it deleted, and deletes again
|
|
442
|
+
every entity it brought back. The undo is validated, stamped and journaled as
|
|
443
|
+
another write; undoing that write provides redo.
|
|
444
|
+
|
|
445
|
+
An entity the transaction deleted comes back with the components the journal
|
|
446
|
+
recorded it holding. That write clears its tombstone, and the entity keeps its
|
|
447
|
+
eid and number (@yaks/graph). Whatever the delete cascaded is in the same
|
|
448
|
+
transaction and comes back with it: the entities deleted with it, edges
|
|
449
|
+
included, the references it detached and the components it released.
|
|
450
|
+
|
|
451
|
+
Property deltas restored by `undo()` carry `$was` preconditions hashed from the
|
|
452
|
+
original transaction's after-values, and a component or entity it brings back
|
|
453
|
+
carries one saying each of its properties is still empty. If any of them changed
|
|
454
|
+
in the meantime, the graph rejects the reversal. A component the undo removes
|
|
455
|
+
whole carries no guard, so this is not a general conflict check for every
|
|
456
|
+
possible intervening edit.
|
|
457
|
+
|
|
458
|
+
An undone delete cannot give back:
|
|
459
|
+
|
|
460
|
+
- Components the journal leaves out, such as the `created` and `updated` stamps.
|
|
461
|
+
The entity comes back stamped by the undo: its `created` names the undo's
|
|
462
|
+
writer and time.
|
|
463
|
+
- Components written before the journal began. If the journal recorded none of a
|
|
464
|
+
deleted entity's components, `undone()` and `undo()` throw `Final` naming it,
|
|
465
|
+
and nothing is written. If it recorded some, the entity comes back with those,
|
|
466
|
+
and nothing tells the missing rest apart.
|
|
467
|
+
|
|
468
|
+
`undone(batch, { guard? })` builds the inverse without applying it; guards are
|
|
469
|
+
off by default there. To reverse part of a transaction, cut `batch.deltas` to
|
|
470
|
+
the entities wanted before passing it. `applied(batch)` reconstructs the forward
|
|
471
|
+
bundles.
|
|
472
|
+
|
|
473
|
+
A nonexistent sequence makes `undo()` throw an `Error`; a batch with no
|
|
474
|
+
reversible changes returns `[]`.
|
|
475
|
+
|
|
476
|
+
## Limitations and recording rules
|
|
477
|
+
|
|
478
|
+
- History can reconstruct only recorded state. Enabling the plugin after data
|
|
479
|
+
already exists does not capture that data's earlier values.
|
|
480
|
+
- Entity deletion is stored as an entity removal; history expands the known
|
|
481
|
+
components and a tombstone from that entity's journal records, and a later
|
|
482
|
+
write that gives the entity a component reads as the tombstone going. The
|
|
483
|
+
retained entity row keeps references valid after deletion.
|
|
484
|
+
- Components declared `journal: false`, including the `created` and `updated`
|
|
485
|
+
stamps, are not restored by undo ("Undo", above), and an entity holding only
|
|
486
|
+
them has no history ("What it leaves out", above).
|
|
487
|
+
- An optional `log({ cas })` configuration records selected text by a reference
|
|
488
|
+
to a content store. Supply its lookup layout, selection function and writer;
|
|
489
|
+
this is not enabled automatically by `plugins(host)`.
|
|
490
|
+
- The journal records changes rather than complete snapshots. Back up its
|
|
491
|
+
tables, the graph and any referenced content together when preserving history.
|
|
492
|
+
|
|
493
|
+
## Exports
|
|
494
|
+
|
|
495
|
+
The root exports `ddl`, `dropDdl`, `log`, `journal`, `backed`, `applied`,
|
|
496
|
+
`undone`, `undo`, `Final`, `merge`, `merged`, value encoding helpers, and types
|
|
497
|
+
including `Log`, `LogOpts`, `Batch`, `Entry`, `Patch`, `Delta`, `Cas`, `Merged`,
|
|
498
|
+
`Conflict` and `Choice`. `dropDdl()` drops what `ddl(vocab)` created, for a
|
|
499
|
+
store that stops keeping a journal.
|
|
500
|
+
|
|
501
|
+
| Sub-module export | Purpose |
|
|
502
|
+
| ----------------------- | ---------------------------------------------------------------------- |
|
|
503
|
+
| `@yaks/journal/vocab` | `journalDoc`, `docs` and `backed`: `_tx`, `_change`, `kept`, `history` |
|
|
504
|
+
| `@yaks/journal/graph` | `schema()` its tables; `plugins(host)` the hooks; `logFor(host)` a log |
|
|
505
|
+
| `@yaks/journal/tools` | `runs(host)` implements the history and branch tools, merge included |
|
|
506
|
+
| `@yaks/journal/service` | the daily duty that keeps a `kept` row |
|
|
507
|
+
| `@yaks/journal/views` | the History page and its place in the sidebar |
|
|
508
|
+
| `@yaks/journal/kept` | reading a `kept` row: history's bytes, changes, per change and per day |
|
|
509
|
+
|
|
510
|
+
## Compatibility
|
|
511
|
+
|
|
512
|
+
The journal builds its statements as @yaks/sql nodes over the graph's entity
|
|
513
|
+
table and needs a synchronous runner for them. It imports no platform-specific
|
|
514
|
+
storage API, so a suitable caller-supplied binding can run it in Deno, Node or a
|
|
515
|
+
browser.
|
|
516
|
+
|
|
517
|
+
A person's confirmed permanent purge calls `purgeEntities(graph, eids)` from
|
|
518
|
+
`@yaks/graph/trash`. The journal's `purge` callback removes fields and changes
|
|
519
|
+
for those exact subjects and clears their actor references. Other subjects in
|
|
520
|
+
the same transaction remain readable. Versioned storage erases the subjects'
|
|
521
|
+
versions separately in the same operation; pooled bytes are released only after
|
|
522
|
+
all surviving history and trash references have been checked. Ordinary deletion
|
|
523
|
+
retains both history and recoverable trash.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Derived } from "@yaks/sql/core";
|
|
2
|
+
import type { Backings } from "@yaks/sql";
|
|
3
|
+
import type { Vocab } from "@yaks/vocab";
|
|
4
|
+
/** The rows `_tx` and `_change` are read from: the journal's tables, a value
|
|
5
|
+
* the store keeps in another form read through `derived`, the store's read
|
|
6
|
+
* overrides. `_change` also tells what each entity last held (`held`). One
|
|
7
|
+
* registry per vocabulary and overrides, so every store given them compiles
|
|
8
|
+
* its reads once (@yaks/sql `Backing`). */ export declare let backed: (vocab: Vocab, derived?: Derived) => Backings;
|
|
9
|
+
//# sourceMappingURL=backed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backed.d.ts","sources":["../backed.ts"],"names":[],"mappings":"AA2BA,SAKE,KAAK,OAAO,8BAcoB;AAClC,cAAc,QAAQ,yBAAoC;AAC1D,cAAc,KAAK,2BAAgC;AA8JnD;;;;yCAIyC,GACzC,OAAO,YAAI,SAAU,OAAO,OAAO,UAAS,YAAiB,SAM5D"}
|
package/_dist/batch.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Comp, Eid } from "@yaks/graph";
|
|
2
|
+
/**
|
|
3
|
+
* One thing that moved. A delta with a `prop` names the property that moved and
|
|
4
|
+
* carries the value on each side of the write; a delta with no property is
|
|
5
|
+
* about the component as a whole — `after` set means the component appeared,
|
|
6
|
+
* `before` set means it went, and either way the set side holds the properties
|
|
7
|
+
* it had. A `tombstone` delta is about the entity itself: `after` set means it
|
|
8
|
+
* was deleted, `before` set means a write brought it back.
|
|
9
|
+
*/ export type Delta = {
|
|
10
|
+
/** the entity that changed */ target: Eid;
|
|
11
|
+
/** the component that changed */ comp: string;
|
|
12
|
+
/** the property that moved, or `null` for the whole component */ prop: string | null;
|
|
13
|
+
/** what it held before (`null` for nothing) */ before: unknown;
|
|
14
|
+
/** what it holds after (`null` for nothing) */ after: unknown;
|
|
15
|
+
};
|
|
16
|
+
/** One committed transaction: where it sits in the log, who wrote it, and what
|
|
17
|
+
* moved. */ export type Batch = {
|
|
18
|
+
/** its place in the total order — the cursor a feed pages by */ seq: number;
|
|
19
|
+
/** when it committed, ISO-8601 */ at: string;
|
|
20
|
+
/** the identity it was written for, from the transaction's `$actor` */ by: Eid | null;
|
|
21
|
+
/** the instrument it was written through */ via: Eid | null;
|
|
22
|
+
/** what moved, in the order it moved */ deltas: Delta[];
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* One recorded operation: a component patched to `value`, or removed when
|
|
26
|
+
* `value` is null. This is the unit the tables store directly, and the unit a
|
|
27
|
+
* client that applies component patches can replay — a transaction is a list of
|
|
28
|
+
* these, in the order they were applied.
|
|
29
|
+
*/ export type Patch = {
|
|
30
|
+
/** the entity the operation was about */ target: Eid;
|
|
31
|
+
/** the component it patched, or `entity` for the entity row itself */ comp: string;
|
|
32
|
+
/** the properties it wrote, or `null` for a removal */ value: Comp | null;
|
|
33
|
+
};
|
|
34
|
+
/** One committed transaction as the tables hold it: who wrote it, the note the
|
|
35
|
+
* writer left beside it, and what it did. */ export type Entry = {
|
|
36
|
+
/** its place in the total order — the cursor a feed pages by */ seq: number;
|
|
37
|
+
/** when it committed, ISO-8601 */ at: string;
|
|
38
|
+
/** the identity it resolved to, or null when nobody was named */ by: Eid | null;
|
|
39
|
+
/** the instrument it was written through */ via: Eid | null;
|
|
40
|
+
/** whatever the writer wrote down beside the transaction, verbatim */ note: string | null;
|
|
41
|
+
/** the host that wrote it (@yaks/journal `LogOpts.host`), or null for a
|
|
42
|
+
* transaction recorded before hosts were */ host: string | null;
|
|
43
|
+
/** what it did, in the order it did it */ patches: Patch[];
|
|
44
|
+
};
|
|
45
|
+
//# sourceMappingURL=batch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"batch.d.ts","sources":["../batch.ts"],"names":[],"mappings":"AAYA,cAAc,IAAI,EAAE,GAAG,2BAAgC;AAEvD;;;;;;;CAOC,GACD,YAAY;EACV,4BAA4B,GAC5B,QAAQ;EACR,+BAA+B,GAC/B,MAAM,MAAM;EACZ,+DAA+D,GAC/D,MAAM,MAAM,GAAG,IAAI;EACnB,6CAA6C,GAC7C,QAAQ,OAAO;EACf,6CAA6C,GAC7C,OAAO,OAAO;;AAGhB;UACU,GACV,YAAY;EACV,8DAA8D,GAC9D,KAAK,MAAM;EACX,gCAAgC,GAChC,IAAI,MAAM;EACV,qEAAqE,GACrE,IAAI,MAAM,IAAI;EACd,0CAA0C,GAC1C,KAAK,MAAM,IAAI;EACf,sCAAsC,GACtC,QAAQ;;AAGV;;;;;CAKC,GACD,YAAY;EACV,uCAAuC,GACvC,QAAQ;EACR,oEAAoE,GACpE,MAAM,MAAM;EACZ,qDAAqD,GACrD,OAAO,OAAO,IAAI;;AAGpB;2CAC2C,GAC3C,YAAY;EACV,8DAA8D,GAC9D,KAAK,MAAM;EACX,gCAAgC,GAChC,IAAI,MAAM;EACV,+DAA+D,GAC/D,IAAI,MAAM,IAAI;EACd,0CAA0C,GAC1C,KAAK,MAAM,IAAI;EACf,oEAAoE,GACpE,MAAM,MAAM,GAAG,IAAI;EACnB;4CAC0C,GAC1C,MAAM,MAAM,GAAG,IAAI;EACnB,wCAAwC,GACxC,SAAS"}
|