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