@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.
- package/LICENSE +202 -0
- package/README.md +1596 -2
- package/_dist/archetype.d.ts +75 -0
- package/_dist/archetype.d.ts.map +1 -0
- package/_dist/branches.d.ts +23 -0
- package/_dist/branches.d.ts.map +1 -0
- package/_dist/budget.d.ts +52 -0
- package/_dist/budget.d.ts.map +1 -0
- package/_dist/bundle.d.ts +2 -0
- package/_dist/bundle.d.ts.map +1 -0
- package/_dist/cache.d.ts +24 -0
- package/_dist/cache.d.ts.map +1 -0
- package/_dist/catalog.d.ts +9 -0
- package/_dist/catalog.d.ts.map +1 -0
- package/_dist/check.d.ts +4 -0
- package/_dist/check.d.ts.map +1 -0
- package/_dist/db.d.ts +36 -0
- package/_dist/db.d.ts.map +1 -0
- package/_dist/ddl.d.ts +143 -0
- package/_dist/ddl.d.ts.map +1 -0
- package/_dist/doom.d.ts +22 -0
- package/_dist/doom.d.ts.map +1 -0
- package/_dist/follow.d.ts +74 -0
- package/_dist/follow.d.ts.map +1 -0
- package/_dist/history-shapes.d.ts +3 -0
- package/_dist/history-shapes.d.ts.map +1 -0
- package/_dist/history.d.ts +18 -0
- package/_dist/history.d.ts.map +1 -0
- package/_dist/inspect.d.ts +43 -0
- package/_dist/inspect.d.ts.map +1 -0
- package/_dist/jsonb.d.ts +17 -0
- package/_dist/jsonb.d.ts.map +1 -0
- package/_dist/layout.d.ts +7 -0
- package/_dist/layout.d.ts.map +1 -0
- package/_dist/ledger.d.ts +41 -0
- package/_dist/ledger.d.ts.map +1 -0
- package/_dist/meta.d.ts +89 -0
- package/_dist/meta.d.ts.map +1 -0
- package/_dist/migration.d.ts +41 -0
- package/_dist/migration.d.ts.map +1 -0
- package/_dist/mod.d.ts +201 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/mode.d.ts +14 -0
- package/_dist/mode.d.ts.map +1 -0
- package/_dist/overlay.d.ts +30 -0
- package/_dist/overlay.d.ts.map +1 -0
- package/_dist/owe.d.ts +180 -0
- package/_dist/owe.d.ts.map +1 -0
- package/_dist/physical.d.ts +89 -0
- package/_dist/physical.d.ts.map +1 -0
- package/_dist/point.d.ts +10 -0
- package/_dist/point.d.ts.map +1 -0
- package/_dist/raise.d.ts +22 -0
- package/_dist/raise.d.ts.map +1 -0
- package/_dist/read-db.d.ts +9 -0
- package/_dist/read-db.d.ts.map +1 -0
- package/_dist/read-store.d.ts +10 -0
- package/_dist/read-store.d.ts.map +1 -0
- package/_dist/read.d.ts +59 -0
- package/_dist/read.d.ts.map +1 -0
- package/_dist/reader.d.ts +11 -0
- package/_dist/reader.d.ts.map +1 -0
- package/_dist/rules.d.ts +19 -0
- package/_dist/rules.d.ts.map +1 -0
- package/_dist/service.d.ts +12 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/shape.d.ts +12 -0
- package/_dist/shape.d.ts.map +1 -0
- package/_dist/statements.d.ts +4 -0
- package/_dist/statements.d.ts.map +1 -0
- package/_dist/survey.d.ts +31 -0
- package/_dist/survey.d.ts.map +1 -0
- package/_dist/tools.d.ts +15 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/trash.d.ts +18 -0
- package/_dist/trash.d.ts.map +1 -0
- package/_dist/unit.d.ts +11 -0
- package/_dist/unit.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/write.d.ts +169 -0
- package/_dist/write.d.ts.map +1 -0
- package/answer.js +110 -0
- package/answer.js.map +1 -0
- package/answer.ts +122 -0
- package/archetype.js +730 -0
- package/archetype.js.map +1 -0
- package/archetype.ts +790 -0
- package/binary_cases.ts +240 -0
- package/branches.js +195 -0
- package/branches.js.map +1 -0
- package/branches.ts +257 -0
- package/budget.js +89 -0
- package/budget.js.map +1 -0
- package/budget.ts +117 -0
- package/builtin.js +34 -0
- package/builtin.js.map +1 -0
- package/builtin.ts +31 -0
- package/bundle.js +21 -0
- package/bundle.js.map +1 -0
- package/bundle.ts +22 -0
- package/buried.js +95 -0
- package/buried.js.map +1 -0
- package/buried.ts +124 -0
- package/cache.js +153 -0
- package/cache.js.map +1 -0
- package/cache.ts +154 -0
- package/cache_worker.ts +16 -0
- package/catalog.js +173 -0
- package/catalog.js.map +1 -0
- package/catalog.ts +224 -0
- package/check.js +80 -0
- package/check.js.map +1 -0
- package/check.ts +92 -0
- package/checkpoint.js +98 -0
- package/checkpoint.js.map +1 -0
- package/checkpoint.ts +129 -0
- package/checkpoint_worker.ts +37 -0
- package/db.js +170 -0
- package/db.js.map +1 -0
- package/db.ts +173 -0
- package/ddl.js +1441 -0
- package/ddl.js.map +1 -0
- package/ddl.ts +1508 -0
- package/deno.json +1 -0
- package/doom.js +59 -0
- package/doom.js.map +1 -0
- package/doom.ts +75 -0
- package/driver.js +69 -0
- package/driver.js.map +1 -0
- package/driver.ts +101 -0
- package/error.js +30 -0
- package/error.js.map +1 -0
- package/error.ts +46 -0
- package/ffi/LICENSE +202 -0
- package/ffi/blob.js +99 -0
- package/ffi/blob.js.map +1 -0
- package/ffi/blob.ts +156 -0
- package/ffi/constants.js +65 -0
- package/ffi/constants.js.map +1 -0
- package/ffi/constants.ts +67 -0
- package/ffi/database.js +613 -0
- package/ffi/database.js.map +1 -0
- package/ffi/database.ts +931 -0
- package/ffi/ffi.js +535 -0
- package/ffi/ffi.js.map +1 -0
- package/ffi/ffi.ts +604 -0
- package/ffi/mod.js +15 -0
- package/ffi/mod.js.map +1 -0
- package/ffi/mod.ts +28 -0
- package/ffi/statement.js +555 -0
- package/ffi/statement.js.map +1 -0
- package/ffi/statement.ts +788 -0
- package/ffi/util.js +32 -0
- package/ffi/util.js.map +1 -0
- package/ffi/util.ts +48 -0
- package/follow.js +342 -0
- package/follow.js.map +1 -0
- package/follow.ts +401 -0
- package/history-branch.js +166 -0
- package/history-branch.js.map +1 -0
- package/history-branch.ts +140 -0
- package/history-shapes.js +254 -0
- package/history-shapes.js.map +1 -0
- package/history-shapes.ts +268 -0
- package/history.js +620 -0
- package/history.js.map +1 -0
- package/history.ts +674 -0
- package/inspect.js +120 -0
- package/inspect.js.map +1 -0
- package/inspect.ts +164 -0
- package/jsonb.js +73 -0
- package/jsonb.js.map +1 -0
- package/jsonb.ts +108 -0
- package/layout.js +22 -0
- package/layout.js.map +1 -0
- package/layout.ts +26 -0
- package/ledger.js +210 -0
- package/ledger.js.map +1 -0
- package/ledger.ts +210 -0
- package/legacy.js +4 -0
- package/legacy.js.map +1 -0
- package/legacy.ts +3 -0
- package/maintenance.js +148 -0
- package/maintenance.js.map +1 -0
- package/maintenance.ts +154 -0
- package/measure.js +133 -0
- package/measure.js.map +1 -0
- package/measure.ts +167 -0
- package/measure_worker.ts +18 -0
- package/memo.js +1250 -0
- package/memo.js.map +1 -0
- package/memo.ts +1480 -0
- package/meta.js +265 -0
- package/meta.js.map +1 -0
- package/meta.ts +321 -0
- package/migration.js +272 -0
- package/migration.js.map +1 -0
- package/migration.ts +286 -0
- package/mod.js +1103 -0
- package/mod.js.map +1 -0
- package/mod.ts +1762 -0
- package/mode.js +192 -0
- package/mode.js.map +1 -0
- package/mode.ts +207 -0
- package/native.js +544 -0
- package/native.js.map +1 -0
- package/native.ts +544 -0
- package/overlay.js +408 -0
- package/overlay.js.map +1 -0
- package/overlay.ts +485 -0
- package/owe.js +719 -0
- package/owe.js.map +1 -0
- package/owe.ts +873 -0
- package/package.json +99 -4
- package/physical.js +350 -0
- package/physical.js.map +1 -0
- package/physical.ts +434 -0
- package/plan.js +78 -0
- package/plan.js.map +1 -0
- package/plan.ts +104 -0
- package/point.js +81 -0
- package/point.js.map +1 -0
- package/point.ts +52 -0
- package/population.js +639 -0
- package/population.js.map +1 -0
- package/population.ts +630 -0
- package/purge.js +117 -0
- package/purge.js.map +1 -0
- package/purge.ts +127 -0
- package/raise.js +270 -0
- package/raise.js.map +1 -0
- package/raise.ts +316 -0
- package/read-db.js +381 -0
- package/read-db.js.map +1 -0
- package/read-db.ts +282 -0
- package/read-store.js +74 -0
- package/read-store.js.map +1 -0
- package/read-store.ts +86 -0
- package/read.js +1100 -0
- package/read.js.map +1 -0
- package/read.ts +1441 -0
- package/reader.js +141 -0
- package/reader.js.map +1 -0
- package/reader.ts +159 -0
- package/rules.js +88 -0
- package/rules.js.map +1 -0
- package/rules.ts +113 -0
- package/service.js +27 -0
- package/service.js.map +1 -0
- package/service.ts +41 -0
- package/shape.js +76 -0
- package/shape.js.map +1 -0
- package/shape.ts +108 -0
- package/sparse_presence_fixture.ts +45 -0
- package/sqlitepath.js +24 -0
- package/sqlitepath.js.map +1 -0
- package/sqlitepath.ts +29 -0
- package/statements.js +78 -0
- package/statements.js.map +1 -0
- package/statements.ts +96 -0
- package/survey.js +135 -0
- package/survey.js.map +1 -0
- package/survey.ts +196 -0
- package/tools.js +17 -0
- package/tools.js.map +1 -0
- package/tools.ts +50 -0
- package/trash.js +144 -0
- package/trash.js.map +1 -0
- package/trash.ts +187 -0
- package/unit.js +215 -0
- package/unit.js.map +1 -0
- package/unit.ts +211 -0
- package/versions.js +115 -0
- package/versions.js.map +1 -0
- package/versions.ts +110 -0
- package/vocab.js +19 -0
- package/vocab.js.map +1 -0
- package/vocab.json +27 -0
- package/vocab.ts +19 -0
- package/watch.js +33 -0
- package/watch.js.map +1 -0
- package/watch.ts +42 -0
- package/write.js +1164 -0
- package/write.js.map +1 -0
- package/write.ts +1356 -0
- package/written.js +200 -0
- package/written.js.map +1 -0
- package/written.ts +202 -0
package/unit.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { isPromise } from "@yaks/fp";
|
|
2
|
+
import { context, scope } from "@yaks/trace";
|
|
3
|
+
import { busy, halfway } from "./error.js";
|
|
4
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
5
|
+
let owner = new AsyncLocalStorage();
|
|
6
|
+
let waiting = new WeakMap();
|
|
7
|
+
/** Whether this async context owns the connection's active transaction. */ export let owned = (driver)=>!!owner.getStore()?.some((o)=>o.connection === (driver.connection ?? driver) && o.active);
|
|
8
|
+
/** Independent asynchronous transactions on one connection take turns.
|
|
9
|
+
* Only calls within the owning body's async context may open savepoints;
|
|
10
|
+
* otherwise an unrelated write could commit inside a transaction that later
|
|
11
|
+
* rolls back. The uncontended synchronous path stays synchronous. */ export let exclusive = (driver, body)=>{
|
|
12
|
+
let connection = driver.connection ?? driver;
|
|
13
|
+
let held = owner.getStore() ?? [];
|
|
14
|
+
if (owned(driver)) return body();
|
|
15
|
+
let scope = {
|
|
16
|
+
connection,
|
|
17
|
+
active: true
|
|
18
|
+
};
|
|
19
|
+
let run = ()=>owner.run([
|
|
20
|
+
...held,
|
|
21
|
+
scope
|
|
22
|
+
], body);
|
|
23
|
+
let before = waiting.get(connection);
|
|
24
|
+
let out;
|
|
25
|
+
try {
|
|
26
|
+
out = before ? before.then(run) : run();
|
|
27
|
+
} catch (error) {
|
|
28
|
+
scope.active = false;
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
if (!(out instanceof Promise)) {
|
|
32
|
+
scope.active = false;
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
let done = out.finally(()=>{
|
|
36
|
+
scope.active = false;
|
|
37
|
+
if (waiting.get(connection) === tail) waiting.delete(connection);
|
|
38
|
+
});
|
|
39
|
+
let tail = done.then(()=>{}, ()=>{});
|
|
40
|
+
waiting.set(connection, tail);
|
|
41
|
+
return done;
|
|
42
|
+
};
|
|
43
|
+
// SQLite has one transaction per connection, so nesting is done with
|
|
44
|
+
// SAVEPOINTs: a store used inside a transaction the caller already opened (an
|
|
45
|
+
// application's own, or another store's) still gets its own all-or-nothing
|
|
46
|
+
// unit. Each is named for its depth: an inner savepoint never shares an outer
|
|
47
|
+
// one's name, so it can never release it, and a connection says only a handful
|
|
48
|
+
// of names, which its driver keeps prepared. A name per unit would be two
|
|
49
|
+
// statements prepared for every unit and never run again, and in memory, where
|
|
50
|
+
// every unit is a savepoint, they would push the statements a store does repeat
|
|
51
|
+
// out of the driver's bounded cache.
|
|
52
|
+
// How many units are open on each driver. A driver that owns a file needs to
|
|
53
|
+
// know whether it is the outermost one, because that is the one that takes the
|
|
54
|
+
// write lock (see `Driver.file`); everything inside it is a savepoint, since
|
|
55
|
+
// one connection has one transaction however deeply the calls nest.
|
|
56
|
+
let depth = new WeakMap();
|
|
57
|
+
// How many writes have been told `halfway`. A driver's transaction runs its
|
|
58
|
+
// body synchronously to its return, so nothing else runs between a unit's
|
|
59
|
+
// body starting and returning: one nested in it that was told already leaves
|
|
60
|
+
// the enclosing ones untold, and one occurrence is one report.
|
|
61
|
+
let told = 0;
|
|
62
|
+
// One all-or-nothing unit of work. A driver that owns its own transactions
|
|
63
|
+
// (see `Driver.tx`) is asked for one; otherwise it is `begin immediate` where
|
|
64
|
+
// this driver owns a file and nobody is inside a unit yet, and a SAVEPOINT in
|
|
65
|
+
// plain SQL everywhere else. On SQL an async body is settled before the unit
|
|
66
|
+
// closes, so a batch that went async is still rolled back by a rejection.
|
|
67
|
+
//
|
|
68
|
+
// A driver's own transaction is synchronous, and commits when the body
|
|
69
|
+
// returns: a body that returned a promise has committed what it did before
|
|
70
|
+
// its first `await`, and does the rest outside any transaction. That is
|
|
71
|
+
// reported to `report` where it happens (./error.ts `halfway`), and the write
|
|
72
|
+
// goes on as it was; the code that went async is the fix.
|
|
73
|
+
//
|
|
74
|
+
// A unit that only reads says so (`read`), and the outermost one then begins
|
|
75
|
+
// deferred: in WAL a reader sees one consistent snapshot without the write
|
|
76
|
+
// lock, so a lookup neither waits on a writer nor makes one wait. It must not
|
|
77
|
+
// write — a deferred transaction that has read cannot take the lock once
|
|
78
|
+
// another connection has committed.
|
|
79
|
+
//
|
|
80
|
+
// A read opens a transaction only where it isolates the read from someone: a
|
|
81
|
+
// file other connections commit to. A read has nothing to roll back, so on a
|
|
82
|
+
// connection nobody else writes (memory, a Durable Object's own storage) a
|
|
83
|
+
// savepoint or a `transactionSync` around it is statements and a call that
|
|
84
|
+
// buy nothing, paid on every lookup a write asks before its own transaction.
|
|
85
|
+
//
|
|
86
|
+
// An outermost write waits out another connection's lock inside SQLite's busy
|
|
87
|
+
// handler, which sleeps the calling thread. A caller that can wait without
|
|
88
|
+
// holding its thread says `wait: false`: a held lock then refuses at once,
|
|
89
|
+
// marked retryable, and the caller waits as it likes (@yaks/graph `enter`).
|
|
90
|
+
export let unit = (driver, body, mode = 'write', { wait = true, report = (e)=>console.error(e) } = {})=>{
|
|
91
|
+
let tracing = context();
|
|
92
|
+
let held = depth.get(driver) ?? 0;
|
|
93
|
+
// A read inside a unit already has its snapshot, and one on a connection no
|
|
94
|
+
// other commits to needs none. Writes still need their rollback boundary.
|
|
95
|
+
if (mode == 'read' && (held || !driver.file)) return body();
|
|
96
|
+
if (driver.tx) {
|
|
97
|
+
depth.set(driver, held + 1);
|
|
98
|
+
let watched = ()=>{
|
|
99
|
+
let before = told;
|
|
100
|
+
let out = body();
|
|
101
|
+
if (isPromise(out) && told == before) {
|
|
102
|
+
told++;
|
|
103
|
+
report(halfway());
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
};
|
|
107
|
+
// The transaction has ended when `tx` returns, whatever its body left
|
|
108
|
+
// running. A generic body's type can't say it is synchronous; `watched`
|
|
109
|
+
// tells.
|
|
110
|
+
try {
|
|
111
|
+
return driver.tx(watched);
|
|
112
|
+
} finally{
|
|
113
|
+
depth.set(driver, held);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
let outer = !!driver.file && held == 0;
|
|
117
|
+
let name = `yaks_tx_${held}`;
|
|
118
|
+
let begin = ()=>driver.query(outer ? {
|
|
119
|
+
t: 'begin',
|
|
120
|
+
mode: mode == 'read' ? 'deferred' : 'immediate'
|
|
121
|
+
} : {
|
|
122
|
+
t: 'savepoint',
|
|
123
|
+
name
|
|
124
|
+
});
|
|
125
|
+
try {
|
|
126
|
+
outer && mode == 'write' && !wait ? unwaiting(driver, begin) : begin();
|
|
127
|
+
} catch (e) {
|
|
128
|
+
// No callback ran and no write landed. Callers may retry this refusal,
|
|
129
|
+
// unlike a failure after the body could have performed external work.
|
|
130
|
+
if (outer && mode == 'write' && busy(e)) {
|
|
131
|
+
throw Object.assign(e, {
|
|
132
|
+
retryable: true
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
throw e;
|
|
136
|
+
}
|
|
137
|
+
depth.set(driver, held + 1);
|
|
138
|
+
let run = (s)=>tracing ? scope(tracing, ()=>driver.query(s)) : driver.query(s);
|
|
139
|
+
let undo = (e)=>{
|
|
140
|
+
try {
|
|
141
|
+
// SQLite may already have rolled back the whole transaction. Tell the
|
|
142
|
+
// driver's caches and ledger even then, so none retain undone writes.
|
|
143
|
+
if (outer || driver.inTransaction?.() === false) {
|
|
144
|
+
run({
|
|
145
|
+
t: 'rollback',
|
|
146
|
+
ifActive: true
|
|
147
|
+
});
|
|
148
|
+
} else {
|
|
149
|
+
run({
|
|
150
|
+
t: 'rollback',
|
|
151
|
+
to: name
|
|
152
|
+
});
|
|
153
|
+
run({
|
|
154
|
+
t: 'release',
|
|
155
|
+
name
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
} catch (cleanup) {
|
|
159
|
+
throw new AggregateError([
|
|
160
|
+
e,
|
|
161
|
+
cleanup
|
|
162
|
+
], 'SQLite transaction rollback failed', {
|
|
163
|
+
cause: e
|
|
164
|
+
});
|
|
165
|
+
} finally{
|
|
166
|
+
depth.set(driver, held);
|
|
167
|
+
}
|
|
168
|
+
throw e;
|
|
169
|
+
};
|
|
170
|
+
let done = (out)=>{
|
|
171
|
+
try {
|
|
172
|
+
run(outer ? {
|
|
173
|
+
t: 'commit'
|
|
174
|
+
} : {
|
|
175
|
+
t: 'release',
|
|
176
|
+
name
|
|
177
|
+
});
|
|
178
|
+
} catch (e) {
|
|
179
|
+
return undo(e);
|
|
180
|
+
}
|
|
181
|
+
depth.set(driver, held);
|
|
182
|
+
return out;
|
|
183
|
+
};
|
|
184
|
+
let out;
|
|
185
|
+
try {
|
|
186
|
+
out = body();
|
|
187
|
+
} catch (e) {
|
|
188
|
+
return undo(e);
|
|
189
|
+
}
|
|
190
|
+
return out instanceof Promise ? out.then(done, undo) : done(out);
|
|
191
|
+
};
|
|
192
|
+
/** `body` with this connection's busy wait at nothing: a lock another
|
|
193
|
+
* connection holds refuses it at once, for work that can be left to later
|
|
194
|
+
* rather than waited for. */ export let unwaiting = (driver, body)=>{
|
|
195
|
+
let [row] = driver.query({
|
|
196
|
+
t: 'pragma',
|
|
197
|
+
name: 'busy_timeout'
|
|
198
|
+
});
|
|
199
|
+
let was = Number(Object.values(row ?? {})[0] ?? 0);
|
|
200
|
+
driver.query({
|
|
201
|
+
t: 'pragma',
|
|
202
|
+
name: 'busy_timeout',
|
|
203
|
+
value: 0
|
|
204
|
+
});
|
|
205
|
+
try {
|
|
206
|
+
return body();
|
|
207
|
+
} finally{
|
|
208
|
+
driver.query({
|
|
209
|
+
t: 'pragma',
|
|
210
|
+
name: 'busy_timeout',
|
|
211
|
+
value: was
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
//# sourceMappingURL=unit.js.map
|
package/unit.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unit.js","sources":["./unit.ts"],"names":[],"mappings":"AACA,SAAS,SAAS,wBAA6B;AAC/C,SAAS,OAAO,EAAE,KAAK,2BAAgC;AACvD,SAAS,IAAI,EAAE,OAAO,qBAAoB;AAC1C,SAAS,iBAAiB,QAAQ,mBAAkB;AAGpD,IAAI,QAAQ,IAAI;AAChB,IAAI,UAAU,IAAI;AAElB,yEAAyE,GACzE,OAAO,IAAI,QAAQ,CAAC,SAClB,CAAC,CAAC,MAAM,QAAQ,IAAI,KAAK,CAAC,IACxB,EAAE,UAAU,KAAK,CAAC,OAAO,UAAU,IAAI,MAAM,KAAK,EAAE,MAAM,EAC3D;AAEH;;;mEAGmE,GACnE,OAAO,IAAI,YAAY,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E;EAC3D,IAAI;IACF,OAAO;EACT,SAAU;IACR,OAAO,KAAK,CAAC;MAAE,GAAG;MAAU,MAAM;MAAgB,OAAO;IAAI;EAC/D;AACF,EAAC"}
|
package/unit.ts
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import type { Driver, Settled, Stmt } from 'jsr:@yaks/sql@0.2.36'
|
|
2
|
+
import { isPromise } from '@yaks/fp'
|
|
3
|
+
import { context, scope } from '@yaks/trace'
|
|
4
|
+
import { busy, halfway } from './error.js'
|
|
5
|
+
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
6
|
+
|
|
7
|
+
type Owner = { connection: object; active: boolean }
|
|
8
|
+
let owner = new AsyncLocalStorage<Owner[]>()
|
|
9
|
+
let waiting = new WeakMap<object, Promise<void>>()
|
|
10
|
+
|
|
11
|
+
/** Whether this async context owns the connection's active transaction. */
|
|
12
|
+
export let owned = (driver: Driver): boolean =>
|
|
13
|
+
!!owner.getStore()?.some((o) =>
|
|
14
|
+
o.connection === (driver.connection ?? driver) && o.active
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
/** Independent asynchronous transactions on one connection take turns.
|
|
18
|
+
* Only calls within the owning body's async context may open savepoints;
|
|
19
|
+
* otherwise an unrelated write could commit inside a transaction that later
|
|
20
|
+
* rolls back. The uncontended synchronous path stays synchronous. */
|
|
21
|
+
export let exclusive = <R>(
|
|
22
|
+
driver: Driver,
|
|
23
|
+
body: () => R,
|
|
24
|
+
): R | Promise<Awaited<R>> => {
|
|
25
|
+
let connection = driver.connection ?? driver
|
|
26
|
+
let held = owner.getStore() ?? []
|
|
27
|
+
if (owned(driver)) return body()
|
|
28
|
+
let scope: Owner = { connection, active: true }
|
|
29
|
+
let run = () => owner.run([...held, scope], body)
|
|
30
|
+
let before = waiting.get(connection)
|
|
31
|
+
let out: R | Promise<Awaited<R>>
|
|
32
|
+
try {
|
|
33
|
+
out = before ? before.then(run) as Promise<Awaited<R>> : run()
|
|
34
|
+
} catch (error) {
|
|
35
|
+
scope.active = false
|
|
36
|
+
throw error
|
|
37
|
+
}
|
|
38
|
+
if (!(out instanceof Promise)) {
|
|
39
|
+
scope.active = false
|
|
40
|
+
return out
|
|
41
|
+
}
|
|
42
|
+
let done = out.finally(() => {
|
|
43
|
+
scope.active = false
|
|
44
|
+
if (waiting.get(connection) === tail) waiting.delete(connection)
|
|
45
|
+
})
|
|
46
|
+
let tail = done.then(() => {}, () => {})
|
|
47
|
+
waiting.set(connection, tail)
|
|
48
|
+
return done
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// SQLite has one transaction per connection, so nesting is done with
|
|
52
|
+
// SAVEPOINTs: a store used inside a transaction the caller already opened (an
|
|
53
|
+
// application's own, or another store's) still gets its own all-or-nothing
|
|
54
|
+
// unit. Each is named for its depth: an inner savepoint never shares an outer
|
|
55
|
+
// one's name, so it can never release it, and a connection says only a handful
|
|
56
|
+
// of names, which its driver keeps prepared. A name per unit would be two
|
|
57
|
+
// statements prepared for every unit and never run again, and in memory, where
|
|
58
|
+
// every unit is a savepoint, they would push the statements a store does repeat
|
|
59
|
+
// out of the driver's bounded cache.
|
|
60
|
+
|
|
61
|
+
// How many units are open on each driver. A driver that owns a file needs to
|
|
62
|
+
// know whether it is the outermost one, because that is the one that takes the
|
|
63
|
+
// write lock (see `Driver.file`); everything inside it is a savepoint, since
|
|
64
|
+
// one connection has one transaction however deeply the calls nest.
|
|
65
|
+
let depth = new WeakMap<Driver, number>()
|
|
66
|
+
|
|
67
|
+
// How many writes have been told `halfway`. A driver's transaction runs its
|
|
68
|
+
// body synchronously to its return, so nothing else runs between a unit's
|
|
69
|
+
// body starting and returning: one nested in it that was told already leaves
|
|
70
|
+
// the enclosing ones untold, and one occurrence is one report.
|
|
71
|
+
let told = 0
|
|
72
|
+
|
|
73
|
+
// One all-or-nothing unit of work. A driver that owns its own transactions
|
|
74
|
+
// (see `Driver.tx`) is asked for one; otherwise it is `begin immediate` where
|
|
75
|
+
// this driver owns a file and nobody is inside a unit yet, and a SAVEPOINT in
|
|
76
|
+
// plain SQL everywhere else. On SQL an async body is settled before the unit
|
|
77
|
+
// closes, so a batch that went async is still rolled back by a rejection.
|
|
78
|
+
//
|
|
79
|
+
// A driver's own transaction is synchronous, and commits when the body
|
|
80
|
+
// returns: a body that returned a promise has committed what it did before
|
|
81
|
+
// its first `await`, and does the rest outside any transaction. That is
|
|
82
|
+
// reported to `report` where it happens (./error.ts `halfway`), and the write
|
|
83
|
+
// goes on as it was; the code that went async is the fix.
|
|
84
|
+
//
|
|
85
|
+
// A unit that only reads says so (`read`), and the outermost one then begins
|
|
86
|
+
// deferred: in WAL a reader sees one consistent snapshot without the write
|
|
87
|
+
// lock, so a lookup neither waits on a writer nor makes one wait. It must not
|
|
88
|
+
// write — a deferred transaction that has read cannot take the lock once
|
|
89
|
+
// another connection has committed.
|
|
90
|
+
//
|
|
91
|
+
// A read opens a transaction only where it isolates the read from someone: a
|
|
92
|
+
// file other connections commit to. A read has nothing to roll back, so on a
|
|
93
|
+
// connection nobody else writes (memory, a Durable Object's own storage) a
|
|
94
|
+
// savepoint or a `transactionSync` around it is statements and a call that
|
|
95
|
+
// buy nothing, paid on every lookup a write asks before its own transaction.
|
|
96
|
+
//
|
|
97
|
+
// An outermost write waits out another connection's lock inside SQLite's busy
|
|
98
|
+
// handler, which sleeps the calling thread. A caller that can wait without
|
|
99
|
+
// holding its thread says `wait: false`: a held lock then refuses at once,
|
|
100
|
+
// marked retryable, and the caller waits as it likes (@yaks/graph `enter`).
|
|
101
|
+
export let unit = <R>(
|
|
102
|
+
driver: Driver,
|
|
103
|
+
body: () => R,
|
|
104
|
+
mode: 'write' | 'read' = 'write',
|
|
105
|
+
{ wait = true, report = (e) => console.error(e) }: {
|
|
106
|
+
wait?: boolean
|
|
107
|
+
/** Where a write that went async inside a driver's own transaction is
|
|
108
|
+
* told (./error.ts `halfway`). */
|
|
109
|
+
report?: (error: Error) => void
|
|
110
|
+
} = {},
|
|
111
|
+
): R => {
|
|
112
|
+
let tracing = context()
|
|
113
|
+
let held = depth.get(driver) ?? 0
|
|
114
|
+
// A read inside a unit already has its snapshot, and one on a connection no
|
|
115
|
+
// other commits to needs none. Writes still need their rollback boundary.
|
|
116
|
+
if (mode == 'read' && (held || !driver.file)) return body()
|
|
117
|
+
if (driver.tx) {
|
|
118
|
+
depth.set(driver, held + 1)
|
|
119
|
+
let watched = () => {
|
|
120
|
+
let before = told
|
|
121
|
+
let out = body()
|
|
122
|
+
if (isPromise(out) && told == before) {
|
|
123
|
+
told++
|
|
124
|
+
report(halfway())
|
|
125
|
+
}
|
|
126
|
+
return out
|
|
127
|
+
}
|
|
128
|
+
// The transaction has ended when `tx` returns, whatever its body left
|
|
129
|
+
// running. A generic body's type can't say it is synchronous; `watched`
|
|
130
|
+
// tells.
|
|
131
|
+
try {
|
|
132
|
+
return driver.tx(watched as () => R & Settled) as R
|
|
133
|
+
} finally {
|
|
134
|
+
depth.set(driver, held)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
let outer = !!driver.file && held == 0
|
|
138
|
+
let name = `yaks_tx_${held}`
|
|
139
|
+
let begin = () =>
|
|
140
|
+
driver.query(
|
|
141
|
+
outer
|
|
142
|
+
? { t: 'begin', mode: mode == 'read' ? 'deferred' : 'immediate' }
|
|
143
|
+
: { t: 'savepoint', name },
|
|
144
|
+
)
|
|
145
|
+
try {
|
|
146
|
+
outer && mode == 'write' && !wait ? unwaiting(driver, begin) : begin()
|
|
147
|
+
} catch (e) {
|
|
148
|
+
// No callback ran and no write landed. Callers may retry this refusal,
|
|
149
|
+
// unlike a failure after the body could have performed external work.
|
|
150
|
+
if (outer && mode == 'write' && busy(e)) {
|
|
151
|
+
throw Object.assign(e, { retryable: true })
|
|
152
|
+
}
|
|
153
|
+
throw e
|
|
154
|
+
}
|
|
155
|
+
depth.set(driver, held + 1)
|
|
156
|
+
let run = (s: Stmt) =>
|
|
157
|
+
tracing ? scope(tracing, () => driver.query(s)) : driver.query(s)
|
|
158
|
+
let undo = (e: unknown): never => {
|
|
159
|
+
try {
|
|
160
|
+
// SQLite may already have rolled back the whole transaction. Tell the
|
|
161
|
+
// driver's caches and ledger even then, so none retain undone writes.
|
|
162
|
+
if (outer || driver.inTransaction?.() === false) {
|
|
163
|
+
run({ t: 'rollback', ifActive: true })
|
|
164
|
+
} else {
|
|
165
|
+
run({ t: 'rollback', to: name })
|
|
166
|
+
run({ t: 'release', name })
|
|
167
|
+
}
|
|
168
|
+
} catch (cleanup) {
|
|
169
|
+
throw new AggregateError(
|
|
170
|
+
[e, cleanup],
|
|
171
|
+
'SQLite transaction rollback failed',
|
|
172
|
+
{
|
|
173
|
+
cause: e,
|
|
174
|
+
},
|
|
175
|
+
)
|
|
176
|
+
} finally {
|
|
177
|
+
depth.set(driver, held)
|
|
178
|
+
}
|
|
179
|
+
throw e
|
|
180
|
+
}
|
|
181
|
+
let done = <T>(out: T): T => {
|
|
182
|
+
try {
|
|
183
|
+
run(outer ? { t: 'commit' } : { t: 'release', name })
|
|
184
|
+
} catch (e) {
|
|
185
|
+
return undo(e)
|
|
186
|
+
}
|
|
187
|
+
depth.set(driver, held)
|
|
188
|
+
return out
|
|
189
|
+
}
|
|
190
|
+
let out: R
|
|
191
|
+
try {
|
|
192
|
+
out = body()
|
|
193
|
+
} catch (e) {
|
|
194
|
+
return undo(e)
|
|
195
|
+
}
|
|
196
|
+
return (out instanceof Promise ? out.then(done, undo) : done(out)) as R
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** `body` with this connection's busy wait at nothing: a lock another
|
|
200
|
+
* connection holds refuses it at once, for work that can be left to later
|
|
201
|
+
* rather than waited for. */
|
|
202
|
+
export let unwaiting = <R>(driver: Driver, body: () => R): R => {
|
|
203
|
+
let [row] = driver.query({ t: 'pragma', name: 'busy_timeout' })
|
|
204
|
+
let was = Number(Object.values(row ?? {})[0] ?? 0)
|
|
205
|
+
driver.query({ t: 'pragma', name: 'busy_timeout', value: 0 })
|
|
206
|
+
try {
|
|
207
|
+
return body()
|
|
208
|
+
} finally {
|
|
209
|
+
driver.query({ t: 'pragma', name: 'busy_timeout', value: was })
|
|
210
|
+
}
|
|
211
|
+
}
|
package/versions.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// A table's committed mutation token, shared by every connection. Triggers
|
|
2
|
+
// move it in the same transaction as the rows, including raw SQL writers.
|
|
3
|
+
import { catalogue, col, eq, fn, lit, render, revision, val } from "@yaks/sql/core";
|
|
4
|
+
import { META } from "./layout.js";
|
|
5
|
+
import { read } from "./meta.js";
|
|
6
|
+
let key = (table)=>`table:${table}`;
|
|
7
|
+
let events = [
|
|
8
|
+
'insert',
|
|
9
|
+
'update',
|
|
10
|
+
'delete'
|
|
11
|
+
];
|
|
12
|
+
/** Add a mutation token without resetting one an earlier install raised. */ export let versioned = (table)=>[
|
|
13
|
+
{
|
|
14
|
+
t: 'insert',
|
|
15
|
+
or: 'ignore',
|
|
16
|
+
into: META,
|
|
17
|
+
cols: [
|
|
18
|
+
'k',
|
|
19
|
+
'v'
|
|
20
|
+
],
|
|
21
|
+
rows: [
|
|
22
|
+
[
|
|
23
|
+
lit(key(table)),
|
|
24
|
+
lit('0')
|
|
25
|
+
]
|
|
26
|
+
]
|
|
27
|
+
},
|
|
28
|
+
...events.map((event)=>({
|
|
29
|
+
t: 'create trigger',
|
|
30
|
+
name: `${table}_version_${event}`,
|
|
31
|
+
ifNot: true,
|
|
32
|
+
timing: 'after',
|
|
33
|
+
event,
|
|
34
|
+
on: table,
|
|
35
|
+
body: [
|
|
36
|
+
{
|
|
37
|
+
t: 'insert',
|
|
38
|
+
into: META,
|
|
39
|
+
cols: [
|
|
40
|
+
'k',
|
|
41
|
+
'v'
|
|
42
|
+
],
|
|
43
|
+
rows: [
|
|
44
|
+
[
|
|
45
|
+
lit(key(table)),
|
|
46
|
+
fn('hex', fn('randomblob', lit(16)))
|
|
47
|
+
]
|
|
48
|
+
],
|
|
49
|
+
upsert: [
|
|
50
|
+
{
|
|
51
|
+
on: [
|
|
52
|
+
col('k')
|
|
53
|
+
],
|
|
54
|
+
set: {
|
|
55
|
+
v: col('v', 'excluded')
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
]
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
}))
|
|
62
|
+
];
|
|
63
|
+
/** A single-owner connection observes mutations without persistent tokens. */ export let unversioned = (table)=>[
|
|
64
|
+
...events.map((event)=>({
|
|
65
|
+
t: 'drop',
|
|
66
|
+
kind: 'trigger',
|
|
67
|
+
name: `${table}_version_${event}`,
|
|
68
|
+
ifExists: true
|
|
69
|
+
})),
|
|
70
|
+
{
|
|
71
|
+
t: 'delete',
|
|
72
|
+
from: META,
|
|
73
|
+
where: eq(col('k'), val(key(table)))
|
|
74
|
+
}
|
|
75
|
+
];
|
|
76
|
+
/** A file's tokens brought to exactly `keep`: each kept table versioned, and
|
|
77
|
+
* every other table's token retired, whoever installed it (a schema that once
|
|
78
|
+
* kept one, or an unlanded checkout's CLI that opened this file). A token
|
|
79
|
+
* nobody reads only taxes every write to its table. */ export let clocked = (driver, keep)=>[
|
|
80
|
+
...keep.flatMap(versioned),
|
|
81
|
+
...[
|
|
82
|
+
...new Set(catalogue(driver).flatMap((o)=>o.type == 'trigger' && !keep.includes(o.tbl_name) && events.some((e)=>o.name == `${o.tbl_name}_version_${e}`) ? [
|
|
83
|
+
o.tbl_name
|
|
84
|
+
] : []))
|
|
85
|
+
].flatMap(unversioned)
|
|
86
|
+
];
|
|
87
|
+
// A counter would reuse a value after rollback and a different mutation. A
|
|
88
|
+
// fresh token cannot confuse that new state with a snapshot of the undone one.
|
|
89
|
+
let tracking = new WeakMap();
|
|
90
|
+
let tracked = (driver, table)=>{
|
|
91
|
+
let schema = revision(driver, 'schema');
|
|
92
|
+
let held = tracking.get(driver);
|
|
93
|
+
if (held?.schema != schema) {
|
|
94
|
+
tracking.set(driver, held = {
|
|
95
|
+
schema,
|
|
96
|
+
tables: new Map()
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
let found = held.tables.get(table);
|
|
100
|
+
if (found !== undefined) return found;
|
|
101
|
+
let objects = new Map(catalogue(driver).map((o)=>[
|
|
102
|
+
o.name,
|
|
103
|
+
o
|
|
104
|
+
]));
|
|
105
|
+
let body = (sql)=>sql.replace(/^create trigger /i, '');
|
|
106
|
+
let complete = versioned(table).every((s)=>s.t != 'create trigger' || body(objects.get(s.name)?.sql ?? '') == body(render({
|
|
107
|
+
...s,
|
|
108
|
+
ifNot: false
|
|
109
|
+
}, undefined, undefined, driver.stored?.v).sql));
|
|
110
|
+
held.tables.set(table, complete);
|
|
111
|
+
return complete;
|
|
112
|
+
};
|
|
113
|
+
/** Undefined means an older file has no token, so caches must reread rows.
|
|
114
|
+
* Read past what `meta` holds: a trigger moves it, which no revision sees. */ export let tableVersion = (driver, table)=>tracked(driver, table) ? read(driver, key(table)) : undefined;
|
|
115
|
+
//# sourceMappingURL=versions.js.map
|
package/versions.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"versions.js","sources":["./versions.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,0EAA0E;AAC1E,SACE,SAAS,EACT,GAAG,EAEH,EAAE,EACF,EAAE,EACF,GAAG,EACH,MAAM,EACN,QAAQ,EAER,GAAG,8BAC6B;AAClC,SAAS,IAAI,sBAAqB;AAClC,SAAS,IAAI,oBAAmB;AAEhC,IAAI,MAAM,CAAC,QAAkB,CAAC,MAAM,EAAE,OAAO;AAC7C,IAAI,SAAS;EAAC;EAAU;EAAU;CAAS;AAE3C,0EAA0E,GAC1E,OAAO,IAAI,YAAY,CAAC,QAA0B;IAChD;MACE,GAAG;MACH,IAAI;MACJ,MAAM;MACN,MAAM;QAAC;QAAK;OAAI;MAChB,MAAM;QAAC;UAAC,IAAI,IAAI;UAAS,IAAI;SAAK;OAAC;IACrC;OACG,OAAO,GAAG,CAAC,CAAC,QAAgB,CAAC;QAC9B,GAAG;QACH,MAAM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,CAAC,QAAQ,OAAO;MAAE;MAAQ,QAAQ,IAAI;IAAM;EAC1D;EACA,IAAI,QAAQ,KAAK,MAAM,CAAC,GAAG,CAAC;EAC5B,IAAI,UAAU,WAAW,OAAO;EAChC,IAAI,UAAU,IAAI,IAAI,UAAU,QAAQ,GAAG,CAAC,CAAC,IAAM;MAAC,EAAE,IAAI;MAAE;KAAE;EAC9D,IAAI,OAAO,CAAC,MAAgB,IAAI,OAAO,CAAC,qBAAqB;EAC7D,IAAI,WAAW,UAAU,OAAO,KAAK,CAAC,CAAC,IACrC,EAAE,CAAC,IAAI,oBACP,KAAK,QAAQ,GAAG,CAAC,EAAE,IAAI,GAAG,OAAO,OAC/B,KACE,OAAO;MAAE,GAAG,CAAC;MAAE,OAAO;IAAM,GAAG,WAAW,WAAW,OAAO,MAAM,EAAE,GACjE,GAAG;EAGZ,KAAK,MAAM,CAAC,GAAG,CAAC,OAAO;EACvB,OAAO;AACT;AAEA;4EAC4E,GAC5E,OAAO,IAAI,eAAe,CAAC,QAAgB,QACzC,QAAQ,QAAQ,SAAS,KAAK,QAAQ,IAAI,UAAU,UAAS"}
|
package/versions.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// A table's committed mutation token, shared by every connection. Triggers
|
|
2
|
+
// move it in the same transaction as the rows, including raw SQL writers.
|
|
3
|
+
import {
|
|
4
|
+
catalogue,
|
|
5
|
+
col,
|
|
6
|
+
type Driver,
|
|
7
|
+
eq,
|
|
8
|
+
fn,
|
|
9
|
+
lit,
|
|
10
|
+
render,
|
|
11
|
+
revision,
|
|
12
|
+
type Stmt,
|
|
13
|
+
val,
|
|
14
|
+
} from '@yaks/sql/core'
|
|
15
|
+
import { META } from './layout.js'
|
|
16
|
+
import { read } from './meta.js'
|
|
17
|
+
|
|
18
|
+
let key = (table: string) => `table:${table}`
|
|
19
|
+
let events = ['insert', 'update', 'delete'] as const
|
|
20
|
+
|
|
21
|
+
/** Add a mutation token without resetting one an earlier install raised. */
|
|
22
|
+
export let versioned = (table: string): Stmt[] => [
|
|
23
|
+
{
|
|
24
|
+
t: 'insert',
|
|
25
|
+
or: 'ignore',
|
|
26
|
+
into: META,
|
|
27
|
+
cols: ['k', 'v'],
|
|
28
|
+
rows: [[lit(key(table)), lit('0')]],
|
|
29
|
+
},
|
|
30
|
+
...events.map((event): Stmt => ({
|
|
31
|
+
t: 'create trigger',
|
|
32
|
+
name: `${table}_version_${event}`,
|
|
33
|
+
ifNot: true,
|
|
34
|
+
timing: 'after',
|
|
35
|
+
event,
|
|
36
|
+
on: table,
|
|
37
|
+
body: [{
|
|
38
|
+
t: 'insert',
|
|
39
|
+
into: META,
|
|
40
|
+
cols: ['k', 'v'],
|
|
41
|
+
rows: [[lit(key(table)), fn('hex', fn('randomblob', lit(16)))]],
|
|
42
|
+
upsert: [{
|
|
43
|
+
on: [col('k')],
|
|
44
|
+
set: { v: col('v', 'excluded') },
|
|
45
|
+
}],
|
|
46
|
+
}],
|
|
47
|
+
})),
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
/** A single-owner connection observes mutations without persistent tokens. */
|
|
51
|
+
export let unversioned = (table: string): Stmt[] => [
|
|
52
|
+
...events.map((event): Stmt => ({
|
|
53
|
+
t: 'drop',
|
|
54
|
+
kind: 'trigger',
|
|
55
|
+
name: `${table}_version_${event}`,
|
|
56
|
+
ifExists: true,
|
|
57
|
+
})),
|
|
58
|
+
{ t: 'delete', from: META, where: eq(col('k'), val(key(table))) },
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
/** A file's tokens brought to exactly `keep`: each kept table versioned, and
|
|
62
|
+
* every other table's token retired, whoever installed it (a schema that once
|
|
63
|
+
* kept one, or an unlanded checkout's CLI that opened this file). A token
|
|
64
|
+
* nobody reads only taxes every write to its table. */
|
|
65
|
+
export let clocked = (driver: Driver, keep: string[]): Stmt[] => [
|
|
66
|
+
...keep.flatMap(versioned),
|
|
67
|
+
...[
|
|
68
|
+
...new Set(
|
|
69
|
+
catalogue(driver).flatMap((o) =>
|
|
70
|
+
o.type == 'trigger' && !keep.includes(o.tbl_name) &&
|
|
71
|
+
events.some((e) => o.name == `${o.tbl_name}_version_${e}`)
|
|
72
|
+
? [o.tbl_name]
|
|
73
|
+
: []
|
|
74
|
+
),
|
|
75
|
+
),
|
|
76
|
+
].flatMap(unversioned),
|
|
77
|
+
]
|
|
78
|
+
|
|
79
|
+
// A counter would reuse a value after rollback and a different mutation. A
|
|
80
|
+
// fresh token cannot confuse that new state with a snapshot of the undone one.
|
|
81
|
+
let tracking = new WeakMap<Driver, {
|
|
82
|
+
schema: number
|
|
83
|
+
tables: Map<string, boolean>
|
|
84
|
+
}>()
|
|
85
|
+
let tracked = (driver: Driver, table: string): boolean => {
|
|
86
|
+
let schema = revision(driver, 'schema')
|
|
87
|
+
let held = tracking.get(driver)
|
|
88
|
+
if (held?.schema != schema) {
|
|
89
|
+
tracking.set(driver, held = { schema, tables: new Map() })
|
|
90
|
+
}
|
|
91
|
+
let found = held.tables.get(table)
|
|
92
|
+
if (found !== undefined) return found
|
|
93
|
+
let objects = new Map(catalogue(driver).map((o) => [o.name, o]))
|
|
94
|
+
let body = (sql: string) => sql.replace(/^create trigger /i, '')
|
|
95
|
+
let complete = versioned(table).every((s) =>
|
|
96
|
+
s.t != 'create trigger' ||
|
|
97
|
+
body(objects.get(s.name)?.sql ?? '') ==
|
|
98
|
+
body(
|
|
99
|
+
render({ ...s, ifNot: false }, undefined, undefined, driver.stored?.v)
|
|
100
|
+
.sql,
|
|
101
|
+
)
|
|
102
|
+
)
|
|
103
|
+
held.tables.set(table, complete)
|
|
104
|
+
return complete
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Undefined means an older file has no token, so caches must reread rows.
|
|
108
|
+
* Read past what `meta` holds: a trigger moves it, which no revision sees. */
|
|
109
|
+
export let tableVersion = (driver: Driver, table: string): string | undefined =>
|
|
110
|
+
tracked(driver, table) ? read(driver, key(table)) : undefined
|
package/vocab.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// The component and tool declarations, and only those: the module exported as
|
|
2
|
+
// `@yaks/sqlite/vocab`. This package declares no component — a store is not a
|
|
3
|
+
// domain, and nothing here is sent to a client. What it declares is two tools,
|
|
4
|
+
// the checks in ./tools.ts, because a tool is declared in a vocabulary and
|
|
5
|
+
// listed by the application the same way a component is: the file's own
|
|
6
|
+
// invariants are checked through the same mechanism every other package's are.
|
|
7
|
+
import doc from './vocab.json' with {
|
|
8
|
+
type: 'json'
|
|
9
|
+
};
|
|
10
|
+
import manifest from './package.json' with {
|
|
11
|
+
type: 'json'
|
|
12
|
+
};
|
|
13
|
+
/** The tool declarations this plugin contributes. */ export let sqliteDoc = doc;
|
|
14
|
+
/** What this package is: its manifest's description, the one place it is
|
|
15
|
+
* written, which a host stamps on each document here. */ export let description = manifest.description;
|
|
16
|
+
/** Every document this plugin declares. */ export let docs = [
|
|
17
|
+
sqliteDoc
|
|
18
|
+
];
|
|
19
|
+
//# sourceMappingURL=vocab.js.map
|