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