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