@yaks/sqlite 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.
Files changed (289) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +1596 -2
  3. package/_dist/archetype.d.ts +75 -0
  4. package/_dist/archetype.d.ts.map +1 -0
  5. package/_dist/branches.d.ts +23 -0
  6. package/_dist/branches.d.ts.map +1 -0
  7. package/_dist/budget.d.ts +52 -0
  8. package/_dist/budget.d.ts.map +1 -0
  9. package/_dist/bundle.d.ts +2 -0
  10. package/_dist/bundle.d.ts.map +1 -0
  11. package/_dist/cache.d.ts +24 -0
  12. package/_dist/cache.d.ts.map +1 -0
  13. package/_dist/catalog.d.ts +9 -0
  14. package/_dist/catalog.d.ts.map +1 -0
  15. package/_dist/check.d.ts +4 -0
  16. package/_dist/check.d.ts.map +1 -0
  17. package/_dist/db.d.ts +36 -0
  18. package/_dist/db.d.ts.map +1 -0
  19. package/_dist/ddl.d.ts +143 -0
  20. package/_dist/ddl.d.ts.map +1 -0
  21. package/_dist/doom.d.ts +22 -0
  22. package/_dist/doom.d.ts.map +1 -0
  23. package/_dist/follow.d.ts +74 -0
  24. package/_dist/follow.d.ts.map +1 -0
  25. package/_dist/history-shapes.d.ts +3 -0
  26. package/_dist/history-shapes.d.ts.map +1 -0
  27. package/_dist/history.d.ts +18 -0
  28. package/_dist/history.d.ts.map +1 -0
  29. package/_dist/inspect.d.ts +43 -0
  30. package/_dist/inspect.d.ts.map +1 -0
  31. package/_dist/jsonb.d.ts +17 -0
  32. package/_dist/jsonb.d.ts.map +1 -0
  33. package/_dist/layout.d.ts +7 -0
  34. package/_dist/layout.d.ts.map +1 -0
  35. package/_dist/ledger.d.ts +41 -0
  36. package/_dist/ledger.d.ts.map +1 -0
  37. package/_dist/meta.d.ts +89 -0
  38. package/_dist/meta.d.ts.map +1 -0
  39. package/_dist/migration.d.ts +41 -0
  40. package/_dist/migration.d.ts.map +1 -0
  41. package/_dist/mod.d.ts +201 -0
  42. package/_dist/mod.d.ts.map +1 -0
  43. package/_dist/mode.d.ts +14 -0
  44. package/_dist/mode.d.ts.map +1 -0
  45. package/_dist/overlay.d.ts +30 -0
  46. package/_dist/overlay.d.ts.map +1 -0
  47. package/_dist/owe.d.ts +180 -0
  48. package/_dist/owe.d.ts.map +1 -0
  49. package/_dist/physical.d.ts +89 -0
  50. package/_dist/physical.d.ts.map +1 -0
  51. package/_dist/point.d.ts +10 -0
  52. package/_dist/point.d.ts.map +1 -0
  53. package/_dist/raise.d.ts +22 -0
  54. package/_dist/raise.d.ts.map +1 -0
  55. package/_dist/read-db.d.ts +9 -0
  56. package/_dist/read-db.d.ts.map +1 -0
  57. package/_dist/read-store.d.ts +10 -0
  58. package/_dist/read-store.d.ts.map +1 -0
  59. package/_dist/read.d.ts +59 -0
  60. package/_dist/read.d.ts.map +1 -0
  61. package/_dist/reader.d.ts +11 -0
  62. package/_dist/reader.d.ts.map +1 -0
  63. package/_dist/rules.d.ts +19 -0
  64. package/_dist/rules.d.ts.map +1 -0
  65. package/_dist/service.d.ts +12 -0
  66. package/_dist/service.d.ts.map +1 -0
  67. package/_dist/shape.d.ts +12 -0
  68. package/_dist/shape.d.ts.map +1 -0
  69. package/_dist/statements.d.ts +4 -0
  70. package/_dist/statements.d.ts.map +1 -0
  71. package/_dist/survey.d.ts +31 -0
  72. package/_dist/survey.d.ts.map +1 -0
  73. package/_dist/tools.d.ts +15 -0
  74. package/_dist/tools.d.ts.map +1 -0
  75. package/_dist/trash.d.ts +18 -0
  76. package/_dist/trash.d.ts.map +1 -0
  77. package/_dist/unit.d.ts +11 -0
  78. package/_dist/unit.d.ts.map +1 -0
  79. package/_dist/vocab.d.ts +6 -0
  80. package/_dist/vocab.d.ts.map +1 -0
  81. package/_dist/write.d.ts +169 -0
  82. package/_dist/write.d.ts.map +1 -0
  83. package/answer.js +110 -0
  84. package/answer.js.map +1 -0
  85. package/answer.ts +122 -0
  86. package/archetype.js +730 -0
  87. package/archetype.js.map +1 -0
  88. package/archetype.ts +790 -0
  89. package/binary_cases.ts +240 -0
  90. package/branches.js +195 -0
  91. package/branches.js.map +1 -0
  92. package/branches.ts +257 -0
  93. package/budget.js +89 -0
  94. package/budget.js.map +1 -0
  95. package/budget.ts +117 -0
  96. package/builtin.js +34 -0
  97. package/builtin.js.map +1 -0
  98. package/builtin.ts +31 -0
  99. package/bundle.js +21 -0
  100. package/bundle.js.map +1 -0
  101. package/bundle.ts +22 -0
  102. package/buried.js +95 -0
  103. package/buried.js.map +1 -0
  104. package/buried.ts +124 -0
  105. package/cache.js +153 -0
  106. package/cache.js.map +1 -0
  107. package/cache.ts +154 -0
  108. package/cache_worker.ts +16 -0
  109. package/catalog.js +173 -0
  110. package/catalog.js.map +1 -0
  111. package/catalog.ts +224 -0
  112. package/check.js +80 -0
  113. package/check.js.map +1 -0
  114. package/check.ts +92 -0
  115. package/checkpoint.js +98 -0
  116. package/checkpoint.js.map +1 -0
  117. package/checkpoint.ts +129 -0
  118. package/checkpoint_worker.ts +37 -0
  119. package/db.js +170 -0
  120. package/db.js.map +1 -0
  121. package/db.ts +173 -0
  122. package/ddl.js +1441 -0
  123. package/ddl.js.map +1 -0
  124. package/ddl.ts +1508 -0
  125. package/deno.json +1 -0
  126. package/doom.js +59 -0
  127. package/doom.js.map +1 -0
  128. package/doom.ts +75 -0
  129. package/driver.js +69 -0
  130. package/driver.js.map +1 -0
  131. package/driver.ts +101 -0
  132. package/error.js +30 -0
  133. package/error.js.map +1 -0
  134. package/error.ts +46 -0
  135. package/ffi/LICENSE +202 -0
  136. package/ffi/blob.js +99 -0
  137. package/ffi/blob.js.map +1 -0
  138. package/ffi/blob.ts +156 -0
  139. package/ffi/constants.js +65 -0
  140. package/ffi/constants.js.map +1 -0
  141. package/ffi/constants.ts +67 -0
  142. package/ffi/database.js +613 -0
  143. package/ffi/database.js.map +1 -0
  144. package/ffi/database.ts +931 -0
  145. package/ffi/ffi.js +535 -0
  146. package/ffi/ffi.js.map +1 -0
  147. package/ffi/ffi.ts +604 -0
  148. package/ffi/mod.js +15 -0
  149. package/ffi/mod.js.map +1 -0
  150. package/ffi/mod.ts +28 -0
  151. package/ffi/statement.js +555 -0
  152. package/ffi/statement.js.map +1 -0
  153. package/ffi/statement.ts +788 -0
  154. package/ffi/util.js +32 -0
  155. package/ffi/util.js.map +1 -0
  156. package/ffi/util.ts +48 -0
  157. package/follow.js +342 -0
  158. package/follow.js.map +1 -0
  159. package/follow.ts +401 -0
  160. package/history-branch.js +166 -0
  161. package/history-branch.js.map +1 -0
  162. package/history-branch.ts +140 -0
  163. package/history-shapes.js +254 -0
  164. package/history-shapes.js.map +1 -0
  165. package/history-shapes.ts +268 -0
  166. package/history.js +620 -0
  167. package/history.js.map +1 -0
  168. package/history.ts +674 -0
  169. package/inspect.js +120 -0
  170. package/inspect.js.map +1 -0
  171. package/inspect.ts +164 -0
  172. package/jsonb.js +73 -0
  173. package/jsonb.js.map +1 -0
  174. package/jsonb.ts +108 -0
  175. package/layout.js +22 -0
  176. package/layout.js.map +1 -0
  177. package/layout.ts +26 -0
  178. package/ledger.js +210 -0
  179. package/ledger.js.map +1 -0
  180. package/ledger.ts +210 -0
  181. package/legacy.js +4 -0
  182. package/legacy.js.map +1 -0
  183. package/legacy.ts +3 -0
  184. package/maintenance.js +148 -0
  185. package/maintenance.js.map +1 -0
  186. package/maintenance.ts +154 -0
  187. package/measure.js +133 -0
  188. package/measure.js.map +1 -0
  189. package/measure.ts +167 -0
  190. package/measure_worker.ts +18 -0
  191. package/memo.js +1250 -0
  192. package/memo.js.map +1 -0
  193. package/memo.ts +1480 -0
  194. package/meta.js +265 -0
  195. package/meta.js.map +1 -0
  196. package/meta.ts +321 -0
  197. package/migration.js +272 -0
  198. package/migration.js.map +1 -0
  199. package/migration.ts +286 -0
  200. package/mod.js +1103 -0
  201. package/mod.js.map +1 -0
  202. package/mod.ts +1762 -0
  203. package/mode.js +192 -0
  204. package/mode.js.map +1 -0
  205. package/mode.ts +207 -0
  206. package/native.js +544 -0
  207. package/native.js.map +1 -0
  208. package/native.ts +544 -0
  209. package/overlay.js +408 -0
  210. package/overlay.js.map +1 -0
  211. package/overlay.ts +485 -0
  212. package/owe.js +719 -0
  213. package/owe.js.map +1 -0
  214. package/owe.ts +873 -0
  215. package/package.json +99 -4
  216. package/physical.js +350 -0
  217. package/physical.js.map +1 -0
  218. package/physical.ts +434 -0
  219. package/plan.js +78 -0
  220. package/plan.js.map +1 -0
  221. package/plan.ts +104 -0
  222. package/point.js +81 -0
  223. package/point.js.map +1 -0
  224. package/point.ts +52 -0
  225. package/population.js +639 -0
  226. package/population.js.map +1 -0
  227. package/population.ts +630 -0
  228. package/purge.js +117 -0
  229. package/purge.js.map +1 -0
  230. package/purge.ts +127 -0
  231. package/raise.js +270 -0
  232. package/raise.js.map +1 -0
  233. package/raise.ts +316 -0
  234. package/read-db.js +381 -0
  235. package/read-db.js.map +1 -0
  236. package/read-db.ts +282 -0
  237. package/read-store.js +74 -0
  238. package/read-store.js.map +1 -0
  239. package/read-store.ts +86 -0
  240. package/read.js +1100 -0
  241. package/read.js.map +1 -0
  242. package/read.ts +1441 -0
  243. package/reader.js +141 -0
  244. package/reader.js.map +1 -0
  245. package/reader.ts +159 -0
  246. package/rules.js +88 -0
  247. package/rules.js.map +1 -0
  248. package/rules.ts +113 -0
  249. package/service.js +27 -0
  250. package/service.js.map +1 -0
  251. package/service.ts +41 -0
  252. package/shape.js +76 -0
  253. package/shape.js.map +1 -0
  254. package/shape.ts +108 -0
  255. package/sparse_presence_fixture.ts +45 -0
  256. package/sqlitepath.js +24 -0
  257. package/sqlitepath.js.map +1 -0
  258. package/sqlitepath.ts +29 -0
  259. package/statements.js +78 -0
  260. package/statements.js.map +1 -0
  261. package/statements.ts +96 -0
  262. package/survey.js +135 -0
  263. package/survey.js.map +1 -0
  264. package/survey.ts +196 -0
  265. package/tools.js +17 -0
  266. package/tools.js.map +1 -0
  267. package/tools.ts +50 -0
  268. package/trash.js +144 -0
  269. package/trash.js.map +1 -0
  270. package/trash.ts +187 -0
  271. package/unit.js +215 -0
  272. package/unit.js.map +1 -0
  273. package/unit.ts +211 -0
  274. package/versions.js +115 -0
  275. package/versions.js.map +1 -0
  276. package/versions.ts +110 -0
  277. package/vocab.js +19 -0
  278. package/vocab.js.map +1 -0
  279. package/vocab.json +27 -0
  280. package/vocab.ts +19 -0
  281. package/watch.js +33 -0
  282. package/watch.js.map +1 -0
  283. package/watch.ts +42 -0
  284. package/write.js +1164 -0
  285. package/write.js.map +1 -0
  286. package/write.ts +1356 -0
  287. package/written.js +200 -0
  288. package/written.js.map +1 -0
  289. package/written.ts +202 -0
package/written.js ADDED
@@ -0,0 +1,200 @@
1
+ // Which entity each write to a file named, in commit order, for the other
2
+ // connections to it (./memo.ts). A connection keeps the entities it read in
3
+ // memory, and another connection's commit says only that something moved, so
4
+ // memory let every entity go: on the box, where other processes commit twice
5
+ // a second, a native step read its whole window again each time (T-130884).
6
+ //
7
+ // Triggers on the spine, the graves and each component's table append the
8
+ // entity a row they insert, update or delete is of to `entity_written`, in
9
+ // the transaction that wrote the row, whoever wrote it, raw SQL included.
10
+ // Writers commit one at a time, so the rows past the newest a connection has
11
+ // read are what was written since, and nothing else. The log keeps about its
12
+ // newest {@link KEEP}; a connection that fell further behind, or a file whose
13
+ // tables lack their triggers, can't say what moved, and memory lets go of
14
+ // everything as before. A store in memory has one connection, which sees its
15
+ // own statements, and keeps no log.
16
+ import { as, catalogue, col, eq, fn, ge, le, left, lit, op, render, select, stored, table, val, when } from "@yaks/sql/core";
17
+ /** The log's table. */ export const WRITTEN = 'entity_written';
18
+ /** How many of the newest rows the log keeps, give or take a prune. */ export const KEEP = 1 << 16;
19
+ // Rows are pruned once per this many, all at once.
20
+ const PRUNE = 1 << 10;
21
+ let events = [
22
+ 'insert',
23
+ 'update',
24
+ 'delete'
25
+ ];
26
+ // The trigger telling the log of each row `event` writes in `table`: the
27
+ // spine's row is the entity, any other table's names it by `entity`. A spine
28
+ // whose eid moved names none, since what memory holds under the old one
29
+ // can't be found from the new.
30
+ let told = (table, event)=>{
31
+ let row = event == 'delete' ? 'old' : 'new';
32
+ let entity = table != 'entity' ? col('entity', row) : event == 'update' ? when([
33
+ [
34
+ eq(col('eid', 'old'), col('eid', 'new')),
35
+ col('id', 'new')
36
+ ]
37
+ ]) : col('id', row);
38
+ return {
39
+ t: 'create trigger',
40
+ name: `written_${table}_${event}`,
41
+ ifNot: true,
42
+ timing: 'after',
43
+ event,
44
+ on: table,
45
+ body: [
46
+ {
47
+ t: 'insert',
48
+ into: WRITTEN,
49
+ cols: [
50
+ 'entity'
51
+ ],
52
+ rows: [
53
+ [
54
+ entity
55
+ ]
56
+ ]
57
+ }
58
+ ]
59
+ };
60
+ };
61
+ /** The triggers on `table` that tell the log of each row written there. */ export let telling = (table)=>events.map((event)=>told(table, event));
62
+ /** The log, and the triggers on the spine, the graves and each of `tables`
63
+ * that write it. */ export let writtenDdl = (tables)=>[
64
+ {
65
+ t: 'create table',
66
+ name: WRITTEN,
67
+ ifNot: true,
68
+ cols: [
69
+ {
70
+ name: 'seq',
71
+ type: 'integer',
72
+ pk: true
73
+ },
74
+ // No key to the spine: a pruned entity's last writes outlive its row.
75
+ {
76
+ name: 'entity',
77
+ type: 'integer'
78
+ }
79
+ ]
80
+ },
81
+ {
82
+ t: 'create trigger',
83
+ name: 'written_kept',
84
+ ifNot: true,
85
+ timing: 'after',
86
+ event: 'insert',
87
+ on: WRITTEN,
88
+ when: eq(op('%', col('seq', 'new'), lit(PRUNE)), lit(0)),
89
+ body: [
90
+ {
91
+ t: 'delete',
92
+ from: WRITTEN,
93
+ where: le(col('seq'), op('-', col('seq', 'new'), lit(KEEP)))
94
+ }
95
+ ]
96
+ },
97
+ ...[
98
+ 'entity',
99
+ 'tombstone',
100
+ ...tables
101
+ ].flatMap((name)=>events.map((event)=>told(name, event)))
102
+ ];
103
+ // Per driver, while its schema stands: what each object was made by, and
104
+ // whether each table has its triggers.
105
+ let checked = new WeakMap();
106
+ /** Whether the log follows every write to the spine, the graves and each of
107
+ * `tables` the file holds: each has its triggers as this package makes them.
108
+ * Asked of the file once while its schema stands. */ export let follows = (driver, tables)=>{
109
+ let objects = catalogue(driver);
110
+ let held = checked.get(driver);
111
+ if (held?.at !== objects) {
112
+ held = {
113
+ at: objects,
114
+ named: new Map(objects.map((o)=>[
115
+ o.name,
116
+ o.sql
117
+ ])),
118
+ tables: new Map()
119
+ };
120
+ checked.set(driver, held);
121
+ }
122
+ let { named } = held;
123
+ if (!named.has(WRITTEN)) return false;
124
+ for (let name of [
125
+ 'entity',
126
+ 'tombstone',
127
+ ...tables
128
+ ]){
129
+ let known = held.tables.get(name);
130
+ if (known === undefined) {
131
+ known = !named.has(name) || events.every((event)=>{
132
+ let [trigger, made] = making(name, event, driver.stored?.v);
133
+ return body(named.get(trigger) ?? '') == made;
134
+ });
135
+ held.tables.set(name, known);
136
+ }
137
+ if (!known) return false;
138
+ }
139
+ return true;
140
+ };
141
+ // SQLite keeps a trigger's statement without its `if not exists`.
142
+ let body = (sql)=>sql.replace(/^create trigger /i, '');
143
+ // Each trigger's name and statement, rendered once.
144
+ let made = new Map();
145
+ let making = (table, event, v)=>{
146
+ let at = v ? stored(v, table) : table;
147
+ let key = `${at} ${event}`;
148
+ let held = made.get(key);
149
+ if (!held) {
150
+ let s = told(table, event);
151
+ made.set(key, held = [
152
+ s.name,
153
+ body(render({
154
+ ...s,
155
+ ifNot: false
156
+ }, undefined, undefined, v).sql)
157
+ ]);
158
+ }
159
+ return held;
160
+ };
161
+ /** The newest seq the log holds, 0 for none. */ export let newest = (driver)=>Number(driver.query(select({
162
+ cols: [
163
+ as(fn('coalesce', fn('max', col('seq')), lit(0)), 'seq')
164
+ ],
165
+ from: table(WRITTEN)
166
+ }))[0].seq);
167
+ /** The eids of the entities written after the seq `at`, and the newest seq
168
+ * read; `undefined` where the log can't say: the rows after `at` were pruned
169
+ * (or `at` is past what it holds), or one names no entity standing (one since
170
+ * pruned, or an eid moved). */ export let since = (driver, at)=>{
171
+ let rows = driver.query(select({
172
+ cols: [
173
+ col('seq', 'w'),
174
+ col('eid', 'e')
175
+ ],
176
+ from: table(WRITTEN, 'w'),
177
+ joins: [
178
+ left(table('entity', 'e'), eq(col('id', 'e'), col('entity', 'w')))
179
+ ],
180
+ where: ge(col('seq', 'w'), val(at)),
181
+ order: [
182
+ col('seq', 'w')
183
+ ]
184
+ }));
185
+ // The row at `at` is the newest read before, unless the log held none.
186
+ let after = at ? rows.slice(1) : rows;
187
+ if (at ? Number(rows[0]?.seq) != at : Number(rows[0]?.seq ?? 1) != 1) {
188
+ return undefined;
189
+ }
190
+ let eids = new Set();
191
+ for (let r of after){
192
+ if (r.eid == null) return undefined;
193
+ eids.add(String(r.eid));
194
+ }
195
+ return {
196
+ at: Number(rows.at(-1)?.seq ?? at),
197
+ eids
198
+ };
199
+ };
200
+ //# sourceMappingURL=written.js.map
package/written.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"written.js","sources":["./written.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,4EAA4E;AAC5E,6EAA6E;AAC7E,6EAA6E;AAC7E,4EAA4E;AAC5E,EAAE;AACF,0EAA0E;AAC1E,2EAA2E;AAC3E,0EAA0E;AAC1E,6EAA6E;AAC7E,6EAA6E;AAC7E,8EAA8E;AAC9E,0EAA0E;AAC1E,6EAA6E;AAC7E,oCAAoC;AACpC,SACE,EAAE,EACF,SAAS,EACT,GAAG,EAGH,EAAE,EACF,EAAE,EACF,EAAE,EACF,EAAE,EACF,IAAI,EACJ,GAAG,EACH,EAAE,EACF,MAAM,EACN,MAAM,EAEN,MAAM,EACN,KAAK,EACL,GAAG,EACH,IAAI,8BAC4B;AAGlC,qBAAqB,GACrB,OAAO,MAAM,UAAU,iBAAgB;AAEvC,qEAAqE,GACrE,OAAO,MAAM,OAAO,KAAK,GAAE;AAC3B,mDAAmD;AACnD,MAAM,QAAQ,KAAK;AAE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// connections to it (./memo.ts). A connection keeps the entities it read in
3
+ // memory, and another connection's commit says only that something moved, so
4
+ // memory let every entity go: on the box, where other processes commit twice
5
+ // a second, a native step read its whole window again each time (T-130884).
6
+ //
7
+ // Triggers on the spine, the graves and each component's table append the
8
+ // entity a row they insert, update or delete is of to `entity_written`, in
9
+ // the transaction that wrote the row, whoever wrote it, raw SQL included.
10
+ // Writers commit one at a time, so the rows past the newest a connection has
11
+ // read are what was written since, and nothing else. The log keeps about its
12
+ // newest {@link KEEP}; a connection that fell further behind, or a file whose
13
+ // tables lack their triggers, can't say what moved, and memory lets go of
14
+ // everything as before. A store in memory has one connection, which sees its
15
+ // own statements, and keeps no log.
16
+ import {
17
+ as,
18
+ catalogue,
19
+ col,
20
+ type CreateTrigger,
21
+ type Driver,
22
+ eq,
23
+ fn,
24
+ ge,
25
+ le,
26
+ left,
27
+ lit,
28
+ op,
29
+ render,
30
+ select,
31
+ type Stmt,
32
+ stored,
33
+ table,
34
+ val,
35
+ when,
36
+ } from '@yaks/sql/core'
37
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
38
+
39
+ /** The log's table. */
40
+ export const WRITTEN = 'entity_written'
41
+
42
+ /** How many of the newest rows the log keeps, give or take a prune. */
43
+ export const KEEP = 1 << 16
44
+ // Rows are pruned once per this many, all at once.
45
+ const PRUNE = 1 << 10
46
+
47
+ let events = ['insert', 'update', 'delete'] as const
48
+
49
+ // The trigger telling the log of each row `event` writes in `table`: the
50
+ // spine's row is the entity, any other table's names it by `entity`. A spine
51
+ // whose eid moved names none, since what memory holds under the old one
52
+ // can't be found from the new.
53
+ let told = (table: string, event: typeof events[number]): CreateTrigger => {
54
+ let row = event == 'delete' ? 'old' : 'new'
55
+ let entity = table != 'entity'
56
+ ? col('entity', row)
57
+ : event == 'update'
58
+ ? when([[eq(col('eid', 'old'), col('eid', 'new')), col('id', 'new')]])
59
+ : col('id', row)
60
+ return {
61
+ t: 'create trigger',
62
+ name: `written_${table}_${event}`,
63
+ ifNot: true,
64
+ timing: 'after',
65
+ event,
66
+ on: table,
67
+ body: [{ t: 'insert', into: WRITTEN, cols: ['entity'], rows: [[entity]] }],
68
+ }
69
+ }
70
+
71
+ /** The triggers on `table` that tell the log of each row written there. */
72
+ export let telling = (table: string): CreateTrigger[] =>
73
+ events.map((event) => told(table, event))
74
+
75
+ /** The log, and the triggers on the spine, the graves and each of `tables`
76
+ * that write it. */
77
+ export let writtenDdl = (tables: string[]): Stmt[] => [
78
+ {
79
+ t: 'create table',
80
+ name: WRITTEN,
81
+ ifNot: true,
82
+ cols: [
83
+ { name: 'seq', type: 'integer', pk: true },
84
+ // No key to the spine: a pruned entity's last writes outlive its row.
85
+ { name: 'entity', type: 'integer' },
86
+ ],
87
+ },
88
+ {
89
+ t: 'create trigger',
90
+ name: 'written_kept',
91
+ ifNot: true,
92
+ timing: 'after',
93
+ event: 'insert',
94
+ on: WRITTEN,
95
+ when: eq(op('%', col('seq', 'new'), lit(PRUNE)), lit(0)),
96
+ body: [{
97
+ t: 'delete',
98
+ from: WRITTEN,
99
+ where: le(col('seq'), op('-', col('seq', 'new'), lit(KEEP))),
100
+ }],
101
+ },
102
+ ...['entity', 'tombstone', ...tables].flatMap((name) =>
103
+ events.map((event) => told(name, event))
104
+ ),
105
+ ]
106
+
107
+ // Per driver, while its schema stands: what each object was made by, and
108
+ // whether each table has its triggers.
109
+ let checked = new WeakMap<Driver, {
110
+ at: readonly unknown[]
111
+ named: Map<string, string | null>
112
+ tables: Map<string, boolean>
113
+ }>()
114
+
115
+ /** Whether the log follows every write to the spine, the graves and each of
116
+ * `tables` the file holds: each has its triggers as this package makes them.
117
+ * Asked of the file once while its schema stands. */
118
+ export let follows = (driver: Driver, tables: Iterable<string>): boolean => {
119
+ let objects = catalogue(driver)
120
+ let held = checked.get(driver)
121
+ if (held?.at !== objects) {
122
+ held = {
123
+ at: objects,
124
+ named: new Map(objects.map((o) => [o.name, o.sql])),
125
+ tables: new Map(),
126
+ }
127
+ checked.set(driver, held)
128
+ }
129
+ let { named } = held
130
+ if (!named.has(WRITTEN)) return false
131
+ for (let name of ['entity', 'tombstone', ...tables]) {
132
+ let known = held.tables.get(name)
133
+ if (known === undefined) {
134
+ known = !named.has(name) || events.every((event) => {
135
+ let [trigger, made] = making(name, event, driver.stored?.v)
136
+ return body(named.get(trigger) ?? '') == made
137
+ })
138
+ held.tables.set(name, known)
139
+ }
140
+ if (!known) return false
141
+ }
142
+ return true
143
+ }
144
+
145
+ // SQLite keeps a trigger's statement without its `if not exists`.
146
+ let body = (sql: string) => sql.replace(/^create trigger /i, '')
147
+
148
+ // Each trigger's name and statement, rendered once.
149
+ let made = new Map<string, [string, string]>()
150
+ let making = (table: string, event: typeof events[number], v?: Vocab) => {
151
+ let at = v ? stored(v, table) : table
152
+ let key = `${at} ${event}`
153
+ let held = made.get(key)
154
+ if (!held) {
155
+ let s = told(table, event)
156
+ made.set(
157
+ key,
158
+ held = [
159
+ s.name,
160
+ body(render({ ...s, ifNot: false }, undefined, undefined, v).sql),
161
+ ],
162
+ )
163
+ }
164
+ return held
165
+ }
166
+
167
+ /** The newest seq the log holds, 0 for none. */
168
+ export let newest = (driver: Driver): number =>
169
+ Number(
170
+ driver.query(select({
171
+ cols: [as(fn('coalesce', fn('max', col('seq')), lit(0)), 'seq')],
172
+ from: table(WRITTEN),
173
+ }))[0].seq,
174
+ )
175
+
176
+ /** The eids of the entities written after the seq `at`, and the newest seq
177
+ * read; `undefined` where the log can't say: the rows after `at` were pruned
178
+ * (or `at` is past what it holds), or one names no entity standing (one since
179
+ * pruned, or an eid moved). */
180
+ export let since = (
181
+ driver: Driver,
182
+ at: number,
183
+ ): { at: number; eids: Set<string> } | undefined => {
184
+ let rows = driver.query(select({
185
+ cols: [col('seq', 'w'), col('eid', 'e')],
186
+ from: table(WRITTEN, 'w'),
187
+ joins: [left(table('entity', 'e'), eq(col('id', 'e'), col('entity', 'w')))],
188
+ where: ge(col('seq', 'w'), val(at)),
189
+ order: [col('seq', 'w')],
190
+ }))
191
+ // The row at `at` is the newest read before, unless the log held none.
192
+ let after = at ? rows.slice(1) : rows
193
+ if (at ? Number(rows[0]?.seq) != at : Number(rows[0]?.seq ?? 1) != 1) {
194
+ return undefined
195
+ }
196
+ let eids = new Set<string>()
197
+ for (let r of after) {
198
+ if (r.eid == null) return undefined
199
+ eids.add(String(r.eid))
200
+ }
201
+ return { at: Number(rows.at(-1)?.seq ?? at), eids }
202
+ }