@aiao/rxdb-adapter-sqlite 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/RxDBAdapterSqlite.d.ts +107 -0
- package/dist/RxDBAdapterSqlite.d.ts.map +1 -0
- package/dist/SqliteClient.d.ts +32 -0
- package/dist/SqliteClient.d.ts.map +1 -0
- package/dist/SqliteRepository.d.ts +27 -0
- package/dist/SqliteRepository.d.ts.map +1 -0
- package/dist/SqliteRepository.interface.d.ts +94 -0
- package/dist/SqliteRepository.interface.d.ts.map +1 -0
- package/dist/SqliteRepositoryBase.d.ts +52 -0
- package/dist/SqliteRepositoryBase.d.ts.map +1 -0
- package/dist/SqliteTreeRepository.d.ts +21 -0
- package/dist/SqliteTreeRepository.d.ts.map +1 -0
- package/dist/SqliteTreeRepository.interface.d.ts +36 -0
- package/dist/SqliteTreeRepository.interface.d.ts.map +1 -0
- package/{execute_statement_helper.d.ts → dist/execute_statement_helper.d.ts} +3 -2
- package/dist/execute_statement_helper.d.ts.map +1 -0
- package/dist/generate_entity_delete_sql.d.ts +10 -0
- package/dist/generate_entity_delete_sql.d.ts.map +1 -0
- package/dist/generate_entity_deletes_sql.d.ts +7 -0
- package/dist/generate_entity_deletes_sql.d.ts.map +1 -0
- package/dist/generate_entity_find_tree_sql.d.ts +58 -0
- package/dist/generate_entity_find_tree_sql.d.ts.map +1 -0
- package/dist/generate_entity_insert_sql.d.ts +10 -0
- package/dist/generate_entity_insert_sql.d.ts.map +1 -0
- package/dist/generate_entity_inserts_sql.d.ts +7 -0
- package/dist/generate_entity_inserts_sql.d.ts.map +1 -0
- package/dist/generate_entity_update_sql.d.ts +10 -0
- package/dist/generate_entity_update_sql.d.ts.map +1 -0
- package/dist/generate_query_find_sql.d.ts +32 -0
- package/dist/generate_query_find_sql.d.ts.map +1 -0
- package/{generate_table_create_sql.d.ts → dist/generate_table_create_sql.d.ts} +2 -1
- package/dist/generate_table_create_sql.d.ts.map +1 -0
- package/dist/generate_table_trigger_sql.d.ts +9 -0
- package/dist/generate_table_trigger_sql.d.ts.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1698 -0
- package/dist/merge_create_cache.d.ts +6 -0
- package/dist/merge_create_cache.d.ts.map +1 -0
- package/dist/merge_remove_cache.d.ts +5 -0
- package/dist/merge_remove_cache.d.ts.map +1 -0
- package/dist/merge_tree_create_cache.d.ts +7 -0
- package/dist/merge_tree_create_cache.d.ts.map +1 -0
- package/dist/merge_tree_remove_cache.d.ts +7 -0
- package/dist/merge_tree_remove_cache.d.ts.map +1 -0
- package/dist/merge_tree_update_cache.d.ts +7 -0
- package/dist/merge_tree_update_cache.d.ts.map +1 -0
- package/dist/merge_update_cache.d.ts +5 -0
- package/dist/merge_update_cache.d.ts.map +1 -0
- package/dist/need_refresh_helper.d.ts +18 -0
- package/dist/need_refresh_helper.d.ts.map +1 -0
- package/dist/need_tree_refresh_helper.d.ts +19 -0
- package/dist/need_tree_refresh_helper.d.ts.map +1 -0
- package/dist/rxdb_adapter_create_branch.d.ts +7 -0
- package/dist/rxdb_adapter_create_branch.d.ts.map +1 -0
- package/dist/rxdb_adapter_create_tables_sql.d.ts +5 -0
- package/dist/rxdb_adapter_create_tables_sql.d.ts.map +1 -0
- package/dist/rxdb_adapter_remove_many.d.ts +8 -0
- package/dist/rxdb_adapter_remove_many.d.ts.map +1 -0
- package/dist/rxdb_adapter_save_many.d.ts +8 -0
- package/dist/rxdb_adapter_save_many.d.ts.map +1 -0
- package/dist/rxdb_adapter_switch_branch.d.ts +4 -0
- package/dist/rxdb_adapter_switch_branch.d.ts.map +1 -0
- package/{sqlite-load.utils.d.ts → dist/sqlite-load.utils.d.ts} +3 -2
- package/dist/sqlite-load.utils.d.ts.map +1 -0
- package/{sqlite.interface.d.ts → dist/sqlite.interface.d.ts} +39 -14
- package/dist/sqlite.interface.d.ts.map +1 -0
- package/{sqlite.utils.d.ts → dist/sqlite.utils.d.ts} +8 -5
- package/dist/sqlite.utils.d.ts.map +1 -0
- package/{wa-sqlite.d.ts → dist/wa-sqlite.interface.d.ts} +1 -7
- package/dist/wa-sqlite.interface.d.ts.map +1 -0
- package/package.json +32 -13
- package/RxDBAdapterSqlite.d.ts +0 -66
- package/SqliteClient.d.ts +0 -19
- package/SqliteRepository.d.ts +0 -23
- package/SqliteRepositoryBase.d.ts +0 -85
- package/SqliteTreeRepository.d.ts +0 -17
- package/generate_entity_delete_sql.d.ts +0 -9
- package/generate_entity_find_tree_sql.d.ts +0 -41
- package/generate_entity_insert_sql.d.ts +0 -9
- package/generate_entity_soft_delete_sql.d.ts +0 -9
- package/generate_entity_update_sql.d.ts +0 -9
- package/generate_query_find_sql.d.ts +0 -30
- package/generate_table_trigger_sql.d.ts +0 -8
- package/index.d.ts +0 -5
- package/index.js +0 -1124
- package/types.d.ts +0 -7
package/dist/index.js
ADDED
|
@@ -0,0 +1,1698 @@
|
|
|
1
|
+
import { RxDBError as We, PropertyType as $, RelationKind as B, getEntityMetadata as R, RxDBChange as Te, getEntityStatus as L, EntityRemoveEvent as Ue, EntityCreateEvent as Ve, EntityUpdateEvent as Pe, RxDBBranch as ie, isEntityMatchWhere as G, isEntityEffectOrderBy as Re, calculateOrderBy as Ne, RepositoryBase as Qe, ENTITY_CREATE_EVENT as se, ENTITY_UPDATE_EVENT as ne, ENTITY_REMOVE_EVENT as P, DATABASE_DELETE as Ge, DATABASE_INSERT as Ke, DATABASE_UPDATE as He, TransactionBeginEvent as Ye, TransactionRollbackEvent as Je, TransactionCommitEvent as Xe, RxDBAdapterLocalBase as ze, uuid as Ze, RxDBAdapterUpdateChangeEvent as et, RxDBAdapterInsertChangeEvent as tt, RxDBAdapterDeleteChangeEvent as st } from "@aiao/rxdb";
|
|
2
|
+
import { proxy as X, wrap as Ee } from "comlink";
|
|
3
|
+
import { firstValueFrom as w, Subject as z, debounceTime as Z, bufferWhen as me, filter as Q, map as g, shareReplay as U, ReplaySubject as re, BehaviorSubject as K, Observable as Ae, takeUntil as Ie, distinctUntilChanged as nt, tap as W, switchMap as m, take as ae, defer as rt, catchError as at, of as ct, from as ye } from "rxjs";
|
|
4
|
+
import { pickBy as ve, isString as ot, isFunction as xe, traverseObjectKeys as it, EventDispatcher as lt, AsyncTaskExecutor as ut, get as ht, once as N } from "@aiao/utils";
|
|
5
|
+
import dt from "object-hash";
|
|
6
|
+
import { Factory as _t, SQLITE_ROW as ft, SQLITE_UTF8 as Oe, SQLITE_DETERMINISTIC as Se } from "wa-sqlite";
|
|
7
|
+
const pt = (s) => `SELECT * FROM sqlite_master WHERE type='table' AND name='${s}' limit 1;`, Et = (s) => !!s.sql && (s.results.length === 0 || s.results[0].rows.length === 0), O = "_rowid", mt = (s) => {
|
|
8
|
+
switch (s.type) {
|
|
9
|
+
case $.uuid:
|
|
10
|
+
case $.string:
|
|
11
|
+
case $.json:
|
|
12
|
+
return "TEXT";
|
|
13
|
+
case $.number:
|
|
14
|
+
return "REAL";
|
|
15
|
+
case $.integer:
|
|
16
|
+
case $.boolean:
|
|
17
|
+
return "INTEGER";
|
|
18
|
+
default:
|
|
19
|
+
return "TEXT";
|
|
20
|
+
}
|
|
21
|
+
}, yt = (s, e) => {
|
|
22
|
+
if (s === null && e.nullable) return null;
|
|
23
|
+
if (s !== void 0)
|
|
24
|
+
switch (e.type) {
|
|
25
|
+
case $.boolean:
|
|
26
|
+
return s ? 1 : 0;
|
|
27
|
+
case $.date:
|
|
28
|
+
return s instanceof Date ? s.toISOString() : s;
|
|
29
|
+
default:
|
|
30
|
+
return s;
|
|
31
|
+
}
|
|
32
|
+
}, bt = (s, e) => {
|
|
33
|
+
if (s === null && e.nullable) return null;
|
|
34
|
+
if (s !== void 0)
|
|
35
|
+
switch (e.type) {
|
|
36
|
+
case $.boolean:
|
|
37
|
+
return !!s;
|
|
38
|
+
case $.date:
|
|
39
|
+
return new Date(s);
|
|
40
|
+
case $.json:
|
|
41
|
+
return s ? JSON.parse(s) : null;
|
|
42
|
+
default:
|
|
43
|
+
return s;
|
|
44
|
+
}
|
|
45
|
+
}, qe = (s, e) => `${e}$${s}`, b = (s) => qe(s.name, s.namespace), Be = (s, e) => `idx_${s.name}_${e.name}`;
|
|
46
|
+
class T extends Error {
|
|
47
|
+
constructor(e) {
|
|
48
|
+
super(e), this.name = "RxDBError", Object.setPrototypeOf(this, We.prototype);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const le = (s, e) => {
|
|
52
|
+
const n = {};
|
|
53
|
+
return Object.keys(e).forEach((t) => {
|
|
54
|
+
t.endsWith("Id") && (n[t] = e[t]);
|
|
55
|
+
const r = s.propertyMap.get(t);
|
|
56
|
+
r && (n[t] = yt(e[t], r));
|
|
57
|
+
}), n;
|
|
58
|
+
}, ke = (s, e) => ve(e, (n, t) => s.propertyMap.has(t) || s.foreignKeyNames.includes(t)), wt = (s, e) => ve(e, (n, t) => s.propertyMap.get(t)?.readonly !== !0), Le = (s) => ot(s) ? `'${s}'` : s ?? "NULL", ee = (s, e = []) => {
|
|
59
|
+
for (; s.includes("?"); ) {
|
|
60
|
+
const n = Le(e.shift());
|
|
61
|
+
s = s.replace("?", n);
|
|
62
|
+
}
|
|
63
|
+
return s;
|
|
64
|
+
}, ce = (s, e, n) => {
|
|
65
|
+
const t = {};
|
|
66
|
+
return n.forEach((r, a) => {
|
|
67
|
+
const c = e[a];
|
|
68
|
+
if (s.isForeignKey(c))
|
|
69
|
+
t[c] = r;
|
|
70
|
+
else {
|
|
71
|
+
const o = s.propertyMap.get(c);
|
|
72
|
+
o && (t[c] = bt(r, o));
|
|
73
|
+
}
|
|
74
|
+
}), t;
|
|
75
|
+
}, gt = async (s, e, n) => {
|
|
76
|
+
const t = s.localRxDBBranch(), r = s.localRxDBChange();
|
|
77
|
+
let a = !1;
|
|
78
|
+
try {
|
|
79
|
+
await w(t.get(e)), a = !0;
|
|
80
|
+
} catch {
|
|
81
|
+
}
|
|
82
|
+
if (a) throw new T(`Branch ID (${e}) already exists`);
|
|
83
|
+
let c;
|
|
84
|
+
if (n ? c = await w(
|
|
85
|
+
t.findOne({
|
|
86
|
+
where: {
|
|
87
|
+
combinator: "and",
|
|
88
|
+
rules: [{ field: "changes.id", operator: "=", value: n }]
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
) : c = await w(
|
|
92
|
+
t.findOne({
|
|
93
|
+
where: {
|
|
94
|
+
combinator: "and",
|
|
95
|
+
rules: [{ field: "activated", operator: "=", value: !0 }]
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
), !c) throw new T("Source branch not found");
|
|
99
|
+
n || (n = (await w(
|
|
100
|
+
r.findOne({
|
|
101
|
+
where: {
|
|
102
|
+
combinator: "and",
|
|
103
|
+
rules: [
|
|
104
|
+
{ field: "branchId", operator: "=", value: c.id },
|
|
105
|
+
{
|
|
106
|
+
field: "revertChangedAt",
|
|
107
|
+
operator: "=",
|
|
108
|
+
value: null
|
|
109
|
+
}
|
|
110
|
+
]
|
|
111
|
+
},
|
|
112
|
+
orderBy: [{ field: "id", sort: "desc" }]
|
|
113
|
+
})
|
|
114
|
+
))?.id), console.log("创建分支", {
|
|
115
|
+
fromBranch: c,
|
|
116
|
+
fromChangeId: n,
|
|
117
|
+
newBranchId: e
|
|
118
|
+
});
|
|
119
|
+
}, Me = (s, e, n) => {
|
|
120
|
+
const t = b(s), r = n?.userId && s.propertyMap.has("createdBy"), a = n?.userId && s.propertyMap.has("updatedBy"), c = [...s.propertyMap.keys(), ...s.foreignKeyNames], o = [];
|
|
121
|
+
e.forEach((u) => {
|
|
122
|
+
const l = ke(s, u);
|
|
123
|
+
r && (l.createdBy = n.userId), a && (l.updatedBy = n.userId);
|
|
124
|
+
const h = le(s, l), d = [];
|
|
125
|
+
c.forEach((f) => {
|
|
126
|
+
const p = Le(h[f]);
|
|
127
|
+
d.push(p);
|
|
128
|
+
}), o.push(d);
|
|
129
|
+
});
|
|
130
|
+
const i = o.map((u) => `(${u.join(",")})`).join(",");
|
|
131
|
+
return `INSERT INTO "${t}" (${c.join(",")}) VALUES ${i};`;
|
|
132
|
+
}, $t = (s, e) => {
|
|
133
|
+
let n = "";
|
|
134
|
+
const t = b(e);
|
|
135
|
+
return Array.from(e.relationMap.values()).forEach((r) => {
|
|
136
|
+
switch (r.kind) {
|
|
137
|
+
case B.ONE_TO_ONE:
|
|
138
|
+
case B.MANY_TO_ONE:
|
|
139
|
+
{
|
|
140
|
+
const a = `${r.name}Id`;
|
|
141
|
+
if (n += `
|
|
142
|
+
ALTER TABLE "${t}" ADD COLUMN "${a}" TEXT`, r.nullable || (n += " NOT NULL"), r.kind === B.MANY_TO_ONE && Reflect.get(r, "default") !== void 0) {
|
|
143
|
+
const c = r;
|
|
144
|
+
let o = c.default;
|
|
145
|
+
xe(c.default) && (o = c.default()), n += ` DEFAULT ${o}`;
|
|
146
|
+
}
|
|
147
|
+
if (r.mappedEntity) {
|
|
148
|
+
const c = qe(r.mappedEntity, r.mappedNamespace);
|
|
149
|
+
n += ` REFERENCES ${c}(id)`;
|
|
150
|
+
}
|
|
151
|
+
r.kind === B.MANY_TO_ONE && (n += " ON DELETE CASCADE"), n += ";", (r.unique || r.kind === B.ONE_TO_ONE) && (n += `
|
|
152
|
+
CREATE UNIQUE INDEX ${Be(e, r)} on "${t}"(${a});`);
|
|
153
|
+
}
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}), n;
|
|
157
|
+
}, Tt = (s, e) => {
|
|
158
|
+
const n = b(e);
|
|
159
|
+
let t = `CREATE TABLE "${n}" (`;
|
|
160
|
+
const r = [], a = [], c = [];
|
|
161
|
+
e.propertyMap.forEach((i) => {
|
|
162
|
+
let u = `"${i.name}" ${mt(i)}`;
|
|
163
|
+
if (i.type === $.integer && i.primary ? u += " PRIMARY KEY AUTOINCREMENT" : (i.type === $.uuid || i.type === $.string) && i.primary && (u += " PRIMARY KEY"), !i.nullable) u += " NOT NULL";
|
|
164
|
+
else if (Reflect.get(i, "default") !== void 0) {
|
|
165
|
+
let l = i.default;
|
|
166
|
+
xe(i.default) && (l = i.default()), l === "CURRENT_TIMESTAMP" ? u += " DEFAULT(strftime('%FT%H:%M:%fZ'))" : u += ` DEFAULT ${String(l)}`;
|
|
167
|
+
}
|
|
168
|
+
switch (i.type) {
|
|
169
|
+
case $.json:
|
|
170
|
+
a.push(`CHECK ( JSON_VALID(${i.name})=1 )`);
|
|
171
|
+
break;
|
|
172
|
+
case $.boolean:
|
|
173
|
+
a.push(`CHECK (${i.name} in(0,1))`);
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
i.unique && r.push(
|
|
177
|
+
`CREATE UNIQUE INDEX ${Be(e, i)} on "${n}"(${i.name});`
|
|
178
|
+
), c.push(u);
|
|
179
|
+
});
|
|
180
|
+
const o = [...c, ...a];
|
|
181
|
+
if (o.length)
|
|
182
|
+
t += o.map((i) => `
|
|
183
|
+
${i}`).join(","), t += `
|
|
184
|
+
);`;
|
|
185
|
+
else
|
|
186
|
+
throw new T("columns is empty!");
|
|
187
|
+
return r.length && (t += `
|
|
188
|
+
` + r.join(`
|
|
189
|
+
`)), t;
|
|
190
|
+
}, Rt = (s, e) => {
|
|
191
|
+
let n = "";
|
|
192
|
+
return n += Tt(s, e), n += $t(s, e), n;
|
|
193
|
+
}, ue = (s, e, n = "main", t = null) => {
|
|
194
|
+
const r = b(e), a = R(Te), c = b(a), { propertyMap: o, name: i, foreignKeyNames: u, namespace: l } = e, h = [...o.keys(), ...u].filter((_) => ["id", "updatedAt"].includes(_) === !1), d = "namespace, entity, type, branchId, parentId, entityId, inversePatch, patch", f = t !== null && t >= 0 ? t : "NULL", p = `
|
|
195
|
+
DROP TRIGGER IF EXISTS ${r}_insert;
|
|
196
|
+
CREATE TRIGGER ${r}_insert AFTER INSERT ON "${r}"
|
|
197
|
+
BEGIN
|
|
198
|
+
INSERT INTO ${c} (${d}) VALUES (
|
|
199
|
+
'${l}', '${i}', 'INSERT', '${n}', ${f}, NEW.id, NULL,
|
|
200
|
+
json_object(${h.map((_) => `'${_}', NEW.${_}`).join(`,
|
|
201
|
+
`)})
|
|
202
|
+
);
|
|
203
|
+
END;`, E = `
|
|
204
|
+
DROP TRIGGER IF EXISTS ${r}_update;
|
|
205
|
+
CREATE TRIGGER ${r}_update AFTER UPDATE ON "${r}"
|
|
206
|
+
WHEN ( ${h.map((_) => `OLD.${_} IS NOT NEW.${_}`).join(` OR
|
|
207
|
+
`)} )
|
|
208
|
+
BEGIN
|
|
209
|
+
INSERT INTO ${c} (${d}) VALUES (
|
|
210
|
+
'${l}','${i}','UPDATE', '${n}', ${f}, NEW.id,
|
|
211
|
+
(
|
|
212
|
+
SELECT json_group_object(key, value) FROM (
|
|
213
|
+
${h.map((_) => `SELECT '${_}' AS key, OLD.${_} AS value WHERE OLD.${_} IS NOT NEW.${_}`).join(`
|
|
214
|
+
UNION `)}
|
|
215
|
+
)
|
|
216
|
+
),
|
|
217
|
+
(
|
|
218
|
+
SELECT json_group_object(key, value) FROM (
|
|
219
|
+
${h.map((_) => `SELECT '${_}' AS key, NEW.${_} AS value WHERE OLD.${_} IS NOT NEW.${_}`).join(`
|
|
220
|
+
UNION `)}
|
|
221
|
+
)
|
|
222
|
+
)
|
|
223
|
+
);
|
|
224
|
+
END;`, A = `
|
|
225
|
+
DROP TRIGGER IF EXISTS ${r}_delete;
|
|
226
|
+
CREATE TRIGGER ${r}_delete AFTER DELETE ON "${r}"
|
|
227
|
+
BEGIN
|
|
228
|
+
INSERT INTO ${c} (${d}) VALUES (
|
|
229
|
+
'${l}', '${i}', 'DELETE', '${n}', ${f}, OLD.id,
|
|
230
|
+
json_object(${h.map((_) => `'${_}', OLD.${_}`).join(`,
|
|
231
|
+
`)}), NULL
|
|
232
|
+
);
|
|
233
|
+
END;`;
|
|
234
|
+
return p + `
|
|
235
|
+
` + E + `
|
|
236
|
+
` + A;
|
|
237
|
+
}, Nt = (s, e, n) => {
|
|
238
|
+
let t = "";
|
|
239
|
+
for (let r = 0; r < e.length; r++) {
|
|
240
|
+
const a = e[r], c = R(a);
|
|
241
|
+
if (t += `
|
|
242
|
+
` + Rt(s, c), c.log !== !1) {
|
|
243
|
+
const o = ue(s, c);
|
|
244
|
+
t += `
|
|
245
|
+
` + o;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (n) {
|
|
249
|
+
const r = /* @__PURE__ */ new Map();
|
|
250
|
+
n.forEach((a) => {
|
|
251
|
+
const c = R(a);
|
|
252
|
+
r.has(c) === !1 && r.set(c, /* @__PURE__ */ new Set()), r.get(c).add(a);
|
|
253
|
+
});
|
|
254
|
+
for (const a of r.keys()) {
|
|
255
|
+
const c = Me(
|
|
256
|
+
a,
|
|
257
|
+
Array.from(r.get(a)),
|
|
258
|
+
s.rxdb.context
|
|
259
|
+
);
|
|
260
|
+
t += `
|
|
261
|
+
` + c;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return t;
|
|
265
|
+
}, At = (s, e) => {
|
|
266
|
+
const n = b(s), t = e.map((r) => `'${r.id}'`).join(",");
|
|
267
|
+
return `DELETE FROM "${n}" WHERE id in (${t});`;
|
|
268
|
+
}, be = async (s, e, n) => {
|
|
269
|
+
const t = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map();
|
|
270
|
+
for (let o = 0; o < e.length; o++) {
|
|
271
|
+
const i = e[o];
|
|
272
|
+
if (L(i).local) {
|
|
273
|
+
let l = [];
|
|
274
|
+
t.has(i.constructor) ? l = t.get(i.constructor) : t.set(i.constructor, l), l.push(i);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
let a = "";
|
|
278
|
+
t.forEach((o, i) => {
|
|
279
|
+
const u = R(i), l = At(u, o);
|
|
280
|
+
o.map((d) => d.id).forEach((d) => r.set(d, i)), a += l;
|
|
281
|
+
});
|
|
282
|
+
const c = await w(s.execute(a));
|
|
283
|
+
return console.log(c), t.forEach((o) => {
|
|
284
|
+
s.rxdb.dispatchEvent(new Ue(o));
|
|
285
|
+
}), r.clear(), t.clear(), e.forEach((o) => {
|
|
286
|
+
const i = L(o);
|
|
287
|
+
o.removedAt = /* @__PURE__ */ new Date(), i.origin = structuredClone({ ...o }), i.modified = !1, i.removed = !0;
|
|
288
|
+
}), e;
|
|
289
|
+
}, De = (s, e, n, t) => {
|
|
290
|
+
const r = wt(s, n);
|
|
291
|
+
s.propertyMap.has("updatedAt") && (r.updatedAt = t?.updatedAt ?? /* @__PURE__ */ new Date()), t?.userId && s.propertyMap.has("updatedBy") && (r.updatedBy = t.userId);
|
|
292
|
+
const a = le(s, r), c = Object.keys(r).map((h) => `${h} = ?`).join(","), o = [...Object.values(a)], i = b(s), u = Array.isArray(e) ? e : [e];
|
|
293
|
+
let l = `UPDATE "${i}" SET ${c} WHERE id `;
|
|
294
|
+
return Array.isArray(e) ? l += `in (${u.map((h) => `'${h.id}'`).join(",")})` : (o.push(e.id), l += "= ?"), t?.returning !== !1 ? l += ` RETURNING rowid as ${O}, *;` : l += ";", {
|
|
295
|
+
sql: l,
|
|
296
|
+
params: o
|
|
297
|
+
};
|
|
298
|
+
}, he = (s) => s?.map((e) => `_."${e.field}" ${e.sort}`).join(", "), C = (s, e = /* @__PURE__ */ new Map()) => {
|
|
299
|
+
const n = s.rules.map((t) => we(t) ? C(t, e) : It(t, e)).filter(Boolean);
|
|
300
|
+
return n.length === 0 ? "" : n.length === 1 ? n[0] : `(${n.join(we(s) ? ` ${s.combinator} ` : " ")})`;
|
|
301
|
+
}, It = (s, e) => {
|
|
302
|
+
let n, t;
|
|
303
|
+
if (s?.value === null ? s.operator === "=" ? (n = "", t = "is null") : s.operator === "!=" && (n = "", t = "is not null") : (n = xt(s.operator).toLowerCase(), t = Ot(s)), !t && ["in", "notIn", "between", "notBetween"].includes(s.operator)) return "";
|
|
304
|
+
let r = String(s.field);
|
|
305
|
+
if (e.has(r))
|
|
306
|
+
r = e.get(r);
|
|
307
|
+
else if (r.includes(".")) {
|
|
308
|
+
const a = r.lastIndexOf("."), c = r.slice(0, a), o = r.slice(a + 1);
|
|
309
|
+
r = `'${c}'.${o}`;
|
|
310
|
+
}
|
|
311
|
+
return `${r} ${n} ${t}`.replaceAll(/ +/g, " ").trim();
|
|
312
|
+
}, we = (s) => "rules" in s && "combinator" in s, vt = {
|
|
313
|
+
"=": "=",
|
|
314
|
+
"!=": "!=",
|
|
315
|
+
">": ">",
|
|
316
|
+
">=": ">=",
|
|
317
|
+
"<": "<",
|
|
318
|
+
"<=": "<=",
|
|
319
|
+
in: "IN",
|
|
320
|
+
notIn: "NOT IN",
|
|
321
|
+
between: "BETWEEN",
|
|
322
|
+
notBetween: "NOT BETWEEN",
|
|
323
|
+
contains: "LIKE",
|
|
324
|
+
notContain: "NOT LIKE",
|
|
325
|
+
beginWith: "LIKE",
|
|
326
|
+
notBeginWith: "NOT LIKE",
|
|
327
|
+
endWith: "LIKE",
|
|
328
|
+
notEndWith: "NOT LIKE"
|
|
329
|
+
}, xt = (s) => vt[s] || s, Ot = (s) => {
|
|
330
|
+
switch (s.operator) {
|
|
331
|
+
case "in":
|
|
332
|
+
case "notIn":
|
|
333
|
+
return `(${s.value.map((e) => `'${e}'`).join(", ")})`;
|
|
334
|
+
case "between":
|
|
335
|
+
case "notBetween": {
|
|
336
|
+
const [e, n] = s.value;
|
|
337
|
+
return `'${e}' and '${n}'`;
|
|
338
|
+
}
|
|
339
|
+
case "contains":
|
|
340
|
+
case "notContain":
|
|
341
|
+
return `'%${s.value}%'`;
|
|
342
|
+
case "beginWith":
|
|
343
|
+
case "notBeginWith":
|
|
344
|
+
return `'${s.value}%'`;
|
|
345
|
+
case "endWith":
|
|
346
|
+
case "notEndWith":
|
|
347
|
+
return `'%${s.value}'`;
|
|
348
|
+
}
|
|
349
|
+
return typeof s.value == "boolean" ? s.value ? "1" : "0" : typeof s.value == "string" ? `'${s.value}'` : typeof s.value == "number" ? s.value.toString() : s.value instanceof Date ? `'${s.value.toISOString()}'` : `'${s.value}'`;
|
|
350
|
+
}, H = (s, e, n) => {
|
|
351
|
+
const t = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map(), o = (l) => {
|
|
352
|
+
let h = l, d = 1;
|
|
353
|
+
for (; r.has(h); )
|
|
354
|
+
h = `${l}_${d}`, d++;
|
|
355
|
+
return r.add(h), h;
|
|
356
|
+
}, i = (l) => {
|
|
357
|
+
if (c.has(l))
|
|
358
|
+
return c.get(l);
|
|
359
|
+
const h = l.includes("_") ? l : l.split("_")[0], d = o(h);
|
|
360
|
+
return c.set(l, d), d;
|
|
361
|
+
};
|
|
362
|
+
it(n, (l, h, d) => {
|
|
363
|
+
const f = h;
|
|
364
|
+
if (l !== "field" || f.includes(".") === !1) return;
|
|
365
|
+
const { relations: p, isForeignKey: E, propertyName: A } = s.rxdb.schemaManager.getFieldRelations(
|
|
366
|
+
e,
|
|
367
|
+
h
|
|
368
|
+
);
|
|
369
|
+
if (E) {
|
|
370
|
+
d.field = A;
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
const _ = f.replace(`.${A}`, ""), y = (v) => p.length === 1 ? v.name : `${_}_${v.name}`, k = (v) => (t.has(v) || t.set(v, []), t.get(v));
|
|
374
|
+
p.forEach(({ metadata: v, relation: S }, fe) => {
|
|
375
|
+
const V = s.rxdb.schemaManager.findMappedRelation(v, S);
|
|
376
|
+
if (!V) throw new T("mappedRelation not found");
|
|
377
|
+
const J = () => {
|
|
378
|
+
const q = k(V.metadata), I = y(S), F = i(I);
|
|
379
|
+
return {
|
|
380
|
+
joinArray: q,
|
|
381
|
+
uniqueAlias: F
|
|
382
|
+
};
|
|
383
|
+
};
|
|
384
|
+
let x = "_";
|
|
385
|
+
if (fe > 0) {
|
|
386
|
+
const q = p[fe - 1], I = y(q.relation);
|
|
387
|
+
x = i(I);
|
|
388
|
+
}
|
|
389
|
+
switch (S.kind) {
|
|
390
|
+
case B.ONE_TO_ONE:
|
|
391
|
+
case B.ONE_TO_MANY:
|
|
392
|
+
{
|
|
393
|
+
const { joinArray: q, uniqueAlias: I } = J();
|
|
394
|
+
q.push({
|
|
395
|
+
joinTableName: I,
|
|
396
|
+
on: `'${I}'.${V.relation.name}Id = ${x === "_" ? x : `'${x}'`}.id`
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
break;
|
|
400
|
+
case B.MANY_TO_ONE:
|
|
401
|
+
{
|
|
402
|
+
const { joinArray: q, uniqueAlias: I } = J();
|
|
403
|
+
q.push({
|
|
404
|
+
joinTableName: I,
|
|
405
|
+
on: `'${I}'.id = ${x === "_" ? x : `'${x}'`}.${S.name}Id`
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
break;
|
|
409
|
+
case B.MANY_TO_MANY:
|
|
410
|
+
{
|
|
411
|
+
const q = R(S.junctionEntityType), I = k(q), F = o(`${S.name}_m_n`);
|
|
412
|
+
I.push({
|
|
413
|
+
joinTableName: F,
|
|
414
|
+
on: `'${F}'.${V.relation.name}Id = ${x === "_" ? x : `'${x}'`}.id`
|
|
415
|
+
});
|
|
416
|
+
const { joinArray: Fe, uniqueAlias: pe } = J();
|
|
417
|
+
Fe.push({
|
|
418
|
+
joinTableName: pe,
|
|
419
|
+
on: `'${pe}'.id = '${F}'.${S.name}Id`
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
break;
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
const M = p[p.length - 1];
|
|
426
|
+
if (s.rxdb.schemaManager.findMappedRelation(
|
|
427
|
+
M.metadata,
|
|
428
|
+
M.relation
|
|
429
|
+
)) {
|
|
430
|
+
const v = y(M.relation), S = i(v);
|
|
431
|
+
a.set(f, `'${S}'.${A}`);
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
let u = "";
|
|
435
|
+
for (const [l, h] of t.entries()) {
|
|
436
|
+
const d = b(l);
|
|
437
|
+
for (const f of h)
|
|
438
|
+
u += ` LEFT JOIN "${d}" "${f.joinTableName}" ON ${f.on}`;
|
|
439
|
+
}
|
|
440
|
+
return { joinSQL: u, fieldAliasMap: a };
|
|
441
|
+
}, Y = (s) => {
|
|
442
|
+
const { tableName: e, where: n, limit: t, offset: r, orderBy: a, join: c } = s;
|
|
443
|
+
let o = `SELECT _.rowid as ${O}, _.* FROM '${e}' _`;
|
|
444
|
+
return c && (o += c), n && (o += ` where ${n}`), a && (o += ` order by ${a}`), t && (o += ` limit ${t}`), r && (o += ` offset ${r}`), o += ";", o;
|
|
445
|
+
}, St = (s, e, n) => {
|
|
446
|
+
const t = n.where, { joinSQL: r, fieldAliasMap: a } = H(s, e, t), c = b(e), o = C(t, a), i = he(n?.orderBy), u = n?.limit || 100, l = n?.offset || 0;
|
|
447
|
+
return {
|
|
448
|
+
sql: Y({ tableName: c, where: o, orderBy: i, join: r, limit: u, offset: l }),
|
|
449
|
+
params: []
|
|
450
|
+
};
|
|
451
|
+
}, oe = (s, e, n) => {
|
|
452
|
+
const t = n.where, { joinSQL: r, fieldAliasMap: a } = H(s, e, t), c = b(e), o = C(t, a), i = he(n?.orderBy);
|
|
453
|
+
return {
|
|
454
|
+
sql: Y({ tableName: c, where: o, orderBy: i, join: r }),
|
|
455
|
+
params: []
|
|
456
|
+
};
|
|
457
|
+
}, qt = (s, e, n) => {
|
|
458
|
+
const t = n.where, { joinSQL: r, fieldAliasMap: a } = H(s, e, t), c = b(e), o = C(t, a);
|
|
459
|
+
let i = `SELECT count(_.rowid) as count FROM "${c}" _`;
|
|
460
|
+
return n?.groupBy, r && (i += r), o && (i += ` where ${o}`), {
|
|
461
|
+
sql: i,
|
|
462
|
+
params: []
|
|
463
|
+
};
|
|
464
|
+
}, te = (s, e, n) => {
|
|
465
|
+
const t = n.where, { joinSQL: r, fieldAliasMap: a } = H(s, e, t), c = b(e), o = C(t, a), i = he(n?.orderBy);
|
|
466
|
+
return {
|
|
467
|
+
sql: Y({ tableName: c, where: o, orderBy: i, join: r, limit: 1 }),
|
|
468
|
+
params: []
|
|
469
|
+
};
|
|
470
|
+
}, Bt = (s, e) => {
|
|
471
|
+
const n = `${O} IN (${e.join(",")})`, t = b(s);
|
|
472
|
+
return {
|
|
473
|
+
sql: Y({ tableName: t, where: n }),
|
|
474
|
+
params: []
|
|
475
|
+
};
|
|
476
|
+
}, ge = async (s, e) => {
|
|
477
|
+
const n = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map();
|
|
478
|
+
for (let o = 0; o < e.length; o++) {
|
|
479
|
+
const i = e[o];
|
|
480
|
+
if (L(i).local) {
|
|
481
|
+
let l = [];
|
|
482
|
+
t.has(i.constructor) ? l = t.get(i.constructor) : t.set(i.constructor, l), l.push(i);
|
|
483
|
+
} else {
|
|
484
|
+
let l = [];
|
|
485
|
+
n.has(i.constructor) ? l = n.get(i.constructor) : n.set(i.constructor, l), l.push(i);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
let a = "";
|
|
489
|
+
return n.forEach((o, i) => {
|
|
490
|
+
const u = R(i), l = Me(u, o, s.rxdb.context), h = o.map((E) => E.id);
|
|
491
|
+
h.forEach((E) => r.set(E, i));
|
|
492
|
+
const { sql: d, params: f } = oe(s, u, {
|
|
493
|
+
where: {
|
|
494
|
+
combinator: "and",
|
|
495
|
+
rules: [
|
|
496
|
+
{
|
|
497
|
+
field: "id",
|
|
498
|
+
operator: "in",
|
|
499
|
+
value: h
|
|
500
|
+
}
|
|
501
|
+
]
|
|
502
|
+
}
|
|
503
|
+
}), p = ee(d, f);
|
|
504
|
+
a += l + p;
|
|
505
|
+
}), t.forEach((o, i) => {
|
|
506
|
+
const u = R(i), l = Object.groupBy(o, (h) => {
|
|
507
|
+
const d = L(h);
|
|
508
|
+
return dt(d.patch);
|
|
509
|
+
});
|
|
510
|
+
Object.values(l).forEach((h) => {
|
|
511
|
+
const d = h[0], f = L(d), p = /* @__PURE__ */ new Date(), E = De(u, h, f.patch, {
|
|
512
|
+
...s.rxdb.context,
|
|
513
|
+
returning: !1,
|
|
514
|
+
updatedAt: p
|
|
515
|
+
}), A = ee(E.sql, E.params);
|
|
516
|
+
a += A;
|
|
517
|
+
const _ = h.map((M) => M.id);
|
|
518
|
+
_.forEach((M) => r.set(M, i));
|
|
519
|
+
const y = oe(s, u, {
|
|
520
|
+
where: {
|
|
521
|
+
combinator: "and",
|
|
522
|
+
rules: [
|
|
523
|
+
{
|
|
524
|
+
field: "id",
|
|
525
|
+
operator: "in",
|
|
526
|
+
value: _
|
|
527
|
+
}
|
|
528
|
+
]
|
|
529
|
+
}
|
|
530
|
+
}), k = ee(y.sql, y.params);
|
|
531
|
+
a += k;
|
|
532
|
+
});
|
|
533
|
+
}), (await w(s.execute(a))).results.forEach(({ columns: o, rows: i }) => {
|
|
534
|
+
const u = o.findIndex((d) => d === "id"), l = o.findIndex((d) => d === O), h = s.rxdb.entityManager;
|
|
535
|
+
i.forEach((d) => {
|
|
536
|
+
const f = d[u], p = r.get(f), E = R(p);
|
|
537
|
+
let A;
|
|
538
|
+
if (h.hasEntityRef(p, f)) {
|
|
539
|
+
A = h.getEntityRef(p, f);
|
|
540
|
+
const _ = ce(E, o, d);
|
|
541
|
+
s.getRepository(p).updateEntity(A, _);
|
|
542
|
+
const y = L(A);
|
|
543
|
+
y.local = !0, l !== -1 && (y.rowId = BigInt(d[l])), y.modified = !1;
|
|
544
|
+
}
|
|
545
|
+
});
|
|
546
|
+
}), n.forEach((o) => {
|
|
547
|
+
s.rxdb.dispatchEvent(new Ve(o));
|
|
548
|
+
}), t.forEach((o) => {
|
|
549
|
+
s.rxdb.dispatchEvent(new Pe(o));
|
|
550
|
+
}), r.clear(), n.clear(), t.clear(), e;
|
|
551
|
+
}, kt = (s, e, n) => {
|
|
552
|
+
let t = "";
|
|
553
|
+
if (s.rxdb.options.entities.forEach((r) => {
|
|
554
|
+
const a = R(r);
|
|
555
|
+
if (a.log !== !1) {
|
|
556
|
+
const c = ue(s, a, e, n);
|
|
557
|
+
t += `
|
|
558
|
+
` + c;
|
|
559
|
+
}
|
|
560
|
+
}), t) {
|
|
561
|
+
const r = R(ie), a = b(r);
|
|
562
|
+
t = `
|
|
563
|
+
${t}
|
|
564
|
+
UPDATE "${a}" SET activated = 1 WHERE id = '${e}' and activated != 1 RETURNING rowid as ${O},*;
|
|
565
|
+
UPDATE "${a}" SET activated = 0 WHERE id != '${e}' and activated = 1 RETURNING rowid as ${O},*;
|
|
566
|
+
`;
|
|
567
|
+
}
|
|
568
|
+
return t;
|
|
569
|
+
}, Lt = async (s, e) => {
|
|
570
|
+
const n = s.localRxDBBranch(), t = s.localRxDBChange(), r = await w(
|
|
571
|
+
n.findOne({
|
|
572
|
+
where: {
|
|
573
|
+
combinator: "and",
|
|
574
|
+
rules: [{ field: "activated", operator: "=", value: !0 }]
|
|
575
|
+
}
|
|
576
|
+
})
|
|
577
|
+
);
|
|
578
|
+
if (!r) throw new T("Current branch not found");
|
|
579
|
+
if (r.id === e) return;
|
|
580
|
+
const a = await w(
|
|
581
|
+
t.findOne({
|
|
582
|
+
where: {
|
|
583
|
+
combinator: "and",
|
|
584
|
+
rules: [
|
|
585
|
+
{ field: "branchId", operator: "=", value: r.id },
|
|
586
|
+
{
|
|
587
|
+
field: "revertChangedAt",
|
|
588
|
+
operator: "=",
|
|
589
|
+
value: null
|
|
590
|
+
}
|
|
591
|
+
]
|
|
592
|
+
},
|
|
593
|
+
orderBy: [{ field: "id", sort: "desc" }]
|
|
594
|
+
})
|
|
595
|
+
);
|
|
596
|
+
a && (a.parentId ? console.log("把当前分支的数据切换记录到:", a.parentId) : console.log("不应该出现这个情况,如果为空,应该还原所有记录"));
|
|
597
|
+
const c = await w(
|
|
598
|
+
t.findOne({
|
|
599
|
+
where: {
|
|
600
|
+
combinator: "and",
|
|
601
|
+
rules: [{ field: "branchId", operator: "=", value: e }]
|
|
602
|
+
},
|
|
603
|
+
orderBy: [{ field: "id", sort: "desc" }]
|
|
604
|
+
})
|
|
605
|
+
);
|
|
606
|
+
let o = "";
|
|
607
|
+
if (s.rxdb.options.entities.forEach((i) => {
|
|
608
|
+
const u = R(i);
|
|
609
|
+
if (u.log !== !1) {
|
|
610
|
+
const l = ue(s, u, e, c?.id);
|
|
611
|
+
o += `
|
|
612
|
+
` + l;
|
|
613
|
+
}
|
|
614
|
+
}), o) {
|
|
615
|
+
o = `
|
|
616
|
+
BEGIN;
|
|
617
|
+
${kt(s, e, c?.id)}
|
|
618
|
+
COMMIT;
|
|
619
|
+
`;
|
|
620
|
+
const u = await w(s.execute(o));
|
|
621
|
+
s.getRepository(ie).addQueryCache(u, !0);
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
var D = /* @__PURE__ */ ((s) => (s[s.SQLITE_DELETE = 9] = "SQLITE_DELETE", s[s.SQLITE_INSERT = 18] = "SQLITE_INSERT", s[s.SQLITE_UPDATE = 23] = "SQLITE_UPDATE", s))(D || {});
|
|
625
|
+
const Mt = "sqlite", Dt = () => import("wa-sqlite/dist/wa-sqlite-async.mjs").then((s) => s.default), Ct = () => import("wa-sqlite/dist/wa-sqlite.mjs").then((s) => s.default), jt = [
|
|
626
|
+
{
|
|
627
|
+
name: "MemoryVFS",
|
|
628
|
+
vfsModule: () => import("wa-sqlite/src/examples/MemoryVFS.js").then((s) => s.MemoryVFS),
|
|
629
|
+
sync: !0,
|
|
630
|
+
async: !0,
|
|
631
|
+
worker: !0,
|
|
632
|
+
sharedWorker: !0,
|
|
633
|
+
jsContext: !0,
|
|
634
|
+
multipleConnections: !1
|
|
635
|
+
},
|
|
636
|
+
{
|
|
637
|
+
name: "MemoryAsyncVFS",
|
|
638
|
+
vfsModule: () => import("wa-sqlite/src/examples/MemoryAsyncVFS.js").then((s) => s.MemoryAsyncVFS),
|
|
639
|
+
sync: !1,
|
|
640
|
+
async: !0,
|
|
641
|
+
worker: !0,
|
|
642
|
+
sharedWorker: !0,
|
|
643
|
+
jsContext: !0,
|
|
644
|
+
multipleConnections: !1
|
|
645
|
+
},
|
|
646
|
+
{
|
|
647
|
+
name: "IDBBatchAtomicVFS",
|
|
648
|
+
vfsModule: () => import("wa-sqlite/src/examples/IDBBatchAtomicVFS.js").then((s) => s.IDBBatchAtomicVFS),
|
|
649
|
+
vfsOptions: { lockPolicy: "shared+hint" },
|
|
650
|
+
sync: !1,
|
|
651
|
+
async: !0,
|
|
652
|
+
worker: !0,
|
|
653
|
+
sharedWorker: !0,
|
|
654
|
+
jsContext: !0,
|
|
655
|
+
multipleConnections: !0
|
|
656
|
+
},
|
|
657
|
+
{
|
|
658
|
+
name: "OPFSAdaptiveVFS",
|
|
659
|
+
vfsModule: () => import("wa-sqlite/src/examples/OPFSAdaptiveVFS.js").then((s) => s.OPFSAdaptiveVFS),
|
|
660
|
+
vfsOptions: { lockPolicy: "shared+hint" },
|
|
661
|
+
sync: !1,
|
|
662
|
+
async: !0,
|
|
663
|
+
worker: !0,
|
|
664
|
+
sharedWorker: !1,
|
|
665
|
+
jsContext: !1,
|
|
666
|
+
multipleConnections: !0
|
|
667
|
+
},
|
|
668
|
+
{
|
|
669
|
+
name: "AccessHandlePoolVFS",
|
|
670
|
+
vfsModule: () => import("wa-sqlite/src/examples/AccessHandlePoolVFS.js").then((s) => s.AccessHandlePoolVFS),
|
|
671
|
+
sync: !0,
|
|
672
|
+
async: !0,
|
|
673
|
+
worker: !0,
|
|
674
|
+
sharedWorker: !1,
|
|
675
|
+
jsContext: !1,
|
|
676
|
+
multipleConnections: !1
|
|
677
|
+
},
|
|
678
|
+
{
|
|
679
|
+
name: "OPFSAnyContextVFS",
|
|
680
|
+
vfsModule: () => import("wa-sqlite/src/examples/OPFSAnyContextVFS.js").then((s) => s.OPFSAnyContextVFS),
|
|
681
|
+
vfsOptions: { lockPolicy: "shared+hint" },
|
|
682
|
+
sync: !1,
|
|
683
|
+
async: !0,
|
|
684
|
+
worker: !0,
|
|
685
|
+
sharedWorker: !1,
|
|
686
|
+
jsContext: !0,
|
|
687
|
+
multipleConnections: !0
|
|
688
|
+
},
|
|
689
|
+
{
|
|
690
|
+
name: "OPFSCoopSyncVFS",
|
|
691
|
+
vfsModule: () => import("wa-sqlite/src/examples/OPFSCoopSyncVFS.js").then((s) => s.OPFSCoopSyncVFS),
|
|
692
|
+
sync: !0,
|
|
693
|
+
async: !0,
|
|
694
|
+
worker: !0,
|
|
695
|
+
sharedWorker: !1,
|
|
696
|
+
jsContext: !1,
|
|
697
|
+
multipleConnections: !0
|
|
698
|
+
},
|
|
699
|
+
{
|
|
700
|
+
name: "OPFSPermutedVFS",
|
|
701
|
+
vfsModule: () => import("wa-sqlite/src/examples/OPFSPermutedVFS.js").then((s) => s.OPFSPermutedVFS),
|
|
702
|
+
sync: !1,
|
|
703
|
+
async: !0,
|
|
704
|
+
worker: !0,
|
|
705
|
+
sharedWorker: !1,
|
|
706
|
+
jsContext: !1,
|
|
707
|
+
multipleConnections: !0
|
|
708
|
+
}
|
|
709
|
+
], Ce = (s) => {
|
|
710
|
+
const e = jt.find((a) => a.name === s.vfs), { vfs: n, async: t, worker: r } = s;
|
|
711
|
+
if (!e) throw new T(`vfs ${n} not found`);
|
|
712
|
+
if (t !== t) throw new T(`vfs ${n} not support async: ${t}`);
|
|
713
|
+
if (r && !e.worker) throw new T(`vfs ${n} not support worker`);
|
|
714
|
+
if (!r && !e.jsContext) throw new T(`vfs ${n} only support worker`);
|
|
715
|
+
return e;
|
|
716
|
+
}, Ft = async (s) => {
|
|
717
|
+
const e = Ce(s), n = s.async ?? !0;
|
|
718
|
+
let t;
|
|
719
|
+
n ? t = Dt : t = Ct;
|
|
720
|
+
const [r, a] = await Promise.all([t(), e.vfsModule()]), c = {};
|
|
721
|
+
s.locateFile ? c.locateFile = s.locateFile : s.wasmPath && (c.locateFile = () => s.wasmPath);
|
|
722
|
+
const o = await r(c), i = _t(o), u = await a.create(s.vfs, o, e.vfsOptions);
|
|
723
|
+
return i.vfs_register(u, !0), i;
|
|
724
|
+
}, Wt = async (s, e, n, t) => {
|
|
725
|
+
const r = performance.now(), a = t ? [t] : [[]], c = [];
|
|
726
|
+
let o = 0;
|
|
727
|
+
const i = {
|
|
728
|
+
sql: n,
|
|
729
|
+
results: c
|
|
730
|
+
};
|
|
731
|
+
for await (const u of s.statements(e, n)) {
|
|
732
|
+
if (t)
|
|
733
|
+
for (const d of a)
|
|
734
|
+
s.reset(u), t && s.bind_collection(u, d);
|
|
735
|
+
const l = [];
|
|
736
|
+
for (; await s.step(u) === ft; ) {
|
|
737
|
+
const d = s.row(u);
|
|
738
|
+
l.push(d);
|
|
739
|
+
}
|
|
740
|
+
const h = s.column_names(u);
|
|
741
|
+
h.length && c.push({ columns: h, rows: l }), o += s.changes(e);
|
|
742
|
+
}
|
|
743
|
+
return {
|
|
744
|
+
...i,
|
|
745
|
+
rowsAffected: o,
|
|
746
|
+
elapsed: performance.now() - r
|
|
747
|
+
};
|
|
748
|
+
}, Ut = (s, e) => s.create_function(e, "regexp", 2, Oe | Se, 0, (n, t) => {
|
|
749
|
+
const r = new RegExp(s.value_text(t[0])), a = s.value_text(t[1]);
|
|
750
|
+
s.result(n, r.test(a) ? 1 : 0);
|
|
751
|
+
}), Vt = (s, e) => s.create_function(e, "regexp_replace", -1, Oe | Se, 0, (n, t) => {
|
|
752
|
+
if (t.length < 3) {
|
|
753
|
+
s.result(n, "");
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
const r = s.value_text(t[0]), a = s.value_text(t[1]), c = s.value_text(t[2]), o = t.length > 3 ? s.value_text(t[3]) : "";
|
|
757
|
+
s.result(n, a.replace(new RegExp(r, o), c));
|
|
758
|
+
});
|
|
759
|
+
class Pt extends lt {
|
|
760
|
+
#e;
|
|
761
|
+
#t;
|
|
762
|
+
#n = [];
|
|
763
|
+
#s;
|
|
764
|
+
#a;
|
|
765
|
+
#r = !1;
|
|
766
|
+
async init(e, n) {
|
|
767
|
+
if (this.#r) return;
|
|
768
|
+
this.#r = !0, this.#a = new ut(1);
|
|
769
|
+
const t = await Ft(n), r = e + ".sqlite";
|
|
770
|
+
this.#t = await t.open_v2(r), this.#e = t, Ut(t, this.#t), Vt(t, this.#t);
|
|
771
|
+
const a = n?.cacheSizeKb || 50 * 1024;
|
|
772
|
+
await this.execute(`
|
|
773
|
+
PRAGMA temp_store = memory;
|
|
774
|
+
PRAGMA journal_mode = delete;
|
|
775
|
+
PRAGMA foreign_keys = ON;
|
|
776
|
+
PRAGMA cache_size = -${a};
|
|
777
|
+
`), this.#e.update_hook(
|
|
778
|
+
this.#t,
|
|
779
|
+
(c, o, i, u) => {
|
|
780
|
+
if (console.log("change"), i !== "public$RxDBChange" || !o || !i) return;
|
|
781
|
+
const l = {
|
|
782
|
+
type: c,
|
|
783
|
+
dbName: o,
|
|
784
|
+
tableName: i,
|
|
785
|
+
rowId: u
|
|
786
|
+
};
|
|
787
|
+
this.#n.push(l), this.#s && clearTimeout(this.#s), this.#s = setTimeout(() => {
|
|
788
|
+
this.#o(), this.#s = void 0;
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
);
|
|
792
|
+
}
|
|
793
|
+
async version() {
|
|
794
|
+
const e = await this.execute("SELECT sqlite_version()");
|
|
795
|
+
return ht(e, "results[0].rows[0][0]");
|
|
796
|
+
}
|
|
797
|
+
async disconnect() {
|
|
798
|
+
await this.#e.close(this.#t);
|
|
799
|
+
}
|
|
800
|
+
/**
|
|
801
|
+
* 执行 sql
|
|
802
|
+
* @param sql
|
|
803
|
+
* @param bindings
|
|
804
|
+
* @returns
|
|
805
|
+
*/
|
|
806
|
+
async execute(e, n) {
|
|
807
|
+
return this.#a.addTask(() => Wt(this.#e, this.#t, e, n));
|
|
808
|
+
}
|
|
809
|
+
#o() {
|
|
810
|
+
const e = Object.groupBy(this.#n, (n) => `${n.type}_${n.dbName}_${n.tableName}`);
|
|
811
|
+
Object.values(e).forEach((n) => {
|
|
812
|
+
const t = n[0], r = n.map((c) => c.rowId), a = {
|
|
813
|
+
type: t.type,
|
|
814
|
+
dbName: t.dbName,
|
|
815
|
+
tableName: t.tableName,
|
|
816
|
+
rowIds: r,
|
|
817
|
+
recordAt: /* @__PURE__ */ new Date()
|
|
818
|
+
};
|
|
819
|
+
this.dispatchEvent(a);
|
|
820
|
+
}), this.#n.length = 0;
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
const Qt = (s, e, n) => {
|
|
824
|
+
const t = b(s), r = [e.id];
|
|
825
|
+
return {
|
|
826
|
+
sql: `DELETE FROM "${t}" WHERE id = ?;`,
|
|
827
|
+
params: r
|
|
828
|
+
};
|
|
829
|
+
}, Gt = (s, e, n) => {
|
|
830
|
+
const t = b(s), r = ke(s, e);
|
|
831
|
+
n?.userId && (s.propertyMap.has("createdBy") && (r.createdBy = n.userId), s.propertyMap.has("updatedBy") && (r.updatedBy = n.userId));
|
|
832
|
+
const a = Object.keys(r), c = le(s, r), o = Array(a.length).fill("?").join(","), i = Object.values(c);
|
|
833
|
+
let u = `INSERT INTO "${t}" (${a.join(",")}) VALUES (${o})`;
|
|
834
|
+
return n?.returning !== !1 ? u += ` RETURNING rowid as ${O}, *;` : u += ";", {
|
|
835
|
+
sql: u,
|
|
836
|
+
params: i
|
|
837
|
+
};
|
|
838
|
+
}, de = (s, e, n, t, r) => {
|
|
839
|
+
if (s.length === 0 && e.length === 0) return { refresh: !1, recalculate: !1 };
|
|
840
|
+
const { options: a } = n, c = t.resultEntityIds, o = N(() => r.map((_) => _.id)), i = N(() => r.some((_) => G(_, a.where))), u = N(
|
|
841
|
+
() => a.orderBy && r.some(
|
|
842
|
+
(_) => Re(_, t.resultEntities || [], a.orderBy)
|
|
843
|
+
)
|
|
844
|
+
), l = N(() => u() !== !1), h = N(() => i() === !1), d = N(
|
|
845
|
+
() => c ? c.some((_) => o().includes(_)) : !0
|
|
846
|
+
), f = N(() => d() === !1), p = {
|
|
847
|
+
match_where: i,
|
|
848
|
+
not_match_where: h,
|
|
849
|
+
match_order_by: l,
|
|
850
|
+
result_contains: d,
|
|
851
|
+
result_not_contains: f
|
|
852
|
+
}, E = e.some(
|
|
853
|
+
(_) => _.every((y) => p[y]())
|
|
854
|
+
);
|
|
855
|
+
return {
|
|
856
|
+
refresh: E === !1 && s.some(
|
|
857
|
+
(_) => _.every((y) => p[y]())
|
|
858
|
+
),
|
|
859
|
+
recalculate: E
|
|
860
|
+
};
|
|
861
|
+
}, Kt = (s, e, n) => {
|
|
862
|
+
switch (e.type) {
|
|
863
|
+
case "findAll":
|
|
864
|
+
{
|
|
865
|
+
const { options: t } = e;
|
|
866
|
+
let r = s.resultEntities;
|
|
867
|
+
r = r.concat(n), r = r.filter((a) => G(a, e.options.where)), t.orderBy && t.orderBy.length && (r = Ne(r, t.orderBy)), s.updateResult(r), e.refresh();
|
|
868
|
+
}
|
|
869
|
+
break;
|
|
870
|
+
}
|
|
871
|
+
}, Ht = (s, e, n) => {
|
|
872
|
+
const t = [], r = [];
|
|
873
|
+
switch (e.type) {
|
|
874
|
+
case "find":
|
|
875
|
+
t.push(["match_where"]);
|
|
876
|
+
break;
|
|
877
|
+
case "findAll":
|
|
878
|
+
r.push(["match_where"]);
|
|
879
|
+
break;
|
|
880
|
+
case "findOne":
|
|
881
|
+
case "findOneOrFail":
|
|
882
|
+
case "count":
|
|
883
|
+
t.push(["match_where"]);
|
|
884
|
+
break;
|
|
885
|
+
}
|
|
886
|
+
const a = de(t, r, e, s, n);
|
|
887
|
+
a.refresh ? s.refresh() : a.recalculate && Kt(s, e, n);
|
|
888
|
+
}, Yt = (s, e, n) => {
|
|
889
|
+
if (Array.isArray(s.result)) {
|
|
890
|
+
const t = s.result.filter((r) => !n.includes(r));
|
|
891
|
+
s.updateResult(t), e.refresh();
|
|
892
|
+
}
|
|
893
|
+
}, Jt = (s, e, n) => {
|
|
894
|
+
const t = [], r = [];
|
|
895
|
+
switch (e.type) {
|
|
896
|
+
case "find":
|
|
897
|
+
t.push(["match_where"], ["not_match_where", "result_contains"]);
|
|
898
|
+
break;
|
|
899
|
+
case "findAll":
|
|
900
|
+
r.push(["result_contains"]);
|
|
901
|
+
break;
|
|
902
|
+
case "findOne":
|
|
903
|
+
case "findOneOrFail":
|
|
904
|
+
case "count":
|
|
905
|
+
t.push(["match_where"]);
|
|
906
|
+
break;
|
|
907
|
+
}
|
|
908
|
+
const a = de(t, r, e, s, n);
|
|
909
|
+
a.refresh ? s.refresh() : a.recalculate && Yt(s, e, n);
|
|
910
|
+
}, Xt = (s, e) => {
|
|
911
|
+
switch (e.type) {
|
|
912
|
+
case "findOne":
|
|
913
|
+
case "findOneOrFail":
|
|
914
|
+
case "find":
|
|
915
|
+
case "findAll": {
|
|
916
|
+
let n = s.resultEntities;
|
|
917
|
+
n = n.filter((t) => G(t, e.options.where)), e.options.orderBy && e.options.orderBy.length && (n = Ne(n, e.options.orderBy)), s.updateResult(n), e.refresh();
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
}, zt = (s, e, n) => {
|
|
922
|
+
const t = [], r = [];
|
|
923
|
+
switch (e.type) {
|
|
924
|
+
case "find":
|
|
925
|
+
t.push(["match_where"], ["not_match_where", "result_contains"]);
|
|
926
|
+
break;
|
|
927
|
+
case "findAll":
|
|
928
|
+
r.push(["match_where"], ["not_match_where", "result_contains"]);
|
|
929
|
+
break;
|
|
930
|
+
case "findOne":
|
|
931
|
+
case "findOneOrFail":
|
|
932
|
+
case "count":
|
|
933
|
+
t.push(["match_where"], ["not_match_where", "result_contains"]);
|
|
934
|
+
break;
|
|
935
|
+
}
|
|
936
|
+
const a = de(t, r, e, s, n);
|
|
937
|
+
a.refresh ? s.refresh() : a.recalculate && Xt(s, e);
|
|
938
|
+
}, $e = (s, e = []) => s + e.join(",");
|
|
939
|
+
class Zt extends Qe {
|
|
940
|
+
constructor(e, n, t) {
|
|
941
|
+
super(e.rxdb, t), this.adapter = e, this.client$ = n, this.metadata = R(t), this.run_task$.subscribe(() => this.#e());
|
|
942
|
+
const r = (c) => {
|
|
943
|
+
const o = c.entities.filter((i) => i.constructor === t);
|
|
944
|
+
o.length && this.db_changes_sub.next({ ...c, entities: o });
|
|
945
|
+
};
|
|
946
|
+
this.rxdb.addEventListener(se, (c) => r(c)), this.rxdb.addEventListener(ne, (c) => r(c)), this.rxdb.addEventListener(P, (c) => r(c));
|
|
947
|
+
const a = (c, o) => {
|
|
948
|
+
if (o.EntityType !== t || this.cache_query_task_map.size === 0) return;
|
|
949
|
+
const { rowIds: i, recordAt: u } = o.data, l = [], h = [];
|
|
950
|
+
if (i.forEach((f) => {
|
|
951
|
+
const p = this.rxdb.entityManager.getEntityByRowId(t, f);
|
|
952
|
+
p ? l.push(p) : h.push(f);
|
|
953
|
+
}), c === P && l.length) {
|
|
954
|
+
this.db_changes_sub.next({ type: c, entities: l });
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
h.length && this.db_checks_sub.next({ type: c, changes: o.data }), l.filter(
|
|
958
|
+
(f) => f.updatedAt && f.updatedAt.getTime && u.getTime() - f.updatedAt.getTime() > 500
|
|
959
|
+
).length && this.db_checks_sub.next({ type: c, changes: o.data });
|
|
960
|
+
};
|
|
961
|
+
this.rxdb.addEventListener(Ge, (c) => a(P, c)), this.rxdb.addEventListener(Ke, (c) => a(se, c)), this.rxdb.addEventListener(He, (c) => a(ne, c));
|
|
962
|
+
}
|
|
963
|
+
metadata;
|
|
964
|
+
// 任务队列
|
|
965
|
+
need_run_task = new z();
|
|
966
|
+
run_task$ = this.need_run_task.asObservable().pipe(Z(0));
|
|
967
|
+
// 根据条件缓存的任务
|
|
968
|
+
cache_query_task_map = /* @__PURE__ */ new Map();
|
|
969
|
+
// 任务队列
|
|
970
|
+
queue_tasks = [];
|
|
971
|
+
// db changes
|
|
972
|
+
db_changes_sub = new z();
|
|
973
|
+
db_changes_next$ = this.db_changes_sub.asObservable().pipe(Z(16));
|
|
974
|
+
db_changes$ = this.db_changes_sub.asObservable().pipe(
|
|
975
|
+
me(() => this.db_changes_next$),
|
|
976
|
+
Q((e) => e.length > 0),
|
|
977
|
+
g((e) => {
|
|
978
|
+
const n = /* @__PURE__ */ new Map();
|
|
979
|
+
e.forEach(({ type: r, entities: a }) => {
|
|
980
|
+
n.has(r) || n.set(r, /* @__PURE__ */ new Set()), a.forEach((c) => n.get(r).add(c));
|
|
981
|
+
});
|
|
982
|
+
const t = Array.from(n.entries()).map(([r, a]) => ({
|
|
983
|
+
type: r,
|
|
984
|
+
entities: Array.from(a)
|
|
985
|
+
}));
|
|
986
|
+
return n.clear(), t;
|
|
987
|
+
}),
|
|
988
|
+
U(1)
|
|
989
|
+
);
|
|
990
|
+
// db checks
|
|
991
|
+
db_checks_sub = new z();
|
|
992
|
+
db_checks_next$ = this.db_checks_sub.asObservable().pipe(Z(16));
|
|
993
|
+
db_checks$ = this.db_checks_sub.asObservable().pipe(
|
|
994
|
+
me(() => this.db_checks_next$),
|
|
995
|
+
Q((e) => e.length > 0),
|
|
996
|
+
g((e) => {
|
|
997
|
+
const n = /* @__PURE__ */ new Map();
|
|
998
|
+
return e.forEach(({ type: t, changes: r }) => {
|
|
999
|
+
n.has(t) || n.set(t, /* @__PURE__ */ new Set());
|
|
1000
|
+
const a = n.get(t);
|
|
1001
|
+
r.rowIds.forEach((c) => a.add(c));
|
|
1002
|
+
}), Array.from(n.entries()).map(([t, r]) => ({
|
|
1003
|
+
type: t,
|
|
1004
|
+
rowIds: Array.from(r)
|
|
1005
|
+
}));
|
|
1006
|
+
}),
|
|
1007
|
+
U(1)
|
|
1008
|
+
);
|
|
1009
|
+
/**
|
|
1010
|
+
* 添加缓存
|
|
1011
|
+
* @param sqliteSuccessResult
|
|
1012
|
+
* @param forcedUpdate 强制刷新,在数据有的情况下也会更新数据,在修改数据的情况下需要
|
|
1013
|
+
*/
|
|
1014
|
+
addQueryCache(e, n = !1) {
|
|
1015
|
+
const t = [];
|
|
1016
|
+
return e.results.forEach(({ columns: r, rows: a }) => {
|
|
1017
|
+
const c = r.findIndex((i) => i === "id"), o = r.findIndex((i) => i === O);
|
|
1018
|
+
a.forEach((i) => {
|
|
1019
|
+
const u = i[c];
|
|
1020
|
+
let l;
|
|
1021
|
+
if (this.hasEntityRef(u)) {
|
|
1022
|
+
if (l = this.getEntityRef(u), t.push(l), n) {
|
|
1023
|
+
const d = ce(this.metadata, r, i);
|
|
1024
|
+
this.updateEntity(l, d);
|
|
1025
|
+
}
|
|
1026
|
+
} else {
|
|
1027
|
+
const d = ce(this.metadata, r, i);
|
|
1028
|
+
l = this.createEntityRef(d), t.push(l);
|
|
1029
|
+
}
|
|
1030
|
+
const h = L(l);
|
|
1031
|
+
h.local = !0, o !== -1 && (h.rowId = BigInt(i[o])), h.modified = !1;
|
|
1032
|
+
});
|
|
1033
|
+
}), t;
|
|
1034
|
+
}
|
|
1035
|
+
/**
|
|
1036
|
+
* 创建查询任务
|
|
1037
|
+
*/
|
|
1038
|
+
create_query_task(e) {
|
|
1039
|
+
const { sql: n, params: t } = e, r = $e(n, t), a = /* @__PURE__ */ new Set();
|
|
1040
|
+
let c = !1;
|
|
1041
|
+
const o = new re(), i = o.asObservable(), u = new K(0), l = u.asObservable();
|
|
1042
|
+
let h;
|
|
1043
|
+
if (this.cache_query_task_map.has(r) === !1) {
|
|
1044
|
+
const d = new Ae((p) => (f.observerCount++, h && p.next(h), a.add(p), c || (c = !0, this.queue_tasks.push(f), this.need_run_task.next()), () => {
|
|
1045
|
+
if (f.observerCount--, f.observerCount <= 0) {
|
|
1046
|
+
o.next(), o.complete(), u.unsubscribe(), o.unsubscribe(), a.delete(p), this.cache_query_task_map.delete(r);
|
|
1047
|
+
const E = this.queue_tasks.indexOf(f);
|
|
1048
|
+
E >= 0 && this.queue_tasks.splice(E, 1);
|
|
1049
|
+
}
|
|
1050
|
+
})), f = {
|
|
1051
|
+
sql: n,
|
|
1052
|
+
params: t,
|
|
1053
|
+
observerCount: 0,
|
|
1054
|
+
refreshCount: 0,
|
|
1055
|
+
result$: d,
|
|
1056
|
+
destroy$: i,
|
|
1057
|
+
refresh$: l,
|
|
1058
|
+
resultEntities: [],
|
|
1059
|
+
resultEntityIds: [],
|
|
1060
|
+
refresh: () => u.next(f.refreshCount + 1),
|
|
1061
|
+
updateResult: (p) => {
|
|
1062
|
+
f.result = p, Array.isArray(p) && (f.resultEntities = p, f.resultEntityIds = p.map((E) => E.id));
|
|
1063
|
+
},
|
|
1064
|
+
next: (p) => {
|
|
1065
|
+
h = p, a.forEach((E) => E.next(p));
|
|
1066
|
+
},
|
|
1067
|
+
error: (p) => a.forEach((E) => E.error(p))
|
|
1068
|
+
};
|
|
1069
|
+
return this.cache_query_task_map.set(r, f), f;
|
|
1070
|
+
}
|
|
1071
|
+
return this.cache_query_task_map.get(r);
|
|
1072
|
+
}
|
|
1073
|
+
delete_task(e) {
|
|
1074
|
+
const n = $e(e.sql, e.params);
|
|
1075
|
+
this.cache_query_task_map.delete(n);
|
|
1076
|
+
}
|
|
1077
|
+
/**
|
|
1078
|
+
* 队列执行任务
|
|
1079
|
+
*/
|
|
1080
|
+
#e() {
|
|
1081
|
+
const e = this.queue_tasks.shift();
|
|
1082
|
+
if (!e) return;
|
|
1083
|
+
const { sql: n, params: t, refresh$: r } = e;
|
|
1084
|
+
r.pipe(
|
|
1085
|
+
Ie(e.destroy$),
|
|
1086
|
+
nt(),
|
|
1087
|
+
W(() => e.refreshCount++),
|
|
1088
|
+
m(
|
|
1089
|
+
() => this.client$.pipe(
|
|
1090
|
+
ae(1),
|
|
1091
|
+
m(() => this.adapter.execute(n, t))
|
|
1092
|
+
)
|
|
1093
|
+
)
|
|
1094
|
+
).subscribe({
|
|
1095
|
+
next: (a) => e.next(a),
|
|
1096
|
+
error: (a) => e.error(a)
|
|
1097
|
+
});
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
class je extends Zt {
|
|
1101
|
+
#e = /* @__PURE__ */ new Map();
|
|
1102
|
+
constructor(e, n, t) {
|
|
1103
|
+
super(e, n, t), this.db_changes$.subscribe((r) => {
|
|
1104
|
+
for (let a = 0; a < r.length; a++) {
|
|
1105
|
+
const { type: c, entities: o } = r[a];
|
|
1106
|
+
for (const i of this.cache_query_task_map.values()) {
|
|
1107
|
+
if (!i.resultEntityIds) return;
|
|
1108
|
+
this.merge_cache(c, i, o);
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
}), this.db_checks$.subscribe((r) => {
|
|
1112
|
+
for (let a = 0; a < r.length; a++) {
|
|
1113
|
+
const { type: c, rowIds: o } = r[a];
|
|
1114
|
+
for (const i of this.cache_query_task_map.values()) {
|
|
1115
|
+
if (!i.resultEntityIds || !this.#e.get(i)) return;
|
|
1116
|
+
const l = () => setTimeout(() => h.unsubscribe(), 0), h = this.findByRowIds(o, !0).subscribe({
|
|
1117
|
+
next: (d) => {
|
|
1118
|
+
d.length && this.db_changes_sub.next({ type: c, entities: d }), l();
|
|
1119
|
+
},
|
|
1120
|
+
complete: l
|
|
1121
|
+
});
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
});
|
|
1125
|
+
}
|
|
1126
|
+
get(e) {
|
|
1127
|
+
const n = {
|
|
1128
|
+
where: {
|
|
1129
|
+
combinator: "and",
|
|
1130
|
+
rules: [
|
|
1131
|
+
{
|
|
1132
|
+
field: "id",
|
|
1133
|
+
operator: "=",
|
|
1134
|
+
value: e
|
|
1135
|
+
}
|
|
1136
|
+
]
|
|
1137
|
+
}
|
|
1138
|
+
}, t = te(this.adapter, this.metadata, n), r = this.create_query_task(t), a = this.#s(r, { type: "get", options: n });
|
|
1139
|
+
return r.result$.pipe(
|
|
1140
|
+
m((c) => a.calculate(c)),
|
|
1141
|
+
g((c) => {
|
|
1142
|
+
if (c.length === 0) throw new T("not found");
|
|
1143
|
+
return c[0];
|
|
1144
|
+
})
|
|
1145
|
+
);
|
|
1146
|
+
}
|
|
1147
|
+
findOne(e) {
|
|
1148
|
+
const n = te(this.adapter, this.metadata, e), t = this.create_query_task(n), r = this.#s(t, { type: "findOne", options: e });
|
|
1149
|
+
return t.result$.pipe(
|
|
1150
|
+
m((a) => r.calculate(a)),
|
|
1151
|
+
g((a) => a[0])
|
|
1152
|
+
);
|
|
1153
|
+
}
|
|
1154
|
+
findOneOrFail(e) {
|
|
1155
|
+
const n = te(this.adapter, this.metadata, e), t = this.create_query_task(n), r = this.#s(t, { type: "findOneOrFail", options: e });
|
|
1156
|
+
return t.result$.pipe(
|
|
1157
|
+
m((a) => r.calculate(a)),
|
|
1158
|
+
g((a) => {
|
|
1159
|
+
if (a.length === 0)
|
|
1160
|
+
throw new T("not found");
|
|
1161
|
+
return a[0];
|
|
1162
|
+
})
|
|
1163
|
+
);
|
|
1164
|
+
}
|
|
1165
|
+
find(e) {
|
|
1166
|
+
const n = St(this.adapter, this.metadata, e), t = this.create_query_task(n), r = this.#s(t, { type: "find", options: e });
|
|
1167
|
+
return t.result$.pipe(m((a) => r.calculate(a)));
|
|
1168
|
+
}
|
|
1169
|
+
findAll(e) {
|
|
1170
|
+
const n = oe(this.adapter, this.metadata, e), t = this.create_query_task(n), r = this.#s(t, { type: "findAll", options: e });
|
|
1171
|
+
return t.result$.pipe(m((a) => r.calculate(a)));
|
|
1172
|
+
}
|
|
1173
|
+
findByRowIds(e, n = !1) {
|
|
1174
|
+
const t = Bt(this.metadata, e), r = this.create_query_task(t);
|
|
1175
|
+
return r.result$.pipe(g((a) => this.transform_query_result(r, a, n)));
|
|
1176
|
+
}
|
|
1177
|
+
count(e) {
|
|
1178
|
+
const n = qt(this.adapter, this.metadata, e), t = this.create_query_task(n), r = this.#s(t, { type: "count", options: e });
|
|
1179
|
+
return t.result$.pipe(m((a) => r.calculate(a)));
|
|
1180
|
+
}
|
|
1181
|
+
create(e) {
|
|
1182
|
+
return w(this.#t(e));
|
|
1183
|
+
}
|
|
1184
|
+
update(e, n) {
|
|
1185
|
+
return w(this.#n(e, n));
|
|
1186
|
+
}
|
|
1187
|
+
remove(e) {
|
|
1188
|
+
const n = L(e), t = () => {
|
|
1189
|
+
this.rxdb.context?.userId && (e.removedBy = this.rxdb.context.userId), n.origin = structuredClone({ ...e }), n.modified = !1, e.removedAt = /* @__PURE__ */ new Date(), n.removed = !0;
|
|
1190
|
+
};
|
|
1191
|
+
if (n.local === !0) {
|
|
1192
|
+
const { sql: a, params: c } = Qt(this.metadata, e, this.rxdb.context), o = this.client$.pipe(
|
|
1193
|
+
ae(1),
|
|
1194
|
+
m(() => this.adapter.execute(a, c)),
|
|
1195
|
+
g(() => (t(), e))
|
|
1196
|
+
);
|
|
1197
|
+
return w(o);
|
|
1198
|
+
} else
|
|
1199
|
+
t();
|
|
1200
|
+
const r = R(e);
|
|
1201
|
+
throw new T(`Remove Error${r.name}(${e.id}) not saved local.`);
|
|
1202
|
+
}
|
|
1203
|
+
merge_cache(e, n, t) {
|
|
1204
|
+
const r = this.#e.get(n);
|
|
1205
|
+
if (r)
|
|
1206
|
+
switch (e) {
|
|
1207
|
+
case se:
|
|
1208
|
+
Ht(n, r, t);
|
|
1209
|
+
break;
|
|
1210
|
+
case ne:
|
|
1211
|
+
zt(n, r, t);
|
|
1212
|
+
break;
|
|
1213
|
+
case P:
|
|
1214
|
+
Jt(n, r, t);
|
|
1215
|
+
break;
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
transform_query_result(e, n, t = !1) {
|
|
1219
|
+
const r = this.addQueryCache(n, t);
|
|
1220
|
+
return e.resultEntities = r, e.resultEntityIds = r.map((a) => a.id), r;
|
|
1221
|
+
}
|
|
1222
|
+
#t(e) {
|
|
1223
|
+
const { sql: n, params: t } = Gt(this.metadata, e, this.rxdb.context);
|
|
1224
|
+
return this.#a(n, t).pipe(g((r) => r[0]));
|
|
1225
|
+
}
|
|
1226
|
+
#n(e, n) {
|
|
1227
|
+
const { sql: t, params: r } = De(this.metadata, e, n, this.rxdb.context);
|
|
1228
|
+
return this.#a(t, r).pipe(g((a) => a[0]));
|
|
1229
|
+
}
|
|
1230
|
+
#s(e, n) {
|
|
1231
|
+
this.#e.set(e, n), e.destroy$.subscribe(() => this.#e.delete(e));
|
|
1232
|
+
const t = new K(0), r = t.asObservable(), a = n;
|
|
1233
|
+
return a.refresh = () => t.next(t.value + 1), a.calculate = (c) => {
|
|
1234
|
+
switch (n.type) {
|
|
1235
|
+
case "get":
|
|
1236
|
+
case "find":
|
|
1237
|
+
case "findAll":
|
|
1238
|
+
case "findOne":
|
|
1239
|
+
case "findOneOrFail":
|
|
1240
|
+
{
|
|
1241
|
+
const o = this.addQueryCache(c);
|
|
1242
|
+
e.updateResult(o);
|
|
1243
|
+
}
|
|
1244
|
+
break;
|
|
1245
|
+
case "count":
|
|
1246
|
+
e.updateResult(c.results[0].rows[0][0]);
|
|
1247
|
+
break;
|
|
1248
|
+
}
|
|
1249
|
+
return r.pipe(g(() => e.result));
|
|
1250
|
+
}, a;
|
|
1251
|
+
}
|
|
1252
|
+
/**
|
|
1253
|
+
* 执行修改语句
|
|
1254
|
+
*/
|
|
1255
|
+
#a(e, n) {
|
|
1256
|
+
return this.client$.pipe(
|
|
1257
|
+
ae(1),
|
|
1258
|
+
m(() => this.adapter.execute(e, n)),
|
|
1259
|
+
g((t) => this.addQueryCache(t, !0))
|
|
1260
|
+
);
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
const j = (s, e) => {
|
|
1264
|
+
const n = e.where, t = e.isFindRoot ?? !1, r = e?.level || 0, a = b(s), c = `c.level < ${r}`;
|
|
1265
|
+
let o = "";
|
|
1266
|
+
const i = [c, n && C(n)].filter(Boolean).join(" AND ");
|
|
1267
|
+
i && (o = `WHERE ${i}`);
|
|
1268
|
+
let u = "*";
|
|
1269
|
+
return e.count && (e.isFindRoot ? u = "count(*)" : u = "count(*)-1"), {
|
|
1270
|
+
sql: `WITH RECURSIVE __children AS (
|
|
1271
|
+
SELECT *,rowid as ${O}, 0 AS level
|
|
1272
|
+
FROM "${a}"
|
|
1273
|
+
WHERE ${t ? "parentId is null" : "id = ?"}
|
|
1274
|
+
UNION ALL
|
|
1275
|
+
SELECT children.*,children.rowid as ${O}, c.level + 1
|
|
1276
|
+
FROM "${a}" children
|
|
1277
|
+
JOIN __children c ON ${e.descendants ? "children.parentId = c.id" : "children.id = c.parentId"}
|
|
1278
|
+
${o}
|
|
1279
|
+
)
|
|
1280
|
+
|
|
1281
|
+
SELECT ${u} FROM __children ORDER BY level, id;`,
|
|
1282
|
+
params: t ? [] : [e.entity && e.entity.id || e.entity]
|
|
1283
|
+
};
|
|
1284
|
+
}, es = (s, e) => j(s, { ...e, isFindRoot: !0, descendants: !0 }), ts = (s, e) => j(s, { ...e, isFindRoot: !0, descendants: !0, count: !0 }), ss = (s, e) => j(s, { ...e, descendants: !0 }), ns = (s, e) => j(s, { ...e, descendants: !0, count: !0 }), rs = (s, e) => j(s, { ...e, descendants: !1 }), as = (s, e) => j(s, { ...e, descendants: !1, count: !0 }), _e = (s, e, n, t, r) => {
|
|
1285
|
+
if (s.length === 0 && e.length === 0) return { refresh: !1, recalculate: !1 };
|
|
1286
|
+
const { options: a } = n, c = t.resultEntityIds, o = N(() => r.map((_) => _.id)), i = N(
|
|
1287
|
+
() => a?.where ? r.some((_) => G(_, a.where)) : !0
|
|
1288
|
+
), u = N(
|
|
1289
|
+
() => a.orderBy && r.some(
|
|
1290
|
+
(_) => Re(_, t.resultEntities || [], a.orderBy)
|
|
1291
|
+
)
|
|
1292
|
+
), l = N(() => u() !== !1), h = N(() => i() === !1), d = N(
|
|
1293
|
+
() => c ? c.some((_) => o().includes(_)) : !0
|
|
1294
|
+
), f = N(() => d() === !1), p = {
|
|
1295
|
+
match_where: i,
|
|
1296
|
+
not_match_where: h,
|
|
1297
|
+
match_order_by: l,
|
|
1298
|
+
result_contains: d,
|
|
1299
|
+
result_not_contains: f
|
|
1300
|
+
}, E = e.some(
|
|
1301
|
+
(_) => _.every((y) => p[y]())
|
|
1302
|
+
);
|
|
1303
|
+
return {
|
|
1304
|
+
refresh: E === !1 && s.some(
|
|
1305
|
+
(_) => _.every((y) => p[y]())
|
|
1306
|
+
),
|
|
1307
|
+
recalculate: E
|
|
1308
|
+
};
|
|
1309
|
+
}, cs = (s, e, n) => {
|
|
1310
|
+
e.type;
|
|
1311
|
+
}, os = (s, e, n) => {
|
|
1312
|
+
const t = [], r = [];
|
|
1313
|
+
switch (e.type) {
|
|
1314
|
+
case "countRoot":
|
|
1315
|
+
case "countAncestors":
|
|
1316
|
+
case "countDescendants":
|
|
1317
|
+
t.push(["match_where"]);
|
|
1318
|
+
break;
|
|
1319
|
+
case "findRoot":
|
|
1320
|
+
case "findDescendants":
|
|
1321
|
+
case "findAncestors":
|
|
1322
|
+
t.push(["match_where"]);
|
|
1323
|
+
break;
|
|
1324
|
+
}
|
|
1325
|
+
const a = _e(t, r, e, s, n);
|
|
1326
|
+
a.refresh ? s.refresh() : a.recalculate && cs(s, e);
|
|
1327
|
+
}, is = (s, e, n) => {
|
|
1328
|
+
e.type;
|
|
1329
|
+
}, ls = (s, e, n) => {
|
|
1330
|
+
const t = [], r = [];
|
|
1331
|
+
switch (e.type) {
|
|
1332
|
+
case "countRoot":
|
|
1333
|
+
case "countAncestors":
|
|
1334
|
+
case "countDescendants":
|
|
1335
|
+
t.push(["match_where"]);
|
|
1336
|
+
break;
|
|
1337
|
+
case "findRoot":
|
|
1338
|
+
case "findDescendants":
|
|
1339
|
+
case "findAncestors":
|
|
1340
|
+
t.push(["match_where"]);
|
|
1341
|
+
break;
|
|
1342
|
+
}
|
|
1343
|
+
const a = _e(t, r, e, s, n);
|
|
1344
|
+
a.refresh ? s.refresh() : a.recalculate && is(s, e);
|
|
1345
|
+
}, us = (s, e, n) => {
|
|
1346
|
+
e.type;
|
|
1347
|
+
}, hs = (s, e, n) => {
|
|
1348
|
+
const t = [], r = [];
|
|
1349
|
+
switch (e.type) {
|
|
1350
|
+
case "countRoot":
|
|
1351
|
+
case "countAncestors":
|
|
1352
|
+
case "countDescendants":
|
|
1353
|
+
t.push(["match_where"]);
|
|
1354
|
+
break;
|
|
1355
|
+
case "findRoot":
|
|
1356
|
+
case "findDescendants":
|
|
1357
|
+
case "findAncestors":
|
|
1358
|
+
t.push(["match_where"]);
|
|
1359
|
+
break;
|
|
1360
|
+
}
|
|
1361
|
+
const a = _e(t, r, e, s, n);
|
|
1362
|
+
a.refresh ? s.refresh() : a.recalculate && us(s, e);
|
|
1363
|
+
};
|
|
1364
|
+
class ds extends je {
|
|
1365
|
+
#e = /* @__PURE__ */ new Map();
|
|
1366
|
+
findRoot(e) {
|
|
1367
|
+
const n = es(this.metadata, e), t = this.create_query_task(n), r = this.#t(t, { type: "findRoot", options: e });
|
|
1368
|
+
return t.result$.pipe(m((a) => r.calculate(a)));
|
|
1369
|
+
}
|
|
1370
|
+
countRoot(e) {
|
|
1371
|
+
const n = ts(this.metadata, e), t = this.create_query_task(n), r = this.#t(t, { type: "countRoot", options: e });
|
|
1372
|
+
return t.result$.pipe(m((a) => r.calculate(a)));
|
|
1373
|
+
}
|
|
1374
|
+
findDescendants(e) {
|
|
1375
|
+
const n = ss(this.metadata, e), t = this.create_query_task(n), r = this.#t(t, { type: "findDescendants", options: e });
|
|
1376
|
+
return t.result$.pipe(m((a) => r.calculate(a)));
|
|
1377
|
+
}
|
|
1378
|
+
countDescendants(e) {
|
|
1379
|
+
const n = ns(this.metadata, e), t = this.create_query_task(n), r = this.#t(t, { type: "countDescendants", options: e });
|
|
1380
|
+
return t.result$.pipe(m((a) => r.calculate(a)));
|
|
1381
|
+
}
|
|
1382
|
+
findAncestors(e) {
|
|
1383
|
+
const n = rs(this.metadata, e), t = this.create_query_task(n), r = this.#t(t, { type: "findAncestors", options: e });
|
|
1384
|
+
return t.result$.pipe(m((a) => r.calculate(a)));
|
|
1385
|
+
}
|
|
1386
|
+
countAncestors(e) {
|
|
1387
|
+
const n = as(this.metadata, e), t = this.create_query_task(n), r = this.#t(t, { type: "countAncestors", options: e });
|
|
1388
|
+
return t.result$.pipe(m((a) => r.calculate(a)));
|
|
1389
|
+
}
|
|
1390
|
+
/**
|
|
1391
|
+
* 根据 type 计算主动更新缓存策略
|
|
1392
|
+
*/
|
|
1393
|
+
merge_cache(e, n, t) {
|
|
1394
|
+
super.merge_cache(e, n, t);
|
|
1395
|
+
const r = this.#e.get(n);
|
|
1396
|
+
if (r)
|
|
1397
|
+
switch (e) {
|
|
1398
|
+
case "create":
|
|
1399
|
+
os(n, r, t);
|
|
1400
|
+
break;
|
|
1401
|
+
case "update":
|
|
1402
|
+
hs(n, r, t);
|
|
1403
|
+
break;
|
|
1404
|
+
case "remove":
|
|
1405
|
+
ls(n, r, t);
|
|
1406
|
+
break;
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
#t(e, n) {
|
|
1410
|
+
this.#e.set(e, n), e.destroy$.subscribe(() => this.#e.delete(e));
|
|
1411
|
+
const t = new K(0), r = t.asObservable(), a = n;
|
|
1412
|
+
return a.calculate = (c) => {
|
|
1413
|
+
switch (a.refresh = () => t.next(t.value + 1), n.type) {
|
|
1414
|
+
case "findRoot":
|
|
1415
|
+
case "findDescendants":
|
|
1416
|
+
case "findAncestors":
|
|
1417
|
+
{
|
|
1418
|
+
const o = this.addQueryCache(c);
|
|
1419
|
+
e.updateResult(o);
|
|
1420
|
+
}
|
|
1421
|
+
break;
|
|
1422
|
+
case "countRoot":
|
|
1423
|
+
case "countAncestors":
|
|
1424
|
+
case "countDescendants":
|
|
1425
|
+
e.updateResult(c.results[0].rows[0][0]);
|
|
1426
|
+
break;
|
|
1427
|
+
}
|
|
1428
|
+
return r.pipe(g(() => e.result));
|
|
1429
|
+
}, a;
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
class Ts extends ze {
|
|
1433
|
+
constructor(e, n) {
|
|
1434
|
+
super(e), this.options = n;
|
|
1435
|
+
const t = (r) => {
|
|
1436
|
+
const { tableName: a, rowIds: c, recordAt: o } = r, [i, u] = a.split("$"), l = this.rxdb.schemaManager.getEntityType(u, i);
|
|
1437
|
+
if (!l) return;
|
|
1438
|
+
const h = { rowIds: c, recordAt: o };
|
|
1439
|
+
switch (r.type) {
|
|
1440
|
+
case D.SQLITE_DELETE:
|
|
1441
|
+
this.rxdb.dispatchEvent(new st(l, h));
|
|
1442
|
+
break;
|
|
1443
|
+
case D.SQLITE_INSERT:
|
|
1444
|
+
this.rxdb.dispatchEvent(new tt(l, h));
|
|
1445
|
+
break;
|
|
1446
|
+
case D.SQLITE_UPDATE:
|
|
1447
|
+
this.rxdb.dispatchEvent(new et(l, h));
|
|
1448
|
+
break;
|
|
1449
|
+
}
|
|
1450
|
+
};
|
|
1451
|
+
this.#c.subscribe((r) => {
|
|
1452
|
+
r.addEventListener(D.SQLITE_INSERT, X(t)), r.addEventListener(D.SQLITE_DELETE, X(t)), r.addEventListener(D.SQLITE_UPDATE, X(t));
|
|
1453
|
+
}), this.#_.pipe(Ie(this.#e)).subscribe();
|
|
1454
|
+
}
|
|
1455
|
+
#e = new re();
|
|
1456
|
+
#t = /* @__PURE__ */ new Map();
|
|
1457
|
+
#n;
|
|
1458
|
+
#s = !1;
|
|
1459
|
+
// 查询缓存
|
|
1460
|
+
#a = [];
|
|
1461
|
+
#r = /* @__PURE__ */ new Map();
|
|
1462
|
+
#o = !1;
|
|
1463
|
+
#i = new K(0);
|
|
1464
|
+
#h = this.#i.asObservable();
|
|
1465
|
+
// sqlite 客户端
|
|
1466
|
+
#c = new Ae((e) => {
|
|
1467
|
+
const { vfs: n, async: t, worker: r, wasmPath: a, locateFile: c, workerInstance: o, sharedWorkerInstance: i, sharedWorker: u } = this.options, l = {
|
|
1468
|
+
vfs: n,
|
|
1469
|
+
async: t,
|
|
1470
|
+
worker: r,
|
|
1471
|
+
wasmPath: a,
|
|
1472
|
+
locateFile: c
|
|
1473
|
+
};
|
|
1474
|
+
Ce(l);
|
|
1475
|
+
let h;
|
|
1476
|
+
r && o ? h = Ee(o) : u && i ? h = Ee(i.port) : h = new Pt(), this.#n = h, h.init(this.rxdb.options.dbName, l).then(() => e.next(h));
|
|
1477
|
+
}).pipe(U(1));
|
|
1478
|
+
// 已连接
|
|
1479
|
+
#d = rt(() => this.rxdb.connect(this.name)).pipe(U(1));
|
|
1480
|
+
// 队列
|
|
1481
|
+
#_ = this.#h.pipe(
|
|
1482
|
+
Q(() => this.#o === !1),
|
|
1483
|
+
g(() => this.#a.shift()),
|
|
1484
|
+
Q(Boolean),
|
|
1485
|
+
W(() => this.#o = !0),
|
|
1486
|
+
m((e) => {
|
|
1487
|
+
const n = this.#r.get(e.key);
|
|
1488
|
+
return this.#d.pipe(
|
|
1489
|
+
m(() => this.#c),
|
|
1490
|
+
m((t) => t.execute(e.sql, e.bindings)),
|
|
1491
|
+
W((t) => n.next(t)),
|
|
1492
|
+
W(
|
|
1493
|
+
() => setTimeout(() => {
|
|
1494
|
+
this.#r.get(e.key)?.unsubscribe(), this.#r.delete(e.key);
|
|
1495
|
+
}, 0)
|
|
1496
|
+
),
|
|
1497
|
+
at((t) => {
|
|
1498
|
+
const r = t.message || "执行错误";
|
|
1499
|
+
return n.error(new T(`${r} ${e.sql} ${JSON.stringify(e.bindings)}`)), ct({ error: t });
|
|
1500
|
+
})
|
|
1501
|
+
);
|
|
1502
|
+
}),
|
|
1503
|
+
W(() => {
|
|
1504
|
+
this.#o = !1, this.#l();
|
|
1505
|
+
})
|
|
1506
|
+
);
|
|
1507
|
+
name = Mt;
|
|
1508
|
+
/**
|
|
1509
|
+
* 数据库版本
|
|
1510
|
+
*/
|
|
1511
|
+
version$ = this.#c.pipe(
|
|
1512
|
+
m((e) => ye(e.version())),
|
|
1513
|
+
U(1)
|
|
1514
|
+
);
|
|
1515
|
+
/**
|
|
1516
|
+
* 连接 wab sqlite
|
|
1517
|
+
*/
|
|
1518
|
+
connect() {
|
|
1519
|
+
return this.#c.pipe(g(() => this));
|
|
1520
|
+
}
|
|
1521
|
+
/**
|
|
1522
|
+
* 断开连接
|
|
1523
|
+
*/
|
|
1524
|
+
async disconnect() {
|
|
1525
|
+
this.#n && (await this.#n.disconnect(), this.#e.next(), this.#e.complete());
|
|
1526
|
+
}
|
|
1527
|
+
/**
|
|
1528
|
+
* 获取版本
|
|
1529
|
+
*/
|
|
1530
|
+
version() {
|
|
1531
|
+
return w(this.version$);
|
|
1532
|
+
}
|
|
1533
|
+
/**
|
|
1534
|
+
* 获取实体仓库
|
|
1535
|
+
* @param entity 实体类
|
|
1536
|
+
*/
|
|
1537
|
+
getRepository(e) {
|
|
1538
|
+
if (!this.#t.has(e)) {
|
|
1539
|
+
const n = R(e);
|
|
1540
|
+
let t;
|
|
1541
|
+
switch (n.repository) {
|
|
1542
|
+
case "Repository":
|
|
1543
|
+
t = new je(this, this.#c, e);
|
|
1544
|
+
break;
|
|
1545
|
+
case "TreeRepository":
|
|
1546
|
+
t = new ds(this, this.#c, e);
|
|
1547
|
+
break;
|
|
1548
|
+
default:
|
|
1549
|
+
throw new T("Unsupported repository type: " + n.repository);
|
|
1550
|
+
}
|
|
1551
|
+
return this.#t.set(e, t), t;
|
|
1552
|
+
}
|
|
1553
|
+
return this.#t.get(e);
|
|
1554
|
+
}
|
|
1555
|
+
/**
|
|
1556
|
+
* 保存多个实体
|
|
1557
|
+
* TODO: 优化事件
|
|
1558
|
+
* @param entities
|
|
1559
|
+
*/
|
|
1560
|
+
async saveMany(e) {
|
|
1561
|
+
return this.#s ? await ge(this, e) : e.length > 1 ? await this.transaction(() => ge(this, e)) : e.length === 1 && await e[0].save(), e;
|
|
1562
|
+
}
|
|
1563
|
+
/**
|
|
1564
|
+
* 删除多个实例
|
|
1565
|
+
* @param entities
|
|
1566
|
+
* @returns
|
|
1567
|
+
*/
|
|
1568
|
+
async removeMany(e) {
|
|
1569
|
+
return this.#s ? await be(this, e) : e.length > 1 ? await this.transaction(() => be(this, e)) : e.length === 1 && await e[0].save(), e;
|
|
1570
|
+
}
|
|
1571
|
+
/**
|
|
1572
|
+
* 判断表是否存在
|
|
1573
|
+
* @param EntityType
|
|
1574
|
+
*/
|
|
1575
|
+
isTableExisted(e) {
|
|
1576
|
+
return w(this.isTableExisted$(e));
|
|
1577
|
+
}
|
|
1578
|
+
/**
|
|
1579
|
+
* 判断表是否存在
|
|
1580
|
+
* @param EntityType
|
|
1581
|
+
*/
|
|
1582
|
+
isTableExisted$(e) {
|
|
1583
|
+
const n = R(e), t = b(n);
|
|
1584
|
+
return this.#u(pt(t)).pipe(g((r) => Et(r) === !1));
|
|
1585
|
+
}
|
|
1586
|
+
/**
|
|
1587
|
+
* 创建表和初始化数据
|
|
1588
|
+
* @param entities 表结构
|
|
1589
|
+
* @param data 视图数据
|
|
1590
|
+
* @returns
|
|
1591
|
+
*/
|
|
1592
|
+
createTables(e, n) {
|
|
1593
|
+
const t = Nt(this, e, n);
|
|
1594
|
+
return w(this.#u(t).pipe(g(() => !0)));
|
|
1595
|
+
}
|
|
1596
|
+
/**
|
|
1597
|
+
* 创建分支
|
|
1598
|
+
* @param branchId 分支 ID
|
|
1599
|
+
* @param fromChangeId
|
|
1600
|
+
*/
|
|
1601
|
+
async createBranch(e, n) {
|
|
1602
|
+
return gt(this, e, n);
|
|
1603
|
+
}
|
|
1604
|
+
/**
|
|
1605
|
+
* 切换分支
|
|
1606
|
+
* @param branchId
|
|
1607
|
+
* @returns
|
|
1608
|
+
*/
|
|
1609
|
+
async switchBranch(e) {
|
|
1610
|
+
return Lt(this, e);
|
|
1611
|
+
}
|
|
1612
|
+
/**
|
|
1613
|
+
* 恢复某个实体
|
|
1614
|
+
* @param entity
|
|
1615
|
+
* @param options
|
|
1616
|
+
*/
|
|
1617
|
+
restoreEntity(e, n) {
|
|
1618
|
+
throw new Error("Method not implemented.");
|
|
1619
|
+
}
|
|
1620
|
+
/**
|
|
1621
|
+
* 执行 sql
|
|
1622
|
+
* @param sql
|
|
1623
|
+
* @param bindings
|
|
1624
|
+
*/
|
|
1625
|
+
execute(e, n) {
|
|
1626
|
+
const t = e + n?.join(",");
|
|
1627
|
+
if (!this.#r.has(t)) {
|
|
1628
|
+
const r = new re(1);
|
|
1629
|
+
this.#r.set(t, r), this.#a.push({ key: t, sql: e, bindings: n }), this.#l();
|
|
1630
|
+
}
|
|
1631
|
+
return this.#r.get(t).asObservable();
|
|
1632
|
+
}
|
|
1633
|
+
/**
|
|
1634
|
+
* 处理事务
|
|
1635
|
+
* @param callback
|
|
1636
|
+
*/
|
|
1637
|
+
transaction(e) {
|
|
1638
|
+
return w(this.transaction$(e));
|
|
1639
|
+
}
|
|
1640
|
+
transaction$(e, n = !0) {
|
|
1641
|
+
return this.rxdb.connect(this.name).pipe(
|
|
1642
|
+
m(() => this.#c),
|
|
1643
|
+
m(async (t) => {
|
|
1644
|
+
this.#s = !0, this.rxdb.dispatchEvent(_s);
|
|
1645
|
+
let r = "", a = "";
|
|
1646
|
+
if (n) {
|
|
1647
|
+
const c = Ze(), o = (i) => `INSERT INTO "public$RxDBChange" (type,branchId,transactionId) VALUES ('${i}','main','${c}');`;
|
|
1648
|
+
r = o("TRANSACTION_BEGIN"), a = o("TRANSACTION_COMMIT");
|
|
1649
|
+
}
|
|
1650
|
+
await t.execute(`
|
|
1651
|
+
BEGIN;
|
|
1652
|
+
PRAGMA defer_foreign_keys = ON;
|
|
1653
|
+
${r}
|
|
1654
|
+
`);
|
|
1655
|
+
try {
|
|
1656
|
+
const c = await e();
|
|
1657
|
+
return await t.execute(`${a}
|
|
1658
|
+
COMMIT;`), this.#s = !1, this.rxdb.dispatchEvent(ps), c;
|
|
1659
|
+
} catch (c) {
|
|
1660
|
+
await t.execute("ROLLBACK"), this.#s = !1, this.rxdb.dispatchEvent(fs);
|
|
1661
|
+
const o = c?.message || "Transaction Error";
|
|
1662
|
+
throw new T(o);
|
|
1663
|
+
}
|
|
1664
|
+
})
|
|
1665
|
+
);
|
|
1666
|
+
}
|
|
1667
|
+
/**
|
|
1668
|
+
* 本地分支仓库
|
|
1669
|
+
*/
|
|
1670
|
+
localRxDBBranch() {
|
|
1671
|
+
return this.getRepository(ie);
|
|
1672
|
+
}
|
|
1673
|
+
/**
|
|
1674
|
+
* 本地变更仓库
|
|
1675
|
+
*/
|
|
1676
|
+
localRxDBChange() {
|
|
1677
|
+
return this.getRepository(Te);
|
|
1678
|
+
}
|
|
1679
|
+
#l() {
|
|
1680
|
+
this.#i.next(this.#i.value + 1);
|
|
1681
|
+
}
|
|
1682
|
+
/**
|
|
1683
|
+
* 内部执行的 sql
|
|
1684
|
+
* @param sql
|
|
1685
|
+
* @param bindings
|
|
1686
|
+
*/
|
|
1687
|
+
#u(e, n) {
|
|
1688
|
+
return this.#c.pipe(m((t) => ye(t.execute(e, n))));
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
const _s = new Ye(), fs = new Je(), ps = new Xe();
|
|
1692
|
+
export {
|
|
1693
|
+
Ts as RxDBAdapterSqlite,
|
|
1694
|
+
T as RxdbAdapterSqliteError,
|
|
1695
|
+
Pt as SqliteClient,
|
|
1696
|
+
jt as WA_SQLITE_VFS_LIST,
|
|
1697
|
+
Ft as sqliteLoad
|
|
1698
|
+
};
|