@aiao/rxdb-adapter-sqlite 0.0.1
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/README.md +1 -0
- package/RxDBAdapterSqlite.d.ts +66 -0
- package/SqliteClient.d.ts +19 -0
- package/SqliteRepository.d.ts +23 -0
- package/SqliteRepositoryBase.d.ts +85 -0
- package/SqliteTreeRepository.d.ts +17 -0
- package/execute_statement_helper.d.ts +7 -0
- package/generate_entity_delete_sql.d.ts +9 -0
- package/generate_entity_find_tree_sql.d.ts +41 -0
- package/generate_entity_insert_sql.d.ts +9 -0
- package/generate_entity_soft_delete_sql.d.ts +9 -0
- package/generate_entity_update_sql.d.ts +9 -0
- package/generate_query_find_sql.d.ts +30 -0
- package/generate_table_create_sql.d.ts +8 -0
- package/generate_table_trigger_sql.d.ts +8 -0
- package/index.d.ts +5 -0
- package/index.js +1136 -0
- package/package.json +20 -0
- package/sqlite-load.utils.d.ts +39 -0
- package/sqlite.interface.d.ts +57 -0
- package/sqlite.utils.d.ts +107 -0
- package/types.d.ts +7 -0
- package/wa-sqlite.d.ts +611 -0
package/index.js
ADDED
|
@@ -0,0 +1,1136 @@
|
|
|
1
|
+
import { RxDBError as Ee, PropertyType as m, RelationKind as y, getEntityMetadata as q, RxDBChange as ye, RepositoryBase as $e, TRANSACTION_BEGIN as be, TRANSACTION_COMMIT as Ne, TRANSACTION_ROLLBACK as Te, getEntityStatus as ae, isEntityMatchRuleGroup as S, isEntityEffectOrderBy as we, TransactionBeginEvent as ge, TransactionCommitEvent as Oe, TransactionRollbackEvent as Ie, RxDBAdapterLocalBase as Ae, RxDBAdapterUpdateChangeEvent as qe, RxDBAdapterInsertChangeEvent as Re, RxDBAdapterDeleteChangeEvent as Se } from "@aiao/rxdb";
|
|
2
|
+
import { wrap as ve } from "comlink";
|
|
3
|
+
import { Subject as ke, auditTime as xe, ReplaySubject as U, BehaviorSubject as ie, Observable as oe, shareReplay as L, switchMap as w, take as Q, takeUntil as K, map as f, firstValueFrom as I, tap as A, fromEvent as Ce, defer as Le, filter as Z, catchError as Me, of as je, from as ee } from "rxjs";
|
|
4
|
+
import { pickBy as ce, isFunction as De, get as Be, traverseObjectKeys as Fe } from "@aiao/utils";
|
|
5
|
+
import { Factory as We, SQLITE_ROW as Pe } from "@journeyapps/wa-sqlite";
|
|
6
|
+
const Ve = (r) => `SELECT * FROM sqlite_master WHERE type='table' AND name='${r}' limit 1;`, Ue = (r) => !!r.sql && (r.results.length === 0 || r.results[0].rows.length === 0), R = "_rowid", te = (r) => {
|
|
7
|
+
switch (r.type) {
|
|
8
|
+
case m.uuid:
|
|
9
|
+
case m.string:
|
|
10
|
+
case m.json:
|
|
11
|
+
return "TEXT";
|
|
12
|
+
case m.number:
|
|
13
|
+
return "REAL";
|
|
14
|
+
case m.integer:
|
|
15
|
+
case m.boolean:
|
|
16
|
+
return "INTEGER";
|
|
17
|
+
case m.date:
|
|
18
|
+
return "TEXT";
|
|
19
|
+
}
|
|
20
|
+
}, Qe = (r, e) => {
|
|
21
|
+
if (r === null && e.nullable) return null;
|
|
22
|
+
if (r !== void 0)
|
|
23
|
+
switch (e.type) {
|
|
24
|
+
case m.boolean:
|
|
25
|
+
return r ? 1 : 0;
|
|
26
|
+
case m.date:
|
|
27
|
+
return r instanceof Date ? r.toISOString() : r;
|
|
28
|
+
default:
|
|
29
|
+
return r;
|
|
30
|
+
}
|
|
31
|
+
}, Ke = (r, e) => {
|
|
32
|
+
if (r === null && e.nullable) return null;
|
|
33
|
+
if (r !== void 0)
|
|
34
|
+
switch (e.type) {
|
|
35
|
+
case m.boolean:
|
|
36
|
+
return !!r;
|
|
37
|
+
case m.date:
|
|
38
|
+
return r;
|
|
39
|
+
case m.json:
|
|
40
|
+
return r ? JSON.parse(r) : null;
|
|
41
|
+
default:
|
|
42
|
+
return r;
|
|
43
|
+
}
|
|
44
|
+
}, le = (r, e) => `${e}$${r}`, E = (r) => le(r.name, r.namespace), ue = (r, e) => `idx_${r.name}_${e.name}`;
|
|
45
|
+
class $ extends Ee {
|
|
46
|
+
}
|
|
47
|
+
const Y = (r, e) => {
|
|
48
|
+
const t = {};
|
|
49
|
+
return Object.keys(e).forEach((s) => {
|
|
50
|
+
s.endsWith("Id") && (t[s] = e[s]);
|
|
51
|
+
const n = r.propertyMap.get(s);
|
|
52
|
+
n && (t[s] = Qe(e[s], n));
|
|
53
|
+
}), t;
|
|
54
|
+
}, Ye = (r, e) => ce(e, (t, s) => r.propertyMap.has(s) || r.foreignKeyNames.includes(s)), He = (r, e) => ce(e, (t, s) => r.propertyMap.get(s)?.readonly !== !0), se = (r, e, t) => {
|
|
55
|
+
const s = {};
|
|
56
|
+
return t.forEach((n, a) => {
|
|
57
|
+
const o = e[a];
|
|
58
|
+
if (r.isForeignKey(o))
|
|
59
|
+
s[o] = n;
|
|
60
|
+
else {
|
|
61
|
+
const c = r.propertyMap.get(o);
|
|
62
|
+
c && (s[o] = Ke(n, c));
|
|
63
|
+
}
|
|
64
|
+
}), s;
|
|
65
|
+
}, ne = {
|
|
66
|
+
[y.ONE_TO_ONE]: 0,
|
|
67
|
+
[y.ONE_TO_MANY]: 1,
|
|
68
|
+
[y.MANY_TO_ONE]: 2,
|
|
69
|
+
[y.MANY_TO_MANY]: 3
|
|
70
|
+
}, Je = (r, e) => {
|
|
71
|
+
let t = "";
|
|
72
|
+
const s = E(e);
|
|
73
|
+
return Array.from(e.relationMap.values())?.toSorted((n, a) => ne[n.kind] - ne[a.kind]).forEach((n) => {
|
|
74
|
+
switch (n.kind) {
|
|
75
|
+
case y.ONE_TO_ONE:
|
|
76
|
+
case y.MANY_TO_ONE:
|
|
77
|
+
{
|
|
78
|
+
const a = `${n.name}Id`;
|
|
79
|
+
if (t += `
|
|
80
|
+
ALTER TABLE ${s} ADD COLUMN "${a}" TEXT`, n.nullable || (t += " NOT NULL"), n.mappedEntity) {
|
|
81
|
+
const o = le(n.mappedEntity, n.mappedNamespace);
|
|
82
|
+
t += ` REFERENCES ${o}(id)`;
|
|
83
|
+
}
|
|
84
|
+
n.kind === y.MANY_TO_ONE && (t += " ON DELETE CASCADE"), t += ";", (n.unique || n.kind === y.ONE_TO_ONE) && (t += `
|
|
85
|
+
CREATE UNIQUE INDEX ${ue(e, n)} on ${s}(${a});`);
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}), t;
|
|
90
|
+
}, Ge = (r, e) => {
|
|
91
|
+
const t = E(e);
|
|
92
|
+
let s = `CREATE TABLE ${t} (`;
|
|
93
|
+
const n = [], a = [], o = [];
|
|
94
|
+
e.propertyMap.forEach((i) => {
|
|
95
|
+
let l = "";
|
|
96
|
+
if (l += `"${i.name}"`, i.primary ? i.type === m.integer ? l += " INTEGER PRIMARY KEY AUTOINCREMENT" : l += ` ${te(i)} PRIMARY KEY` : l += ` ${te(i)}`, !i.nullable) l += " NOT NULL";
|
|
97
|
+
else if (Reflect.get(i, "default") !== void 0) {
|
|
98
|
+
let u = i.default;
|
|
99
|
+
De(i.default) && (u = i.default()), l += ` DEFAULT ${String(u)}`;
|
|
100
|
+
}
|
|
101
|
+
switch (i.type) {
|
|
102
|
+
case m.json:
|
|
103
|
+
a.push(`CHECK ( JSON_VALID(${i.name})=1 )`);
|
|
104
|
+
break;
|
|
105
|
+
case m.boolean:
|
|
106
|
+
a.push(`CHECK (${i.name} in(0,1))`);
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
i.unique && n.push(
|
|
110
|
+
`CREATE UNIQUE INDEX ${ue(e, i)} on ${t}(${i.name});`
|
|
111
|
+
), o.push(l);
|
|
112
|
+
});
|
|
113
|
+
const c = [...o, ...a];
|
|
114
|
+
if (c.length)
|
|
115
|
+
s += c.map((i) => `
|
|
116
|
+
${i}`).join(","), s += `
|
|
117
|
+
);`;
|
|
118
|
+
else
|
|
119
|
+
throw new $("columns is empty!");
|
|
120
|
+
return n.length && (s += `
|
|
121
|
+
` + n.join(`
|
|
122
|
+
`)), s;
|
|
123
|
+
}, ze = (r, e) => {
|
|
124
|
+
let t = "";
|
|
125
|
+
return t += Ge(r, e), t += Je(r, e), t;
|
|
126
|
+
}, Xe = (r, e) => {
|
|
127
|
+
const t = E(e), s = q(ye), n = E(s), { propertyMap: a, name: o, foreignKeyNames: c, namespace: i } = e, l = [...a.keys(), ...c].filter((d) => ["id", "updatedAt"].includes(d) === !1), u = "namespace, entity, type, entityId, inversePatch, patch", h = `CREATE TRIGGER ${t}_insert AFTER INSERT ON ${t}
|
|
128
|
+
BEGIN
|
|
129
|
+
INSERT INTO ${n} (${u}) VALUES (
|
|
130
|
+
'${i}', '${o}', 'INSERT', NEW.id, NULL,
|
|
131
|
+
json_object(${l.map((d) => `'${d}', NEW.${d}`).join(`,
|
|
132
|
+
`)})
|
|
133
|
+
);
|
|
134
|
+
END;`, _ = `CREATE TRIGGER ${t}_update AFTER UPDATE ON ${t}
|
|
135
|
+
WHEN ( ${l.map((d) => `OLD.${d} IS NOT NEW.${d}`).join(` OR
|
|
136
|
+
`)} )
|
|
137
|
+
BEGIN
|
|
138
|
+
INSERT INTO ${n} (${u}) VALUES (
|
|
139
|
+
'${i}','${o}','UPDATE', NEW.id,
|
|
140
|
+
(
|
|
141
|
+
SELECT json_group_object(key, value) FROM (
|
|
142
|
+
${l.map((d) => `SELECT '${d}' AS key, OLD.${d} AS value WHERE OLD.${d} IS NOT NEW.${d}`).join(`
|
|
143
|
+
UNION `)}
|
|
144
|
+
)
|
|
145
|
+
),
|
|
146
|
+
(
|
|
147
|
+
SELECT json_group_object(key, value) FROM (
|
|
148
|
+
${l.map((d) => `SELECT '${d}' AS key, NEW.${d} AS value WHERE OLD.${d} IS NOT NEW.${d}`).join(`
|
|
149
|
+
UNION `)}
|
|
150
|
+
)
|
|
151
|
+
)
|
|
152
|
+
);
|
|
153
|
+
END;
|
|
154
|
+
`, p = `CREATE TRIGGER ${t}_delete AFTER DELETE ON ${t}
|
|
155
|
+
BEGIN
|
|
156
|
+
INSERT INTO ${n} (${u}) VALUES (
|
|
157
|
+
'${i}', '${o}', 'DELETE', OLD.id,
|
|
158
|
+
json_object(${l.map((d) => `'${d}', OLD.${d}`).join(`,
|
|
159
|
+
`)}), NULL
|
|
160
|
+
);
|
|
161
|
+
END;`;
|
|
162
|
+
return h + `
|
|
163
|
+
` + _ + `
|
|
164
|
+
` + p;
|
|
165
|
+
};
|
|
166
|
+
var M = /* @__PURE__ */ ((r) => (r[r.SQLITE_DELETE = 9] = "SQLITE_DELETE", r[r.SQLITE_INSERT = 18] = "SQLITE_INSERT", r[r.SQLITE_UPDATE = 23] = "SQLITE_UPDATE", r))(M || {});
|
|
167
|
+
const de = "sqlite", Ze = () => import("@journeyapps/wa-sqlite/dist/wa-sqlite-async.mjs").then((r) => r.default), et = () => import("@journeyapps/wa-sqlite/dist/wa-sqlite.mjs").then((r) => r.default), tt = [
|
|
168
|
+
{
|
|
169
|
+
name: "MemoryVFS",
|
|
170
|
+
vfsModule: () => import("@journeyapps/wa-sqlite/src/examples/MemoryVFS.js").then((r) => r.MemoryVFS),
|
|
171
|
+
sync: !0,
|
|
172
|
+
async: !0,
|
|
173
|
+
worker: !0,
|
|
174
|
+
sharedWorker: !0,
|
|
175
|
+
jsContext: !0,
|
|
176
|
+
multipleConnections: !1
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
name: "MemoryAsyncVFS",
|
|
180
|
+
vfsModule: () => import("@journeyapps/wa-sqlite/src/examples/MemoryAsyncVFS.js").then((r) => r.MemoryAsyncVFS),
|
|
181
|
+
sync: !1,
|
|
182
|
+
async: !0,
|
|
183
|
+
worker: !0,
|
|
184
|
+
sharedWorker: !0,
|
|
185
|
+
jsContext: !0,
|
|
186
|
+
multipleConnections: !1
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
name: "IDBBatchAtomicVFS",
|
|
190
|
+
vfsModule: () => import("@journeyapps/wa-sqlite/src/examples/IDBBatchAtomicVFS.js").then((r) => r.IDBBatchAtomicVFS),
|
|
191
|
+
vfsOptions: { lockPolicy: "shared+hint" },
|
|
192
|
+
sync: !1,
|
|
193
|
+
async: !0,
|
|
194
|
+
worker: !0,
|
|
195
|
+
sharedWorker: !0,
|
|
196
|
+
jsContext: !0,
|
|
197
|
+
multipleConnections: !0
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
name: "OPFSAdaptiveVFS",
|
|
201
|
+
vfsModule: () => import("@journeyapps/wa-sqlite/src/examples/OPFSAdaptiveVFS.js").then((r) => r.OPFSAdaptiveVFS),
|
|
202
|
+
vfsOptions: { lockPolicy: "shared+hint" },
|
|
203
|
+
sync: !1,
|
|
204
|
+
async: !0,
|
|
205
|
+
worker: !0,
|
|
206
|
+
sharedWorker: !1,
|
|
207
|
+
jsContext: !1,
|
|
208
|
+
multipleConnections: !0
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
name: "AccessHandlePoolVFS",
|
|
212
|
+
vfsModule: () => import("@journeyapps/wa-sqlite/src/examples/AccessHandlePoolVFS.js").then((r) => r.AccessHandlePoolVFS),
|
|
213
|
+
sync: !0,
|
|
214
|
+
async: !0,
|
|
215
|
+
worker: !0,
|
|
216
|
+
sharedWorker: !1,
|
|
217
|
+
jsContext: !1,
|
|
218
|
+
multipleConnections: !1
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
name: "OPFSAnyContextVFS",
|
|
222
|
+
vfsModule: () => import("@journeyapps/wa-sqlite/src/examples/OPFSAnyContextVFS.js").then((r) => r.OPFSAnyContextVFS),
|
|
223
|
+
vfsOptions: { lockPolicy: "shared+hint" },
|
|
224
|
+
sync: !1,
|
|
225
|
+
async: !0,
|
|
226
|
+
worker: !0,
|
|
227
|
+
sharedWorker: !1,
|
|
228
|
+
jsContext: !0,
|
|
229
|
+
multipleConnections: !0
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
name: "OPFSCoopSyncVFS",
|
|
233
|
+
vfsModule: () => import("@journeyapps/wa-sqlite/src/examples/OPFSCoopSyncVFS.js").then((r) => r.OPFSCoopSyncVFS),
|
|
234
|
+
sync: !0,
|
|
235
|
+
async: !0,
|
|
236
|
+
worker: !0,
|
|
237
|
+
sharedWorker: !1,
|
|
238
|
+
jsContext: !1,
|
|
239
|
+
multipleConnections: !0
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
name: "OPFSPermutedVFS",
|
|
243
|
+
vfsModule: () => import("@journeyapps/wa-sqlite/src/examples/OPFSPermutedVFS.js").then((r) => r.OPFSPermutedVFS),
|
|
244
|
+
sync: !1,
|
|
245
|
+
async: !0,
|
|
246
|
+
worker: !0,
|
|
247
|
+
sharedWorker: !1,
|
|
248
|
+
jsContext: !1,
|
|
249
|
+
multipleConnections: !0
|
|
250
|
+
}
|
|
251
|
+
], he = (r) => {
|
|
252
|
+
const e = tt.find((a) => a.name === r.vfs), { vfs: t, async: s, worker: n } = r;
|
|
253
|
+
if (!e) throw new $(`vfs ${t} not found`);
|
|
254
|
+
if (s !== s) throw new $(`vfs ${t} not support async: ${s}`);
|
|
255
|
+
if (n && !e.worker) throw new $(`vfs ${t} not support worker`);
|
|
256
|
+
if (!n && !e.jsContext) throw new $(`vfs ${t} only support worker`);
|
|
257
|
+
return e;
|
|
258
|
+
}, st = async (r) => {
|
|
259
|
+
const e = he(r), t = r.async ?? !0;
|
|
260
|
+
let s;
|
|
261
|
+
t ? s = Ze : s = et;
|
|
262
|
+
const [n, a] = await Promise.all([s(), e.vfsModule()]), o = {};
|
|
263
|
+
r.wasmPath && (o.locateFile = () => r.wasmPath);
|
|
264
|
+
const c = await n(o), i = We(c), l = await a.create(r.vfs, c, e.vfsOptions);
|
|
265
|
+
return i.vfs_register(l, !0), i;
|
|
266
|
+
}, _e = (r, e) => `${de}:${r}:${e}`, nt = async (r, e, t, s) => {
|
|
267
|
+
const n = performance.now(), a = s ? [s] : [[]], o = [];
|
|
268
|
+
let c = 0;
|
|
269
|
+
const i = {
|
|
270
|
+
sql: t,
|
|
271
|
+
runStart: n,
|
|
272
|
+
results: o
|
|
273
|
+
};
|
|
274
|
+
for await (const l of r.statements(e, t)) {
|
|
275
|
+
if (s)
|
|
276
|
+
for (const _ of a)
|
|
277
|
+
r.reset(l), s && r.bind_collection(l, _);
|
|
278
|
+
const u = [];
|
|
279
|
+
for (; await r.step(l) === Pe; ) {
|
|
280
|
+
const _ = r.row(l);
|
|
281
|
+
u.push(_);
|
|
282
|
+
}
|
|
283
|
+
const h = r.column_names(l);
|
|
284
|
+
h.length && o.push({ columns: h, rows: u }), c += r.changes(e);
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
...i,
|
|
288
|
+
rowsAffected: c,
|
|
289
|
+
runEnd: performance.now()
|
|
290
|
+
};
|
|
291
|
+
};
|
|
292
|
+
class rt {
|
|
293
|
+
#e;
|
|
294
|
+
#t;
|
|
295
|
+
changeChanel;
|
|
296
|
+
async init(e, t) {
|
|
297
|
+
const s = await st(t), n = e + ".sqlite";
|
|
298
|
+
this.#t = await s.open_v2(n), this.#e = s, await this.execute("PRAGMA temp_store = memory;"), await this.execute("PRAGMA foreign_keys = ON;");
|
|
299
|
+
const a = t?.cacheSizeKb || 50 * 1024;
|
|
300
|
+
await this.execute(`PRAGMA cache_size = -${a};`);
|
|
301
|
+
const o = _e(e, "changes");
|
|
302
|
+
this.changeChanel = new BroadcastChannel(o), this.#e.update_hook(
|
|
303
|
+
this.#t,
|
|
304
|
+
(c, i, l, u) => {
|
|
305
|
+
if (!l) return;
|
|
306
|
+
const h = {
|
|
307
|
+
type: c,
|
|
308
|
+
dbName: i,
|
|
309
|
+
tableName: l,
|
|
310
|
+
rowid: u,
|
|
311
|
+
recordAt: Date.now(),
|
|
312
|
+
timeStamp: performance.now()
|
|
313
|
+
};
|
|
314
|
+
this.changeChanel.postMessage(h);
|
|
315
|
+
}
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
async version() {
|
|
319
|
+
const e = await this.execute("SELECT sqlite_version()");
|
|
320
|
+
return Be(e, "results[0].rows[0][0]");
|
|
321
|
+
}
|
|
322
|
+
async disconnect() {
|
|
323
|
+
await this.#e.close(this.#t);
|
|
324
|
+
}
|
|
325
|
+
async execute(e, t) {
|
|
326
|
+
return nt(this.#e, this.#t, e, t);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
const at = (r, e, t) => {
|
|
330
|
+
const s = E(r), n = [e.id];
|
|
331
|
+
return {
|
|
332
|
+
sql: `DELETE FROM ${s} WHERE id = ?;`,
|
|
333
|
+
params: n
|
|
334
|
+
};
|
|
335
|
+
}, it = (r, e, t) => {
|
|
336
|
+
const s = E(r), n = Ye(r, e);
|
|
337
|
+
t?.userId && (n.createdBy = t.userId, n.updatedBy = t.userId), n.createdAt = /* @__PURE__ */ new Date(), n.updatedAt = n.createdAt;
|
|
338
|
+
const a = Object.keys(n), o = Y(r, n), c = Array(a.length).fill("?").join(","), i = Object.values(o);
|
|
339
|
+
return {
|
|
340
|
+
sql: `INSERT INTO ${s} (${a.join(",")}) VALUES (${c}) RETURNING rowid as ${R},*;`,
|
|
341
|
+
params: i
|
|
342
|
+
};
|
|
343
|
+
}, ot = (r, e, t) => {
|
|
344
|
+
const s = {};
|
|
345
|
+
s.removedAt = /* @__PURE__ */ new Date(), t?.userId && (s.removedBy = t.userId);
|
|
346
|
+
const n = E(r), o = Object.keys(s).map((l) => `${l} = ?`).join(","), c = Y(r, s), i = [...Object.values(c), e.id];
|
|
347
|
+
return {
|
|
348
|
+
sql: `UPDATE ${n} SET ${o} WHERE id = ? and removedAt is null and removedBy is null RETURNING rowid as ${R},*;`,
|
|
349
|
+
params: i
|
|
350
|
+
};
|
|
351
|
+
}, ct = (r, e, t, s) => {
|
|
352
|
+
const n = He(r, t);
|
|
353
|
+
n.updatedAt = /* @__PURE__ */ new Date(), s?.userId && (n.updatedBy = s.userId);
|
|
354
|
+
const a = Object.keys(n), o = Y(r, n), c = a.map((h) => `${h} = ?`).join(","), i = [...Object.values(o), e.id];
|
|
355
|
+
return {
|
|
356
|
+
sql: `UPDATE ${E(r)} SET ${c} WHERE id = ? RETURNING rowid as ${R},*;`,
|
|
357
|
+
params: i
|
|
358
|
+
};
|
|
359
|
+
}, H = (r) => r?.map((e) => `${e.field} ${e.order}`).join(", "), v = (r, e = /* @__PURE__ */ new Map()) => {
|
|
360
|
+
const t = r.rules.map((s) => re(s) ? v(s, e) : lt(s, e)).filter(Boolean);
|
|
361
|
+
return t.length === 0 ? "" : t.length === 1 ? t[0] : `(${t.join(re(r) ? ` ${r.combinator} ` : " ")})`;
|
|
362
|
+
}, lt = (r, e) => {
|
|
363
|
+
let t, s;
|
|
364
|
+
if (r?.value === null ? r.operator === "=" ? (t = "", s = "is null") : r.operator === "!=" && (t = "", s = "is not null") : (t = dt(r.operator).toLowerCase(), s = ht(r)), !s && ["in", "notIn", "between", "notBetween"].includes(r.operator)) return "";
|
|
365
|
+
let n = String(r.field);
|
|
366
|
+
if (e.has(n))
|
|
367
|
+
n = e.get(n);
|
|
368
|
+
else if (n.includes(".")) {
|
|
369
|
+
const a = n.lastIndexOf("."), o = n.slice(0, a), c = n.slice(a + 1);
|
|
370
|
+
n = `'${o}'.${c}`;
|
|
371
|
+
}
|
|
372
|
+
return `${n} ${t} ${s}`.replaceAll(/ +/g, " ").trim();
|
|
373
|
+
}, re = (r) => "rules" in r && "combinator" in r, ut = {
|
|
374
|
+
"=": "=",
|
|
375
|
+
"!=": "!=",
|
|
376
|
+
">": ">",
|
|
377
|
+
">=": ">=",
|
|
378
|
+
"<": "<",
|
|
379
|
+
"<=": "<=",
|
|
380
|
+
in: "IN",
|
|
381
|
+
notIn: "NOT IN",
|
|
382
|
+
between: "BETWEEN",
|
|
383
|
+
notBetween: "NOT BETWEEN",
|
|
384
|
+
contains: "LIKE",
|
|
385
|
+
notContain: "NOT LIKE",
|
|
386
|
+
beginWith: "LIKE",
|
|
387
|
+
notBeginWith: "NOT LIKE",
|
|
388
|
+
endWith: "LIKE",
|
|
389
|
+
notEndWith: "NOT LIKE"
|
|
390
|
+
}, dt = (r) => ut[r] || r, ht = (r) => {
|
|
391
|
+
switch (r.operator) {
|
|
392
|
+
case "in":
|
|
393
|
+
case "notIn":
|
|
394
|
+
return `(${r.value.map((e) => `'${e}'`).join(", ")})`;
|
|
395
|
+
case "between":
|
|
396
|
+
case "notBetween": {
|
|
397
|
+
const [e, t] = r.value;
|
|
398
|
+
return `'${e}' and '${t}'`;
|
|
399
|
+
}
|
|
400
|
+
case "contains":
|
|
401
|
+
case "notContain":
|
|
402
|
+
return `'%${r.value}%'`;
|
|
403
|
+
case "beginWith":
|
|
404
|
+
case "notBeginWith":
|
|
405
|
+
return `'${r.value}%'`;
|
|
406
|
+
case "endWith":
|
|
407
|
+
case "notEndWith":
|
|
408
|
+
return `'%${r.value}'`;
|
|
409
|
+
}
|
|
410
|
+
return typeof r.value == "boolean" ? r.value ? "1" : "0" : typeof r.value == "string" ? `'${r.value}'` : typeof r.value == "number" ? r.value.toString() : r.value instanceof Date ? `'${r.value.toISOString()}'` : `'${r.value}'`;
|
|
411
|
+
}, j = (r, e, t) => {
|
|
412
|
+
const s = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), c = (u) => {
|
|
413
|
+
let h = u, _ = 1;
|
|
414
|
+
for (; n.has(h); )
|
|
415
|
+
h = `${u}_${_}`, _++;
|
|
416
|
+
return n.add(h), h;
|
|
417
|
+
}, i = (u) => {
|
|
418
|
+
if (o.has(u))
|
|
419
|
+
return o.get(u);
|
|
420
|
+
const h = u.includes("_") ? u : u.split("_")[0], _ = c(h);
|
|
421
|
+
return o.set(u, _), _;
|
|
422
|
+
};
|
|
423
|
+
Fe(t, (u, h, _) => {
|
|
424
|
+
const p = h;
|
|
425
|
+
if (u !== "field" || p.includes(".") === !1) return;
|
|
426
|
+
const { relations: d, isForeignKey: x, propertyName: B } = r.rxdb.schema.getFieldRelations(e, h);
|
|
427
|
+
if (x) {
|
|
428
|
+
_.field = B;
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
const pe = p.replace(`.${B}`, ""), F = (N) => d.length === 1 ? N.name : `${pe}_${N.name}`, G = (N) => (s.has(N) || s.set(N, []), s.get(N));
|
|
432
|
+
d.forEach(({ metadata: N, relation: g }, z) => {
|
|
433
|
+
const C = r.rxdb.schema.findMappedRelation(N, g);
|
|
434
|
+
if (!C) throw new $("mappedRelation not found");
|
|
435
|
+
const P = () => {
|
|
436
|
+
const O = G(C.metadata), b = F(g), k = i(b);
|
|
437
|
+
return {
|
|
438
|
+
joinArray: O,
|
|
439
|
+
uniqueAlias: k
|
|
440
|
+
};
|
|
441
|
+
};
|
|
442
|
+
let T = "_";
|
|
443
|
+
if (z > 0) {
|
|
444
|
+
const O = d[z - 1], b = F(O.relation);
|
|
445
|
+
T = i(b);
|
|
446
|
+
}
|
|
447
|
+
switch (g.kind) {
|
|
448
|
+
case y.ONE_TO_ONE:
|
|
449
|
+
case y.ONE_TO_MANY:
|
|
450
|
+
{
|
|
451
|
+
const { joinArray: O, uniqueAlias: b } = P();
|
|
452
|
+
O.push({
|
|
453
|
+
joinTableName: b,
|
|
454
|
+
on: `'${b}'.${C.relation.name}Id = ${T === "_" ? T : `'${T}'`}.id`
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
break;
|
|
458
|
+
case y.MANY_TO_ONE:
|
|
459
|
+
{
|
|
460
|
+
const { joinArray: O, uniqueAlias: b } = P();
|
|
461
|
+
O.push({
|
|
462
|
+
joinTableName: b,
|
|
463
|
+
on: `'${b}'.id = ${T === "_" ? T : `'${T}'`}.${g.name}Id`
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
break;
|
|
467
|
+
case y.MANY_TO_MANY:
|
|
468
|
+
{
|
|
469
|
+
const O = q(g.junctionEntityType), b = G(O), k = c(`${g.name}_m_n`);
|
|
470
|
+
b.push({
|
|
471
|
+
joinTableName: k,
|
|
472
|
+
on: `'${k}'.${C.relation.name}Id = ${T === "_" ? T : `'${T}'`}.id`
|
|
473
|
+
});
|
|
474
|
+
const { joinArray: me, uniqueAlias: X } = P();
|
|
475
|
+
me.push({
|
|
476
|
+
joinTableName: X,
|
|
477
|
+
on: `'${X}'.id = '${k}'.${g.name}Id`
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
break;
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
const W = d[d.length - 1];
|
|
484
|
+
if (r.rxdb.schema.findMappedRelation(W.metadata, W.relation)) {
|
|
485
|
+
const N = F(W.relation), g = i(N);
|
|
486
|
+
a.set(p, `'${g}'.${B}`);
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
let l = "";
|
|
490
|
+
for (const [u, h] of s.entries()) {
|
|
491
|
+
const _ = E(u);
|
|
492
|
+
for (const p of h)
|
|
493
|
+
l += ` LEFT JOIN ${_} '${p.joinTableName}' ON ${p.on}`;
|
|
494
|
+
}
|
|
495
|
+
return { joinSQL: l, fieldAliasMap: a };
|
|
496
|
+
}, J = (r) => {
|
|
497
|
+
const { tableName: e, where: t, limit: s, orderBy: n, join: a } = r;
|
|
498
|
+
let o = `SELECT _.rowid as ${R}, _.* FROM ${e} _`;
|
|
499
|
+
return a && (o += a), t && (o += ` where ${t}`), n && (o += ` order by ${n}`), s && (o += ` limit ${s}`), o += ";", o;
|
|
500
|
+
}, _t = (r, e, t, s) => {
|
|
501
|
+
t = structuredClone(t);
|
|
502
|
+
const { joinSQL: n, fieldAliasMap: a } = j(r, e, t), o = E(e), c = v(t, a), i = H(s?.orderBy);
|
|
503
|
+
return {
|
|
504
|
+
sql: J({ tableName: o, where: c, orderBy: i, join: n }),
|
|
505
|
+
params: []
|
|
506
|
+
};
|
|
507
|
+
}, ft = (r, e, t, s) => {
|
|
508
|
+
t = structuredClone(t);
|
|
509
|
+
const { joinSQL: n, fieldAliasMap: a } = j(r, e, t), o = E(e), c = v(t, a), i = H(s?.orderBy);
|
|
510
|
+
return {
|
|
511
|
+
sql: J({ tableName: o, where: c, orderBy: i, join: n }),
|
|
512
|
+
params: []
|
|
513
|
+
};
|
|
514
|
+
}, pt = (r, e, t, s) => {
|
|
515
|
+
t = structuredClone(t);
|
|
516
|
+
const { joinSQL: n, fieldAliasMap: a } = j(r, e, t), o = E(e), c = v(t, a);
|
|
517
|
+
let i = `SELECT count(_.rowid) as count FROM ${o} _`;
|
|
518
|
+
return n && (i += n), c && (i += ` where ${c}`), {
|
|
519
|
+
sql: i,
|
|
520
|
+
params: []
|
|
521
|
+
};
|
|
522
|
+
}, V = (r, e, t, s) => {
|
|
523
|
+
t = structuredClone(t);
|
|
524
|
+
const { joinSQL: n, fieldAliasMap: a } = j(r, e, t), o = E(e), c = v(t, a), i = H(s?.orderBy);
|
|
525
|
+
return {
|
|
526
|
+
sql: J({ tableName: o, where: c, orderBy: i, join: n, limit: 1 }),
|
|
527
|
+
params: []
|
|
528
|
+
};
|
|
529
|
+
};
|
|
530
|
+
class mt extends $e {
|
|
531
|
+
constructor(e, t, s) {
|
|
532
|
+
super(e.rxdb, s), this.adapter = e, this.client$ = t, this.metadata = q(s), this.run_task$.subscribe(() => this.#e()), this.rxdb.addEventListener(be, () => {
|
|
533
|
+
this.transaction_pending = !0;
|
|
534
|
+
}), this.rxdb.addEventListener(Ne, () => {
|
|
535
|
+
this.transaction_pending = !1, this.transaction_entities.forEach((n) => this.merge_cache(n.type, n.entities)), this.transaction_entities = [];
|
|
536
|
+
}), this.rxdb.addEventListener(Te, () => {
|
|
537
|
+
this.transaction_pending = !1, this.transaction_entities = [];
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
metadata;
|
|
541
|
+
need_run_task = new ke();
|
|
542
|
+
run_task$ = this.need_run_task.asObservable().pipe(xe(16));
|
|
543
|
+
cache_query_task_map = /* @__PURE__ */ new Map();
|
|
544
|
+
cache_query_tasks = [];
|
|
545
|
+
transaction_entities = [];
|
|
546
|
+
transaction_pending = !1;
|
|
547
|
+
/**
|
|
548
|
+
* 根据 type 计算主动更新缓存策略
|
|
549
|
+
*/
|
|
550
|
+
merge_cache(e, t) {
|
|
551
|
+
if (this.transaction_pending) {
|
|
552
|
+
this.transaction_entities.push({ type: e, entities: t });
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* 添加缓存
|
|
558
|
+
* @param sqliteSuccessResult
|
|
559
|
+
* @param forcedUpdate 强制刷新,在数据有的情况下也会更新数据,在修改数据的情况下需要
|
|
560
|
+
*/
|
|
561
|
+
add_query_cache(e, t = !1) {
|
|
562
|
+
const s = [];
|
|
563
|
+
return e.results.forEach(({ columns: n, rows: a }) => {
|
|
564
|
+
const o = n.findIndex((i) => i === "id"), c = n.findIndex((i) => i === R);
|
|
565
|
+
a.forEach((i) => {
|
|
566
|
+
const l = i[o];
|
|
567
|
+
let u;
|
|
568
|
+
if (this.hasEntityRef(l)) {
|
|
569
|
+
if (u = this.getEntityRef(l), s.push(u), t) {
|
|
570
|
+
const _ = se(this.metadata, n, i);
|
|
571
|
+
this.updateEntity(u, _);
|
|
572
|
+
}
|
|
573
|
+
} else {
|
|
574
|
+
const _ = se(this.metadata, n, i);
|
|
575
|
+
u = this.createEntityRef(_), s.push(u);
|
|
576
|
+
}
|
|
577
|
+
const h = ae(u);
|
|
578
|
+
h.local = !0, c !== -1 && (h.rowId = BigInt(i[c])), h.modified = !1;
|
|
579
|
+
});
|
|
580
|
+
}), s;
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* 创建查询任务
|
|
584
|
+
*/
|
|
585
|
+
create_query_task(e) {
|
|
586
|
+
const { sql: t, params: s } = e, n = t + s.join(",");
|
|
587
|
+
let a;
|
|
588
|
+
if (this.cache_query_task_map.has(n) === !1) {
|
|
589
|
+
const o = new U(), c = o.asObservable(), i = new ie(0), l = i.asObservable(), u = new oe((p) => {
|
|
590
|
+
const d = this.cache_query_task_map.get(n);
|
|
591
|
+
return d.observerCount++, a = p, _(), () => {
|
|
592
|
+
if (d.observerCount--, d.observerCount <= 0) {
|
|
593
|
+
o.next(), o.complete(), this.cache_query_task_map.delete(n);
|
|
594
|
+
const x = this.cache_query_tasks.indexOf(d);
|
|
595
|
+
x >= 0 && this.cache_query_tasks.splice(x, 1);
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
}).pipe(L(1)), h = {
|
|
599
|
+
sql: t,
|
|
600
|
+
params: s,
|
|
601
|
+
observerCount: 0,
|
|
602
|
+
result$: u,
|
|
603
|
+
destroy$: c,
|
|
604
|
+
refresh$: l,
|
|
605
|
+
refresh: () => i.next(i.value + 1),
|
|
606
|
+
next: (p) => a.next(p),
|
|
607
|
+
error: (p) => a.error(p)
|
|
608
|
+
}, _ = () => {
|
|
609
|
+
this.cache_query_tasks.push(h), this.need_run_task.next();
|
|
610
|
+
};
|
|
611
|
+
return this.cache_query_task_map.set(n, h), h;
|
|
612
|
+
}
|
|
613
|
+
return this.cache_query_task_map.get(n);
|
|
614
|
+
}
|
|
615
|
+
#e() {
|
|
616
|
+
const e = this.cache_query_tasks.shift();
|
|
617
|
+
if (!e) return;
|
|
618
|
+
const { sql: t, params: s, refresh$: n } = e;
|
|
619
|
+
return n.pipe(
|
|
620
|
+
w(
|
|
621
|
+
() => this.client$.pipe(
|
|
622
|
+
Q(1),
|
|
623
|
+
K(e.destroy$),
|
|
624
|
+
w(() => this.adapter.execute(t, s))
|
|
625
|
+
)
|
|
626
|
+
)
|
|
627
|
+
).subscribe({
|
|
628
|
+
next: (a) => e.next(a),
|
|
629
|
+
error: (a) => e.error(a)
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
class fe extends mt {
|
|
634
|
+
#e = /* @__PURE__ */ new Map();
|
|
635
|
+
get(e, t) {
|
|
636
|
+
const s = {
|
|
637
|
+
combinator: "and",
|
|
638
|
+
rules: [
|
|
639
|
+
{
|
|
640
|
+
field: "id",
|
|
641
|
+
operator: "=",
|
|
642
|
+
value: e
|
|
643
|
+
}
|
|
644
|
+
]
|
|
645
|
+
}, n = V(this.adapter, this.metadata, s, t), a = this.create_query_task(n);
|
|
646
|
+
return this.#s("get", a, s, t), a.result$.pipe(
|
|
647
|
+
f((o) => this.#t(a, o)),
|
|
648
|
+
f((o) => {
|
|
649
|
+
if (o.length === 0) throw new $("not found");
|
|
650
|
+
return o[0];
|
|
651
|
+
})
|
|
652
|
+
);
|
|
653
|
+
}
|
|
654
|
+
findOne(e, t) {
|
|
655
|
+
const s = V(this.adapter, this.metadata, e, t), n = this.create_query_task(s);
|
|
656
|
+
return this.#s("findOne", n, e, t), n.result$.pipe(
|
|
657
|
+
f((a) => this.#t(n, a)),
|
|
658
|
+
f((a) => a[0])
|
|
659
|
+
);
|
|
660
|
+
}
|
|
661
|
+
findOneOrFail(e, t) {
|
|
662
|
+
const s = V(this.adapter, this.metadata, e, t), n = this.create_query_task(s);
|
|
663
|
+
return this.#s("findOneOrFail", n, e, t), n.result$.pipe(
|
|
664
|
+
f((a) => this.#t(n, a)),
|
|
665
|
+
f((a) => {
|
|
666
|
+
if (a.length === 0)
|
|
667
|
+
throw new $("not found");
|
|
668
|
+
return a[0];
|
|
669
|
+
})
|
|
670
|
+
);
|
|
671
|
+
}
|
|
672
|
+
find(e, t) {
|
|
673
|
+
const s = _t(this.adapter, this.metadata, e, t), n = this.create_query_task(s);
|
|
674
|
+
return this.#s("find", n, e, t), n.result$.pipe(f((a) => this.#t(n, a)));
|
|
675
|
+
}
|
|
676
|
+
findAll(e, t) {
|
|
677
|
+
const s = ft(this.adapter, this.metadata, e, t), n = this.create_query_task(s);
|
|
678
|
+
return this.#s("findAll", n, e, t), n.result$.pipe(f((a) => this.#t(n, a)));
|
|
679
|
+
}
|
|
680
|
+
findByCursor(e, t) {
|
|
681
|
+
throw new Error("Method not implemented.");
|
|
682
|
+
}
|
|
683
|
+
count(e, t) {
|
|
684
|
+
const s = pt(this.adapter, this.metadata, e), n = this.create_query_task(s);
|
|
685
|
+
return this.#s("count", n, e, t), n.result$.pipe(f((a) => a.results[0].rows[0][0]));
|
|
686
|
+
}
|
|
687
|
+
create(e) {
|
|
688
|
+
return I(this.#r(e));
|
|
689
|
+
}
|
|
690
|
+
update(e, t) {
|
|
691
|
+
return I(this.#i(e, t));
|
|
692
|
+
}
|
|
693
|
+
remove(e) {
|
|
694
|
+
const t = ae(e), s = () => {
|
|
695
|
+
t.origin = structuredClone({ ...e }), t.modified = !1, t.removed = !0;
|
|
696
|
+
};
|
|
697
|
+
if (t.local === !0) {
|
|
698
|
+
const { sql: a, params: o } = at(this.metadata, e, this.rxdb.context), c = this.client$.pipe(
|
|
699
|
+
Q(1),
|
|
700
|
+
w(() => this.adapter.execute(a, o)),
|
|
701
|
+
f(() => (e.removedAt = /* @__PURE__ */ new Date(), this.rxdb.context?.userId && (e.removedBy = this.rxdb.context.userId), s(), e)),
|
|
702
|
+
A(() => s()),
|
|
703
|
+
A((i) => this.merge_cache("remove", [i]))
|
|
704
|
+
);
|
|
705
|
+
return I(c);
|
|
706
|
+
} else if (t.remote === !0) {
|
|
707
|
+
const { sql: a, params: o } = ot(this.metadata, e, this.rxdb.context), c = this.#n("remove", a, o).pipe(
|
|
708
|
+
A(() => s()),
|
|
709
|
+
f((i) => i[0])
|
|
710
|
+
);
|
|
711
|
+
return I(c);
|
|
712
|
+
}
|
|
713
|
+
const n = q(e);
|
|
714
|
+
throw new $(`Remove Error${n.name}(${e.id}) not saved local.`);
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
* 根据 type 计算主动更新缓存策略
|
|
718
|
+
*/
|
|
719
|
+
merge_cache(e, t) {
|
|
720
|
+
super.merge_cache(e, t);
|
|
721
|
+
for (const s of this.cache_query_task_map.values()) {
|
|
722
|
+
if (!s.resultEntityIds) return;
|
|
723
|
+
const n = this.#e.get(s);
|
|
724
|
+
if (!n) return;
|
|
725
|
+
switch (e) {
|
|
726
|
+
case "create":
|
|
727
|
+
this.#o(n, s, t);
|
|
728
|
+
break;
|
|
729
|
+
case "update":
|
|
730
|
+
this.#c(n, s, t);
|
|
731
|
+
break;
|
|
732
|
+
case "remove":
|
|
733
|
+
this.#l(n, s, t);
|
|
734
|
+
break;
|
|
735
|
+
default:
|
|
736
|
+
throw new Error("unknown type");
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
#t(e, t) {
|
|
741
|
+
const s = this.add_query_cache(t);
|
|
742
|
+
return e.resultEntities = s, e.resultEntityIds = s.map((n) => n.id), s;
|
|
743
|
+
}
|
|
744
|
+
#r(e) {
|
|
745
|
+
const { sql: t, params: s } = it(this.metadata, e, this.rxdb.context);
|
|
746
|
+
return this.#n("create", t, s).pipe(f((n) => n[0]));
|
|
747
|
+
}
|
|
748
|
+
#i(e, t) {
|
|
749
|
+
const { sql: s, params: n } = ct(this.metadata, e, t, this.rxdb.context);
|
|
750
|
+
return this.#n("update", s, n).pipe(f((a) => a[0]));
|
|
751
|
+
}
|
|
752
|
+
#s(e, t, s, n) {
|
|
753
|
+
this.#e.set(t, {
|
|
754
|
+
type: e,
|
|
755
|
+
where: s,
|
|
756
|
+
options: n
|
|
757
|
+
}), t.destroy$.subscribe(() => this.#e.delete(t));
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* 执行修改语句
|
|
761
|
+
*/
|
|
762
|
+
#n(e, t, s) {
|
|
763
|
+
return this.client$.pipe(
|
|
764
|
+
Q(1),
|
|
765
|
+
w(() => this.adapter.execute(t, s)),
|
|
766
|
+
f((n) => this.add_query_cache(n, !0)),
|
|
767
|
+
A((n) => this.merge_cache(e, n))
|
|
768
|
+
);
|
|
769
|
+
}
|
|
770
|
+
#o(e, t, s) {
|
|
771
|
+
const { type: n, where: a } = e;
|
|
772
|
+
switch (n) {
|
|
773
|
+
case "find":
|
|
774
|
+
case "findOne":
|
|
775
|
+
case "findOneOrFail":
|
|
776
|
+
case "findByCursor":
|
|
777
|
+
case "count":
|
|
778
|
+
case "findAll":
|
|
779
|
+
s.some((c) => S(c, a)) && t.refresh();
|
|
780
|
+
break;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
#c(e, t, s) {
|
|
784
|
+
const n = s.map((u) => u.id), a = t.resultEntityIds.some((u) => n.includes(u)), { type: o, where: c, options: i } = e, l = s.some((u) => S(u, c));
|
|
785
|
+
switch (o) {
|
|
786
|
+
case "count":
|
|
787
|
+
l !== a && t.refresh();
|
|
788
|
+
break;
|
|
789
|
+
case "find":
|
|
790
|
+
case "findOne":
|
|
791
|
+
case "findOneOrFail":
|
|
792
|
+
case "findByCursor":
|
|
793
|
+
case "findAll":
|
|
794
|
+
(l !== a || a && l && i?.orderBy?.length && t.resultEntities && s.some((h) => we(t.resultEntities, h, i.orderBy) !== !1)) && t.refresh();
|
|
795
|
+
break;
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
#l(e, t, s) {
|
|
799
|
+
const n = s.map((i) => i.id), a = t.resultEntityIds.some((i) => n.includes(i)), { type: o, where: c } = e;
|
|
800
|
+
switch (o) {
|
|
801
|
+
case "get":
|
|
802
|
+
a && t.refresh();
|
|
803
|
+
break;
|
|
804
|
+
case "findOne":
|
|
805
|
+
case "findOneOrFail":
|
|
806
|
+
case "findByCursor":
|
|
807
|
+
case "count":
|
|
808
|
+
case "findAll":
|
|
809
|
+
(a || s.some((i) => S(i, c))) && t.refresh();
|
|
810
|
+
break;
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
const D = (r, e, t, s) => {
|
|
815
|
+
const n = s?.level || 0, a = E(r), o = n > 0 ? `c.level < ${n}` : "";
|
|
816
|
+
let c = "";
|
|
817
|
+
const i = [o, v(t)].filter(Boolean).join(" AND ");
|
|
818
|
+
return i && (c = `WHERE ${i}`), {
|
|
819
|
+
sql: `WITH RECURSIVE __children AS (
|
|
820
|
+
SELECT *,rowid as ${R}, 0 AS level
|
|
821
|
+
FROM ${a}
|
|
822
|
+
WHERE id = ?
|
|
823
|
+
UNION ALL
|
|
824
|
+
SELECT children.*,children.rowid as ${R}, c.level + 1
|
|
825
|
+
FROM ${a} children
|
|
826
|
+
JOIN __children c ON ${s.descendants ? "children.parentId = c.id" : "children.id = c.parentId"}
|
|
827
|
+
${c}
|
|
828
|
+
)
|
|
829
|
+
SELECT ${s.count ? "count(*)-1 as count" : "*"} FROM __children
|
|
830
|
+
ORDER BY level, id;`,
|
|
831
|
+
params: [e]
|
|
832
|
+
};
|
|
833
|
+
}, Et = (r, e, t, s) => D(r, e, t, { ...s, descendants: !0 }), yt = (r, e, t, s) => D(r, e, t, { ...s, descendants: !1 }), $t = (r, e, t, s) => D(r, e, t, { ...s, descendants: !0, count: !0 }), bt = (r, e, t, s) => D(r, e, t, { ...s, descendants: !1, count: !0 });
|
|
834
|
+
class Nt extends fe {
|
|
835
|
+
#e = /* @__PURE__ */ new Map();
|
|
836
|
+
findDescendants(e, t, s) {
|
|
837
|
+
const n = Et(this.metadata, e.id, t, s), a = this.create_query_task(n);
|
|
838
|
+
return this.#r("findDescendants", a, e.id, t, s), a.result$.pipe(f((o) => this.#t(a, o)));
|
|
839
|
+
}
|
|
840
|
+
countDescendants(e, t, s) {
|
|
841
|
+
const n = $t(this.metadata, e.id, t, s), a = this.create_query_task(n);
|
|
842
|
+
return this.#r("countDescendants", a, e.id, t, s), a.result$.pipe(f((o) => o.results[0].rows[0][0]));
|
|
843
|
+
}
|
|
844
|
+
findAncestors(e, t, s) {
|
|
845
|
+
const n = yt(this.metadata, e.id, t, s), a = this.create_query_task(n);
|
|
846
|
+
return this.#r("findAncestors", a, e.id, t, s), a.result$.pipe(f((o) => this.#t(a, o)));
|
|
847
|
+
}
|
|
848
|
+
countAncestors(e, t, s) {
|
|
849
|
+
const n = bt(this.metadata, e.id, t, s), a = this.create_query_task(n);
|
|
850
|
+
return this.#r("countAncestors", a, e.id, t, s), a.result$.pipe(f((o) => o.results[0].rows[0][0]));
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
853
|
+
* 根据 type 计算主动更新缓存策略
|
|
854
|
+
*/
|
|
855
|
+
merge_cache(e, t) {
|
|
856
|
+
super.merge_cache(e, t);
|
|
857
|
+
for (const s of this.cache_query_task_map.values()) {
|
|
858
|
+
if (!s.resultEntityIds) return;
|
|
859
|
+
const n = this.#e.get(s);
|
|
860
|
+
if (!n) return;
|
|
861
|
+
switch (e) {
|
|
862
|
+
case "create":
|
|
863
|
+
this.#i(n, s, t);
|
|
864
|
+
break;
|
|
865
|
+
case "update":
|
|
866
|
+
this.#s(n, s, t);
|
|
867
|
+
break;
|
|
868
|
+
case "remove":
|
|
869
|
+
this.#n(n, s, t);
|
|
870
|
+
break;
|
|
871
|
+
default:
|
|
872
|
+
throw new Error("unknown type");
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
#t(e, t) {
|
|
877
|
+
const s = this.add_query_cache(t);
|
|
878
|
+
return e.resultEntities = s, e.resultEntityIds = s.map((n) => n.id), s;
|
|
879
|
+
}
|
|
880
|
+
#r(e, t, s, n, a) {
|
|
881
|
+
this.#e.has(t) || (this.#e.set(t, {
|
|
882
|
+
type: e,
|
|
883
|
+
entityId: s,
|
|
884
|
+
where: n,
|
|
885
|
+
options: a
|
|
886
|
+
}), t.destroy$.subscribe(() => this.#e.delete(t)));
|
|
887
|
+
}
|
|
888
|
+
#i(e, t, s) {
|
|
889
|
+
const { type: n, where: a } = e, o = s.map((l) => l.parentId), c = t.resultEntityIds.some((l) => o.includes(l));
|
|
890
|
+
let i = !0;
|
|
891
|
+
switch (a.rules.length > 0 && (i = s.some((l) => S(l, a))), n) {
|
|
892
|
+
case "findDescendants":
|
|
893
|
+
case "countDescendants":
|
|
894
|
+
c && i && t.refresh();
|
|
895
|
+
break;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
#s(e, t, s) {
|
|
899
|
+
const { type: n, where: a } = e, o = s.map((l) => l.parentId), c = t.resultEntityIds.some((l) => o.includes(l));
|
|
900
|
+
let i = !0;
|
|
901
|
+
switch (a.rules.length > 0 && (i = s.some((l) => S(l, a))), n) {
|
|
902
|
+
case "findDescendants":
|
|
903
|
+
case "countDescendants":
|
|
904
|
+
case "findAncestors":
|
|
905
|
+
case "countAncestors":
|
|
906
|
+
c && i && t.refresh();
|
|
907
|
+
break;
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
#n(e, t, s) {
|
|
911
|
+
const { type: n, where: a } = e, o = s.map((l) => l.parentId), c = t.resultEntityIds.some((l) => o.includes(l));
|
|
912
|
+
let i = !0;
|
|
913
|
+
switch (a.rules.length > 0 && (i = s.some((l) => S(l, a))), n) {
|
|
914
|
+
case "findDescendants":
|
|
915
|
+
case "countDescendants":
|
|
916
|
+
case "findAncestors":
|
|
917
|
+
case "countAncestors":
|
|
918
|
+
c && i && t.refresh();
|
|
919
|
+
break;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
class vt extends Ae {
|
|
924
|
+
constructor(e, t) {
|
|
925
|
+
super(e), this.options = t;
|
|
926
|
+
const s = _e(e.options.dbName, "changes");
|
|
927
|
+
this.#r = new BroadcastChannel(s), this.#i = Ce(this.#r, "message"), this.#i.pipe(K(this.#e)).subscribe((n) => {
|
|
928
|
+
const a = n.data, { dbName: o, tableName: c, rowid: i, recordAt: l, timeStamp: u } = a;
|
|
929
|
+
if (!o || !c || !i) return;
|
|
930
|
+
const [h, _] = c.split("$"), p = this.rxdb.schema.getEntityType(_, h);
|
|
931
|
+
if (!p) return;
|
|
932
|
+
const d = {
|
|
933
|
+
rowId: i,
|
|
934
|
+
recordAt: l,
|
|
935
|
+
timeStamp: u
|
|
936
|
+
};
|
|
937
|
+
switch (a.type) {
|
|
938
|
+
case M.SQLITE_DELETE:
|
|
939
|
+
this.rxdb.dispatchEvent(new Se(p, d));
|
|
940
|
+
break;
|
|
941
|
+
case M.SQLITE_INSERT:
|
|
942
|
+
this.rxdb.dispatchEvent(new Re(p, d));
|
|
943
|
+
break;
|
|
944
|
+
case M.SQLITE_UPDATE:
|
|
945
|
+
this.rxdb.dispatchEvent(new qe(p, d));
|
|
946
|
+
break;
|
|
947
|
+
}
|
|
948
|
+
}), this.#_.pipe(K(this.#e)).subscribe();
|
|
949
|
+
}
|
|
950
|
+
#e = new U();
|
|
951
|
+
#t = /* @__PURE__ */ new Map();
|
|
952
|
+
// db change
|
|
953
|
+
#r;
|
|
954
|
+
#i;
|
|
955
|
+
// 查询缓存
|
|
956
|
+
#s = [];
|
|
957
|
+
#n = /* @__PURE__ */ new Map();
|
|
958
|
+
#o = !1;
|
|
959
|
+
#c = new ie(0);
|
|
960
|
+
#l = this.#c.asObservable();
|
|
961
|
+
// sqlite 客户端
|
|
962
|
+
#a = new oe((e) => {
|
|
963
|
+
const { vfs: t, async: s, worker: n, wasmPath: a, workerInstance: o } = this.options, c = {
|
|
964
|
+
vfs: t,
|
|
965
|
+
async: s,
|
|
966
|
+
worker: n,
|
|
967
|
+
wasmPath: a
|
|
968
|
+
};
|
|
969
|
+
he(c);
|
|
970
|
+
let i;
|
|
971
|
+
n && o ? i = ve(o) : i = new rt(), i.init(this.rxdb.options.dbName, c).then(() => e.next(i));
|
|
972
|
+
}).pipe(L(1));
|
|
973
|
+
// 已连接
|
|
974
|
+
#h = Le(() => this.rxdb.connect(this.name)).pipe(L(1));
|
|
975
|
+
// 队列
|
|
976
|
+
#_ = this.#l.pipe(
|
|
977
|
+
Z(() => this.#o === !1),
|
|
978
|
+
f(() => this.#s.shift()),
|
|
979
|
+
Z(Boolean),
|
|
980
|
+
A(() => this.#o = !0),
|
|
981
|
+
w((e) => {
|
|
982
|
+
const t = this.#n.get(e.key);
|
|
983
|
+
return this.#h.pipe(
|
|
984
|
+
w(() => this.#a),
|
|
985
|
+
w((s) => s.execute(e.sql, e.bindings)),
|
|
986
|
+
A((s) => t.next(s)),
|
|
987
|
+
A(() => setTimeout(() => this.#n.delete(e.key), 0)),
|
|
988
|
+
Me((s) => {
|
|
989
|
+
const n = s.message || "执行错误";
|
|
990
|
+
return t.error(
|
|
991
|
+
new $("${msg} ${sql} ${bindings}", {
|
|
992
|
+
msg: n,
|
|
993
|
+
sql: e.sql,
|
|
994
|
+
bindings: JSON.stringify(e.bindings)
|
|
995
|
+
})
|
|
996
|
+
), je({ error: s });
|
|
997
|
+
})
|
|
998
|
+
);
|
|
999
|
+
}),
|
|
1000
|
+
A(() => {
|
|
1001
|
+
this.#o = !1, this.#u();
|
|
1002
|
+
})
|
|
1003
|
+
);
|
|
1004
|
+
name = de;
|
|
1005
|
+
/**
|
|
1006
|
+
* 数据库版本
|
|
1007
|
+
*/
|
|
1008
|
+
version$ = this.#a.pipe(
|
|
1009
|
+
w((e) => ee(e.version())),
|
|
1010
|
+
L(1)
|
|
1011
|
+
);
|
|
1012
|
+
/**
|
|
1013
|
+
* 连接 wab sqlite
|
|
1014
|
+
*/
|
|
1015
|
+
connect() {
|
|
1016
|
+
return this.#a.pipe(f(() => this));
|
|
1017
|
+
}
|
|
1018
|
+
/**
|
|
1019
|
+
* 断开连接
|
|
1020
|
+
*/
|
|
1021
|
+
async disconnect() {
|
|
1022
|
+
this.#e.next();
|
|
1023
|
+
}
|
|
1024
|
+
/**
|
|
1025
|
+
* 获取版本
|
|
1026
|
+
*/
|
|
1027
|
+
version() {
|
|
1028
|
+
return I(this.version$);
|
|
1029
|
+
}
|
|
1030
|
+
/**
|
|
1031
|
+
* 获取实体仓库
|
|
1032
|
+
* @param entity 实体类
|
|
1033
|
+
*/
|
|
1034
|
+
getRepository(e) {
|
|
1035
|
+
if (!this.#t.has(e)) {
|
|
1036
|
+
const t = q(e);
|
|
1037
|
+
let s;
|
|
1038
|
+
switch (t.repository) {
|
|
1039
|
+
case "Repository":
|
|
1040
|
+
s = new fe(this, this.#a, e);
|
|
1041
|
+
break;
|
|
1042
|
+
case "TreeRepository":
|
|
1043
|
+
s = new Nt(this, this.#a, e);
|
|
1044
|
+
break;
|
|
1045
|
+
default:
|
|
1046
|
+
throw new $("Unsupported repository type: " + t.repository);
|
|
1047
|
+
}
|
|
1048
|
+
return this.#t.set(e, s), s;
|
|
1049
|
+
}
|
|
1050
|
+
return this.#t.get(e);
|
|
1051
|
+
}
|
|
1052
|
+
/**
|
|
1053
|
+
* 判断表是否存在
|
|
1054
|
+
* @param EntityType
|
|
1055
|
+
*/
|
|
1056
|
+
isTableExisted(e) {
|
|
1057
|
+
return I(this.isTableExisted$(e));
|
|
1058
|
+
}
|
|
1059
|
+
/**
|
|
1060
|
+
* 判断表是否存在
|
|
1061
|
+
* @param EntityType
|
|
1062
|
+
*/
|
|
1063
|
+
isTableExisted$(e) {
|
|
1064
|
+
const t = q(e), s = E(t);
|
|
1065
|
+
return this.#d(Ve(s)).pipe(f((n) => Ue(n) === !1));
|
|
1066
|
+
}
|
|
1067
|
+
/**
|
|
1068
|
+
* 创建 table
|
|
1069
|
+
* @param EntityType
|
|
1070
|
+
*/
|
|
1071
|
+
createTable(e) {
|
|
1072
|
+
const t = q(e);
|
|
1073
|
+
let s = ze(this, t);
|
|
1074
|
+
if (t.log !== !1) {
|
|
1075
|
+
const n = Xe(this, t);
|
|
1076
|
+
s += `
|
|
1077
|
+
` + n;
|
|
1078
|
+
}
|
|
1079
|
+
return I(this.#d(s).pipe(f(() => !0)));
|
|
1080
|
+
}
|
|
1081
|
+
/**
|
|
1082
|
+
* 执行 sql
|
|
1083
|
+
* @param sql
|
|
1084
|
+
* @param bindings
|
|
1085
|
+
*/
|
|
1086
|
+
execute(e, t) {
|
|
1087
|
+
const s = e + t?.join(",");
|
|
1088
|
+
if (!this.#n.has(s)) {
|
|
1089
|
+
const n = new U();
|
|
1090
|
+
this.#n.set(s, n), this.#s.push({ key: s, sql: e, bindings: t }), this.#u();
|
|
1091
|
+
}
|
|
1092
|
+
return this.#n.get(s).asObservable();
|
|
1093
|
+
}
|
|
1094
|
+
/**
|
|
1095
|
+
* 处理事务
|
|
1096
|
+
* @param callback
|
|
1097
|
+
*/
|
|
1098
|
+
transaction(e) {
|
|
1099
|
+
return I(this.transaction$(e));
|
|
1100
|
+
}
|
|
1101
|
+
transaction$(e) {
|
|
1102
|
+
return this.#a.pipe(
|
|
1103
|
+
w(async (t) => {
|
|
1104
|
+
this.rxdb.dispatchEvent(Tt), await t.execute("BEGIN; PRAGMA defer_foreign_keys = ON;");
|
|
1105
|
+
try {
|
|
1106
|
+
const s = await e();
|
|
1107
|
+
return await t.execute("COMMIT"), this.rxdb.dispatchEvent(wt), s;
|
|
1108
|
+
} catch (s) {
|
|
1109
|
+
await t.execute("ROLLBACK"), this.rxdb.dispatchEvent(gt);
|
|
1110
|
+
const n = s?.message || "Transaction Error";
|
|
1111
|
+
throw new $(n);
|
|
1112
|
+
}
|
|
1113
|
+
})
|
|
1114
|
+
);
|
|
1115
|
+
}
|
|
1116
|
+
#u() {
|
|
1117
|
+
this.#c.next(this.#c.value + 1);
|
|
1118
|
+
}
|
|
1119
|
+
/**
|
|
1120
|
+
* 内部执行的 sql
|
|
1121
|
+
* @param sql
|
|
1122
|
+
* @param bindings
|
|
1123
|
+
* @returns
|
|
1124
|
+
*/
|
|
1125
|
+
#d(e, t) {
|
|
1126
|
+
return this.#a.pipe(w((s) => ee(s.execute(e, t))));
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
const Tt = new ge(), wt = new Oe(), gt = new Ie();
|
|
1130
|
+
export {
|
|
1131
|
+
vt as RxDBAdapterSqlite,
|
|
1132
|
+
$ as RxdbAdapterSqliteError,
|
|
1133
|
+
rt as SqliteClient,
|
|
1134
|
+
tt as WA_SQLITE_VFS_LIST,
|
|
1135
|
+
st as sqliteLoad
|
|
1136
|
+
};
|