@voltro/sql-sqlite 0.85.0 → 0.87.0
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/CHANGELOG.md +5987 -0
- package/README.md +2 -0
- package/THIRD-PARTY-NOTICES.md +5 -257
- package/dist/index.d.ts +31 -2
- package/dist/index.js +9 -513
- package/dist/retry.js +17 -0
- package/dist/sqlLayer.js +23 -0
- package/dist/store.js +502 -0
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,517 +1,13 @@
|
|
|
1
|
-
import { SqliteClient as e } from "
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import { SqlClient as V } from "effect/sql/SqlClient";
|
|
7
|
-
//#region src/sqlLayer.ts
|
|
8
|
-
var H = (r) => e.layerConfig({
|
|
9
|
-
transactionService: t.succeed(c),
|
|
10
|
-
filename: t.succeed(r.filename),
|
|
11
|
-
prepareCacheSize: t.succeed(0),
|
|
12
|
-
transactionMode: t.succeed("deferred"),
|
|
13
|
-
...r.readonly === void 0 ? {} : { readonly: t.succeed(r.readonly) }
|
|
14
|
-
}).pipe(i.tap((t) => n.get(t, e.SqliteClient)`PRAGMA foreign_keys = ON`)), U = (e) => {
|
|
15
|
-
if (e.url) {
|
|
16
|
-
if (e.url === ":memory:") return { filename: ":memory:" };
|
|
17
|
-
if (e.url.startsWith("file:")) return { filename: e.url.slice(5) };
|
|
18
|
-
throw Error(`@voltro/sql-sqlite: unsupported url '${e.url}'. Accepted: 'file:./path.sqlite', 'file:/abs/path.sqlite', ':memory:'.`);
|
|
19
|
-
}
|
|
20
|
-
if (e.database) return { filename: e.database };
|
|
21
|
-
throw Error("@voltro/sql-sqlite: no filename supplied. Set DB_URL=file:./db.sqlite or DB_DATABASE=path/to/db.sqlite.");
|
|
22
|
-
}, W = (e) => H(U(e)), G = l, K = (e) => {
|
|
23
|
-
let t = e;
|
|
24
|
-
for (let e = 0; e < 5 && typeof t == "object" && t; e++) {
|
|
25
|
-
let { code: e, errcode: n } = t;
|
|
26
|
-
if (typeof n == "number") return String(n);
|
|
27
|
-
if (typeof e == "string" && e !== "ERR_SQLITE_ERROR") return e;
|
|
28
|
-
if (typeof e == "number") return String(e);
|
|
29
|
-
t = t.cause;
|
|
30
|
-
}
|
|
31
|
-
}, q = (e) => p(e).some((e) => {
|
|
32
|
-
let t = K(e);
|
|
33
|
-
return t !== void 0 && G.has(t);
|
|
34
|
-
}), J = (e) => q(e) ? "retry" : "noRetry", Y = B({ scope: "voltro:sqlite" }), X = async (e) => {
|
|
35
|
-
let t = e.tracerLayer ? i.mergeAll(e.sqlLayer, e.tracerLayer) : e.sqlLayer, n = a.make(t);
|
|
36
|
-
return new le(await n.runPromise(V), n, null, void 0, e.isRetryable ?? q, e.systemName ?? "sqlite", e.wrapTransaction, e.fullText ?? "fts5");
|
|
37
|
-
}, se = (e) => e, Z = (e) => h(e), ce = (e) => e.predicate === void 0 || e.predicate === null ? e : {
|
|
38
|
-
...e,
|
|
39
|
-
predicate: Z(e.predicate)
|
|
40
|
-
}, Q = (e, t) => {
|
|
41
|
-
let n = ee(e, t, [
|
|
42
|
-
"json",
|
|
43
|
-
"array",
|
|
44
|
-
"decimal"
|
|
45
|
-
]), r = {};
|
|
46
|
-
for (let [e, t] of Object.entries(n)) r[e] = t instanceof Date ? t.toISOString() : typeof t == "boolean" ? +!!t : t;
|
|
47
|
-
return r;
|
|
48
|
-
}, le = class e {
|
|
49
|
-
sql;
|
|
50
|
-
runtime;
|
|
51
|
-
namespace;
|
|
52
|
-
isRetryable;
|
|
53
|
-
systemName;
|
|
54
|
-
wrapTransaction;
|
|
55
|
-
fullText;
|
|
56
|
-
emitter;
|
|
57
|
-
inflightTxns = 0;
|
|
58
|
-
reportEagerFallback;
|
|
59
|
-
constructor(e, t, n = null, r, i = q, a = "sqlite", o = se, s = "fts5") {
|
|
60
|
-
this.sql = e, this.runtime = t, this.namespace = n, this.isRetryable = i, this.systemName = a, this.wrapTransaction = o, this.fullText = s, this.emitter = r ?? new z(), ae(this.emitter), this.reportEagerFallback = E(Y);
|
|
61
|
-
}
|
|
62
|
-
withNamespace(t) {
|
|
63
|
-
return t === this.namespace ? this : new e(this.sql, this.runtime, t, this.emitter, this.isRetryable, this.systemName, this.wrapTransaction, this.fullText);
|
|
64
|
-
}
|
|
65
|
-
withFullTextMode(e) {
|
|
66
|
-
return this.fullText === "like" && e.fullTextSearch !== void 0 ? {
|
|
67
|
-
...e,
|
|
68
|
-
fullTextSearch: {
|
|
69
|
-
...e.fullTextSearch,
|
|
70
|
-
fallback: "like"
|
|
71
|
-
}
|
|
72
|
-
} : e;
|
|
73
|
-
}
|
|
74
|
-
nsT(e) {
|
|
75
|
-
return re(this.namespace, e);
|
|
76
|
-
}
|
|
77
|
-
async attachNamespace(e, t) {
|
|
78
|
-
let n = this.sql, r = t ?? `${e}.db`;
|
|
79
|
-
await this.runtime.runPromise(n`ATTACH DATABASE ${r} AS ${n(e)}`);
|
|
80
|
-
}
|
|
81
|
-
async executeQuery(e, t, n) {
|
|
82
|
-
let i = x(ce(this.withFullTextMode(e)), this.sql, this.namespace), a = t ? r.provideService(i, this.sql.transactionService, t) : i, o = await O(this.systemName, "select", () => this.runtime.runPromise(a));
|
|
83
|
-
return S(o, e.table, "sqlite", e);
|
|
84
|
-
}
|
|
85
|
-
keyWhere(e, t) {
|
|
86
|
-
let n = this.sql, r = Q(Object.fromEntries(A(t)), e);
|
|
87
|
-
return n.and(Object.entries(r).map(([e, t]) => n`${n(e)} = ${t}`));
|
|
88
|
-
}
|
|
89
|
-
async executeInsert(e, t, n, i, a) {
|
|
90
|
-
t = P(e, t);
|
|
91
|
-
let o = this.sql, s = o`INSERT INTO ${o(this.nsT(e))} ${o.insert(Q(t, e))} RETURNING ${b(e, o)}`, c = n ? r.provideService(s, this.sql.transactionService, n) : s, l = await this.runtime.runPromise(c), u = S(l, e, "sqlite")[0];
|
|
92
|
-
if (!u) throw Error(`SqliteStore.insert: no row returned for table '${e}'`);
|
|
93
|
-
return await this.routeEvent({
|
|
94
|
-
table: e,
|
|
95
|
-
op: "insert",
|
|
96
|
-
old: null,
|
|
97
|
-
new: u
|
|
98
|
-
}, i, n, a), u;
|
|
99
|
-
}
|
|
100
|
-
async executeUpdate(e, t, n, i, a, o) {
|
|
101
|
-
let s = this.sql, c = s`UPDATE ${s(this.nsT(e))} SET ${s.update(Q(n, e))} WHERE ${this.keyWhere(e, t)} RETURNING ${b(e, s)}`, l = i ? r.provideService(c, this.sql.transactionService, i) : c, u = await this.runtime.runPromise(l), d = S(u, e, "sqlite")[0];
|
|
102
|
-
return d ? (await this.routeEvent({
|
|
103
|
-
table: e,
|
|
104
|
-
op: "update",
|
|
105
|
-
old: null,
|
|
106
|
-
new: d
|
|
107
|
-
}, a, i, o), d) : null;
|
|
108
|
-
}
|
|
109
|
-
async executeInsertMany(e, t, n, i, a) {
|
|
110
|
-
if (t = F(e, t), t.length === 0) return [];
|
|
111
|
-
let o = this.sql, s = t.map((t) => Q(t, e)), c = m(s, f("sqlite")), l = (t) => o`INSERT INTO ${o(this.nsT(e))} ${o.insert(t)} RETURNING ${b(e, o)}`, u;
|
|
112
|
-
if (c.length === 1) {
|
|
113
|
-
let e = l(c[0]), t = n ? r.provideService(e, this.sql.transactionService, n) : e;
|
|
114
|
-
u = await this.runtime.runPromise(t);
|
|
115
|
-
} else if (n) {
|
|
116
|
-
let e = [];
|
|
117
|
-
for (let t of c) e.push(...await this.runtime.runPromise(r.provideService(l(t), this.sql.transactionService, n)));
|
|
118
|
-
u = e;
|
|
119
|
-
} else u = await this.runtime.runPromise(o.withTransaction(r.map(r.forEach(c, l, { concurrency: 1 }), (e) => e.flat())));
|
|
120
|
-
let d = S(u, e, "sqlite");
|
|
121
|
-
for (let t of d) await this.routeEvent({
|
|
122
|
-
table: e,
|
|
123
|
-
op: "insert",
|
|
124
|
-
old: null,
|
|
125
|
-
new: t
|
|
126
|
-
}, i, n, a);
|
|
127
|
-
return d;
|
|
128
|
-
}
|
|
129
|
-
async executePatchJson(e, t, n, i, a, o, s, c) {
|
|
130
|
-
let l = this.sql, u = Q(T(t, n, c), e), d = l.csv([_(l, "sqlite", n, i, l`${l(this.nsT(e))}.${l(n.split(".")[0])}`), ...Object.entries(u).map(([e, t]) => l`${l(e)} = ${t}`)]), f = l`UPDATE ${l(this.nsT(e))} SET ${d} WHERE ${this.keyWhere(e, t)} RETURNING ${b(e, l)}`, p = a ? r.provideService(f, this.sql.transactionService, a) : f, m = await this.runtime.runPromise(p), h = S(m, e, "sqlite")[0];
|
|
131
|
-
return h ? (await this.routeEvent({
|
|
132
|
-
table: e,
|
|
133
|
-
op: "update",
|
|
134
|
-
old: null,
|
|
135
|
-
new: h
|
|
136
|
-
}, o, a, s), h) : null;
|
|
137
|
-
}
|
|
138
|
-
async executeDelete(e, t, n, i, a) {
|
|
139
|
-
let o = this.sql, s = o`DELETE FROM ${o(this.nsT(e))} WHERE ${this.keyWhere(e, t)} RETURNING ${b(e, o)}`, c = n ? r.provideService(s, this.sql.transactionService, n) : s, l = await this.runtime.runPromise(c), u = S(l, e, "sqlite")[0];
|
|
140
|
-
return u ? (await this.routeEvent({
|
|
141
|
-
table: e,
|
|
142
|
-
op: "delete",
|
|
143
|
-
old: u,
|
|
144
|
-
new: null
|
|
145
|
-
}, i, n, a), !0) : !1;
|
|
146
|
-
}
|
|
147
|
-
async appendInTxn(e, t, n) {
|
|
148
|
-
let i = this.sql, a = i`INSERT INTO ${i(this.nsT(e))} ${i.insert(Q(t, e))}`;
|
|
149
|
-
await this.runtime.runPromise(n ? r.provideService(a, this.sql.transactionService, n) : a);
|
|
150
|
-
}
|
|
151
|
-
async maxInTxn(e, t, n, i) {
|
|
152
|
-
let a = this.sql, o = Object.entries(n).map(([e, t]) => a`${a(e)} = ${t}`), s = a`SELECT MAX(${a(t)}) AS ${a("m")} FROM ${a(this.nsT(e))} WHERE ${a.and(o)}`, c = (await this.runtime.runPromise(i ? r.provideService(s, this.sql.transactionService, i) : s))[0]?.m;
|
|
153
|
-
return c == null ? null : Number(c);
|
|
154
|
-
}
|
|
155
|
-
async routeEvent(e, t, n = null, r) {
|
|
156
|
-
e = {
|
|
157
|
-
...d(r),
|
|
158
|
-
...e
|
|
159
|
-
}, k(e.table) && await N({
|
|
160
|
-
append: (e, t) => this.appendInTxn(e, t, n),
|
|
161
|
-
maxOf: (e, t, r) => this.maxInTxn(e, t, r, n)
|
|
162
|
-
}, {
|
|
163
|
-
table: e.table,
|
|
164
|
-
op: e.op,
|
|
165
|
-
next: e.new,
|
|
166
|
-
prev: e.old,
|
|
167
|
-
traceId: e.traceId,
|
|
168
|
-
subjectId: e.subjectId
|
|
169
|
-
}), w(e.table) && (t === null ? this.emitter.emit("change", e) : t.push(e));
|
|
170
|
-
}
|
|
171
|
-
async executeUpsert(e, t, n, i, a, o) {
|
|
172
|
-
let s = L(n);
|
|
173
|
-
if (t = P(e, t), typeof n.update == "function") {
|
|
174
|
-
let r = await this.findByConflict(e, t, n.conflictColumns, i);
|
|
175
|
-
if (r) {
|
|
176
|
-
let s = Object.fromEntries(n.conflictColumns.map((e) => [e, r[e]]));
|
|
177
|
-
return await this.executeUpdate(e, s, I(t, r, n), i, a, o) ?? r;
|
|
178
|
-
}
|
|
179
|
-
return this.executeInsert(e, t, i, a, o);
|
|
180
|
-
}
|
|
181
|
-
let c = this.sql, l = n.conflictColumns.map((e) => c`${c(e)}`), u = Object.keys(t).filter((e) => t[e] !== void 0), d = n.update === void 0 ? u.filter((e) => e !== "id" && !n.conflictColumns.includes(e)) : n.update, f = Q(s, e), p = [...d.filter((e) => !Object.hasOwn(s, e)).map((e) => c`${c(e)} = excluded.${c(e)}`), ...Object.entries(f).map(([e, t]) => c`${c(e)} = ${t}`)], m = p.length > 0 ? c.csv(p) : c`${c(n.conflictColumns[0])} = excluded.${c(n.conflictColumns[0])}`, h = c`INSERT INTO ${c(this.nsT(e))} ${c.insert(Q(t, e))} ON CONFLICT (${c.csv(l)}) DO UPDATE SET ${m} RETURNING ${b(e, c)}`, g = i ? r.provideService(h, this.sql.transactionService, i) : h, _ = await this.runtime.runPromise(g), v = S(_, e, "sqlite")[0];
|
|
182
|
-
if (!v) throw Error(`SqliteStore.upsert: no row returned for table '${e}'`);
|
|
183
|
-
let y = t.id !== void 0 && t.id === v.id ? "insert" : "update";
|
|
184
|
-
return await this.routeEvent({
|
|
185
|
-
table: e,
|
|
186
|
-
op: y,
|
|
187
|
-
old: null,
|
|
188
|
-
new: v
|
|
189
|
-
}, a, i, o), v;
|
|
190
|
-
}
|
|
191
|
-
async executeInsertIgnore(e, t, n, i, a, o) {
|
|
192
|
-
t = P(e, t);
|
|
193
|
-
let s = this.sql, c = n.conflictColumns.map((e) => s`${s(e)}`), l = s`INSERT INTO ${s(this.nsT(e))} ${s.insert(Q(t, e))} ON CONFLICT (${s.csv(c)}) DO NOTHING RETURNING ${b(e, s)}`, u = i ? r.provideService(l, this.sql.transactionService, i) : l, d = await this.runtime.runPromise(u), f = S(d, e, "sqlite")[0];
|
|
194
|
-
if (f) return await this.routeEvent({
|
|
195
|
-
table: e,
|
|
196
|
-
op: "insert",
|
|
197
|
-
old: null,
|
|
198
|
-
new: f
|
|
199
|
-
}, a, i, o), {
|
|
200
|
-
row: f,
|
|
201
|
-
outcome: "inserted"
|
|
202
|
-
};
|
|
203
|
-
let p = await this.findByConflict(e, t, n.conflictColumns, i);
|
|
204
|
-
if (!p) throw Error(`SqliteStore.insertIgnore: conflict fired but no existing row found in '${e}'`);
|
|
205
|
-
return {
|
|
206
|
-
row: p,
|
|
207
|
-
outcome: "ignored"
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
async findByConflict(e, t, n, i) {
|
|
211
|
-
if (n.length === 0) return;
|
|
212
|
-
let a = this.sql, o = Q(Object.fromEntries(n.map((e) => [e, t[e]])), e), s = n.map((e) => a`${a(e)} = ${o[e]}`), c = a`SELECT ${b(e, a)} FROM ${a(this.nsT(e))} WHERE ${a.and(s)} LIMIT 1`, l = i ? r.provideService(c, this.sql.transactionService, i) : c, u = await this.runtime.runPromise(l);
|
|
213
|
-
return S(u, e, "sqlite")[0];
|
|
214
|
-
}
|
|
215
|
-
query(e) {
|
|
216
|
-
return this.runWithEager(e, null);
|
|
217
|
-
}
|
|
218
|
-
raw(e, t) {
|
|
219
|
-
return this.rawOn(e, null);
|
|
220
|
-
}
|
|
221
|
-
rawOn(e, t) {
|
|
222
|
-
let n = y(e, this.sql), i = t ? r.provideService(n, this.sql.transactionService, t) : n;
|
|
223
|
-
return O(this.systemName, "raw", () => this.runtime.runPromise(i));
|
|
224
|
-
}
|
|
225
|
-
async runWithEager(e, t) {
|
|
226
|
-
if (!C(e)) return this.executeQuery(e, t);
|
|
227
|
-
let n = this.namespace === null ? g(e, this.sql, "sqlite") : null;
|
|
228
|
-
if (n !== null) try {
|
|
229
|
-
let e = t ? r.provideService(n.fragment, this.sql.transactionService, t) : n.fragment, i = await O(this.systemName, "select", () => this.runtime.runPromise(e));
|
|
230
|
-
return n.decode(i);
|
|
231
|
-
} catch (t) {
|
|
232
|
-
if (t instanceof s) throw t;
|
|
233
|
-
this.reportEagerFallback({
|
|
234
|
-
dialect: this.systemName,
|
|
235
|
-
table: e.table,
|
|
236
|
-
reason: "execute-failed",
|
|
237
|
-
error: t
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
else this.reportEagerFallback({
|
|
241
|
-
dialect: this.systemName,
|
|
242
|
-
table: e.table,
|
|
243
|
-
reason: "not-compilable"
|
|
244
|
-
});
|
|
245
|
-
let i = await this.executeQuery(e, t);
|
|
246
|
-
return u(i, e.eager, e.sourceTable ?? oe(e.table), (e) => this.executeQuery(e, t));
|
|
247
|
-
}
|
|
248
|
-
getInternalRunWithEager() {
|
|
249
|
-
return (e, t) => this.runWithEager(e, t);
|
|
250
|
-
}
|
|
251
|
-
localWrite(e, t, n) {
|
|
252
|
-
let r = (e) => k(t) ? this.writeWithRecorders(n, e) : n(e, null, null);
|
|
253
|
-
return O(this.systemName, e, () => R(r));
|
|
254
|
-
}
|
|
255
|
-
async writeWithRecorders(e, t) {
|
|
256
|
-
let n = [], i = this.sql, a = await this.runtime.runPromise(i.withTransaction(r.flatMap(r.serviceOption(this.sql.transactionService), (i) => o.isNone(i) ? r.fail(/* @__PURE__ */ Error("SqliteStore.write: TransactionConnection missing.")) : r.tryPromise({
|
|
257
|
-
try: () => e(t, i.value, n),
|
|
258
|
-
catch: (e) => e
|
|
259
|
-
}))));
|
|
260
|
-
for (let e of n) this.emitChange(e);
|
|
261
|
-
return a;
|
|
262
|
-
}
|
|
263
|
-
insert(e, t) {
|
|
264
|
-
return this.localWrite("insert", e, (n, r, i) => this.executeInsert(e, t, r, i, n));
|
|
265
|
-
}
|
|
266
|
-
insertMany(e, t) {
|
|
267
|
-
return this.localWrite("insert", e, (n, r, i) => this.executeInsertMany(e, t, r, i, n));
|
|
268
|
-
}
|
|
269
|
-
patchJson(e, t, n, r, i) {
|
|
270
|
-
return this.localWrite("update", e, (a, o, s) => this.executePatchJson(e, t, n, r, o, s, a, i));
|
|
271
|
-
}
|
|
272
|
-
update(e, t, n) {
|
|
273
|
-
return this.localWrite("update", e, (r, i, a) => this.executeUpdate(e, t, n, i, a, r));
|
|
274
|
-
}
|
|
275
|
-
delete(e, t) {
|
|
276
|
-
return this.localWrite("delete", e, (n, r, i) => this.executeDelete(e, t, r, i, n));
|
|
277
|
-
}
|
|
278
|
-
updateMany(e, t, n) {
|
|
279
|
-
return this.executeUpdateMany(e, t, n, null, null);
|
|
280
|
-
}
|
|
281
|
-
deleteMany(e, t) {
|
|
282
|
-
return this.executeDeleteMany(e, t, null, null);
|
|
283
|
-
}
|
|
284
|
-
async executeUpdateMany(e, t, n, i, a, o) {
|
|
285
|
-
let s = this.sql, c = v(Z(n.where), s, this.namespace, e), l = s`UPDATE ${s(this.nsT(e))} SET ${s.update(Q(t, e))} WHERE ${c} RETURNING ${b(e, s)}`, u = i ? r.provideService(l, this.sql.transactionService, i) : l, d = await O(this.systemName, "update", () => this.runtime.runPromise(u));
|
|
286
|
-
for (let t of S(d, e, "sqlite")) await this.routeEvent({
|
|
287
|
-
table: e,
|
|
288
|
-
op: "update",
|
|
289
|
-
old: null,
|
|
290
|
-
new: t
|
|
291
|
-
}, a, i, o);
|
|
292
|
-
return d.length;
|
|
293
|
-
}
|
|
294
|
-
async executeDeleteMany(e, t, n, i, a) {
|
|
295
|
-
let o = this.sql, s = v(Z(t.where), o, this.namespace, e), c = o`DELETE FROM ${o(this.nsT(e))} WHERE ${s} RETURNING ${b(e, o)}`, l = n ? r.provideService(c, this.sql.transactionService, n) : c, u = await O(this.systemName, "delete", () => this.runtime.runPromise(l));
|
|
296
|
-
for (let t of S(u, e, "sqlite")) await this.routeEvent({
|
|
297
|
-
table: e,
|
|
298
|
-
op: "delete",
|
|
299
|
-
old: t,
|
|
300
|
-
new: null
|
|
301
|
-
}, i, n, a);
|
|
302
|
-
return u.length;
|
|
303
|
-
}
|
|
304
|
-
upsert(e, t, n) {
|
|
305
|
-
return this.localWrite("upsert", e, (r, i, a) => this.executeUpsert(e, t, n, i, a, r));
|
|
306
|
-
}
|
|
307
|
-
insertIgnore(e, t, n) {
|
|
308
|
-
return this.insertIgnoreWithOutcome(e, t, n).then((e) => e.row);
|
|
309
|
-
}
|
|
310
|
-
insertIgnoreWithOutcome(e, t, n) {
|
|
311
|
-
return this.localWrite("upsert", e, (r, i, a) => this.executeInsertIgnore(e, t, n, i, a, r));
|
|
312
|
-
}
|
|
313
|
-
emitChange(e) {
|
|
314
|
-
this.emitter.emit("change", e);
|
|
315
|
-
}
|
|
316
|
-
getInternalExecuteQuery() {
|
|
317
|
-
return this.executeQuery.bind(this);
|
|
318
|
-
}
|
|
319
|
-
getInternalExecuteInsert() {
|
|
320
|
-
return this.executeInsert.bind(this);
|
|
321
|
-
}
|
|
322
|
-
getInternalExecuteInsertMany() {
|
|
323
|
-
return this.executeInsertMany.bind(this);
|
|
324
|
-
}
|
|
325
|
-
getInternalExecutePatchJson() {
|
|
326
|
-
return this.executePatchJson.bind(this);
|
|
327
|
-
}
|
|
328
|
-
getInternalExecuteUpdate() {
|
|
329
|
-
return this.executeUpdate.bind(this);
|
|
330
|
-
}
|
|
331
|
-
getInternalExecuteUpdateMany() {
|
|
332
|
-
return this.executeUpdateMany.bind(this);
|
|
333
|
-
}
|
|
334
|
-
getInternalExecuteDeleteMany() {
|
|
335
|
-
return this.executeDeleteMany.bind(this);
|
|
336
|
-
}
|
|
337
|
-
getInternalExecuteDelete() {
|
|
338
|
-
return this.executeDelete.bind(this);
|
|
339
|
-
}
|
|
340
|
-
getInternalExecuteUpsert() {
|
|
341
|
-
return this.executeUpsert.bind(this);
|
|
342
|
-
}
|
|
343
|
-
getInternalExecuteInsertIgnore() {
|
|
344
|
-
return this.executeInsertIgnore.bind(this);
|
|
345
|
-
}
|
|
346
|
-
async emptyTables(e) {
|
|
347
|
-
await this.emptyTablesOn(e, null);
|
|
348
|
-
}
|
|
349
|
-
async incomingForeignKeys(e) {
|
|
350
|
-
if (e.length === 0) return [];
|
|
351
|
-
let t = this.sql, n = new Set(e), r = await this.runtime.runPromise(t`SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'`), i = [];
|
|
352
|
-
for (let e of r) {
|
|
353
|
-
if (n.has(e.name)) continue;
|
|
354
|
-
let r = await this.runtime.runPromise(t`${t.unsafe(`PRAGMA foreign_key_list(${ie(e.name, "sqlite")})`)}`).catch(() => []);
|
|
355
|
-
for (let t of r) n.has(t.table) && i.push({
|
|
356
|
-
from: e.name,
|
|
357
|
-
column: t.from,
|
|
358
|
-
to: t.table
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
return i;
|
|
362
|
-
}
|
|
363
|
-
async emptyTablesOn(e, t) {
|
|
364
|
-
if (e.length === 0) return;
|
|
365
|
-
let n = this.sql, i = (e) => this.nsT(e), a = r.acquireUseRelease(n.unsafe("PRAGMA defer_foreign_keys = ON"), () => r.forEach(e, (e) => n`DELETE FROM ${n(i(e))}`, { discard: !0 }), () => r.orDie(r.ignore(n.unsafe("PRAGMA defer_foreign_keys = OFF")))), o = t === null ? n.withTransaction(a) : r.provideService(a, this.sql.transactionService, t);
|
|
366
|
-
await this.runtime.runPromise(o);
|
|
367
|
-
}
|
|
368
|
-
transactionEndingFailure(e) {
|
|
369
|
-
return this.isRetryable(e);
|
|
370
|
-
}
|
|
371
|
-
async transactional(e) {
|
|
372
|
-
this.inflightTxns++;
|
|
373
|
-
try {
|
|
374
|
-
return await M({
|
|
375
|
-
...this.txnSpec("SqliteStore.transactional"),
|
|
376
|
-
work: e,
|
|
377
|
-
makeView: (e, t) => new $(this, e, t)
|
|
378
|
-
});
|
|
379
|
-
} finally {
|
|
380
|
-
this.inflightTxns--;
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
get transactionService() {
|
|
384
|
-
return this.sql.transactionService;
|
|
385
|
-
}
|
|
386
|
-
txnSpec(e) {
|
|
387
|
-
return {
|
|
388
|
-
label: e,
|
|
389
|
-
dialect: this.systemName === "turso" ? "turso" : "sqlite",
|
|
390
|
-
withTransaction: (e) => this.sql.withTransaction(e),
|
|
391
|
-
transactionService: this.sql.transactionService,
|
|
392
|
-
runPromiseExit: (e) => this.runtime.runPromiseExit(e),
|
|
393
|
-
isRetryable: this.isRetryable,
|
|
394
|
-
wrapProgram: (e) => this.wrapTransaction(e),
|
|
395
|
-
span: {
|
|
396
|
-
name: "store.transactional",
|
|
397
|
-
attributes: {
|
|
398
|
-
"db.system": this.systemName,
|
|
399
|
-
"db.operation": "transaction"
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
};
|
|
403
|
-
}
|
|
404
|
-
onChange(e) {
|
|
405
|
-
return this.emitter.on("change", e), () => {
|
|
406
|
-
this.emitter.off("change", e);
|
|
407
|
-
};
|
|
408
|
-
}
|
|
409
|
-
injectExternalChange(e) {
|
|
410
|
-
w(e.table) && this.emitter.emit("change", te(e));
|
|
411
|
-
}
|
|
412
|
-
run(e) {
|
|
413
|
-
return this.runtime.runPromise(e);
|
|
414
|
-
}
|
|
415
|
-
async close(e = 5e3) {
|
|
416
|
-
if (this.inflightTxns > 0) {
|
|
417
|
-
let t = Date.now() + e;
|
|
418
|
-
for (; this.inflightTxns > 0 && Date.now() < t;) await new Promise((e) => setTimeout(e, 25));
|
|
419
|
-
this.inflightTxns > 0 && Y.warn("close: grace period expired with in-flight transactions", {
|
|
420
|
-
gracePeriodMs: e,
|
|
421
|
-
inflight: this.inflightTxns
|
|
422
|
-
});
|
|
423
|
-
}
|
|
424
|
-
await this.runtime.dispose();
|
|
425
|
-
}
|
|
426
|
-
async ping() {
|
|
427
|
-
await this.runtime.runPromise(this.sql`SELECT 1`);
|
|
428
|
-
}
|
|
429
|
-
}, $ = class {
|
|
430
|
-
parent;
|
|
431
|
-
txn;
|
|
432
|
-
attr;
|
|
433
|
-
events = [];
|
|
434
|
-
aborts = D((e) => this.parent.transactionEndingFailure(e));
|
|
435
|
-
committed = !1;
|
|
436
|
-
constructor(e, t, n = void 0) {
|
|
437
|
-
this.parent = e, this.txn = t, this.attr = n;
|
|
438
|
-
}
|
|
439
|
-
savepoint(e) {
|
|
440
|
-
return j({
|
|
441
|
-
tracker: this.aborts,
|
|
442
|
-
name: `voltro_sp_${++this.savepointCounter}`,
|
|
443
|
-
spelling: "standard",
|
|
444
|
-
exec: (e) => this.parent.run(r.provideService(V.pipe(r.flatMap((t) => t.unsafe(e))), this.parent.transactionService, this.txn))
|
|
445
|
-
}, e);
|
|
446
|
-
}
|
|
447
|
-
savepointCounter = 0;
|
|
448
|
-
run(e) {
|
|
449
|
-
return this.aborts.track(() => this.parent.run(ne(e, this.parent.transactionService, this.txn)));
|
|
450
|
-
}
|
|
451
|
-
abortedBy() {
|
|
452
|
-
return this.aborts.abortedBy();
|
|
453
|
-
}
|
|
454
|
-
raw(e, t) {
|
|
455
|
-
return this.aborts.track(() => this.parent.rawOn(e, this.txn));
|
|
456
|
-
}
|
|
457
|
-
query(e) {
|
|
458
|
-
return this.aborts.track(() => this.parent.getInternalRunWithEager()(e, this.txn));
|
|
459
|
-
}
|
|
460
|
-
emptyTables(e) {
|
|
461
|
-
return this.aborts.track(() => this.parent.emptyTablesOn(e, this.txn));
|
|
462
|
-
}
|
|
463
|
-
insert(e, t) {
|
|
464
|
-
return this.aborts.track(() => this.parent.getInternalExecuteInsert()(e, t, this.txn, this.events, this.attr));
|
|
465
|
-
}
|
|
466
|
-
insertMany(e, t) {
|
|
467
|
-
return this.aborts.track(() => this.parent.getInternalExecuteInsertMany()(e, t, this.txn, this.events, this.attr));
|
|
468
|
-
}
|
|
469
|
-
patchJson(e, t, n, r, i) {
|
|
470
|
-
return this.aborts.track(() => this.parent.getInternalExecutePatchJson()(e, t, n, r, this.txn, this.events, this.attr, i));
|
|
471
|
-
}
|
|
472
|
-
update(e, t, n) {
|
|
473
|
-
return this.aborts.track(() => this.parent.getInternalExecuteUpdate()(e, t, n, this.txn, this.events, this.attr));
|
|
474
|
-
}
|
|
475
|
-
delete(e, t) {
|
|
476
|
-
return this.aborts.track(() => this.parent.getInternalExecuteDelete()(e, t, this.txn, this.events, this.attr));
|
|
477
|
-
}
|
|
478
|
-
updateMany(e, t, n) {
|
|
479
|
-
return this.aborts.track(() => this.parent.getInternalExecuteUpdateMany()(e, t, n, this.txn, this.events, this.attr));
|
|
480
|
-
}
|
|
481
|
-
deleteMany(e, t) {
|
|
482
|
-
return this.aborts.track(() => this.parent.getInternalExecuteDeleteMany()(e, t, this.txn, this.events, this.attr));
|
|
483
|
-
}
|
|
484
|
-
upsert(e, t, n) {
|
|
485
|
-
return this.aborts.track(() => this.parent.getInternalExecuteUpsert()(e, t, n, this.txn, this.events, this.attr));
|
|
486
|
-
}
|
|
487
|
-
insertIgnore(e, t, n) {
|
|
488
|
-
return this.insertIgnoreWithOutcome(e, t, n).then((e) => e.row);
|
|
489
|
-
}
|
|
490
|
-
insertIgnoreWithOutcome(e, t, n) {
|
|
491
|
-
return this.aborts.track(() => this.parent.getInternalExecuteInsertIgnore()(e, t, n, this.txn, this.events, this.attr));
|
|
492
|
-
}
|
|
493
|
-
transactional(e) {
|
|
494
|
-
return Promise.reject(/* @__PURE__ */ Error("SqliteStore.transactional: nested transactions are not supported. Mutations should execute a single top-level transaction."));
|
|
495
|
-
}
|
|
496
|
-
onChange(e) {
|
|
497
|
-
throw Error("SqliteTransactionalView.onChange: subscribing from inside a transaction is not supported.");
|
|
498
|
-
}
|
|
499
|
-
injectExternalChange(e) {
|
|
500
|
-
throw Error("SqliteTransactionalView.injectExternalChange: injecting external changes from inside a transaction is not supported.");
|
|
501
|
-
}
|
|
502
|
-
commitEvents() {
|
|
503
|
-
if (!this.committed) {
|
|
504
|
-
this.committed = !0;
|
|
505
|
-
for (let e of this.events) this.parent.emitChange(e);
|
|
506
|
-
this.events.length = 0;
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
}, ue = {
|
|
1
|
+
import { SqliteClient as e, connectionFromConfig as t, makeSqliteSqlLayer as n, makeSqliteSqlLayerFromConfig as r } from "./sqlLayer.js";
|
|
2
|
+
import { sqliteRetryFilter as i } from "./retry.js";
|
|
3
|
+
import { makeSqliteStore as a } from "./store.js";
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
var o = {
|
|
510
6
|
id: "sqlite",
|
|
511
|
-
makeSqlLayer: (e) =>
|
|
512
|
-
makeStore: (e) =>
|
|
7
|
+
makeSqlLayer: (e) => r(e),
|
|
8
|
+
makeStore: (e) => a(e),
|
|
513
9
|
compileContains: (e, t, n) => n`${e} LIKE ${`%${t.replace(/[\\%_]/g, (e) => `\\${e}`)}%`} ESCAPE '\\'`,
|
|
514
|
-
retryFilter:
|
|
10
|
+
retryFilter: i
|
|
515
11
|
};
|
|
516
12
|
//#endregion
|
|
517
|
-
export { e as SqliteClient,
|
|
13
|
+
export { e as SqliteClient, t as connectionFromConfig, n as makeSqliteSqlLayer, r as makeSqliteSqlLayerFromConfig, a as makeSqliteStore, o as sqliteDialect, i as sqliteRetryFilter };
|
package/dist/retry.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SQLITE_TRANSIENT_CODES as e, causeBranches as t } from "@voltro/database";
|
|
2
|
+
//#region src/retry.ts
|
|
3
|
+
var n = e, r = (e) => {
|
|
4
|
+
let t = e;
|
|
5
|
+
for (let e = 0; e < 5 && typeof t == "object" && t; e++) {
|
|
6
|
+
let { code: e, errcode: n } = t;
|
|
7
|
+
if (typeof n == "number") return String(n);
|
|
8
|
+
if (typeof e == "string" && e !== "ERR_SQLITE_ERROR") return e;
|
|
9
|
+
if (typeof e == "number") return String(e);
|
|
10
|
+
t = t.cause;
|
|
11
|
+
}
|
|
12
|
+
}, i = (e) => t(e).some((e) => {
|
|
13
|
+
let t = r(e);
|
|
14
|
+
return t !== void 0 && n.has(t);
|
|
15
|
+
}), a = (e) => i(e) ? "retry" : "noRetry";
|
|
16
|
+
//#endregion
|
|
17
|
+
export { i as isRetryableSqliteFailure, a as sqliteRetryFilter };
|
package/dist/sqlLayer.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SqliteClient as e } from "@effect/sql-sqlite-node";
|
|
2
|
+
import { Config as t, Context as n, Duration as r, Layer as i } from "effect";
|
|
3
|
+
import { SHARED_TRANSACTION_SERVICE as a } from "@voltro/database";
|
|
4
|
+
//#region src/sqlLayer.ts
|
|
5
|
+
var o = r.seconds(2), s = (s) => e.layerConfig({
|
|
6
|
+
transactionService: t.succeed(a),
|
|
7
|
+
filename: t.succeed(s.filename),
|
|
8
|
+
prepareCacheSize: t.succeed(0),
|
|
9
|
+
transactionMode: t.succeed("deferred"),
|
|
10
|
+
busyTimeout: t.succeed(r.zero),
|
|
11
|
+
...s.readonly === void 0 ? {} : { readonly: t.succeed(s.readonly) },
|
|
12
|
+
prepareCacheTTL: t.succeed(o)
|
|
13
|
+
}).pipe(i.tap((t) => n.get(t, e.SqliteClient)`PRAGMA foreign_keys = ON`)), c = (e) => {
|
|
14
|
+
if (e.url) {
|
|
15
|
+
if (e.url === ":memory:") return { filename: ":memory:" };
|
|
16
|
+
if (e.url.startsWith("file:")) return { filename: e.url.slice(5) };
|
|
17
|
+
throw Error(`@voltro/sql-sqlite: unsupported url '${e.url}'. Accepted: 'file:./path.sqlite', 'file:/abs/path.sqlite', ':memory:'.`);
|
|
18
|
+
}
|
|
19
|
+
if (e.database) return { filename: e.database };
|
|
20
|
+
throw Error("@voltro/sql-sqlite: no filename supplied. Set DB_URL=file:./db.sqlite or DB_DATABASE=path/to/db.sqlite.");
|
|
21
|
+
}, l = (e) => s(c(e));
|
|
22
|
+
//#endregion
|
|
23
|
+
export { o as PREPARE_CACHE_TTL, e as SqliteClient, c as connectionFromConfig, s as makeSqliteSqlLayer, l as makeSqliteSqlLayerFromConfig };
|