@aiao/rxdb-adapter-sqlite 0.0.11 → 0.0.13

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