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