@aiao/rxdb-adapter-sqlite 0.0.16 → 0.0.17
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 +36 -0
- package/dist/RxDBAdapterSqlite.d.ts +4 -2
- package/dist/RxDBAdapterSqlite.d.ts.map +1 -1
- package/dist/index.js +460 -423
- package/dist/repository/SqliteRepositoryBase.d.ts.map +1 -1
- package/dist/sqlite.utils.d.ts.map +1 -1
- package/dist/table/create_table_sql.d.ts.map +1 -1
- package/dist/version/execute_switch_actions.d.ts +3 -1
- package/dist/version/execute_switch_actions.d.ts.map +1 -1
- package/dist/version/switch-result.utils.d.ts.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { getEntityMetadata as
|
|
2
|
-
import { isString as
|
|
3
|
-
import { wrap as Ee, proxy as
|
|
4
|
-
import { defer as ne, of as se, from as re, map as
|
|
5
|
-
import { Factory as
|
|
6
|
-
const
|
|
1
|
+
import { getEntityMetadata as w, PropertyType as E, RelationKind as I, isRuleGroup as fe, getEntityStatus as F, RepositoryBase as Ue, RxDBChange as B, EntityLocalCreatedEvent as K, EntityLocalRemovedEvent as ce, EntityLocalUpdatedEvent as Y, parseRxDBChangeKey as ee, RxDBBranch as H, getEntityType as te, TransactionBeginEvent as We, TransactionRollbackEvent as Ke, TransactionCommitEvent as He, RxDBAdapterLocalBase as Ve, getEntityMutations as he, uuid as Qe } from "@aiao/rxdb";
|
|
2
|
+
import { isString as Ge, isNil as Je, traverseObjectKeys as Xe, EventDispatcher as Ye, AsyncQueueExecutor as Ae, get as ze, isFunction as me } from "@aiao/utils";
|
|
3
|
+
import { wrap as Ee, proxy as Ze } from "comlink";
|
|
4
|
+
import { defer as ne, of as se, from as re, map as et } from "rxjs";
|
|
5
|
+
import { Factory as tt, SQLITE_ROW as nt, SQLITE_UTF8 as Se, SQLITE_DETERMINISTIC as be } from "wa-sqlite";
|
|
6
|
+
const st = (e) => ({
|
|
7
7
|
sql: "SELECT * FROM sqlite_master WHERE type='table' AND name=? LIMIT 1;",
|
|
8
8
|
params: [e]
|
|
9
|
-
}),
|
|
9
|
+
}), rt = (e) => !!e.sql && (e.results.length === 0 || e.results[0].rows.length === 0), x = "__rowid", ot = (e) => {
|
|
10
10
|
switch (e.type) {
|
|
11
11
|
case E.uuid:
|
|
12
12
|
case E.string:
|
|
@@ -31,7 +31,7 @@ const nt = (e) => ({
|
|
|
31
31
|
case E.boolean:
|
|
32
32
|
return e ? 1 : 0;
|
|
33
33
|
case E.date:
|
|
34
|
-
return e instanceof Date ? e.toISOString() : e;
|
|
34
|
+
return e instanceof Date ? isNaN(e.getTime()) ? null : e.toISOString() : e === "" ? null : e;
|
|
35
35
|
case E.keyValue: {
|
|
36
36
|
if (!e) return null;
|
|
37
37
|
if (n.properties && typeof e == "object" && !Array.isArray(e)) {
|
|
@@ -55,8 +55,15 @@ const nt = (e) => ({
|
|
|
55
55
|
switch (n.type) {
|
|
56
56
|
case E.boolean:
|
|
57
57
|
return !!e;
|
|
58
|
-
case E.date:
|
|
59
|
-
|
|
58
|
+
case E.date: {
|
|
59
|
+
if (e === "") return null;
|
|
60
|
+
if (e instanceof Date) return isNaN(e.getTime()) ? null : e;
|
|
61
|
+
if (typeof e == "string") {
|
|
62
|
+
const t = new Date(e);
|
|
63
|
+
return isNaN(t.getTime()) ? null : t;
|
|
64
|
+
}
|
|
65
|
+
return e;
|
|
66
|
+
}
|
|
60
67
|
case E.keyValue: {
|
|
61
68
|
if (!e) return null;
|
|
62
69
|
const t = typeof e == "object" ? e : JSON.parse(e);
|
|
@@ -72,10 +79,10 @@ const nt = (e) => ({
|
|
|
72
79
|
default:
|
|
73
80
|
return e;
|
|
74
81
|
}
|
|
75
|
-
}, P = (e, n) => `${n}$${e}`,
|
|
82
|
+
}, P = (e, n) => `${n}$${e}`, at = (e) => {
|
|
76
83
|
const n = e.indexOf("$");
|
|
77
84
|
return n === -1 ? ["", e] : [e.substring(0, n), e.substring(n + 1)];
|
|
78
|
-
},
|
|
85
|
+
}, S = (e) => P(e.tableName, e.namespace), ye = (e) => S(w(e)), _e = (e, n) => `idx_${e.name}_${n.name}`;
|
|
79
86
|
class R extends Error {
|
|
80
87
|
constructor(n, t) {
|
|
81
88
|
super(n, t), this.name = "RxDBAdapterSqliteError", Object.setPrototypeOf(this, R.prototype);
|
|
@@ -97,19 +104,19 @@ const le = (e, n) => {
|
|
|
97
104
|
t[c] = n[c];
|
|
98
105
|
continue;
|
|
99
106
|
}
|
|
100
|
-
const
|
|
101
|
-
if (
|
|
102
|
-
t[
|
|
107
|
+
const d = e.propertyMap.get(c);
|
|
108
|
+
if (d) {
|
|
109
|
+
t[d.columnName] = V(n[c], d);
|
|
103
110
|
continue;
|
|
104
111
|
}
|
|
105
|
-
const
|
|
106
|
-
if (
|
|
107
|
-
const f = e.propertyMap.get(
|
|
112
|
+
const u = e.columnNameToPropertyName?.get(c);
|
|
113
|
+
if (u) {
|
|
114
|
+
const f = e.propertyMap.get(u);
|
|
108
115
|
f && (t[c] = V(n[c], f));
|
|
109
116
|
}
|
|
110
117
|
}
|
|
111
118
|
return t;
|
|
112
|
-
},
|
|
119
|
+
}, Ie = (e, n) => {
|
|
113
120
|
const t = {};
|
|
114
121
|
for (const [o, i] of e.propertyMap)
|
|
115
122
|
o in n && (t[i.columnName] = n[o]);
|
|
@@ -119,7 +126,7 @@ const le = (e, n) => {
|
|
|
119
126
|
i in n && (t[r[o]] = n[i]);
|
|
120
127
|
}
|
|
121
128
|
return t;
|
|
122
|
-
},
|
|
129
|
+
}, it = (e, n) => {
|
|
123
130
|
const t = {};
|
|
124
131
|
for (const [o, i] of e.propertyMap)
|
|
125
132
|
o in n && i.readonly !== !0 && (t[i.columnName] = n[o]);
|
|
@@ -132,9 +139,9 @@ const le = (e, n) => {
|
|
|
132
139
|
}
|
|
133
140
|
}
|
|
134
141
|
return t;
|
|
135
|
-
},
|
|
142
|
+
}, T = (e) => Ge(e) ? `'${e.replaceAll("\0", "").replaceAll("'", "''")}'` : Je(e) ? "NULL" : e, k = (e, n = []) => {
|
|
136
143
|
let t = 0;
|
|
137
|
-
return e.replace(/\?/g, () => String(
|
|
144
|
+
return e.replace(/\?/g, () => String(T(n[t++])));
|
|
138
145
|
}, oe = (e, n, t) => {
|
|
139
146
|
const s = {};
|
|
140
147
|
return t.forEach((r, o) => {
|
|
@@ -145,18 +152,18 @@ const le = (e, n) => {
|
|
|
145
152
|
s[c[l]] = r;
|
|
146
153
|
return;
|
|
147
154
|
}
|
|
148
|
-
const
|
|
155
|
+
const d = e.columnNameToPropertyName, p = d?.get(i);
|
|
149
156
|
if (p) {
|
|
150
157
|
const f = e.propertyMap.get(p);
|
|
151
158
|
f ? s[p] = v(r, f) : console.warn(`Property ${p} not found in metadata ${e.name}`);
|
|
152
159
|
return;
|
|
153
160
|
}
|
|
154
|
-
const
|
|
155
|
-
if (
|
|
156
|
-
s[i] = v(r,
|
|
161
|
+
const u = e.computedPropertyMap?.get(i);
|
|
162
|
+
if (u) {
|
|
163
|
+
s[i] = v(r, u);
|
|
157
164
|
return;
|
|
158
165
|
}
|
|
159
|
-
if (!
|
|
166
|
+
if (!d) {
|
|
160
167
|
const f = e.propertyMap.get(i);
|
|
161
168
|
if (f) {
|
|
162
169
|
s[i] = v(r, f);
|
|
@@ -165,7 +172,7 @@ const le = (e, n) => {
|
|
|
165
172
|
}
|
|
166
173
|
console.warn(`Column ${i}=${r} not found in metadata ${e.name}`);
|
|
167
174
|
}), s;
|
|
168
|
-
},
|
|
175
|
+
}, A = "_", ct = {
|
|
169
176
|
"=": "=",
|
|
170
177
|
"!=": "!=",
|
|
171
178
|
">": ">",
|
|
@@ -206,11 +213,11 @@ const le = (e, n) => {
|
|
|
206
213
|
return r[o];
|
|
207
214
|
}
|
|
208
215
|
return e;
|
|
209
|
-
},
|
|
216
|
+
}, Ne = (e, n, t) => {
|
|
210
217
|
if (n) return n;
|
|
211
218
|
if (!e.includes(".")) {
|
|
212
219
|
const c = ae(e, t);
|
|
213
|
-
return `${
|
|
220
|
+
return `${A}."${c}"`;
|
|
214
221
|
}
|
|
215
222
|
const s = e.lastIndexOf("."), r = e.slice(0, s), o = e.slice(s + 1), i = ae(o, t);
|
|
216
223
|
return `"${r}"."${i}"`;
|
|
@@ -219,71 +226,71 @@ const le = (e, n) => {
|
|
|
219
226
|
switch (n) {
|
|
220
227
|
case "in":
|
|
221
228
|
case "notIn":
|
|
222
|
-
return
|
|
229
|
+
return ut(t);
|
|
223
230
|
case "between":
|
|
224
231
|
case "notBetween":
|
|
225
|
-
return
|
|
232
|
+
return dt(t);
|
|
226
233
|
case "contains":
|
|
227
234
|
case "notContains":
|
|
228
|
-
return
|
|
235
|
+
return T(`%${t}%`);
|
|
229
236
|
case "startsWith":
|
|
230
237
|
case "notStartsWith":
|
|
231
|
-
return
|
|
238
|
+
return T(`${t}%`);
|
|
232
239
|
case "endsWith":
|
|
233
240
|
case "notEndsWith":
|
|
234
|
-
return
|
|
241
|
+
return T(`%${t}`);
|
|
235
242
|
default:
|
|
236
|
-
return
|
|
243
|
+
return pt(t);
|
|
237
244
|
}
|
|
238
|
-
},
|
|
245
|
+
}, lt = (e) => ct[e] || e, Q = (e, n, t) => e.length === 1 ? t.name : `${n}_${t.name}`, ut = (e) => !Array.isArray(e) || !e.length ? "(NULL)" : `(${e.map(T).join(", ")})`, dt = (e) => {
|
|
239
246
|
if (!Array.isArray(e) || e.length < 2) return "";
|
|
240
247
|
const [n, t] = e;
|
|
241
248
|
if (n == null || t == null) return "";
|
|
242
249
|
const s = n instanceof Date ? n.toISOString() : n, r = t instanceof Date ? t.toISOString() : t;
|
|
243
|
-
return `${
|
|
244
|
-
},
|
|
250
|
+
return `${T(s)} and ${T(r)}`;
|
|
251
|
+
}, pt = (e) => typeof e == "boolean" ? e ? "1" : "0" : typeof e == "string" ? T(e) : typeof e == "number" ? e.toString() : e instanceof Date ? T(e.toISOString()) : T(e), ft = (e, n, t) => {
|
|
245
252
|
const s = e.propertyMap.get(n);
|
|
246
253
|
if (!s || s.type !== E.keyValue || typeof t.value != "object" || Array.isArray(t.value)) return null;
|
|
247
254
|
const r = Object.entries(t.value).filter(([, a]) => a != null);
|
|
248
255
|
if (!r.length) return "";
|
|
249
256
|
const o = t.operator === "notContains" ? "NOT LIKE" : "LIKE", i = t.operator === "contains" ? " OR " : " AND ";
|
|
250
257
|
return `(${r.map(([a, l]) => {
|
|
251
|
-
const
|
|
252
|
-
return `json_extract(${
|
|
258
|
+
const d = l instanceof Date ? l.toISOString() : typeof l == "string" ? l : String(l), p = a.replace(/'/g, "''");
|
|
259
|
+
return `json_extract(${A}."${s.columnName}", '$.${p}') ${o} ${T(`%${d}%`)}`;
|
|
253
260
|
}).join(i)})`;
|
|
254
|
-
},
|
|
261
|
+
}, ht = (e, n, t) => {
|
|
255
262
|
const s = e.propertyMap.get(n);
|
|
256
263
|
if (!s || s.type !== E.stringArray && s.type !== E.numberArray) return null;
|
|
257
|
-
const r = Re(t), o = `EXISTS (SELECT 1 FROM json_each(${
|
|
264
|
+
const r = Re(t), o = `EXISTS (SELECT 1 FROM json_each(${A}."${s.columnName}") WHERE json_each.value IN ${r})`;
|
|
258
265
|
return t.operator === "notIn" ? `NOT ${o}` : o;
|
|
259
|
-
},
|
|
266
|
+
}, mt = (e, n, t, s, r) => {
|
|
260
267
|
if (t && s && r && (e.operator === "exists" || e.operator === "notExists")) {
|
|
261
|
-
const
|
|
262
|
-
const
|
|
263
|
-
return h.propertyMap.forEach((m,
|
|
264
|
-
|
|
265
|
-
}), r(f,
|
|
268
|
+
const u = gt(e, t, s, (f, h) => {
|
|
269
|
+
const y = /* @__PURE__ */ new Map();
|
|
270
|
+
return h.propertyMap.forEach((m, _) => {
|
|
271
|
+
y.set(_, `"child"."${m.columnName}"`);
|
|
272
|
+
}), r(f, y, h, s);
|
|
266
273
|
});
|
|
267
|
-
if (
|
|
274
|
+
if (u !== null) return u;
|
|
268
275
|
}
|
|
269
276
|
if (e.operator === "null" || e.operator === "notNull") {
|
|
270
|
-
const p = String(e.field),
|
|
277
|
+
const p = String(e.field), u = n.get(p), f = Ne(p, u, t);
|
|
271
278
|
return e.operator === "null" ? `${f} IS NULL` : `${f} IS NOT NULL`;
|
|
272
279
|
}
|
|
273
|
-
const o = e.value === null, i = o ? "" :
|
|
280
|
+
const o = e.value === null, i = o ? "" : lt(e.operator).toLowerCase(), c = o ? e.operator === "=" ? "IS NULL" : e.operator === "!=" ? "IS NOT NULL" : "" : Re(e);
|
|
274
281
|
if (!c && ["in", "notIn", "between", "notBetween"].includes(e.operator)) return "";
|
|
275
282
|
const a = String(e.field), l = n.get(a);
|
|
276
283
|
if (t && !a.includes(".") && (e.operator === "contains" || e.operator === "notContains")) {
|
|
277
|
-
const p =
|
|
284
|
+
const p = ft(t, a, e);
|
|
278
285
|
if (p !== null) return p;
|
|
279
286
|
}
|
|
280
287
|
if (t && !a.includes(".") && (e.operator === "in" || e.operator === "notIn")) {
|
|
281
|
-
const p =
|
|
288
|
+
const p = ht(t, a, e);
|
|
282
289
|
if (p !== null) return p;
|
|
283
290
|
}
|
|
284
|
-
return `${
|
|
285
|
-
}, ue = (e) => e ===
|
|
286
|
-
if (e.kind !==
|
|
291
|
+
return `${Ne(a, l, t)} ${i} ${c}`.replace(/\s+/g, " ").trim();
|
|
292
|
+
}, ue = (e) => e === A ? e : `"${e}"`, Et = (e, n, t) => {
|
|
293
|
+
if (e.kind !== I.ONE_TO_MANY)
|
|
287
294
|
throw new Error("_build_one_to_many_exists requires ONE_TO_MANY relation");
|
|
288
295
|
const s = P(n.tableName, n.namespace), r = "child", o = n.relations.find((a) => a.name === e.mappedProperty);
|
|
289
296
|
if (!o)
|
|
@@ -292,52 +299,52 @@ const le = (e, n) => {
|
|
|
292
299
|
);
|
|
293
300
|
const i = o.columnName;
|
|
294
301
|
let c = `EXISTS (SELECT 1 FROM "${s}" "${r}"`;
|
|
295
|
-
return c += ` WHERE "${r}"."${i}" = ${
|
|
296
|
-
},
|
|
302
|
+
return c += ` WHERE "${r}"."${i}" = ${A}."id"`, t && (c += ` AND ${t}`), c += ")", c;
|
|
303
|
+
}, yt = (e, n, t) => {
|
|
297
304
|
const s = e.columnName;
|
|
298
305
|
if (!t)
|
|
299
|
-
return `${
|
|
306
|
+
return `${A}."${s}" IS NOT NULL`;
|
|
300
307
|
const r = P(n.tableName, n.namespace), o = "child";
|
|
301
308
|
let i = `EXISTS (SELECT 1 FROM "${r}" "${o}"`;
|
|
302
|
-
return i += ` WHERE ${
|
|
309
|
+
return i += ` WHERE ${A}."${s}" = "${o}"."id"`, i += ` AND ${t}`, i += ")", i;
|
|
303
310
|
}, _t = (e, n, t, s, r) => {
|
|
304
311
|
const o = s.rxdb.schemaManager.findMappedRelation(e, n);
|
|
305
312
|
if (o?.relation) {
|
|
306
313
|
const i = P(t.tableName, t.namespace), c = "child", a = o.relation.columnName;
|
|
307
314
|
let l = `EXISTS (SELECT 1 FROM "${i}" "${c}"`;
|
|
308
|
-
return l += ` WHERE "${c}"."${a}" = ${
|
|
315
|
+
return l += ` WHERE "${c}"."${a}" = ${A}."id"`, r && (l += ` AND ${r}`), l += ")", l;
|
|
309
316
|
} else {
|
|
310
317
|
const i = n.columnName;
|
|
311
318
|
if (!r)
|
|
312
|
-
return `${
|
|
319
|
+
return `${A}."${i}" IS NOT NULL`;
|
|
313
320
|
const c = P(t.tableName, t.namespace), a = "child";
|
|
314
321
|
let l = `EXISTS (SELECT 1 FROM "${c}" "${a}"`;
|
|
315
|
-
return l += ` WHERE ${
|
|
322
|
+
return l += ` WHERE ${A}."${i}" = "${a}"."id"`, l += ` AND ${r}`, l += ")", l;
|
|
316
323
|
}
|
|
317
|
-
},
|
|
324
|
+
}, Nt = (e, n, t, s) => {
|
|
318
325
|
const r = P(t.tableName, t.namespace), o = "child", i = n.junctionEntityType;
|
|
319
326
|
if (!i)
|
|
320
327
|
throw new Error(`MANY_TO_MANY relation "${n.name}" missing junctionEntityType`);
|
|
321
|
-
const c =
|
|
328
|
+
const c = w(i);
|
|
322
329
|
if (!c)
|
|
323
330
|
throw new Error("Cannot find metadata for junction entity");
|
|
324
|
-
const a = P(c.tableName, c.namespace), l = "junction",
|
|
325
|
-
(
|
|
331
|
+
const a = P(c.tableName, c.namespace), l = "junction", d = c.relations.find(
|
|
332
|
+
(y) => y.mappedEntity === e.name && y.mappedNamespace === e.namespace
|
|
326
333
|
);
|
|
327
|
-
if (!
|
|
334
|
+
if (!d)
|
|
328
335
|
throw new Error(
|
|
329
336
|
`Cannot find relation in junction entity ${c.name} pointing to ${e.name}`
|
|
330
337
|
);
|
|
331
338
|
const p = c.relations.find(
|
|
332
|
-
(
|
|
339
|
+
(y) => y.mappedEntity === t.name && y.mappedNamespace === t.namespace
|
|
333
340
|
);
|
|
334
341
|
if (!p)
|
|
335
342
|
throw new Error(
|
|
336
343
|
`Cannot find relation in junction entity ${c.name} pointing to ${t.name}`
|
|
337
344
|
);
|
|
338
|
-
const
|
|
345
|
+
const u = d.columnName, f = p.columnName;
|
|
339
346
|
let h = `EXISTS (SELECT 1 FROM "${r}" "${o}"`;
|
|
340
|
-
return h += ` INNER JOIN "${a}" "${l}"`, h += ` ON "${l}"."${f}" = "${o}"."id"`, h += ` WHERE "${l}"."${
|
|
347
|
+
return h += ` INNER JOIN "${a}" "${l}"`, h += ` ON "${l}"."${f}" = "${o}"."id"`, h += ` WHERE "${l}"."${u}" = ${A}."id"`, s && (h += ` AND ${s}`), h += ")", h;
|
|
341
348
|
}, gt = (e, n, t, s) => {
|
|
342
349
|
if (e.operator !== "exists" && e.operator !== "notExists")
|
|
343
350
|
return null;
|
|
@@ -353,26 +360,26 @@ const le = (e, n) => {
|
|
|
353
360
|
e.where && s && (i = s(e.where, o));
|
|
354
361
|
let c;
|
|
355
362
|
switch (r.kind) {
|
|
356
|
-
case
|
|
357
|
-
c = mt(r, o, i);
|
|
358
|
-
break;
|
|
359
|
-
case b.MANY_TO_ONE:
|
|
363
|
+
case I.ONE_TO_MANY:
|
|
360
364
|
c = Et(r, o, i);
|
|
361
365
|
break;
|
|
362
|
-
case
|
|
366
|
+
case I.MANY_TO_ONE:
|
|
367
|
+
c = yt(r, o, i);
|
|
368
|
+
break;
|
|
369
|
+
case I.ONE_TO_ONE:
|
|
363
370
|
c = _t(n, r, o, t, i);
|
|
364
371
|
break;
|
|
365
|
-
case
|
|
366
|
-
c =
|
|
372
|
+
case I.MANY_TO_MANY:
|
|
373
|
+
c = Nt(n, r, o, i);
|
|
367
374
|
break;
|
|
368
375
|
}
|
|
369
376
|
return e.operator === "notExists" ? `NOT ${c}` : c;
|
|
370
377
|
}, G = (e, n) => {
|
|
371
378
|
if (e.relationAliasMap.has(n))
|
|
372
379
|
return e.relationAliasMap.get(n);
|
|
373
|
-
const t = n.includes("_") ? n : n.split("_")[0], s =
|
|
380
|
+
const t = n.includes("_") ? n : n.split("_")[0], s = Ce(e, t);
|
|
374
381
|
return e.relationAliasMap.set(n, s), s;
|
|
375
|
-
},
|
|
382
|
+
}, $t = (e, n, t, s) => {
|
|
376
383
|
const r = t.slice(0, s);
|
|
377
384
|
let o = n;
|
|
378
385
|
const i = [];
|
|
@@ -383,142 +390,142 @@ const le = (e, n) => {
|
|
|
383
390
|
i.push({ metadata: o, relation: a }), o = e.rxdb.schemaManager.getEntityMetadata(a.mappedEntity, a.mappedNamespace);
|
|
384
391
|
}
|
|
385
392
|
return { metaWalker: o, relPairs: i };
|
|
386
|
-
},
|
|
393
|
+
}, Tt = (e, n, t, s, r) => {
|
|
387
394
|
for (let o = r.length - 1; o > 0; o--) {
|
|
388
395
|
const i = r.slice(o);
|
|
389
396
|
if (i.length === 0) continue;
|
|
390
|
-
const { metaWalker: c, relPairs: a } =
|
|
397
|
+
const { metaWalker: c, relPairs: a } = $t(e, t, r, o);
|
|
391
398
|
if (!c || a.length === 0) continue;
|
|
392
|
-
const l = i[0],
|
|
399
|
+
const l = i[0], d = i.slice(1).join("."), p = c.propertyMap.get(l);
|
|
393
400
|
if (p && p.type === E.keyValue) {
|
|
394
|
-
const
|
|
395
|
-
|
|
396
|
-
const f = a[a.length - 1].relation, h = Q(a,
|
|
397
|
-
return n.fieldAliasMap.set(s, `json_extract("${
|
|
401
|
+
const u = r.slice(0, o).join(".");
|
|
402
|
+
Oe(e, n, a, u);
|
|
403
|
+
const f = a[a.length - 1].relation, h = Q(a, u, f), y = G(n, h), m = d ? `$.${d}` : "$";
|
|
404
|
+
return n.fieldAliasMap.set(s, `json_extract("${y}"."${p.columnName}", '${m}')`), !0;
|
|
398
405
|
}
|
|
399
406
|
}
|
|
400
407
|
return !1;
|
|
401
|
-
},
|
|
408
|
+
}, Oe = (e, n, t, s) => {
|
|
402
409
|
t.forEach(({ metadata: r, relation: o }, i) => {
|
|
403
410
|
let c = e.rxdb.schemaManager.findMappedRelation(r, o);
|
|
404
411
|
if (!c)
|
|
405
|
-
if (o.kind ===
|
|
412
|
+
if (o.kind === I.ONE_TO_MANY || o.kind === I.MANY_TO_MANY) {
|
|
406
413
|
const p = r.relationMap.get(o.mappedProperty);
|
|
407
414
|
p && (c = { metadata: r, relation: p });
|
|
408
|
-
} else (o.kind ===
|
|
415
|
+
} else (o.kind === I.MANY_TO_ONE || o.kind === I.ONE_TO_ONE) && (c = { metadata: r, relation: o });
|
|
409
416
|
if (!c) throw new R("mappedRelation not found");
|
|
410
417
|
const a = Q(t, s, o), l = G(n, a);
|
|
411
|
-
let
|
|
418
|
+
let d = A;
|
|
412
419
|
if (i > 0) {
|
|
413
|
-
const p = t[i - 1],
|
|
414
|
-
|
|
420
|
+
const p = t[i - 1], u = Q(t, s, p.relation);
|
|
421
|
+
d = G(n, u);
|
|
415
422
|
}
|
|
416
423
|
switch (o.kind) {
|
|
417
|
-
case
|
|
418
|
-
|
|
424
|
+
case I.ONE_TO_MANY:
|
|
425
|
+
wt(
|
|
419
426
|
n,
|
|
420
427
|
c,
|
|
421
428
|
l,
|
|
422
|
-
|
|
429
|
+
d
|
|
423
430
|
);
|
|
424
431
|
break;
|
|
425
|
-
case
|
|
426
|
-
case
|
|
427
|
-
|
|
432
|
+
case I.ONE_TO_ONE:
|
|
433
|
+
case I.MANY_TO_ONE:
|
|
434
|
+
At(
|
|
428
435
|
n,
|
|
429
436
|
c,
|
|
430
437
|
l,
|
|
431
|
-
|
|
438
|
+
d,
|
|
432
439
|
o
|
|
433
440
|
);
|
|
434
441
|
break;
|
|
435
|
-
case
|
|
436
|
-
|
|
442
|
+
case I.MANY_TO_MANY:
|
|
443
|
+
St(
|
|
437
444
|
e,
|
|
438
445
|
n,
|
|
439
446
|
c,
|
|
440
447
|
l,
|
|
441
|
-
|
|
448
|
+
d,
|
|
442
449
|
o
|
|
443
450
|
);
|
|
444
451
|
break;
|
|
445
452
|
}
|
|
446
453
|
});
|
|
447
|
-
},
|
|
454
|
+
}, wt = (e, n, t, s) => {
|
|
448
455
|
const r = J(e, n.metadata), o = `"${t}".${n.relation.columnName} = ${ue(s)}.id`;
|
|
449
456
|
r.find((c) => c.joinTableName === t && c.on === o) || r.push({
|
|
450
457
|
joinTableName: t,
|
|
451
458
|
on: o
|
|
452
459
|
});
|
|
453
|
-
},
|
|
460
|
+
}, At = (e, n, t, s, r) => {
|
|
454
461
|
const o = J(e, n.metadata), i = `"${t}".id = ${ue(s)}.${r.columnName}`;
|
|
455
462
|
o.find((a) => a.joinTableName === t && a.on === i) || o.push({
|
|
456
463
|
joinTableName: t,
|
|
457
464
|
on: i
|
|
458
465
|
});
|
|
459
|
-
},
|
|
460
|
-
const i =
|
|
466
|
+
}, St = (e, n, t, s, r, o) => {
|
|
467
|
+
const i = w(o.junctionEntityType), c = J(n, i), a = Ce(n, `${o.name}_m_n`), l = `"${a}".${t.relation.columnName} = ${ue(r)}.id`;
|
|
461
468
|
c.find(
|
|
462
469
|
(h) => h.joinTableName === a && h.on === l
|
|
463
470
|
) || c.push({
|
|
464
471
|
joinTableName: a,
|
|
465
472
|
on: l
|
|
466
473
|
});
|
|
467
|
-
const p = J(n, t.metadata),
|
|
468
|
-
p.find((h) => h.joinTableName === s && h.on ===
|
|
474
|
+
const p = J(n, t.metadata), u = `"${s}".id = "${a}".${o.columnName}`;
|
|
475
|
+
p.find((h) => h.joinTableName === s && h.on === u) || p.push({
|
|
469
476
|
joinTableName: s,
|
|
470
|
-
on:
|
|
477
|
+
on: u
|
|
471
478
|
});
|
|
472
|
-
}, J = (e, n) => (e.joinMap.has(n) || e.joinMap.set(n, []), e.joinMap.get(n)),
|
|
479
|
+
}, J = (e, n) => (e.joinMap.has(n) || e.joinMap.set(n, []), e.joinMap.get(n)), Ce = (e, n) => {
|
|
473
480
|
let t = n, s = 1;
|
|
474
481
|
for (; e.usedAliases.has(t); )
|
|
475
482
|
t = `${n}_${s}`, s++;
|
|
476
483
|
return e.usedAliases.add(t), t;
|
|
477
|
-
},
|
|
484
|
+
}, bt = (e, n) => {
|
|
478
485
|
if (e?.length)
|
|
479
486
|
return e.map((t) => {
|
|
480
487
|
const s = ae(t.field, n);
|
|
481
|
-
return `${
|
|
488
|
+
return `${A}."${s}" ${t.sort}`;
|
|
482
489
|
}).join(", ");
|
|
483
490
|
}, U = (e, n = /* @__PURE__ */ new Map(), t, s) => {
|
|
484
491
|
if (!e?.rules) return "";
|
|
485
492
|
const r = e.rules.map(
|
|
486
|
-
(o) => fe(o) ? U(o, n, t, s) :
|
|
493
|
+
(o) => fe(o) ? U(o, n, t, s) : mt(o, n, t, s, U)
|
|
487
494
|
).filter(Boolean);
|
|
488
495
|
return r.length ? r.length === 1 ? r[0] : `(${r.join(fe(e) ? ` ${e.combinator} ` : " ")})` : "";
|
|
489
496
|
}, Me = (e) => {
|
|
490
497
|
const { tableName: n, where: t, limit: s, offset: r, orderBy: o, join: i, hasJoin: c, metadata: a } = e;
|
|
491
|
-
let l = `${
|
|
498
|
+
let l = `${A}.rowid as ${x}, ${A}.*`;
|
|
492
499
|
if (a.features?.tree?.hasChildren && a.features?.tree?.type === "adjacency-list") {
|
|
493
|
-
const
|
|
494
|
-
l += `, EXISTS(SELECT 1 FROM "${n}" __sub WHERE __sub."${
|
|
500
|
+
const u = a.relationMap.get("parent").columnName;
|
|
501
|
+
l += `, EXISTS(SELECT 1 FROM "${n}" __sub WHERE __sub."${u}" = ${A}."id") AS "hasChildren"`;
|
|
495
502
|
}
|
|
496
|
-
const
|
|
497
|
-
return i &&
|
|
498
|
-
},
|
|
503
|
+
const d = [`SELECT ${c ? "DISTINCT " : ""}${l} FROM "${n}" ${A}`];
|
|
504
|
+
return i && d.push(i), t && d.push(` WHERE ${t}`), o && d.push(` ORDER BY ${o}`), s && d.push(` LIMIT ${s}`), r && d.push(` OFFSET ${r}`), d.join("") + ";";
|
|
505
|
+
}, Le = (e, n, t) => {
|
|
499
506
|
const s = {
|
|
500
507
|
joinMap: /* @__PURE__ */ new Map(),
|
|
501
508
|
usedAliases: /* @__PURE__ */ new Set(),
|
|
502
509
|
fieldAliasMap: /* @__PURE__ */ new Map(),
|
|
503
510
|
relationAliasMap: /* @__PURE__ */ new Map()
|
|
504
511
|
};
|
|
505
|
-
|
|
512
|
+
Xe(t, (o, i, c) => {
|
|
506
513
|
const a = i;
|
|
507
514
|
o !== "field" || !a.includes(".") || Rt(e, s, n, a, c);
|
|
508
515
|
});
|
|
509
516
|
const r = [];
|
|
510
517
|
for (const [o, i] of s.joinMap.entries()) {
|
|
511
|
-
const c =
|
|
518
|
+
const c = S(o);
|
|
512
519
|
for (const a of i)
|
|
513
520
|
r.push(` LEFT JOIN "${c}" "${a.joinTableName}" ON ${a.on}`);
|
|
514
521
|
}
|
|
515
522
|
return { joinSQL: r.join(""), fieldAliasMap: s.fieldAliasMap };
|
|
516
|
-
},
|
|
523
|
+
}, It = (e, n, t, s) => {
|
|
517
524
|
if (s.length <= 1) return !1;
|
|
518
525
|
const [r, ...o] = s, i = n.propertyMap.get(r);
|
|
519
526
|
if (i && i.type === E.keyValue && o.length > 0) {
|
|
520
527
|
const c = o.join(".");
|
|
521
|
-
return e.fieldAliasMap.set(t, `json_extract(${
|
|
528
|
+
return e.fieldAliasMap.set(t, `json_extract(${A}."${i.columnName}", '$.${c}')`), !0;
|
|
522
529
|
}
|
|
523
530
|
return !1;
|
|
524
531
|
}, Rt = (e, n, t, s, r) => {
|
|
@@ -530,96 +537,96 @@ const le = (e, n) => {
|
|
|
530
537
|
return;
|
|
531
538
|
}
|
|
532
539
|
const c = s.replace(`.${i.propertyName}`, "");
|
|
533
|
-
|
|
534
|
-
const a = i.relations[i.relations.length - 1], l = Q(i.relations, c, a.relation),
|
|
535
|
-
n.fieldAliasMap.set(s, `"${
|
|
540
|
+
Oe(e, n, i.relations, c);
|
|
541
|
+
const a = i.relations[i.relations.length - 1], l = Q(i.relations, c, a.relation), d = G(n, l);
|
|
542
|
+
n.fieldAliasMap.set(s, `"${d}"."${i.property.columnName}"`);
|
|
536
543
|
return;
|
|
537
544
|
} catch {
|
|
538
545
|
}
|
|
539
|
-
|
|
540
|
-
},
|
|
541
|
-
const t =
|
|
546
|
+
It(n, t, s, o) || Tt(e, n, t, s, o);
|
|
547
|
+
}, Ot = (e, n) => {
|
|
548
|
+
const t = S(e), s = [n.id];
|
|
542
549
|
return {
|
|
543
550
|
sql: `DELETE FROM "${t}" WHERE id = ?;`,
|
|
544
551
|
params: s
|
|
545
552
|
};
|
|
546
|
-
},
|
|
547
|
-
const s =
|
|
553
|
+
}, je = (e, n, t) => {
|
|
554
|
+
const s = S(e), r = Ie(e, n);
|
|
548
555
|
t?.userId && (e.propertyMap.has("createdBy") && (r.createdBy = t.userId), e.propertyMap.has("updatedBy") && (r.updatedBy = t.userId));
|
|
549
556
|
const o = /* @__PURE__ */ new Date();
|
|
550
557
|
e.propertyMap.has("createdAt") && r.createdAt === void 0 && (r.createdAt = t?.createdAt ?? o), e.propertyMap.has("updatedAt") && r.updatedAt === void 0 && (r.updatedAt = t?.updatedAt ?? o);
|
|
551
558
|
const i = le(e, r), c = Object.keys(i), a = Array(c.length).fill("?").join(","), l = Object.values(i);
|
|
552
559
|
let p = `${t?.useReplace ? "INSERT OR REPLACE" : "INSERT"} INTO "${s}" (${c.join(",")}) VALUES (${a})`;
|
|
553
|
-
return t?.returning !== !1 ? p += ` RETURNING rowid as ${
|
|
560
|
+
return t?.returning !== !1 ? p += ` RETURNING rowid as ${x}, *;` : p += ";", {
|
|
554
561
|
sql: p,
|
|
555
562
|
params: l
|
|
556
563
|
};
|
|
557
564
|
}, de = (e, n, t, s) => {
|
|
558
|
-
const r =
|
|
565
|
+
const r = it(e, t);
|
|
559
566
|
e.propertyMap.has("updatedAt") && (r.updatedAt = s?.updatedAt ?? /* @__PURE__ */ new Date()), s?.userId && e.propertyMap.has("updatedBy") && (r.updatedBy = s.userId);
|
|
560
|
-
const o = le(e, r), i = Object.keys(o).map((
|
|
567
|
+
const o = le(e, r), i = Object.keys(o).map((u) => `${u} = ?`).join(","), c = [...Object.values(o)], a = S(e), l = Array.isArray(n), d = l ? n : [n];
|
|
561
568
|
let p = `UPDATE "${a}" SET ${i} WHERE id `;
|
|
562
|
-
return l ? p += `in (${
|
|
569
|
+
return l ? p += `in (${d.map((u) => T(u.id)).join(",")})` : (c.push(n.id), p += "= ?"), s?.returning !== !1 ? p += ` RETURNING rowid as ${x}, *;` : p += ";", {
|
|
563
570
|
sql: p,
|
|
564
571
|
params: c
|
|
565
572
|
};
|
|
566
|
-
},
|
|
573
|
+
}, Ct = (e, n, t) => {
|
|
567
574
|
if (t.groupBy)
|
|
568
575
|
throw new R("groupBy not supported yet");
|
|
569
|
-
const { joinSQL: s, fieldAliasMap: r } =
|
|
576
|
+
const { joinSQL: s, fieldAliasMap: r } = Le(e, n, t.where), o = S(n), i = U(t.where, r, n, e), c = [`SELECT COUNT(${A}.rowid) AS count FROM "${o}" ${A}`];
|
|
570
577
|
return s && c.push(s), i && c.push(` WHERE ${i}`), { sql: c.join("") };
|
|
571
578
|
}, ie = (e, n, t) => {
|
|
572
|
-
const { joinSQL: s, fieldAliasMap: r } =
|
|
579
|
+
const { joinSQL: s, fieldAliasMap: r } = Le(e, n, t.where);
|
|
573
580
|
return { sql: Me({
|
|
574
|
-
tableName:
|
|
581
|
+
tableName: S(n),
|
|
575
582
|
where: U(t.where, r, n, e),
|
|
576
|
-
orderBy:
|
|
583
|
+
orderBy: bt(t.orderBy, n),
|
|
577
584
|
join: s,
|
|
578
585
|
hasJoin: !!s,
|
|
579
586
|
metadata: n,
|
|
580
587
|
limit: t.limit,
|
|
581
588
|
offset: t.offset
|
|
582
589
|
}) };
|
|
583
|
-
},
|
|
590
|
+
}, ge = (e, n) => {
|
|
584
591
|
const t = n.map(() => "?").join(",");
|
|
585
592
|
return { sql: Me({
|
|
586
|
-
tableName:
|
|
587
|
-
where: `${
|
|
593
|
+
tableName: S(e),
|
|
594
|
+
where: `${x} IN (${t})`,
|
|
588
595
|
metadata: e
|
|
589
596
|
}), params: n };
|
|
590
597
|
}, X = (e, n, t, s = !1, r = !1) => {
|
|
591
|
-
const o = [], i =
|
|
598
|
+
const o = [], i = w(n), c = e.rxdb.entityManager;
|
|
592
599
|
return t.results.forEach(({ columns: a, rows: l }) => {
|
|
593
|
-
const
|
|
594
|
-
l.forEach((
|
|
595
|
-
const f = d
|
|
600
|
+
const d = a.findIndex((u) => u === "id"), p = a.findIndex((u) => u === x);
|
|
601
|
+
l.forEach((u) => {
|
|
602
|
+
const f = u[d];
|
|
596
603
|
let h;
|
|
597
604
|
if (e.rxdb.entityManager.hasEntityRef(n, f)) {
|
|
598
605
|
h = c.getEntityRef(n, f);
|
|
599
|
-
const
|
|
600
|
-
if (i.computedPropertyMap.forEach((
|
|
601
|
-
$ in
|
|
606
|
+
const _ = oe(i, a, u);
|
|
607
|
+
if (i.computedPropertyMap.forEach((g, $) => {
|
|
608
|
+
$ in _ && (h[$] = _[$]);
|
|
602
609
|
}), s) {
|
|
603
|
-
const
|
|
604
|
-
|
|
610
|
+
const g = F(h);
|
|
611
|
+
g.origin = { ..._ }, Object.assign(h, _);
|
|
605
612
|
}
|
|
606
613
|
} else {
|
|
607
614
|
if (r)
|
|
608
615
|
return;
|
|
609
616
|
{
|
|
610
|
-
const
|
|
611
|
-
h = c.createEntityRef(n,
|
|
617
|
+
const _ = oe(i, a, u);
|
|
618
|
+
h = c.createEntityRef(n, _);
|
|
612
619
|
}
|
|
613
620
|
}
|
|
614
621
|
o.push(h);
|
|
615
622
|
const m = F(h);
|
|
616
623
|
if (m.local = !0, m.modified = !1, p !== -1) {
|
|
617
|
-
const
|
|
618
|
-
e.cacheRowIdEntity(
|
|
624
|
+
const _ = BigInt(u[p]);
|
|
625
|
+
e.cacheRowIdEntity(_, h);
|
|
619
626
|
}
|
|
620
627
|
});
|
|
621
628
|
}), o;
|
|
622
|
-
},
|
|
629
|
+
}, Mt = (e, n, t) => {
|
|
623
630
|
t.forEach((s) => {
|
|
624
631
|
if (e.rxdb.entityManager.hasEntityRef(n, s)) {
|
|
625
632
|
const r = e.rxdb.entityManager.getEntityRef(n, s), o = F(r);
|
|
@@ -627,14 +634,26 @@ const le = (e, n) => {
|
|
|
627
634
|
}
|
|
628
635
|
});
|
|
629
636
|
};
|
|
630
|
-
class Lt extends
|
|
637
|
+
class Lt extends Ue {
|
|
631
638
|
constructor(n, t) {
|
|
632
|
-
super(n.rxdb, t), this.adapter = n, this.metadata =
|
|
639
|
+
super(n.rxdb, t), this.adapter = n, this.metadata = w(t);
|
|
633
640
|
}
|
|
634
641
|
metadata;
|
|
635
642
|
async findByRowIds(n) {
|
|
636
|
-
|
|
637
|
-
|
|
643
|
+
if (n.length <= 999) {
|
|
644
|
+
const { sql: i, params: c } = ge(this.metadata, n), a = await this.adapter.query(i, c);
|
|
645
|
+
return this.addQueryCache(a);
|
|
646
|
+
}
|
|
647
|
+
const s = [];
|
|
648
|
+
for (let i = 0; i < n.length; i += 999) {
|
|
649
|
+
const c = n.slice(i, i + 999), { sql: a, params: l } = ge(this.metadata, c);
|
|
650
|
+
s.push(await this.adapter.query(a, l));
|
|
651
|
+
}
|
|
652
|
+
const r = s[0], o = {
|
|
653
|
+
...r,
|
|
654
|
+
results: [{ columns: r.results[0]?.columns ?? [], rows: s.flatMap((i) => i.results[0]?.rows ?? []) }]
|
|
655
|
+
};
|
|
656
|
+
return this.addQueryCache(o);
|
|
638
657
|
}
|
|
639
658
|
/**
|
|
640
659
|
* 添加缓存
|
|
@@ -645,7 +664,7 @@ class Lt extends Be {
|
|
|
645
664
|
return X(this.adapter, this.EntityType, n, t);
|
|
646
665
|
}
|
|
647
666
|
}
|
|
648
|
-
class
|
|
667
|
+
class qe extends Lt {
|
|
649
668
|
async get(n) {
|
|
650
669
|
const t = {
|
|
651
670
|
where: {
|
|
@@ -664,11 +683,11 @@ class je extends Lt {
|
|
|
664
683
|
return this.addQueryCache(r);
|
|
665
684
|
}
|
|
666
685
|
async count(n) {
|
|
667
|
-
const { sql: t, params: s } =
|
|
686
|
+
const { sql: t, params: s } = Ct(this.adapter, this.metadata, n);
|
|
668
687
|
return (await this.adapter.query(t, s)).results[0].rows[0][0];
|
|
669
688
|
}
|
|
670
689
|
async create(n) {
|
|
671
|
-
const { sql: t, params: s } =
|
|
690
|
+
const { sql: t, params: s } = je(this.metadata, n, this.rxdb.context), r = await this.adapter.query(t, s);
|
|
672
691
|
return this.addQueryCache(r, !0), n;
|
|
673
692
|
}
|
|
674
693
|
async update(n, t) {
|
|
@@ -680,10 +699,10 @@ class je extends Lt {
|
|
|
680
699
|
t.origin = structuredClone({ ...n }), t.modified = !1, t.removed = !0;
|
|
681
700
|
};
|
|
682
701
|
if (t.local === !0) {
|
|
683
|
-
const { sql: o, params: i } =
|
|
702
|
+
const { sql: o, params: i } = Ot(this.metadata, n);
|
|
684
703
|
return await this.adapter.query(o, i), s(), n;
|
|
685
704
|
}
|
|
686
|
-
const r =
|
|
705
|
+
const r = w(n);
|
|
687
706
|
throw new R(`Remove Error${r.name}(${n.id}) not saved local.`);
|
|
688
707
|
}
|
|
689
708
|
}
|
|
@@ -697,7 +716,7 @@ const jt = (e) => {
|
|
|
697
716
|
case 23:
|
|
698
717
|
return "UPDATE";
|
|
699
718
|
}
|
|
700
|
-
},
|
|
719
|
+
}, qt = "sqlite", xt = () => import("wa-sqlite/dist/wa-sqlite-async.mjs").then((e) => e.default), Dt = () => import("wa-sqlite/dist/wa-sqlite.mjs").then((e) => e.default), vt = [
|
|
701
720
|
{
|
|
702
721
|
name: "MemoryVFS",
|
|
703
722
|
vfsModule: () => import("wa-sqlite/src/examples/MemoryVFS.js").then((e) => e.MemoryVFS),
|
|
@@ -796,10 +815,10 @@ const jt = (e) => {
|
|
|
796
815
|
}, Pt = async (e) => {
|
|
797
816
|
const n = xe(e), t = e.async ?? !0;
|
|
798
817
|
let s;
|
|
799
|
-
t ? s =
|
|
818
|
+
t ? s = xt : s = Dt;
|
|
800
819
|
const [r, o] = await Promise.all([s(), n.vfsModule()]), i = {};
|
|
801
820
|
e.locateFile ? i.locateFile = e.locateFile : e.wasmPath && (i.locateFile = () => e.wasmPath);
|
|
802
|
-
const c = await r(i), a =
|
|
821
|
+
const c = await r(i), a = tt(c), l = await o.create(e.vfs, c, n.vfsOptions);
|
|
803
822
|
return a.vfs_register(l, !0), a;
|
|
804
823
|
};
|
|
805
824
|
async function kt(e, n, t, s) {
|
|
@@ -811,13 +830,13 @@ async function kt(e, n, t, s) {
|
|
|
811
830
|
};
|
|
812
831
|
for await (const l of e.statements(n, t)) {
|
|
813
832
|
c && (e.reset(l), e.bind_collection(l, c), c = void 0);
|
|
814
|
-
const
|
|
815
|
-
for (; await e.step(l) ===
|
|
816
|
-
const
|
|
817
|
-
|
|
833
|
+
const d = [];
|
|
834
|
+
for (; await e.step(l) === nt; ) {
|
|
835
|
+
const u = e.row(l);
|
|
836
|
+
d.push(u);
|
|
818
837
|
}
|
|
819
838
|
const p = e.column_names(l);
|
|
820
|
-
p.length && o.push({ columns: p, rows:
|
|
839
|
+
p.length && o.push({ columns: p, rows: d }), i += e.changes(n);
|
|
821
840
|
}
|
|
822
841
|
return {
|
|
823
842
|
...a,
|
|
@@ -827,8 +846,8 @@ async function kt(e, n, t, s) {
|
|
|
827
846
|
}
|
|
828
847
|
const Ft = {
|
|
829
848
|
BALANCED: 16
|
|
830
|
-
},
|
|
831
|
-
class Wt extends
|
|
849
|
+
}, $e = Ft.BALANCED, Bt = 50 * 1024, Ut = /* @__PURE__ */ new Set(["public$rxdb_change", "public$rxdb_branch", "public$rxdb_migration"]);
|
|
850
|
+
class Wt extends Ye {
|
|
832
851
|
#n;
|
|
833
852
|
#e;
|
|
834
853
|
#a = !1;
|
|
@@ -836,7 +855,7 @@ class Wt extends Xe {
|
|
|
836
855
|
#c;
|
|
837
856
|
#s = [];
|
|
838
857
|
#t;
|
|
839
|
-
#r =
|
|
858
|
+
#r = $e;
|
|
840
859
|
/**
|
|
841
860
|
* 初始化数据库
|
|
842
861
|
*
|
|
@@ -845,9 +864,9 @@ class Wt extends Xe {
|
|
|
845
864
|
*/
|
|
846
865
|
async init(n, t) {
|
|
847
866
|
if (this.#a) return;
|
|
848
|
-
this.#a = !0, this.#c = new
|
|
867
|
+
this.#a = !0, this.#c = new Ae(1);
|
|
849
868
|
const s = await Pt(t), r = `${n}.sqlite`;
|
|
850
|
-
this.#e = await s.open_v2(r), this.#n = s, this.#u(s, this.#e), this.#r = t?.batchTimeout ??
|
|
869
|
+
this.#e = await s.open_v2(r), this.#n = s, this.#u(s, this.#e), this.#r = t?.batchTimeout ?? $e;
|
|
851
870
|
const o = t?.cacheSizeKb ?? Bt;
|
|
852
871
|
await this.execute(`
|
|
853
872
|
PRAGMA temp_store = memory;
|
|
@@ -864,7 +883,7 @@ class Wt extends Xe {
|
|
|
864
883
|
*/
|
|
865
884
|
async version() {
|
|
866
885
|
const n = await this.execute("SELECT sqlite_version()");
|
|
867
|
-
return
|
|
886
|
+
return ze(n, "results[0].rows[0][0]");
|
|
868
887
|
}
|
|
869
888
|
/**
|
|
870
889
|
* 断开数据库连接
|
|
@@ -938,7 +957,7 @@ class Wt extends Xe {
|
|
|
938
957
|
}
|
|
939
958
|
}
|
|
940
959
|
const W = /* @__PURE__ */ new Map(), Kt = 128;
|
|
941
|
-
function
|
|
960
|
+
function De(e, n = "") {
|
|
942
961
|
const t = `${e}\0${n}`;
|
|
943
962
|
let s = W.get(t);
|
|
944
963
|
if (!s) {
|
|
@@ -948,9 +967,9 @@ function qe(e, n = "") {
|
|
|
948
967
|
return s;
|
|
949
968
|
}
|
|
950
969
|
function Ht(e, n) {
|
|
951
|
-
e.create_function(n, "regexp", 2,
|
|
970
|
+
e.create_function(n, "regexp", 2, Se | be, 0, (t, s) => {
|
|
952
971
|
try {
|
|
953
|
-
const r =
|
|
972
|
+
const r = De(e.value_text(s[0])), o = e.value_text(s[1]);
|
|
954
973
|
e.result(t, r.test(o) ? 1 : 0);
|
|
955
974
|
} catch {
|
|
956
975
|
e.result(t, 0);
|
|
@@ -958,30 +977,30 @@ function Ht(e, n) {
|
|
|
958
977
|
});
|
|
959
978
|
}
|
|
960
979
|
function Vt(e, n) {
|
|
961
|
-
e.create_function(n, "regexp_replace", -1,
|
|
980
|
+
e.create_function(n, "regexp_replace", -1, Se | be, 0, (t, s) => {
|
|
962
981
|
if (s.length < 3) {
|
|
963
982
|
e.result(t, "");
|
|
964
983
|
return;
|
|
965
984
|
}
|
|
966
985
|
try {
|
|
967
986
|
const r = e.value_text(s[0]), o = e.value_text(s[1]), i = e.value_text(s[2]), c = s.length > 3 ? e.value_text(s[3]) : "";
|
|
968
|
-
e.result(t, o.replace(
|
|
987
|
+
e.result(t, o.replace(De(r, c), i));
|
|
969
988
|
} catch {
|
|
970
989
|
e.result(t, e.value_text(s[1]));
|
|
971
990
|
}
|
|
972
991
|
});
|
|
973
992
|
}
|
|
974
993
|
async function Qt(e, n) {
|
|
975
|
-
const { vfs: t, async: s, worker: r, wasmPath: o, locateFile: i, workerInstance: c, sharedWorkerInstance: a, sharedWorker: l } = n,
|
|
994
|
+
const { vfs: t, async: s, worker: r, wasmPath: o, locateFile: i, workerInstance: c, sharedWorkerInstance: a, sharedWorker: l } = n, d = {
|
|
976
995
|
vfs: t,
|
|
977
996
|
async: s,
|
|
978
997
|
worker: r,
|
|
979
998
|
wasmPath: o,
|
|
980
999
|
locateFile: i
|
|
981
1000
|
};
|
|
982
|
-
xe(
|
|
1001
|
+
xe(d);
|
|
983
1002
|
let p;
|
|
984
|
-
return r && c ? p = Ee(c) : l && a ? p = Ee(a.port) : p = new Wt(), await p.init(e,
|
|
1003
|
+
return r && c ? p = Ee(c) : l && a ? p = Ee(a.port) : p = new Wt(), await p.init(e, d), p;
|
|
985
1004
|
}
|
|
986
1005
|
const Gt = (e, n) => n.map((t) => {
|
|
987
1006
|
const s = e.rxdb.schemaManager.getEntityMetadata(t.entity, t.namespace);
|
|
@@ -1043,28 +1062,28 @@ const Gt = (e, n) => n.map((t) => {
|
|
|
1043
1062
|
e.rxdb.dispatchEvent(new ce(s));
|
|
1044
1063
|
}
|
|
1045
1064
|
}, Xt = (e, n) => {
|
|
1046
|
-
const { tableName: t } = n, [s, r] =
|
|
1065
|
+
const { tableName: t } = n, [s, r] = at(t), o = e.rxdb.schemaManager.getEntityTypeByTableName(r, s);
|
|
1047
1066
|
if (!o) return;
|
|
1048
|
-
const i = e.getRepository(o), c =
|
|
1067
|
+
const i = e.getRepository(o), c = w(o), a = w(B);
|
|
1049
1068
|
i.findByRowIds(n.rowIds).then((l) => {
|
|
1050
1069
|
try {
|
|
1051
|
-
let
|
|
1070
|
+
let d = l;
|
|
1052
1071
|
if (c === a) {
|
|
1053
|
-
if (
|
|
1054
|
-
Jt(e,
|
|
1055
|
-
const p =
|
|
1072
|
+
if (d = Gt(e, l), n.type === D.SQLITE_INSERT) {
|
|
1073
|
+
Jt(e, d);
|
|
1074
|
+
const p = d.map((u) => ({
|
|
1056
1075
|
namespace: c.namespace,
|
|
1057
1076
|
entity: c.name,
|
|
1058
|
-
type:
|
|
1059
|
-
id:
|
|
1060
|
-
patch: { ...
|
|
1077
|
+
type: u.type,
|
|
1078
|
+
id: u.id,
|
|
1079
|
+
patch: { ...u },
|
|
1061
1080
|
inversePatch: null,
|
|
1062
|
-
recordAt:
|
|
1081
|
+
recordAt: u.createdAt
|
|
1063
1082
|
}));
|
|
1064
1083
|
e.rxdb.dispatchEvent(new K(p));
|
|
1065
1084
|
}
|
|
1066
1085
|
} else {
|
|
1067
|
-
const p = jt(n.type),
|
|
1086
|
+
const p = jt(n.type), u = d.map((f) => {
|
|
1068
1087
|
const h = {
|
|
1069
1088
|
namespace: c.namespace,
|
|
1070
1089
|
entity: c.name,
|
|
@@ -1096,48 +1115,48 @@ const Gt = (e, n) => n.map((t) => {
|
|
|
1096
1115
|
switch (p) {
|
|
1097
1116
|
case "UPDATE":
|
|
1098
1117
|
e.rxdb.dispatchEvent(
|
|
1099
|
-
new Y(
|
|
1118
|
+
new Y(u)
|
|
1100
1119
|
);
|
|
1101
1120
|
break;
|
|
1102
1121
|
case "DELETE":
|
|
1103
1122
|
e.rxdb.dispatchEvent(
|
|
1104
|
-
new ce(
|
|
1123
|
+
new ce(u)
|
|
1105
1124
|
);
|
|
1106
1125
|
break;
|
|
1107
1126
|
case "INSERT":
|
|
1108
1127
|
e.rxdb.dispatchEvent(
|
|
1109
|
-
new K(
|
|
1128
|
+
new K(u)
|
|
1110
1129
|
);
|
|
1111
1130
|
break;
|
|
1112
1131
|
}
|
|
1113
1132
|
}
|
|
1114
|
-
} catch (
|
|
1115
|
-
console.error(`[rxdb-adapter-sqlite] Error processing ${t} change event:`,
|
|
1133
|
+
} catch (d) {
|
|
1134
|
+
console.error(`[rxdb-adapter-sqlite] Error processing ${t} change event:`, d);
|
|
1116
1135
|
}
|
|
1117
1136
|
}).catch((l) => {
|
|
1118
1137
|
console.error(`[rxdb-adapter-sqlite] Failed to query ${t} change (rowIds: ${n.rowIds}):`, l);
|
|
1119
1138
|
});
|
|
1120
1139
|
}, z = (e, n, t) => {
|
|
1121
|
-
const { isCount: s, isFindDescendants: r, entityId: o, where: i } = t, c = !o, a =
|
|
1140
|
+
const { isCount: s, isFindDescendants: r, entityId: o, where: i } = t, c = !o, a = S(n), d = n.relationMap.get("parent").columnName;
|
|
1122
1141
|
let p = "";
|
|
1123
1142
|
const f = [`c.__level < ${t.level || 0}`, i && U(i, /* @__PURE__ */ new Map(), n, e)].filter(Boolean).join(" AND ");
|
|
1124
1143
|
f && (p = `WHERE ${f}`);
|
|
1125
1144
|
let h = "__children.*";
|
|
1126
|
-
s ? c ? h = "count(*)" : h = "count(*)-1" : t.hasChildren && (h += `, EXISTS(SELECT 1 FROM "${a}" __sub WHERE __sub."${
|
|
1127
|
-
const
|
|
1145
|
+
s ? c ? h = "count(*)" : h = "count(*)-1" : t.hasChildren && (h += `, EXISTS(SELECT 1 FROM "${a}" __sub WHERE __sub."${d}" = __children.id) AS hasChildren`);
|
|
1146
|
+
const y = [];
|
|
1128
1147
|
let m;
|
|
1129
|
-
return c ? m = `"${
|
|
1130
|
-
SELECT *,rowid as ${
|
|
1148
|
+
return c ? m = `"${d}" is null` : (m = "id = ?", y.push(o)), { sql: `WITH RECURSIVE __children AS (
|
|
1149
|
+
SELECT *,rowid as ${x}, 0 AS __level
|
|
1131
1150
|
FROM "${a}"
|
|
1132
1151
|
WHERE ${m}
|
|
1133
1152
|
UNION ALL
|
|
1134
|
-
SELECT children.*,children.rowid as ${
|
|
1153
|
+
SELECT children.*,children.rowid as ${x}, c.__level + 1 AS __level
|
|
1135
1154
|
FROM "${a}" children
|
|
1136
|
-
JOIN __children c ON ${r ? `children."${
|
|
1155
|
+
JOIN __children c ON ${r ? `children."${d}" = c.id` : `children.id = c."${d}"`}
|
|
1137
1156
|
${p}
|
|
1138
1157
|
)
|
|
1139
1158
|
|
|
1140
|
-
SELECT ${h} FROM __children ORDER BY __level, id;`, params:
|
|
1159
|
+
SELECT ${h} FROM __children ORDER BY __level, id;`, params: y };
|
|
1141
1160
|
}, Yt = (e, n, t) => z(e, n, {
|
|
1142
1161
|
...t,
|
|
1143
1162
|
isFindDescendants: !0,
|
|
@@ -1147,7 +1166,7 @@ SELECT ${h} FROM __children ORDER BY __level, id;`, params: _ };
|
|
|
1147
1166
|
isFindDescendants: !1,
|
|
1148
1167
|
hasChildren: n.features?.tree?.hasChildren
|
|
1149
1168
|
}), en = (e, n, t) => z(e, n, { ...t, isFindDescendants: !1, isCount: !0 });
|
|
1150
|
-
class tn extends
|
|
1169
|
+
class tn extends qe {
|
|
1151
1170
|
async findDescendants(n) {
|
|
1152
1171
|
const { sql: t, params: s } = Yt(this.adapter, this.metadata, n), r = await this.adapter.query(t, s);
|
|
1153
1172
|
return this.addQueryCache(r, !0);
|
|
@@ -1166,76 +1185,76 @@ class tn extends je {
|
|
|
1166
1185
|
}
|
|
1167
1186
|
}
|
|
1168
1187
|
const nn = (e, n) => {
|
|
1169
|
-
const t =
|
|
1188
|
+
const t = S(e), s = n.map((r) => T(r.id)).join(",");
|
|
1170
1189
|
return `DELETE FROM "${t}" WHERE id in (${s});`;
|
|
1171
|
-
},
|
|
1172
|
-
const s =
|
|
1190
|
+
}, ve = (e, n, t) => {
|
|
1191
|
+
const s = S(e), r = t?.userId && e.propertyMap.has("createdBy"), o = t?.userId && e.propertyMap.has("updatedBy"), i = e.propertyMap.has("createdAt"), c = e.propertyMap.has("updatedAt"), a = /* @__PURE__ */ new Date(), l = [
|
|
1173
1192
|
...Array.from(e.propertyMap.values()).map((f) => f.columnName),
|
|
1174
1193
|
...e.foreignKeyColumnNames || e.foreignKeyNames
|
|
1175
|
-
],
|
|
1194
|
+
], d = [], p = [];
|
|
1176
1195
|
return n.forEach((f) => {
|
|
1177
|
-
const h =
|
|
1196
|
+
const h = Ie(e, f);
|
|
1178
1197
|
r && (h.createdBy = t.userId), o && (h.updatedBy = t.userId), i && h.createdAt === void 0 && (h.createdAt = a), c && h.updatedAt === void 0 && (h.updatedAt = a);
|
|
1179
|
-
const
|
|
1180
|
-
l.forEach((
|
|
1181
|
-
m.push("?"),
|
|
1198
|
+
const y = le(e, h), m = [];
|
|
1199
|
+
l.forEach((_) => {
|
|
1200
|
+
m.push("?"), d.push(y[_] ?? null);
|
|
1182
1201
|
}), p.push(`(${m.join(",")})`);
|
|
1183
|
-
}), { sql: `INSERT INTO "${s}" (${l.join(",")}) VALUES ${p.join(",")};`, params:
|
|
1184
|
-
},
|
|
1202
|
+
}), { sql: `INSERT INTO "${s}" (${l.join(",")}) VALUES ${p.join(",")};`, params: d };
|
|
1203
|
+
}, Te = async (e, n) => {
|
|
1185
1204
|
const t = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Date();
|
|
1186
1205
|
let r = "";
|
|
1187
1206
|
const o = /* @__PURE__ */ new Set();
|
|
1188
1207
|
return n.create.forEach((i, c) => {
|
|
1189
1208
|
const a = Array.from(i);
|
|
1190
1209
|
a.forEach((m) => o.add(m));
|
|
1191
|
-
const l =
|
|
1192
|
-
|
|
1210
|
+
const l = w(c), d = ve(l, a, e.rxdb.context), p = k(d.sql, d.params), u = a.map((m) => m.id);
|
|
1211
|
+
u.forEach((m) => t.set(m, c));
|
|
1193
1212
|
const { sql: f, params: h } = ie(e, l, {
|
|
1194
1213
|
where: {
|
|
1195
1214
|
combinator: "and",
|
|
1196
|
-
rules: [{ field: "id", operator: "in", value:
|
|
1215
|
+
rules: [{ field: "id", operator: "in", value: u }]
|
|
1197
1216
|
}
|
|
1198
|
-
}),
|
|
1199
|
-
r += p +
|
|
1217
|
+
}), y = k(f, h);
|
|
1218
|
+
r += p + y;
|
|
1200
1219
|
}), n.update.forEach((i, c) => {
|
|
1201
1220
|
const a = Array.from(i);
|
|
1202
1221
|
a.forEach((f) => o.add(f));
|
|
1203
|
-
const l =
|
|
1222
|
+
const l = w(c);
|
|
1204
1223
|
a.forEach((f) => {
|
|
1205
|
-
const h = F(f),
|
|
1224
|
+
const h = F(f), y = de(l, f, h.patch, {
|
|
1206
1225
|
...e.rxdb.context,
|
|
1207
1226
|
returning: !1,
|
|
1208
1227
|
updatedAt: s
|
|
1209
1228
|
});
|
|
1210
1229
|
t.set(f.id, c);
|
|
1211
|
-
const m = k(
|
|
1230
|
+
const m = k(y.sql, y.params);
|
|
1212
1231
|
r += m;
|
|
1213
1232
|
});
|
|
1214
|
-
const
|
|
1215
|
-
|
|
1233
|
+
const d = a.map((f) => f.id);
|
|
1234
|
+
d.forEach((f) => t.set(f, c));
|
|
1216
1235
|
const p = ie(e, l, {
|
|
1217
1236
|
where: {
|
|
1218
1237
|
combinator: "and",
|
|
1219
|
-
rules: [{ field: "id", operator: "in", value:
|
|
1238
|
+
rules: [{ field: "id", operator: "in", value: d }]
|
|
1220
1239
|
}
|
|
1221
|
-
}),
|
|
1222
|
-
r += d;
|
|
1223
|
-
}), n.remove.forEach((i, c) => {
|
|
1224
|
-
const a = Array.from(i), l = T(c), u = nn(l, a);
|
|
1240
|
+
}), u = k(p.sql, p.params);
|
|
1225
1241
|
r += u;
|
|
1242
|
+
}), n.remove.forEach((i, c) => {
|
|
1243
|
+
const a = Array.from(i), l = w(c), d = nn(l, a);
|
|
1244
|
+
r += d;
|
|
1226
1245
|
}), r && (await e.query(r)).results.forEach(({ columns: c, rows: a }) => {
|
|
1227
|
-
const l = c.findIndex((
|
|
1228
|
-
a.forEach((
|
|
1229
|
-
const f =
|
|
1246
|
+
const l = c.findIndex((u) => u === "id"), d = c.findIndex((u) => u === x), p = e.rxdb.entityManager;
|
|
1247
|
+
a.forEach((u) => {
|
|
1248
|
+
const f = u[l], h = t.get(f), y = w(h);
|
|
1230
1249
|
if (p.hasEntityRef(h, f)) {
|
|
1231
|
-
const m = p.getEntityRef(h, f),
|
|
1232
|
-
e.getRepository(h).updateEntity(m,
|
|
1233
|
-
const
|
|
1234
|
-
if (
|
|
1235
|
-
const $ = BigInt(d
|
|
1250
|
+
const m = p.getEntityRef(h, f), _ = oe(y, c, u);
|
|
1251
|
+
e.getRepository(h).updateEntity(m, _);
|
|
1252
|
+
const g = F(m);
|
|
1253
|
+
if (g.local = !0, d !== -1) {
|
|
1254
|
+
const $ = BigInt(u[d]);
|
|
1236
1255
|
e.cacheRowIdEntity($, m);
|
|
1237
1256
|
}
|
|
1238
|
-
|
|
1257
|
+
g.modified = !1;
|
|
1239
1258
|
}
|
|
1240
1259
|
});
|
|
1241
1260
|
}), n.remove.forEach(
|
|
@@ -1246,17 +1265,17 @@ const nn = (e, n) => {
|
|
|
1246
1265
|
})
|
|
1247
1266
|
), Array.from(o);
|
|
1248
1267
|
}, sn = (e, n) => {
|
|
1249
|
-
const t =
|
|
1268
|
+
const t = S(n);
|
|
1250
1269
|
let s = `CREATE TABLE "${t}" (`;
|
|
1251
1270
|
const r = [], o = [], i = [];
|
|
1252
1271
|
n.propertyMap.forEach((a) => {
|
|
1253
1272
|
const l = a.columnName;
|
|
1254
|
-
let
|
|
1255
|
-
if (a.type === E.integer && a.primary ?
|
|
1256
|
-
let
|
|
1257
|
-
me(a.default) && (
|
|
1273
|
+
let d = `"${l}" ${ot(a)}`;
|
|
1274
|
+
if (a.type === E.integer && a.primary ? d += " PRIMARY KEY AUTOINCREMENT" : (a.type === E.uuid || a.type === E.string) && a.primary && (d += " PRIMARY KEY", a.type === E.uuid && (d += " DEFAULT (lower(hex(randomblob(16))))")), !(a.type === E.uuid && a.primary) && a.default !== void 0) {
|
|
1275
|
+
let u = a.default;
|
|
1276
|
+
me(a.default) && (u = a.default()), u === "CURRENT_TIMESTAMP" ? d += " DEFAULT(strftime('%FT%H:%M:%fZ'))" : (u = V(u, a), a.type === E.boolean || a.type === E.integer || a.type === E.number ? d += ` DEFAULT ${u}` : d += ` DEFAULT '${u}'`);
|
|
1258
1277
|
}
|
|
1259
|
-
switch (a.nullable || (
|
|
1278
|
+
switch (a.nullable || (d += " NOT NULL"), a.type) {
|
|
1260
1279
|
case E.json:
|
|
1261
1280
|
case E.keyValue:
|
|
1262
1281
|
case E.stringArray:
|
|
@@ -1268,42 +1287,42 @@ const nn = (e, n) => {
|
|
|
1268
1287
|
break;
|
|
1269
1288
|
case E.enum:
|
|
1270
1289
|
if ("enum" in a && a.enum && a.enum.length > 0) {
|
|
1271
|
-
const
|
|
1272
|
-
o.push(`CHECK (${l} in(${
|
|
1290
|
+
const u = a.enum.map((f) => `'${String(f).replace(/'/g, "''")}'`).join(",");
|
|
1291
|
+
a.nullable ? o.push(`CHECK (${l} IN(${u},null))`) : o.push(`CHECK (${l} in(${u}))`);
|
|
1273
1292
|
}
|
|
1274
1293
|
break;
|
|
1275
1294
|
}
|
|
1276
1295
|
a.unique && r.push(
|
|
1277
|
-
`CREATE UNIQUE INDEX ${
|
|
1278
|
-
), i.push(
|
|
1296
|
+
`CREATE UNIQUE INDEX ${_e(n, a)} on "${t}"(${l});`
|
|
1297
|
+
), i.push(d);
|
|
1279
1298
|
}), n.relationMap.forEach((a) => {
|
|
1280
|
-
if (a.kind ===
|
|
1281
|
-
const l = a.columnName,
|
|
1299
|
+
if (a.kind === I.ONE_TO_ONE || a.kind === I.MANY_TO_ONE) {
|
|
1300
|
+
const l = a.columnName, d = e.rxdb.schemaManager.getEntityMetadata(
|
|
1282
1301
|
a.mappedEntity,
|
|
1283
1302
|
a.mappedNamespace
|
|
1284
1303
|
);
|
|
1285
1304
|
let p = "TEXT";
|
|
1286
|
-
const
|
|
1287
|
-
|
|
1305
|
+
const u = d.propertyMap.get("id");
|
|
1306
|
+
u.type === E.integer ? p = "INTEGER" : (u.type === E.uuid || u.type === E.string) && (p = "TEXT");
|
|
1288
1307
|
const f = a.onDelete === "SET NULL" || a.onUpdate === "SET NULL";
|
|
1289
1308
|
let h = `"${l}" ${p}`;
|
|
1290
|
-
if (!a.nullable && !f && (h += " NOT NULL"), a.kind ===
|
|
1291
|
-
const
|
|
1292
|
-
let m =
|
|
1293
|
-
me(
|
|
1309
|
+
if (!a.nullable && !f && (h += " NOT NULL"), a.kind === I.MANY_TO_ONE && a.default !== void 0) {
|
|
1310
|
+
const y = a;
|
|
1311
|
+
let m = y.default;
|
|
1312
|
+
me(y.default) && (m = y.default()), h += ` DEFAULT ${m}`;
|
|
1294
1313
|
}
|
|
1295
1314
|
if (a.mappedEntity) {
|
|
1296
|
-
const
|
|
1315
|
+
const y = e.rxdb.schemaManager.getEntityMetadata(
|
|
1297
1316
|
a.mappedEntity,
|
|
1298
1317
|
a.mappedNamespace
|
|
1299
1318
|
), m = P(
|
|
1300
|
-
|
|
1319
|
+
y?.tableName ?? a.mappedEntity,
|
|
1301
1320
|
a.mappedNamespace
|
|
1302
1321
|
);
|
|
1303
1322
|
h += ` REFERENCES ${m}(id)`, a.onDelete && (h += ` ON DELETE ${a.onDelete}`), a.onUpdate && (h += ` ON UPDATE ${a.onUpdate}`);
|
|
1304
1323
|
}
|
|
1305
|
-
i.push(h), (a.unique || a.kind ===
|
|
1306
|
-
`CREATE UNIQUE INDEX ${
|
|
1324
|
+
i.push(h), (a.unique || a.kind === I.ONE_TO_ONE) && r.push(
|
|
1325
|
+
`CREATE UNIQUE INDEX ${_e(n, a)} on "${t}"(${l});`
|
|
1307
1326
|
);
|
|
1308
1327
|
}
|
|
1309
1328
|
});
|
|
@@ -1317,50 +1336,50 @@ ${a}`).join(","), s += `
|
|
|
1317
1336
|
return r.length && (s += `
|
|
1318
1337
|
` + r.join(`
|
|
1319
1338
|
`)), n.indexes && n.indexes.length > 0 && n.indexes.forEach((a) => {
|
|
1320
|
-
const l = `idx_${n.name}_${a.name}`,
|
|
1321
|
-
const f = n.propertyMap.get(
|
|
1339
|
+
const l = `idx_${n.name}_${a.name}`, d = a.properties?.map((u) => {
|
|
1340
|
+
const f = n.propertyMap.get(u);
|
|
1322
1341
|
if (f) return `"${f.columnName}"`;
|
|
1323
|
-
const h = n.foreignKeyNames,
|
|
1324
|
-
if (h &&
|
|
1325
|
-
const m = h.indexOf(
|
|
1326
|
-
if (m >= 0) return `"${
|
|
1342
|
+
const h = n.foreignKeyNames, y = n.foreignKeyColumnNames;
|
|
1343
|
+
if (h && y) {
|
|
1344
|
+
const m = h.indexOf(u);
|
|
1345
|
+
if (m >= 0) return `"${y[m]}"`;
|
|
1327
1346
|
}
|
|
1328
|
-
return `"${
|
|
1347
|
+
return `"${u}"`;
|
|
1329
1348
|
}).join(", ") || `"${a.name}"`, p = a.unique ? "UNIQUE " : "";
|
|
1330
1349
|
s += `
|
|
1331
|
-
CREATE ${p}INDEX "${l}" ON "${t}"(${
|
|
1350
|
+
CREATE ${p}INDEX "${l}" ON "${t}"(${d});`;
|
|
1332
1351
|
}), s;
|
|
1333
1352
|
}, rn = (e, n) => sn(e, n), pe = (e, n = {}) => {
|
|
1334
|
-
const t =
|
|
1335
|
-
for (const [
|
|
1336
|
-
|
|
1353
|
+
const t = S(e), s = w(B), r = S(s), { propertyMap: o, name: i, foreignKeyNames: c, foreignKeyColumnNames: a, namespace: l } = e, d = [];
|
|
1354
|
+
for (const [N, M] of o)
|
|
1355
|
+
N !== "id" && d.push({ jsName: N, dbColumn: M.columnName });
|
|
1337
1356
|
const p = a || c;
|
|
1338
|
-
for (let
|
|
1339
|
-
c[
|
|
1340
|
-
const
|
|
1341
|
-
const Z = o.get(
|
|
1342
|
-
return Z?.type === E.boolean ? `CASE WHEN ${
|
|
1343
|
-
},
|
|
1357
|
+
for (let N = 0; N < c.length; N++)
|
|
1358
|
+
c[N] !== "id" && d.push({ jsName: c[N], dbColumn: p[N] });
|
|
1359
|
+
const u = " type, namespace, entity, branchId, transactionId, entityId, inversePatch, patch", { transactionId: f, branchId: h } = n, y = T(f), m = T(h || "main"), _ = `'${l}','${i}',${m},${y}`, g = (N) => N === E.stringArray || N === E.numberArray || N === E.json || N === E.keyValue, $ = (N, M, q) => {
|
|
1360
|
+
const Z = o.get(M);
|
|
1361
|
+
return Z?.type === E.boolean ? `CASE WHEN ${q}.${N} = 1 THEN 1 ELSE 0 END` : g(Z?.type) ? `CASE WHEN ${q}.${N} IS NOT NULL THEN json(${q}.${N}) ELSE NULL END` : `${q}.${N}`;
|
|
1362
|
+
}, b = (N, M, q) => o.get(M)?.type === E.boolean ? `CASE WHEN ${q}.${N} = 1 THEN 1 ELSE 0 END` : `${q}.${N}`, L = `
|
|
1344
1363
|
DROP TRIGGER IF EXISTS ${t}_insert;
|
|
1345
1364
|
CREATE TRIGGER ${t}_insert AFTER INSERT ON "${t}"
|
|
1346
1365
|
BEGIN
|
|
1347
|
-
INSERT INTO ${r} (${
|
|
1348
|
-
'INSERT',${
|
|
1349
|
-
json_object(${
|
|
1366
|
+
INSERT INTO ${r} (${u}) VALUES (
|
|
1367
|
+
'INSERT',${_}, NEW.id, NULL,
|
|
1368
|
+
json_object(${d.map((N) => `'${N.jsName}', ${$(N.dbColumn, N.jsName, "NEW")}`).join(`,
|
|
1350
1369
|
`)})
|
|
1351
1370
|
);
|
|
1352
|
-
END;`, O = (
|
|
1353
|
-
const
|
|
1354
|
-
return `SELECT '${
|
|
1371
|
+
END;`, O = (N) => d.map((M) => {
|
|
1372
|
+
const q = g(o.get(M.jsName)?.type) ? 1 : 0;
|
|
1373
|
+
return `SELECT '${M.jsName}' AS key, ${b(M.dbColumn, M.jsName, N)} AS value, ${q} AS is_json WHERE OLD.${M.dbColumn} IS NOT NEW.${M.dbColumn}`;
|
|
1355
1374
|
}).join(`
|
|
1356
|
-
UNION `),
|
|
1375
|
+
UNION `), C = `
|
|
1357
1376
|
DROP TRIGGER IF EXISTS ${t}_update;
|
|
1358
1377
|
CREATE TRIGGER ${t}_update AFTER UPDATE ON "${t}"
|
|
1359
|
-
WHEN ( ${
|
|
1378
|
+
WHEN ( ${d.map((N) => `OLD.${N.dbColumn} IS NOT NEW.${N.dbColumn}`).join(` OR
|
|
1360
1379
|
`)} )
|
|
1361
1380
|
BEGIN
|
|
1362
|
-
INSERT INTO ${r} (${
|
|
1363
|
-
'UPDATE',${
|
|
1381
|
+
INSERT INTO ${r} (${u}) VALUES (
|
|
1382
|
+
'UPDATE',${_}, NEW.id,
|
|
1364
1383
|
(
|
|
1365
1384
|
SELECT json_group_object(key, CASE WHEN is_json AND value IS NOT NULL THEN json(value) ELSE value END) FROM (
|
|
1366
1385
|
${O("OLD")}
|
|
@@ -1376,19 +1395,19 @@ CREATE ${p}INDEX "${l}" ON "${t}"(${u});`;
|
|
|
1376
1395
|
DROP TRIGGER IF EXISTS ${t}_delete;
|
|
1377
1396
|
CREATE TRIGGER ${t}_delete AFTER DELETE ON "${t}"
|
|
1378
1397
|
BEGIN
|
|
1379
|
-
INSERT INTO ${r} (${
|
|
1380
|
-
'DELETE',${
|
|
1381
|
-
json_object(${
|
|
1398
|
+
INSERT INTO ${r} (${u}) VALUES (
|
|
1399
|
+
'DELETE',${_}, OLD.id,
|
|
1400
|
+
json_object(${d.map((N) => `'${N.jsName}', ${$(N.dbColumn, N.jsName, "OLD")}`).join(`,
|
|
1382
1401
|
`)}), NULL
|
|
1383
1402
|
);
|
|
1384
1403
|
END;`;
|
|
1385
1404
|
return L + `
|
|
1386
|
-
` +
|
|
1405
|
+
` + C + `
|
|
1387
1406
|
` + j;
|
|
1388
1407
|
}, on = (e, n, t) => {
|
|
1389
1408
|
let s = "";
|
|
1390
1409
|
for (let r = 0; r < n.length; r++) {
|
|
1391
|
-
const o = n[r], i =
|
|
1410
|
+
const o = n[r], i = w(o);
|
|
1392
1411
|
if (s += `
|
|
1393
1412
|
` + rn(e, i), i.log !== !1) {
|
|
1394
1413
|
const c = pe(i);
|
|
@@ -1399,11 +1418,11 @@ CREATE ${p}INDEX "${l}" ON "${t}"(${u});`;
|
|
|
1399
1418
|
if (t) {
|
|
1400
1419
|
const r = /* @__PURE__ */ new Map();
|
|
1401
1420
|
t.forEach((o) => {
|
|
1402
|
-
const i =
|
|
1421
|
+
const i = w(o);
|
|
1403
1422
|
r.has(i) === !1 && r.set(i, /* @__PURE__ */ new Set()), r.get(i).add(o);
|
|
1404
1423
|
});
|
|
1405
1424
|
for (const o of r.keys()) {
|
|
1406
|
-
const i =
|
|
1425
|
+
const i = ve(
|
|
1407
1426
|
o,
|
|
1408
1427
|
Array.from(r.get(o)),
|
|
1409
1428
|
e.rxdb.context
|
|
@@ -1414,106 +1433,108 @@ CREATE ${p}INDEX "${l}" ON "${t}"(${u});`;
|
|
|
1414
1433
|
}
|
|
1415
1434
|
return s;
|
|
1416
1435
|
}, an = (e) => {
|
|
1417
|
-
const n =
|
|
1436
|
+
const n = S(e);
|
|
1418
1437
|
return `DROP TRIGGER IF EXISTS ${n}_insert;
|
|
1419
1438
|
DROP TRIGGER IF EXISTS ${n}_update;
|
|
1420
1439
|
DROP TRIGGER IF EXISTS ${n}_delete;`;
|
|
1421
|
-
},
|
|
1440
|
+
}, Pe = (e) => {
|
|
1422
1441
|
let n = "";
|
|
1423
1442
|
return e.rxdb.config.entities.forEach((t) => {
|
|
1424
|
-
const s =
|
|
1443
|
+
const s = w(t);
|
|
1425
1444
|
s.log !== !1 && (n += an(s));
|
|
1426
1445
|
}), n;
|
|
1427
|
-
},
|
|
1446
|
+
}, ke = (e, n) => {
|
|
1428
1447
|
const { deletes: t, inserts: s, updates: r } = n, o = { deletes: [], inserts: [], updates: [] }, i = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map();
|
|
1429
|
-
for (const [
|
|
1430
|
-
const [h,
|
|
1431
|
-
|
|
1432
|
-
let $ = c.get(
|
|
1433
|
-
$ || ($ = /* @__PURE__ */ new Map(), c.set(
|
|
1448
|
+
for (const [u, f] of t.entries()) {
|
|
1449
|
+
const [h, y, m] = ee(u), _ = `${h}:${y}`, g = i.get(_);
|
|
1450
|
+
g ? g.add(m) : i.set(_, /* @__PURE__ */ new Set([m]));
|
|
1451
|
+
let $ = c.get(_);
|
|
1452
|
+
$ || ($ = /* @__PURE__ */ new Map(), c.set(_, $)), $.set(m, {
|
|
1434
1453
|
patch: f.patch,
|
|
1435
1454
|
inversePatch: f.inversePatch
|
|
1436
1455
|
});
|
|
1437
1456
|
}
|
|
1438
|
-
for (const [
|
|
1439
|
-
const [h,
|
|
1457
|
+
for (const [u, f] of i) {
|
|
1458
|
+
const [h, y] = u.split(":"), m = e.rxdb.schemaManager.getEntityMetadata(y, h), _ = S(m), g = m.propertyMap.get("id").type === "integer";
|
|
1440
1459
|
let $ = "";
|
|
1441
|
-
|
|
1442
|
-
const
|
|
1460
|
+
g ? $ = Array.from(f).join(",") : $ = Array.from(f).map((L) => T(L)).join(",");
|
|
1461
|
+
const b = `DELETE FROM "${_}" WHERE id in (${$});`;
|
|
1443
1462
|
o.deletes.push({
|
|
1444
1463
|
metadata: m,
|
|
1445
1464
|
ids: f,
|
|
1446
|
-
sql:
|
|
1447
|
-
changes: c.get(
|
|
1465
|
+
sql: b,
|
|
1466
|
+
changes: c.get(u)
|
|
1448
1467
|
});
|
|
1449
1468
|
}
|
|
1450
1469
|
const a = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map();
|
|
1451
|
-
for (const [
|
|
1452
|
-
const [h,
|
|
1453
|
-
|
|
1454
|
-
let
|
|
1455
|
-
|
|
1470
|
+
for (const [u, f] of s.entries()) {
|
|
1471
|
+
const [h, y, m] = ee(u), _ = `${h}:${y}`, g = a.get(_), $ = { ...f.patch, id: m };
|
|
1472
|
+
g ? g.push($) : a.set(_, [$]);
|
|
1473
|
+
let b = l.get(_);
|
|
1474
|
+
b || (b = /* @__PURE__ */ new Map(), l.set(_, b)), b.set(m, {
|
|
1456
1475
|
patch: f.patch,
|
|
1457
1476
|
inversePatch: f.inversePatch
|
|
1458
1477
|
});
|
|
1459
1478
|
}
|
|
1460
|
-
for (const [
|
|
1461
|
-
const [h,
|
|
1462
|
-
let
|
|
1463
|
-
f.forEach((
|
|
1464
|
-
for (const [O,
|
|
1465
|
-
|
|
1466
|
-
const { sql: $, params:
|
|
1479
|
+
for (const [u, f] of a) {
|
|
1480
|
+
const [h, y] = u.split(":"), m = e.rxdb.schemaManager.getEntityMetadata(y, h);
|
|
1481
|
+
let _ = "";
|
|
1482
|
+
f.forEach((g) => {
|
|
1483
|
+
for (const [O, C] of m.propertyMap)
|
|
1484
|
+
g[O] === void 0 && C.default !== void 0 && (typeof C.default == "function" ? g[O] = C.default() : g[O] = C.default);
|
|
1485
|
+
const { sql: $, params: b } = je(m, g, {
|
|
1467
1486
|
useReplace: !0
|
|
1468
|
-
}), L = k($,
|
|
1469
|
-
|
|
1487
|
+
}), L = k($, b);
|
|
1488
|
+
_ += L;
|
|
1470
1489
|
}), o.inserts.push({
|
|
1471
1490
|
metadata: m,
|
|
1472
|
-
ids: new Set(f.filter((
|
|
1473
|
-
sql:
|
|
1474
|
-
changes: l.get(
|
|
1491
|
+
ids: new Set(f.filter((g) => g != null).map((g) => g.id)),
|
|
1492
|
+
sql: _,
|
|
1493
|
+
changes: l.get(u)
|
|
1475
1494
|
});
|
|
1476
1495
|
}
|
|
1477
|
-
const
|
|
1478
|
-
for (const [
|
|
1479
|
-
const [h,
|
|
1480
|
-
|
|
1481
|
-
let
|
|
1482
|
-
|
|
1483
|
-
const L = e.rxdb.schemaManager.getEntityMetadata(
|
|
1496
|
+
const d = /* @__PURE__ */ new Map(), p = /* @__PURE__ */ new Map();
|
|
1497
|
+
for (const [u, f] of r.entries()) {
|
|
1498
|
+
const [h, y, m] = ee(u), _ = `${h}:${y}`, g = d.get(_), $ = { ...f.patch, id: m };
|
|
1499
|
+
g ? g.push($) : d.set(_, [$]);
|
|
1500
|
+
let b = p.get(_);
|
|
1501
|
+
b || (b = /* @__PURE__ */ new Map(), p.set(_, b));
|
|
1502
|
+
const L = e.rxdb.schemaManager.getEntityMetadata(y, h), O = f.patch ? { ...f.patch } : null, C = f.inversePatch ? { ...f.inversePatch } : null;
|
|
1484
1503
|
L && (O && Object.keys(O).forEach((j) => {
|
|
1485
|
-
const
|
|
1486
|
-
|
|
1487
|
-
}),
|
|
1488
|
-
const
|
|
1489
|
-
|
|
1490
|
-
})),
|
|
1504
|
+
const N = L.propertyMap.get(j);
|
|
1505
|
+
N && (O[j] = v(O[j], N));
|
|
1506
|
+
}), C && Object.keys(C).forEach((j) => {
|
|
1507
|
+
const N = L.propertyMap.get(j);
|
|
1508
|
+
N && (C[j] = v(C[j], N));
|
|
1509
|
+
})), b.set(m, {
|
|
1491
1510
|
patch: O,
|
|
1492
|
-
inversePatch:
|
|
1511
|
+
inversePatch: C
|
|
1493
1512
|
});
|
|
1494
1513
|
}
|
|
1495
|
-
for (const [
|
|
1496
|
-
const [h,
|
|
1497
|
-
|
|
1498
|
-
|
|
1514
|
+
for (const [u, f] of d) {
|
|
1515
|
+
const [h, y] = u.split(":"), m = e.rxdb.schemaManager.getEntityMetadata(y, h);
|
|
1516
|
+
if (!m)
|
|
1517
|
+
throw new Error(`找不到 namespace=${h} entityName=${y} 对应的实体元数据`);
|
|
1518
|
+
let _ = "";
|
|
1519
|
+
f.forEach((g) => {
|
|
1499
1520
|
let $;
|
|
1500
|
-
m.propertyMap.has("updatedAt") && ($ =
|
|
1501
|
-
const { id:
|
|
1521
|
+
m.propertyMap.has("updatedAt") && ($ = g.updatedAt);
|
|
1522
|
+
const { id: b, ...L } = g, { sql: O, params: C } = de(m, { id: b }, L, {
|
|
1502
1523
|
updatedAt: $
|
|
1503
1524
|
});
|
|
1504
|
-
|
|
1525
|
+
_ += k(O, C);
|
|
1505
1526
|
}), o.updates.push({
|
|
1506
1527
|
metadata: m,
|
|
1507
|
-
ids: new Set(f.filter((
|
|
1508
|
-
sql:
|
|
1509
|
-
changes: p.get(
|
|
1528
|
+
ids: new Set(f.filter((g) => g != null).map((g) => g.id)),
|
|
1529
|
+
sql: _,
|
|
1530
|
+
changes: p.get(u)
|
|
1510
1531
|
});
|
|
1511
1532
|
}
|
|
1512
1533
|
return o;
|
|
1513
|
-
},
|
|
1534
|
+
}, Fe = (e, n) => {
|
|
1514
1535
|
let t = "";
|
|
1515
1536
|
e.rxdb.config.entities.forEach((i) => {
|
|
1516
|
-
const c =
|
|
1537
|
+
const c = w(i);
|
|
1517
1538
|
if (c.log !== !1)
|
|
1518
1539
|
try {
|
|
1519
1540
|
const a = pe(c, {
|
|
@@ -1524,7 +1545,7 @@ DROP TRIGGER IF EXISTS ${n}_delete;`;
|
|
|
1524
1545
|
console.warn(`Failed to generate trigger SQL for entity ${c.name}:`, a);
|
|
1525
1546
|
}
|
|
1526
1547
|
});
|
|
1527
|
-
const s =
|
|
1548
|
+
const s = w(H), r = S(s), o = n.replace(/'/g, "''");
|
|
1528
1549
|
return t += `
|
|
1529
1550
|
UPDATE "${r}"
|
|
1530
1551
|
SET
|
|
@@ -1537,76 +1558,81 @@ DROP TRIGGER IF EXISTS ${n}_delete;`;
|
|
|
1537
1558
|
ELSE updatedAt
|
|
1538
1559
|
END
|
|
1539
1560
|
WHERE id = '${o}' OR activated = 1
|
|
1540
|
-
RETURNING rowid as ${
|
|
1561
|
+
RETURNING rowid as ${x},*;
|
|
1541
1562
|
`, t;
|
|
1542
1563
|
}, cn = async (e, n) => {
|
|
1543
|
-
const { branchId: t, actions: s } = n, r = s &&
|
|
1564
|
+
const { branchId: t, actions: s } = n, r = s && ke(e, s);
|
|
1544
1565
|
let o;
|
|
1545
1566
|
try {
|
|
1546
1567
|
await e.transaction(async (a) => {
|
|
1547
|
-
const l =
|
|
1568
|
+
const l = Pe(e);
|
|
1548
1569
|
if (l && await a.execute(l), r) {
|
|
1549
1570
|
if (r.deletes.length)
|
|
1550
|
-
for (const
|
|
1551
|
-
await a.execute(
|
|
1571
|
+
for (const d of r.deletes)
|
|
1572
|
+
await a.execute(d.sql);
|
|
1552
1573
|
if (r.inserts.length)
|
|
1553
|
-
for (const
|
|
1554
|
-
|
|
1574
|
+
for (const d of r.inserts)
|
|
1575
|
+
d.successResults = await a.execute(d.sql);
|
|
1555
1576
|
if (r.updates.length)
|
|
1556
|
-
for (const
|
|
1557
|
-
|
|
1577
|
+
for (const d of r.updates)
|
|
1578
|
+
d.successResults = await a.execute(d.sql);
|
|
1558
1579
|
}
|
|
1559
1580
|
s?.updateRxDBChangeSequence !== void 0 && await e.setRxDBChangeSequence(s.updateRxDBChangeSequence);
|
|
1560
1581
|
}, !1);
|
|
1561
|
-
const i =
|
|
1582
|
+
const i = Fe(e, t);
|
|
1562
1583
|
o = await e.internalQuery(i);
|
|
1563
1584
|
const c = (a, l) => {
|
|
1564
1585
|
if (l.length === 0) return;
|
|
1565
|
-
const
|
|
1566
|
-
const
|
|
1586
|
+
const d = l.map((p) => {
|
|
1587
|
+
const u = { ...p };
|
|
1567
1588
|
return {
|
|
1568
1589
|
namespace: a.namespace,
|
|
1569
1590
|
entity: a.name,
|
|
1570
1591
|
type: "UPDATE",
|
|
1571
1592
|
id: p.id,
|
|
1572
|
-
patch:
|
|
1573
|
-
inversePatch:
|
|
1593
|
+
patch: u,
|
|
1594
|
+
inversePatch: u,
|
|
1574
1595
|
recordAt: p.createdAt
|
|
1575
1596
|
};
|
|
1576
1597
|
});
|
|
1577
|
-
e.rxdb.dispatchEvent(new Y(
|
|
1598
|
+
e.rxdb.dispatchEvent(new Y(d));
|
|
1578
1599
|
};
|
|
1579
1600
|
if (o) {
|
|
1580
|
-
const a = X(e, H, o, !0), l =
|
|
1601
|
+
const a = X(e, H, o, !0), l = w(H);
|
|
1581
1602
|
c(l, a);
|
|
1582
1603
|
}
|
|
1583
|
-
r &&
|
|
1604
|
+
r && Be(e, r);
|
|
1584
1605
|
} catch (i) {
|
|
1585
1606
|
throw new R(`switch branch ${t} failed`, { cause: i });
|
|
1586
1607
|
}
|
|
1587
1608
|
};
|
|
1588
|
-
async function ln(e, n, t = !1) {
|
|
1589
|
-
if (await e.transaction(async (
|
|
1609
|
+
async function ln(e, n, t = !1, s) {
|
|
1610
|
+
if (await e.transaction(async (r) => {
|
|
1590
1611
|
if (t) {
|
|
1591
|
-
const
|
|
1592
|
-
|
|
1612
|
+
const o = Pe(e);
|
|
1613
|
+
o && await r.execute(o);
|
|
1614
|
+
}
|
|
1615
|
+
for (const o of n.deletes)
|
|
1616
|
+
await r.execute(o.sql);
|
|
1617
|
+
for (const o of n.inserts)
|
|
1618
|
+
o.successResults = await r.execute(o.sql);
|
|
1619
|
+
for (const o of n.updates)
|
|
1620
|
+
o.successResults = await r.execute(o.sql);
|
|
1621
|
+
if (t && s?.length) {
|
|
1622
|
+
console.log("localChanges", s);
|
|
1623
|
+
const o = un(s);
|
|
1624
|
+
o && await r.execute(o);
|
|
1593
1625
|
}
|
|
1594
|
-
for (const r of n.deletes)
|
|
1595
|
-
await s.execute(r.sql);
|
|
1596
|
-
for (const r of n.inserts)
|
|
1597
|
-
r.successResults = await s.execute(r.sql);
|
|
1598
|
-
for (const r of n.updates)
|
|
1599
|
-
r.successResults = await s.execute(r.sql);
|
|
1600
1626
|
}, !1), t) {
|
|
1601
|
-
const
|
|
1602
|
-
await e.internalQuery(
|
|
1627
|
+
const r = await e.rxdb.versionManager.getCurrentBranch(), o = Fe(e, r.id);
|
|
1628
|
+
await e.internalQuery(o);
|
|
1603
1629
|
}
|
|
1604
|
-
|
|
1630
|
+
Be(e, n);
|
|
1605
1631
|
}
|
|
1606
|
-
function
|
|
1632
|
+
function Be(e, n) {
|
|
1607
1633
|
for (const t of n.deletes) {
|
|
1608
1634
|
const s = te(t.metadata);
|
|
1609
|
-
|
|
1635
|
+
Mt(e, s, Array.from(t.ids));
|
|
1610
1636
|
const r = t.metadata, o = Array.from(t.ids).map((i) => {
|
|
1611
1637
|
const c = t.changes.get(String(i));
|
|
1612
1638
|
return {
|
|
@@ -1652,10 +1678,19 @@ function Fe(e, n) {
|
|
|
1652
1678
|
e.rxdb.dispatchEvent(new Y(i));
|
|
1653
1679
|
}
|
|
1654
1680
|
}
|
|
1655
|
-
|
|
1681
|
+
function un(e) {
|
|
1682
|
+
if (!e.length) return "";
|
|
1683
|
+
const n = w(B), t = S(n), s = "type, namespace, entity, branchId, transactionId, entityId, remoteId, inversePatch, patch", r = e.map((o) => {
|
|
1684
|
+
const i = T(o.type), c = T(o.namespace), a = T(o.entity), l = T(o.branchId), d = T(o.transactionId ?? null), p = T(o.entityId), u = o.remoteId != null ? o.remoteId : "NULL", f = o.inversePatch != null ? T(JSON.stringify(o.inversePatch)) : "NULL", h = o.patch != null ? T(JSON.stringify(o.patch)) : "NULL";
|
|
1685
|
+
return `(${i},${c},${a},${l},${d},${p},${u},${f},${h})`;
|
|
1686
|
+
});
|
|
1687
|
+
return `INSERT INTO "${t}" (${s}) VALUES ${r.join(`,
|
|
1688
|
+
`)};`;
|
|
1689
|
+
}
|
|
1690
|
+
const we = (e, n, t) => {
|
|
1656
1691
|
let s = "";
|
|
1657
1692
|
return e.rxdb.config.entities.forEach((r) => {
|
|
1658
|
-
const o =
|
|
1693
|
+
const o = w(r);
|
|
1659
1694
|
if (o.log !== !1) {
|
|
1660
1695
|
const i = pe(o, {
|
|
1661
1696
|
branchId: n,
|
|
@@ -1666,17 +1701,17 @@ const Te = (e, n, t) => {
|
|
|
1666
1701
|
}
|
|
1667
1702
|
}), s;
|
|
1668
1703
|
};
|
|
1669
|
-
class
|
|
1704
|
+
class Nn extends Ve {
|
|
1670
1705
|
constructor(n, t) {
|
|
1671
1706
|
super(n), this.options = t, t.repositories && Object.keys(t.repositories).forEach((r) => {
|
|
1672
1707
|
this.repository(r, t.repositories[r]);
|
|
1673
1708
|
});
|
|
1674
|
-
const s =
|
|
1709
|
+
const s = Ze((r) => {
|
|
1675
1710
|
this.#t || Xt(this, r);
|
|
1676
1711
|
});
|
|
1677
1712
|
this.#r().then((r) => {
|
|
1678
1713
|
r.addEventListener(D.SQLITE_INSERT, s), r.addEventListener(D.SQLITE_UPDATE, s), r.addEventListener(D.SQLITE_DELETE, s);
|
|
1679
|
-
}).catch((r) => console.error("[RxDBAdapterSqlite] Failed to register event listeners", r)), this.repository("Repository",
|
|
1714
|
+
}).catch((r) => console.error("[RxDBAdapterSqlite] Failed to register event listeners", r)), this.repository("Repository", qe), this.repository("TreeRepository", tn);
|
|
1680
1715
|
}
|
|
1681
1716
|
// SQLite 客户端缓存
|
|
1682
1717
|
#n;
|
|
@@ -1690,7 +1725,7 @@ class yn extends He {
|
|
|
1690
1725
|
#a = /* @__PURE__ */ new Map();
|
|
1691
1726
|
#o = /* @__PURE__ */ new WeakMap();
|
|
1692
1727
|
// 查询任务队列执行器
|
|
1693
|
-
#c = new
|
|
1728
|
+
#c = new Ae(1);
|
|
1694
1729
|
// sqlite 客户端
|
|
1695
1730
|
#s;
|
|
1696
1731
|
// 是否已断开连接
|
|
@@ -1698,14 +1733,14 @@ class yn extends He {
|
|
|
1698
1733
|
/**
|
|
1699
1734
|
* 适配器名称
|
|
1700
1735
|
*/
|
|
1701
|
-
name =
|
|
1736
|
+
name = qt;
|
|
1702
1737
|
/**
|
|
1703
1738
|
* 获取实体仓库
|
|
1704
1739
|
* @param EntityType 实体类
|
|
1705
1740
|
*/
|
|
1706
1741
|
getRepository(n) {
|
|
1707
1742
|
if (!this.repository_cache.has(n)) {
|
|
1708
|
-
const t =
|
|
1743
|
+
const t = w(n), s = this.repository_map.get(t.repository);
|
|
1709
1744
|
if (!s)
|
|
1710
1745
|
throw new R(`Repository '${t.repository}' not found`);
|
|
1711
1746
|
const r = new s(this, n);
|
|
@@ -1759,15 +1794,15 @@ class yn extends He {
|
|
|
1759
1794
|
* @param options
|
|
1760
1795
|
*/
|
|
1761
1796
|
async mutations(n) {
|
|
1762
|
-
return this.#e ? await
|
|
1797
|
+
return this.#e ? await Te(this, n) : await this.transaction(() => Te(this, n));
|
|
1763
1798
|
}
|
|
1764
1799
|
/**
|
|
1765
1800
|
* 判断表是否存在
|
|
1766
1801
|
* @param EntityType
|
|
1767
1802
|
*/
|
|
1768
1803
|
async isTableExisted(n) {
|
|
1769
|
-
const t =
|
|
1770
|
-
return
|
|
1804
|
+
const t = w(n), s = S(t), { sql: r, params: o } = st(s), i = await this.#i(r, o);
|
|
1805
|
+
return rt(i) === !1;
|
|
1771
1806
|
}
|
|
1772
1807
|
/**
|
|
1773
1808
|
* 创建表和初始化数据
|
|
@@ -1791,14 +1826,16 @@ class yn extends He {
|
|
|
1791
1826
|
* 应用压缩后的变更到本地实体表
|
|
1792
1827
|
*
|
|
1793
1828
|
* 将 actions 转换为 SQL 操作实体表。
|
|
1829
|
+
* 当 disableTriggers=true(pull 场景)且提供了 localChanges 时,
|
|
1830
|
+
* 手动将变更记录插入 RxDBChange 表。
|
|
1794
1831
|
*
|
|
1795
1832
|
* @param actions - 压缩后的变更操作
|
|
1796
|
-
* @param
|
|
1833
|
+
* @param localChanges - 需要记录到 RxDBChange 表的变更(pull 场景)
|
|
1797
1834
|
* @param disableTriggers - 是否禁用触发器(用于 pull 等操作,避免创建 RxDBChange)
|
|
1798
1835
|
*/
|
|
1799
1836
|
async mergeChanges(n, t, s = !1) {
|
|
1800
|
-
const r =
|
|
1801
|
-
await ln(this, r, s);
|
|
1837
|
+
const r = ke(this, n);
|
|
1838
|
+
await ln(this, r, s, t);
|
|
1802
1839
|
}
|
|
1803
1840
|
/**
|
|
1804
1841
|
* 缓存 RowId 与实体映射
|
|
@@ -1853,13 +1890,13 @@ class yn extends He {
|
|
|
1853
1890
|
async transaction(n, t = !0) {
|
|
1854
1891
|
await this.rxdb.connect(this.name);
|
|
1855
1892
|
const s = await this.#r();
|
|
1856
|
-
this.#e = !0, this.rxdb.dispatchEvent(
|
|
1893
|
+
this.#e = !0, this.rxdb.dispatchEvent(dn);
|
|
1857
1894
|
let r = "", o = "";
|
|
1858
1895
|
if (t) {
|
|
1859
|
-
const i =
|
|
1896
|
+
const i = Qe(), c = await this.rxdb.versionManager.getCurrentBranch();
|
|
1860
1897
|
if (!c)
|
|
1861
1898
|
throw new Error("currentBranch is undefined! Cannot start transaction with logging.");
|
|
1862
|
-
r =
|
|
1899
|
+
r = we(this, c.id, i), o = we(this, c.id);
|
|
1863
1900
|
}
|
|
1864
1901
|
await s.execute(`
|
|
1865
1902
|
BEGIN;
|
|
@@ -1869,12 +1906,12 @@ class yn extends He {
|
|
|
1869
1906
|
try {
|
|
1870
1907
|
const i = await n(s);
|
|
1871
1908
|
return await s.execute(`${o}
|
|
1872
|
-
COMMIT;`), this.#e = !1, this.rxdb.dispatchEvent(
|
|
1909
|
+
COMMIT;`), this.#e = !1, this.rxdb.dispatchEvent(fn), i;
|
|
1873
1910
|
} catch (i) {
|
|
1874
1911
|
try {
|
|
1875
1912
|
await s.execute("ROLLBACK");
|
|
1876
1913
|
} finally {
|
|
1877
|
-
this.#e = !1, this.rxdb.dispatchEvent(
|
|
1914
|
+
this.#e = !1, this.rxdb.dispatchEvent(pn);
|
|
1878
1915
|
}
|
|
1879
1916
|
const c = i?.message || "Transaction Error";
|
|
1880
1917
|
throw new R(c, { cause: i });
|
|
@@ -1906,7 +1943,7 @@ COMMIT;`), this.#e = !1, this.rxdb.dispatchEvent(pn), i;
|
|
|
1906
1943
|
* @returns
|
|
1907
1944
|
*/
|
|
1908
1945
|
async getRxDBChangeSequence() {
|
|
1909
|
-
const n =
|
|
1946
|
+
const n = ye(B);
|
|
1910
1947
|
return (await this.#i("SELECT seq FROM sqlite_sequence WHERE name = ?", [n])).results[0]?.rows?.[0]?.[0] ?? 0;
|
|
1911
1948
|
}
|
|
1912
1949
|
/**
|
|
@@ -1914,7 +1951,7 @@ COMMIT;`), this.#e = !1, this.rxdb.dispatchEvent(pn), i;
|
|
|
1914
1951
|
* @param sequence 序列值
|
|
1915
1952
|
*/
|
|
1916
1953
|
async setRxDBChangeSequence(n) {
|
|
1917
|
-
const t =
|
|
1954
|
+
const t = ye(B);
|
|
1918
1955
|
await this.#i("UPDATE sqlite_sequence SET seq = ? WHERE name = ?", [n, t]);
|
|
1919
1956
|
}
|
|
1920
1957
|
// ============================================
|
|
@@ -1941,7 +1978,7 @@ COMMIT;`), this.#e = !1, this.rxdb.dispatchEvent(pn), i;
|
|
|
1941
1978
|
return se(/* @__PURE__ */ new Map());
|
|
1942
1979
|
const s = t.map(() => "?").join(", "), r = `SELECT id, updatedAt FROM "${n}" WHERE id IN (${s})`;
|
|
1943
1980
|
return re(this.internalQuery(r, t)).pipe(
|
|
1944
|
-
|
|
1981
|
+
et((o) => {
|
|
1945
1982
|
const i = /* @__PURE__ */ new Map();
|
|
1946
1983
|
if (o.results?.[0]?.rows)
|
|
1947
1984
|
for (const c of o.results[0].rows)
|
|
@@ -2023,16 +2060,16 @@ COMMIT;`), this.#e = !1, this.rxdb.dispatchEvent(pn), i;
|
|
|
2023
2060
|
return this.#a.has(n) === !1 && this.#a.set(n, /* @__PURE__ */ new Map()), this.#a.get(n);
|
|
2024
2061
|
}
|
|
2025
2062
|
}
|
|
2026
|
-
const
|
|
2063
|
+
const dn = new We(), pn = new Ke(), fn = new He();
|
|
2027
2064
|
export {
|
|
2028
|
-
|
|
2029
|
-
|
|
2065
|
+
x as ROWID,
|
|
2066
|
+
Nn as RxDBAdapterSqlite,
|
|
2030
2067
|
R as RxDBAdapterSqliteError,
|
|
2031
2068
|
Wt as SqliteClient,
|
|
2032
|
-
|
|
2069
|
+
qe as SqliteRepository,
|
|
2033
2070
|
vt as WA_SQLITE_VFS_LIST,
|
|
2034
2071
|
U as buildRuleGroup,
|
|
2035
2072
|
P as sqliteGetTableName,
|
|
2036
|
-
|
|
2073
|
+
S as sqliteGetTableNameByMetadata,
|
|
2037
2074
|
Pt as sqliteLoad
|
|
2038
2075
|
};
|