@aiao/rxdb-adapter-sqlite 0.0.16 → 0.0.17

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,12 +1,12 @@
1
- import { getEntityMetadata as T, PropertyType as E, RelationKind as b, isRuleGroup as fe, getEntityStatus as F, 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) => ({
1
+ import { getEntityMetadata as w, PropertyType as E, RelationKind as I, isRuleGroup as fe, getEntityStatus as F, RepositoryBase as Ue, RxDBChange as B, EntityLocalCreatedEvent as K, EntityLocalRemovedEvent as ce, EntityLocalUpdatedEvent as Y, parseRxDBChangeKey as ee, RxDBBranch as H, getEntityType as te, TransactionBeginEvent as We, TransactionRollbackEvent as Ke, TransactionCommitEvent as He, RxDBAdapterLocalBase as Ve, getEntityMutations as he, uuid as Qe } from "@aiao/rxdb";
2
+ import { isString as Ge, isNil as Je, traverseObjectKeys as Xe, EventDispatcher as Ye, AsyncQueueExecutor as Ae, get as ze, 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 et } from "rxjs";
5
+ import { Factory as tt, SQLITE_ROW as nt, SQLITE_UTF8 as Se, SQLITE_DETERMINISTIC as be } from "wa-sqlite";
6
+ const st = (e) => ({
7
7
  sql: "SELECT * FROM sqlite_master WHERE type='table' AND name=? LIMIT 1;",
8
8
  params: [e]
9
- }), st = (e) => !!e.sql && (e.results.length === 0 || e.results[0].rows.length === 0), q = "__rowid", rt = (e) => {
9
+ }), rt = (e) => !!e.sql && (e.results.length === 0 || e.results[0].rows.length === 0), x = "__rowid", ot = (e) => {
10
10
  switch (e.type) {
11
11
  case E.uuid:
12
12
  case E.string:
@@ -31,7 +31,7 @@ const nt = (e) => ({
31
31
  case E.boolean:
32
32
  return e ? 1 : 0;
33
33
  case E.date:
34
- return e instanceof Date ? e.toISOString() : e;
34
+ return e instanceof Date ? isNaN(e.getTime()) ? null : e.toISOString() : e === "" ? null : e;
35
35
  case E.keyValue: {
36
36
  if (!e) return null;
37
37
  if (n.properties && typeof e == "object" && !Array.isArray(e)) {
@@ -55,8 +55,15 @@ const nt = (e) => ({
55
55
  switch (n.type) {
56
56
  case E.boolean:
57
57
  return !!e;
58
- case E.date:
59
- return e instanceof Date ? e : typeof e == "string" ? new Date(e) : e;
58
+ case E.date: {
59
+ if (e === "") return null;
60
+ if (e instanceof Date) return isNaN(e.getTime()) ? null : e;
61
+ if (typeof e == "string") {
62
+ const t = new Date(e);
63
+ return isNaN(t.getTime()) ? null : t;
64
+ }
65
+ return e;
66
+ }
60
67
  case E.keyValue: {
61
68
  if (!e) return null;
62
69
  const t = typeof e == "object" ? e : JSON.parse(e);
@@ -72,10 +79,10 @@ const nt = (e) => ({
72
79
  default:
73
80
  return e;
74
81
  }
75
- }, P = (e, n) => `${n}$${e}`, ot = (e) => {
82
+ }, P = (e, n) => `${n}$${e}`, at = (e) => {
76
83
  const n = e.indexOf("$");
77
84
  return n === -1 ? ["", e] : [e.substring(0, n), e.substring(n + 1)];
78
- }, A = (e) => P(e.tableName, e.namespace), _e = (e) => A(T(e)), ye = (e, n) => `idx_${e.name}_${n.name}`;
85
+ }, S = (e) => P(e.tableName, e.namespace), ye = (e) => S(w(e)), _e = (e, n) => `idx_${e.name}_${n.name}`;
79
86
  class R extends Error {
80
87
  constructor(n, t) {
81
88
  super(n, t), this.name = "RxDBAdapterSqliteError", Object.setPrototypeOf(this, R.prototype);
@@ -97,19 +104,19 @@ const le = (e, n) => {
97
104
  t[c] = n[c];
98
105
  continue;
99
106
  }
100
- const u = e.propertyMap.get(c);
101
- if (u) {
102
- t[u.columnName] = V(n[c], u);
107
+ const d = e.propertyMap.get(c);
108
+ if (d) {
109
+ t[d.columnName] = V(n[c], d);
103
110
  continue;
104
111
  }
105
- const d = e.columnNameToPropertyName?.get(c);
106
- if (d) {
107
- const f = e.propertyMap.get(d);
112
+ const u = e.columnNameToPropertyName?.get(c);
113
+ if (u) {
114
+ const f = e.propertyMap.get(u);
108
115
  f && (t[c] = V(n[c], f));
109
116
  }
110
117
  }
111
118
  return t;
112
- }, be = (e, n) => {
119
+ }, Ie = (e, n) => {
113
120
  const t = {};
114
121
  for (const [o, i] of e.propertyMap)
115
122
  o in n && (t[i.columnName] = n[o]);
@@ -119,7 +126,7 @@ const le = (e, n) => {
119
126
  i in n && (t[r[o]] = n[i]);
120
127
  }
121
128
  return t;
122
- }, at = (e, n) => {
129
+ }, it = (e, n) => {
123
130
  const t = {};
124
131
  for (const [o, i] of e.propertyMap)
125
132
  o in n && i.readonly !== !0 && (t[i.columnName] = n[o]);
@@ -132,9 +139,9 @@ const le = (e, n) => {
132
139
  }
133
140
  }
134
141
  return t;
135
- }, I = (e) => Qe(e) ? `'${e.replaceAll("\0", "").replaceAll("'", "''")}'` : Ge(e) ? "NULL" : e, k = (e, n = []) => {
142
+ }, T = (e) => Ge(e) ? `'${e.replaceAll("\0", "").replaceAll("'", "''")}'` : Je(e) ? "NULL" : e, k = (e, n = []) => {
136
143
  let t = 0;
137
- return e.replace(/\?/g, () => String(I(n[t++])));
144
+ return e.replace(/\?/g, () => String(T(n[t++])));
138
145
  }, oe = (e, n, t) => {
139
146
  const s = {};
140
147
  return t.forEach((r, o) => {
@@ -145,18 +152,18 @@ const le = (e, n) => {
145
152
  s[c[l]] = r;
146
153
  return;
147
154
  }
148
- const u = e.columnNameToPropertyName, p = u?.get(i);
155
+ const d = e.columnNameToPropertyName, p = d?.get(i);
149
156
  if (p) {
150
157
  const f = e.propertyMap.get(p);
151
158
  f ? s[p] = v(r, f) : console.warn(`Property ${p} not found in metadata ${e.name}`);
152
159
  return;
153
160
  }
154
- const d = e.computedPropertyMap?.get(i);
155
- if (d) {
156
- s[i] = v(r, d);
161
+ const u = e.computedPropertyMap?.get(i);
162
+ if (u) {
163
+ s[i] = v(r, u);
157
164
  return;
158
165
  }
159
- if (!u) {
166
+ if (!d) {
160
167
  const f = e.propertyMap.get(i);
161
168
  if (f) {
162
169
  s[i] = v(r, f);
@@ -165,7 +172,7 @@ const le = (e, n) => {
165
172
  }
166
173
  console.warn(`Column ${i}=${r} not found in metadata ${e.name}`);
167
174
  }), s;
168
- }, w = "_", it = {
175
+ }, A = "_", ct = {
169
176
  "=": "=",
170
177
  "!=": "!=",
171
178
  ">": ">",
@@ -206,11 +213,11 @@ const le = (e, n) => {
206
213
  return r[o];
207
214
  }
208
215
  return e;
209
- }, ge = (e, n, t) => {
216
+ }, Ne = (e, n, t) => {
210
217
  if (n) return n;
211
218
  if (!e.includes(".")) {
212
219
  const c = ae(e, t);
213
- return `${w}."${c}"`;
220
+ return `${A}."${c}"`;
214
221
  }
215
222
  const s = e.lastIndexOf("."), r = e.slice(0, s), o = e.slice(s + 1), i = ae(o, t);
216
223
  return `"${r}"."${i}"`;
@@ -219,71 +226,71 @@ const le = (e, n) => {
219
226
  switch (n) {
220
227
  case "in":
221
228
  case "notIn":
222
- return lt(t);
229
+ return ut(t);
223
230
  case "between":
224
231
  case "notBetween":
225
- return ut(t);
232
+ return dt(t);
226
233
  case "contains":
227
234
  case "notContains":
228
- return I(`%${t}%`);
235
+ return T(`%${t}%`);
229
236
  case "startsWith":
230
237
  case "notStartsWith":
231
- return I(`${t}%`);
238
+ return T(`${t}%`);
232
239
  case "endsWith":
233
240
  case "notEndsWith":
234
- return I(`%${t}`);
241
+ return T(`%${t}`);
235
242
  default:
236
- return dt(t);
243
+ return pt(t);
237
244
  }
238
- }, 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) => {
245
+ }, lt = (e) => ct[e] || e, Q = (e, n, t) => e.length === 1 ? t.name : `${n}_${t.name}`, ut = (e) => !Array.isArray(e) || !e.length ? "(NULL)" : `(${e.map(T).join(", ")})`, dt = (e) => {
239
246
  if (!Array.isArray(e) || e.length < 2) return "";
240
247
  const [n, t] = e;
241
248
  if (n == null || t == null) return "";
242
249
  const s = n instanceof Date ? n.toISOString() : n, r = t instanceof Date ? t.toISOString() : t;
243
- return `${I(s)} and ${I(r)}`;
244
- }, 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) => {
250
+ return `${T(s)} and ${T(r)}`;
251
+ }, pt = (e) => typeof e == "boolean" ? e ? "1" : "0" : typeof e == "string" ? T(e) : typeof e == "number" ? e.toString() : e instanceof Date ? T(e.toISOString()) : T(e), ft = (e, n, t) => {
245
252
  const s = e.propertyMap.get(n);
246
253
  if (!s || s.type !== E.keyValue || typeof t.value != "object" || Array.isArray(t.value)) return null;
247
254
  const r = Object.entries(t.value).filter(([, a]) => a != null);
248
255
  if (!r.length) return "";
249
256
  const o = t.operator === "notContains" ? "NOT LIKE" : "LIKE", i = t.operator === "contains" ? " OR " : " AND ";
250
257
  return `(${r.map(([a, l]) => {
251
- const u = l instanceof Date ? l.toISOString() : typeof l == "string" ? l : String(l), p = a.replace(/'/g, "''");
252
- return `json_extract(${w}."${s.columnName}", '$.${p}') ${o} ${I(`%${u}%`)}`;
258
+ const d = l instanceof Date ? l.toISOString() : typeof l == "string" ? l : String(l), p = a.replace(/'/g, "''");
259
+ return `json_extract(${A}."${s.columnName}", '$.${p}') ${o} ${T(`%${d}%`)}`;
253
260
  }).join(i)})`;
254
- }, ft = (e, n, t) => {
261
+ }, ht = (e, n, t) => {
255
262
  const s = e.propertyMap.get(n);
256
263
  if (!s || s.type !== E.stringArray && s.type !== E.numberArray) return null;
257
- const r = Re(t), o = `EXISTS (SELECT 1 FROM json_each(${w}."${s.columnName}") WHERE json_each.value IN ${r})`;
264
+ const r = Re(t), o = `EXISTS (SELECT 1 FROM json_each(${A}."${s.columnName}") WHERE json_each.value IN ${r})`;
258
265
  return t.operator === "notIn" ? `NOT ${o}` : o;
259
- }, ht = (e, n, t, s, r) => {
266
+ }, mt = (e, n, t, s, r) => {
260
267
  if (t && s && r && (e.operator === "exists" || e.operator === "notExists")) {
261
- const d = gt(e, t, s, (f, h) => {
262
- const _ = /* @__PURE__ */ new Map();
263
- return h.propertyMap.forEach((m, y) => {
264
- _.set(y, `"child"."${m.columnName}"`);
265
- }), r(f, _, h, s);
268
+ const u = gt(e, t, s, (f, h) => {
269
+ const y = /* @__PURE__ */ new Map();
270
+ return h.propertyMap.forEach((m, _) => {
271
+ y.set(_, `"child"."${m.columnName}"`);
272
+ }), r(f, y, h, s);
266
273
  });
267
- if (d !== null) return d;
274
+ if (u !== null) return u;
268
275
  }
269
276
  if (e.operator === "null" || e.operator === "notNull") {
270
- const p = String(e.field), d = n.get(p), f = ge(p, d, t);
277
+ const p = String(e.field), u = n.get(p), f = Ne(p, u, t);
271
278
  return e.operator === "null" ? `${f} IS NULL` : `${f} IS NOT NULL`;
272
279
  }
273
- const o = e.value === null, i = o ? "" : ct(e.operator).toLowerCase(), c = o ? e.operator === "=" ? "IS NULL" : e.operator === "!=" ? "IS NOT NULL" : "" : Re(e);
280
+ const o = e.value === null, i = o ? "" : lt(e.operator).toLowerCase(), c = o ? e.operator === "=" ? "IS NULL" : e.operator === "!=" ? "IS NOT NULL" : "" : Re(e);
274
281
  if (!c && ["in", "notIn", "between", "notBetween"].includes(e.operator)) return "";
275
282
  const a = String(e.field), l = n.get(a);
276
283
  if (t && !a.includes(".") && (e.operator === "contains" || e.operator === "notContains")) {
277
- const p = pt(t, a, e);
284
+ const p = ft(t, a, e);
278
285
  if (p !== null) return p;
279
286
  }
280
287
  if (t && !a.includes(".") && (e.operator === "in" || e.operator === "notIn")) {
281
- const p = ft(t, a, e);
288
+ const p = ht(t, a, e);
282
289
  if (p !== null) return p;
283
290
  }
284
- return `${ge(a, l, t)} ${i} ${c}`.replace(/\s+/g, " ").trim();
285
- }, ue = (e) => e === w ? e : `"${e}"`, mt = (e, n, t) => {
286
- if (e.kind !== b.ONE_TO_MANY)
291
+ return `${Ne(a, l, t)} ${i} ${c}`.replace(/\s+/g, " ").trim();
292
+ }, ue = (e) => e === A ? e : `"${e}"`, Et = (e, n, t) => {
293
+ if (e.kind !== I.ONE_TO_MANY)
287
294
  throw new Error("_build_one_to_many_exists requires ONE_TO_MANY relation");
288
295
  const s = P(n.tableName, n.namespace), r = "child", o = n.relations.find((a) => a.name === e.mappedProperty);
289
296
  if (!o)
@@ -292,52 +299,52 @@ const le = (e, n) => {
292
299
  );
293
300
  const i = o.columnName;
294
301
  let c = `EXISTS (SELECT 1 FROM "${s}" "${r}"`;
295
- return c += ` WHERE "${r}"."${i}" = ${w}."id"`, t && (c += ` AND ${t}`), c += ")", c;
296
- }, Et = (e, n, t) => {
302
+ return c += ` WHERE "${r}"."${i}" = ${A}."id"`, t && (c += ` AND ${t}`), c += ")", c;
303
+ }, yt = (e, n, t) => {
297
304
  const s = e.columnName;
298
305
  if (!t)
299
- return `${w}."${s}" IS NOT NULL`;
306
+ return `${A}."${s}" IS NOT NULL`;
300
307
  const r = P(n.tableName, n.namespace), o = "child";
301
308
  let i = `EXISTS (SELECT 1 FROM "${r}" "${o}"`;
302
- return i += ` WHERE ${w}."${s}" = "${o}"."id"`, i += ` AND ${t}`, i += ")", i;
309
+ return i += ` WHERE ${A}."${s}" = "${o}"."id"`, i += ` AND ${t}`, i += ")", i;
303
310
  }, _t = (e, n, t, s, r) => {
304
311
  const o = s.rxdb.schemaManager.findMappedRelation(e, n);
305
312
  if (o?.relation) {
306
313
  const i = P(t.tableName, t.namespace), c = "child", a = o.relation.columnName;
307
314
  let l = `EXISTS (SELECT 1 FROM "${i}" "${c}"`;
308
- return l += ` WHERE "${c}"."${a}" = ${w}."id"`, r && (l += ` AND ${r}`), l += ")", l;
315
+ return l += ` WHERE "${c}"."${a}" = ${A}."id"`, r && (l += ` AND ${r}`), l += ")", l;
309
316
  } else {
310
317
  const i = n.columnName;
311
318
  if (!r)
312
- return `${w}."${i}" IS NOT NULL`;
319
+ return `${A}."${i}" IS NOT NULL`;
313
320
  const c = P(t.tableName, t.namespace), a = "child";
314
321
  let l = `EXISTS (SELECT 1 FROM "${c}" "${a}"`;
315
- return l += ` WHERE ${w}."${i}" = "${a}"."id"`, l += ` AND ${r}`, l += ")", l;
322
+ return l += ` WHERE ${A}."${i}" = "${a}"."id"`, l += ` AND ${r}`, l += ")", l;
316
323
  }
317
- }, yt = (e, n, t, s) => {
324
+ }, Nt = (e, n, t, s) => {
318
325
  const r = P(t.tableName, t.namespace), o = "child", i = n.junctionEntityType;
319
326
  if (!i)
320
327
  throw new Error(`MANY_TO_MANY relation "${n.name}" missing junctionEntityType`);
321
- const c = T(i);
328
+ const c = w(i);
322
329
  if (!c)
323
330
  throw new Error("Cannot find metadata for junction entity");
324
- const a = P(c.tableName, c.namespace), l = "junction", u = c.relations.find(
325
- (_) => _.mappedEntity === e.name && _.mappedNamespace === e.namespace
331
+ const a = P(c.tableName, c.namespace), l = "junction", d = c.relations.find(
332
+ (y) => y.mappedEntity === e.name && y.mappedNamespace === e.namespace
326
333
  );
327
- if (!u)
334
+ if (!d)
328
335
  throw new Error(
329
336
  `Cannot find relation in junction entity ${c.name} pointing to ${e.name}`
330
337
  );
331
338
  const p = c.relations.find(
332
- (_) => _.mappedEntity === t.name && _.mappedNamespace === t.namespace
339
+ (y) => y.mappedEntity === t.name && y.mappedNamespace === t.namespace
333
340
  );
334
341
  if (!p)
335
342
  throw new Error(
336
343
  `Cannot find relation in junction entity ${c.name} pointing to ${t.name}`
337
344
  );
338
- const d = u.columnName, f = p.columnName;
345
+ const u = d.columnName, f = p.columnName;
339
346
  let h = `EXISTS (SELECT 1 FROM "${r}" "${o}"`;
340
- return h += ` INNER JOIN "${a}" "${l}"`, h += ` ON "${l}"."${f}" = "${o}"."id"`, h += ` WHERE "${l}"."${d}" = ${w}."id"`, s && (h += ` AND ${s}`), h += ")", h;
347
+ return h += ` INNER JOIN "${a}" "${l}"`, h += ` ON "${l}"."${f}" = "${o}"."id"`, h += ` WHERE "${l}"."${u}" = ${A}."id"`, s && (h += ` AND ${s}`), h += ")", h;
341
348
  }, gt = (e, n, t, s) => {
342
349
  if (e.operator !== "exists" && e.operator !== "notExists")
343
350
  return null;
@@ -353,26 +360,26 @@ const le = (e, n) => {
353
360
  e.where && s && (i = s(e.where, o));
354
361
  let c;
355
362
  switch (r.kind) {
356
- case b.ONE_TO_MANY:
357
- c = mt(r, o, i);
358
- break;
359
- case b.MANY_TO_ONE:
363
+ case I.ONE_TO_MANY:
360
364
  c = Et(r, o, i);
361
365
  break;
362
- case b.ONE_TO_ONE:
366
+ case I.MANY_TO_ONE:
367
+ c = yt(r, o, i);
368
+ break;
369
+ case I.ONE_TO_ONE:
363
370
  c = _t(n, r, o, t, i);
364
371
  break;
365
- case b.MANY_TO_MANY:
366
- c = yt(n, r, o, i);
372
+ case I.MANY_TO_MANY:
373
+ c = Nt(n, r, o, i);
367
374
  break;
368
375
  }
369
376
  return e.operator === "notExists" ? `NOT ${c}` : c;
370
377
  }, G = (e, n) => {
371
378
  if (e.relationAliasMap.has(n))
372
379
  return e.relationAliasMap.get(n);
373
- const t = n.includes("_") ? n : n.split("_")[0], s = Oe(e, t);
380
+ const t = n.includes("_") ? n : n.split("_")[0], s = Ce(e, t);
374
381
  return e.relationAliasMap.set(n, s), s;
375
- }, Nt = (e, n, t, s) => {
382
+ }, $t = (e, n, t, s) => {
376
383
  const r = t.slice(0, s);
377
384
  let o = n;
378
385
  const i = [];
@@ -383,142 +390,142 @@ const le = (e, n) => {
383
390
  i.push({ metadata: o, relation: a }), o = e.rxdb.schemaManager.getEntityMetadata(a.mappedEntity, a.mappedNamespace);
384
391
  }
385
392
  return { metaWalker: o, relPairs: i };
386
- }, $t = (e, n, t, s, r) => {
393
+ }, Tt = (e, n, t, s, r) => {
387
394
  for (let o = r.length - 1; o > 0; o--) {
388
395
  const i = r.slice(o);
389
396
  if (i.length === 0) continue;
390
- const { metaWalker: c, relPairs: a } = Nt(e, t, r, o);
397
+ const { metaWalker: c, relPairs: a } = $t(e, t, r, o);
391
398
  if (!c || a.length === 0) continue;
392
- const l = i[0], u = i.slice(1).join("."), p = c.propertyMap.get(l);
399
+ const l = i[0], d = i.slice(1).join("."), p = c.propertyMap.get(l);
393
400
  if (p && p.type === E.keyValue) {
394
- const d = r.slice(0, o).join(".");
395
- Ie(e, n, a, d);
396
- const f = a[a.length - 1].relation, h = Q(a, d, f), _ = G(n, h), m = u ? `$.${u}` : "$";
397
- return n.fieldAliasMap.set(s, `json_extract("${_}"."${p.columnName}", '${m}')`), !0;
401
+ const u = r.slice(0, o).join(".");
402
+ Oe(e, n, a, u);
403
+ const f = a[a.length - 1].relation, h = Q(a, u, f), y = G(n, h), m = d ? `$.${d}` : "$";
404
+ return n.fieldAliasMap.set(s, `json_extract("${y}"."${p.columnName}", '${m}')`), !0;
398
405
  }
399
406
  }
400
407
  return !1;
401
- }, Ie = (e, n, t, s) => {
408
+ }, Oe = (e, n, t, s) => {
402
409
  t.forEach(({ metadata: r, relation: o }, i) => {
403
410
  let c = e.rxdb.schemaManager.findMappedRelation(r, o);
404
411
  if (!c)
405
- if (o.kind === b.ONE_TO_MANY || o.kind === b.MANY_TO_MANY) {
412
+ if (o.kind === I.ONE_TO_MANY || o.kind === I.MANY_TO_MANY) {
406
413
  const p = r.relationMap.get(o.mappedProperty);
407
414
  p && (c = { metadata: r, relation: p });
408
- } else (o.kind === b.MANY_TO_ONE || o.kind === b.ONE_TO_ONE) && (c = { metadata: r, relation: o });
415
+ } else (o.kind === I.MANY_TO_ONE || o.kind === I.ONE_TO_ONE) && (c = { metadata: r, relation: o });
409
416
  if (!c) throw new R("mappedRelation not found");
410
417
  const a = Q(t, s, o), l = G(n, a);
411
- let u = w;
418
+ let d = A;
412
419
  if (i > 0) {
413
- const p = t[i - 1], d = Q(t, s, p.relation);
414
- u = G(n, d);
420
+ const p = t[i - 1], u = Q(t, s, p.relation);
421
+ d = G(n, u);
415
422
  }
416
423
  switch (o.kind) {
417
- case b.ONE_TO_MANY:
418
- Tt(
424
+ case I.ONE_TO_MANY:
425
+ wt(
419
426
  n,
420
427
  c,
421
428
  l,
422
- u
429
+ d
423
430
  );
424
431
  break;
425
- case b.ONE_TO_ONE:
426
- case b.MANY_TO_ONE:
427
- wt(
432
+ case I.ONE_TO_ONE:
433
+ case I.MANY_TO_ONE:
434
+ At(
428
435
  n,
429
436
  c,
430
437
  l,
431
- u,
438
+ d,
432
439
  o
433
440
  );
434
441
  break;
435
- case b.MANY_TO_MANY:
436
- At(
442
+ case I.MANY_TO_MANY:
443
+ St(
437
444
  e,
438
445
  n,
439
446
  c,
440
447
  l,
441
- u,
448
+ d,
442
449
  o
443
450
  );
444
451
  break;
445
452
  }
446
453
  });
447
- }, Tt = (e, n, t, s) => {
454
+ }, wt = (e, n, t, s) => {
448
455
  const r = J(e, n.metadata), o = `"${t}".${n.relation.columnName} = ${ue(s)}.id`;
449
456
  r.find((c) => c.joinTableName === t && c.on === o) || r.push({
450
457
  joinTableName: t,
451
458
  on: o
452
459
  });
453
- }, wt = (e, n, t, s, r) => {
460
+ }, At = (e, n, t, s, r) => {
454
461
  const o = J(e, n.metadata), i = `"${t}".id = ${ue(s)}.${r.columnName}`;
455
462
  o.find((a) => a.joinTableName === t && a.on === i) || o.push({
456
463
  joinTableName: t,
457
464
  on: i
458
465
  });
459
- }, At = (e, n, t, s, r, o) => {
460
- const i = T(o.junctionEntityType), c = J(n, i), a = Oe(n, `${o.name}_m_n`), l = `"${a}".${t.relation.columnName} = ${ue(r)}.id`;
466
+ }, St = (e, n, t, s, r, o) => {
467
+ const i = w(o.junctionEntityType), c = J(n, i), a = Ce(n, `${o.name}_m_n`), l = `"${a}".${t.relation.columnName} = ${ue(r)}.id`;
461
468
  c.find(
462
469
  (h) => h.joinTableName === a && h.on === l
463
470
  ) || c.push({
464
471
  joinTableName: a,
465
472
  on: l
466
473
  });
467
- const p = J(n, t.metadata), d = `"${s}".id = "${a}".${o.columnName}`;
468
- p.find((h) => h.joinTableName === s && h.on === d) || p.push({
474
+ const p = J(n, t.metadata), u = `"${s}".id = "${a}".${o.columnName}`;
475
+ p.find((h) => h.joinTableName === s && h.on === u) || p.push({
469
476
  joinTableName: s,
470
- on: d
477
+ on: u
471
478
  });
472
- }, J = (e, n) => (e.joinMap.has(n) || e.joinMap.set(n, []), e.joinMap.get(n)), Oe = (e, n) => {
479
+ }, J = (e, n) => (e.joinMap.has(n) || e.joinMap.set(n, []), e.joinMap.get(n)), Ce = (e, n) => {
473
480
  let t = n, s = 1;
474
481
  for (; e.usedAliases.has(t); )
475
482
  t = `${n}_${s}`, s++;
476
483
  return e.usedAliases.add(t), t;
477
- }, St = (e, n) => {
484
+ }, bt = (e, n) => {
478
485
  if (e?.length)
479
486
  return e.map((t) => {
480
487
  const s = ae(t.field, n);
481
- return `${w}."${s}" ${t.sort}`;
488
+ return `${A}."${s}" ${t.sort}`;
482
489
  }).join(", ");
483
490
  }, U = (e, n = /* @__PURE__ */ new Map(), t, s) => {
484
491
  if (!e?.rules) return "";
485
492
  const r = e.rules.map(
486
- (o) => fe(o) ? U(o, n, t, s) : ht(o, n, t, s, U)
493
+ (o) => fe(o) ? U(o, n, t, s) : mt(o, n, t, s, U)
487
494
  ).filter(Boolean);
488
495
  return r.length ? r.length === 1 ? r[0] : `(${r.join(fe(e) ? ` ${e.combinator} ` : " ")})` : "";
489
496
  }, Me = (e) => {
490
497
  const { tableName: n, where: t, limit: s, offset: r, orderBy: o, join: i, hasJoin: c, metadata: a } = e;
491
- let l = `${w}.rowid as ${q}, ${w}.*`;
498
+ let l = `${A}.rowid as ${x}, ${A}.*`;
492
499
  if (a.features?.tree?.hasChildren && a.features?.tree?.type === "adjacency-list") {
493
- const d = a.relationMap.get("parent").columnName;
494
- l += `, EXISTS(SELECT 1 FROM "${n}" __sub WHERE __sub."${d}" = ${w}."id") AS "hasChildren"`;
500
+ const u = a.relationMap.get("parent").columnName;
501
+ l += `, EXISTS(SELECT 1 FROM "${n}" __sub WHERE __sub."${u}" = ${A}."id") AS "hasChildren"`;
495
502
  }
496
- const u = [`SELECT ${c ? "DISTINCT " : ""}${l} FROM "${n}" ${w}`];
497
- return i && u.push(i), t && u.push(` WHERE ${t}`), o && u.push(` ORDER BY ${o}`), s && u.push(` LIMIT ${s}`), r && u.push(` OFFSET ${r}`), u.join("") + ";";
498
- }, Ce = (e, n, t) => {
503
+ const d = [`SELECT ${c ? "DISTINCT " : ""}${l} FROM "${n}" ${A}`];
504
+ 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("") + ";";
505
+ }, Le = (e, n, t) => {
499
506
  const s = {
500
507
  joinMap: /* @__PURE__ */ new Map(),
501
508
  usedAliases: /* @__PURE__ */ new Set(),
502
509
  fieldAliasMap: /* @__PURE__ */ new Map(),
503
510
  relationAliasMap: /* @__PURE__ */ new Map()
504
511
  };
505
- Je(t, (o, i, c) => {
512
+ Xe(t, (o, i, c) => {
506
513
  const a = i;
507
514
  o !== "field" || !a.includes(".") || Rt(e, s, n, a, c);
508
515
  });
509
516
  const r = [];
510
517
  for (const [o, i] of s.joinMap.entries()) {
511
- const c = A(o);
518
+ const c = S(o);
512
519
  for (const a of i)
513
520
  r.push(` LEFT JOIN "${c}" "${a.joinTableName}" ON ${a.on}`);
514
521
  }
515
522
  return { joinSQL: r.join(""), fieldAliasMap: s.fieldAliasMap };
516
- }, bt = (e, n, t, s) => {
523
+ }, It = (e, n, t, s) => {
517
524
  if (s.length <= 1) return !1;
518
525
  const [r, ...o] = s, i = n.propertyMap.get(r);
519
526
  if (i && i.type === E.keyValue && o.length > 0) {
520
527
  const c = o.join(".");
521
- return e.fieldAliasMap.set(t, `json_extract(${w}."${i.columnName}", '$.${c}')`), !0;
528
+ return e.fieldAliasMap.set(t, `json_extract(${A}."${i.columnName}", '$.${c}')`), !0;
522
529
  }
523
530
  return !1;
524
531
  }, Rt = (e, n, t, s, r) => {
@@ -530,96 +537,96 @@ const le = (e, n) => {
530
537
  return;
531
538
  }
532
539
  const c = s.replace(`.${i.propertyName}`, "");
533
- Ie(e, n, i.relations, c);
534
- const a = i.relations[i.relations.length - 1], l = Q(i.relations, c, a.relation), u = G(n, l);
535
- n.fieldAliasMap.set(s, `"${u}"."${i.property.columnName}"`);
540
+ Oe(e, n, i.relations, c);
541
+ const a = i.relations[i.relations.length - 1], l = Q(i.relations, c, a.relation), d = G(n, l);
542
+ n.fieldAliasMap.set(s, `"${d}"."${i.property.columnName}"`);
536
543
  return;
537
544
  } catch {
538
545
  }
539
- bt(n, t, s, o) || $t(e, n, t, s, o);
540
- }, It = (e, n) => {
541
- const t = A(e), s = [n.id];
546
+ It(n, t, s, o) || Tt(e, n, t, s, o);
547
+ }, Ot = (e, n) => {
548
+ const t = S(e), s = [n.id];
542
549
  return {
543
550
  sql: `DELETE FROM "${t}" WHERE id = ?;`,
544
551
  params: s
545
552
  };
546
- }, Le = (e, n, t) => {
547
- const s = A(e), r = be(e, n);
553
+ }, je = (e, n, t) => {
554
+ const s = S(e), r = Ie(e, n);
548
555
  t?.userId && (e.propertyMap.has("createdBy") && (r.createdBy = t.userId), e.propertyMap.has("updatedBy") && (r.updatedBy = t.userId));
549
556
  const o = /* @__PURE__ */ new Date();
550
557
  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);
551
558
  const i = le(e, r), c = Object.keys(i), a = Array(c.length).fill("?").join(","), l = Object.values(i);
552
559
  let p = `${t?.useReplace ? "INSERT OR REPLACE" : "INSERT"} INTO "${s}" (${c.join(",")}) VALUES (${a})`;
553
- return t?.returning !== !1 ? p += ` RETURNING rowid as ${q}, *;` : p += ";", {
560
+ return t?.returning !== !1 ? p += ` RETURNING rowid as ${x}, *;` : p += ";", {
554
561
  sql: p,
555
562
  params: l
556
563
  };
557
564
  }, de = (e, n, t, s) => {
558
- const r = at(e, t);
565
+ const r = it(e, t);
559
566
  e.propertyMap.has("updatedAt") && (r.updatedAt = s?.updatedAt ?? /* @__PURE__ */ new Date()), s?.userId && e.propertyMap.has("updatedBy") && (r.updatedBy = s.userId);
560
- const o = le(e, r), i = Object.keys(o).map((d) => `${d} = ?`).join(","), c = [...Object.values(o)], a = A(e), l = Array.isArray(n), u = l ? n : [n];
567
+ const o = le(e, r), i = Object.keys(o).map((u) => `${u} = ?`).join(","), c = [...Object.values(o)], a = S(e), l = Array.isArray(n), d = l ? n : [n];
561
568
  let p = `UPDATE "${a}" SET ${i} WHERE id `;
562
- return l ? p += `in (${u.map((d) => I(d.id)).join(",")})` : (c.push(n.id), p += "= ?"), s?.returning !== !1 ? p += ` RETURNING rowid as ${q}, *;` : p += ";", {
569
+ return l ? p += `in (${d.map((u) => T(u.id)).join(",")})` : (c.push(n.id), p += "= ?"), s?.returning !== !1 ? p += ` RETURNING rowid as ${x}, *;` : p += ";", {
563
570
  sql: p,
564
571
  params: c
565
572
  };
566
- }, Ot = (e, n, t) => {
573
+ }, Ct = (e, n, t) => {
567
574
  if (t.groupBy)
568
575
  throw new R("groupBy not supported yet");
569
- 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}`];
576
+ const { joinSQL: s, fieldAliasMap: r } = Le(e, n, t.where), o = S(n), i = U(t.where, r, n, e), c = [`SELECT COUNT(${A}.rowid) AS count FROM "${o}" ${A}`];
570
577
  return s && c.push(s), i && c.push(` WHERE ${i}`), { sql: c.join("") };
571
578
  }, ie = (e, n, t) => {
572
- const { joinSQL: s, fieldAliasMap: r } = Ce(e, n, t.where);
579
+ const { joinSQL: s, fieldAliasMap: r } = Le(e, n, t.where);
573
580
  return { sql: Me({
574
- tableName: A(n),
581
+ tableName: S(n),
575
582
  where: U(t.where, r, n, e),
576
- orderBy: St(t.orderBy, n),
583
+ orderBy: bt(t.orderBy, n),
577
584
  join: s,
578
585
  hasJoin: !!s,
579
586
  metadata: n,
580
587
  limit: t.limit,
581
588
  offset: t.offset
582
589
  }) };
583
- }, Mt = (e, n) => {
590
+ }, ge = (e, n) => {
584
591
  const t = n.map(() => "?").join(",");
585
592
  return { sql: Me({
586
- tableName: A(e),
587
- where: `${q} IN (${t})`,
593
+ tableName: S(e),
594
+ where: `${x} IN (${t})`,
588
595
  metadata: e
589
596
  }), params: n };
590
597
  }, X = (e, n, t, s = !1, r = !1) => {
591
- const o = [], i = T(n), c = e.rxdb.entityManager;
598
+ const o = [], i = w(n), c = e.rxdb.entityManager;
592
599
  return t.results.forEach(({ columns: a, rows: l }) => {
593
- const u = a.findIndex((d) => d === "id"), p = a.findIndex((d) => d === q);
594
- l.forEach((d) => {
595
- const f = d[u];
600
+ const d = a.findIndex((u) => u === "id"), p = a.findIndex((u) => u === x);
601
+ l.forEach((u) => {
602
+ const f = u[d];
596
603
  let h;
597
604
  if (e.rxdb.entityManager.hasEntityRef(n, f)) {
598
605
  h = c.getEntityRef(n, f);
599
- const y = oe(i, a, d);
600
- if (i.computedPropertyMap.forEach((N, $) => {
601
- $ in y && (h[$] = y[$]);
606
+ const _ = oe(i, a, u);
607
+ if (i.computedPropertyMap.forEach((g, $) => {
608
+ $ in _ && (h[$] = _[$]);
602
609
  }), s) {
603
- const N = F(h);
604
- N.origin = { ...y }, Object.assign(h, y);
610
+ const g = F(h);
611
+ g.origin = { ..._ }, Object.assign(h, _);
605
612
  }
606
613
  } else {
607
614
  if (r)
608
615
  return;
609
616
  {
610
- const y = oe(i, a, d);
611
- h = c.createEntityRef(n, y);
617
+ const _ = oe(i, a, u);
618
+ h = c.createEntityRef(n, _);
612
619
  }
613
620
  }
614
621
  o.push(h);
615
622
  const m = F(h);
616
623
  if (m.local = !0, m.modified = !1, p !== -1) {
617
- const y = BigInt(d[p]);
618
- e.cacheRowIdEntity(y, h);
624
+ const _ = BigInt(u[p]);
625
+ e.cacheRowIdEntity(_, h);
619
626
  }
620
627
  });
621
628
  }), o;
622
- }, Ct = (e, n, t) => {
629
+ }, Mt = (e, n, t) => {
623
630
  t.forEach((s) => {
624
631
  if (e.rxdb.entityManager.hasEntityRef(n, s)) {
625
632
  const r = e.rxdb.entityManager.getEntityRef(n, s), o = F(r);
@@ -627,14 +634,26 @@ const le = (e, n) => {
627
634
  }
628
635
  });
629
636
  };
630
- class Lt extends Be {
637
+ class Lt extends Ue {
631
638
  constructor(n, t) {
632
- super(n.rxdb, t), this.adapter = n, this.metadata = T(t);
639
+ super(n.rxdb, t), this.adapter = n, this.metadata = w(t);
633
640
  }
634
641
  metadata;
635
642
  async findByRowIds(n) {
636
- const { sql: t, params: s } = Mt(this.metadata, n), r = await this.adapter.query(t, s);
637
- return this.addQueryCache(r);
643
+ if (n.length <= 999) {
644
+ const { sql: i, params: c } = ge(this.metadata, n), a = await this.adapter.query(i, c);
645
+ return this.addQueryCache(a);
646
+ }
647
+ const s = [];
648
+ for (let i = 0; i < n.length; i += 999) {
649
+ const c = n.slice(i, i + 999), { sql: a, params: l } = ge(this.metadata, c);
650
+ s.push(await this.adapter.query(a, l));
651
+ }
652
+ const r = s[0], o = {
653
+ ...r,
654
+ results: [{ columns: r.results[0]?.columns ?? [], rows: s.flatMap((i) => i.results[0]?.rows ?? []) }]
655
+ };
656
+ return this.addQueryCache(o);
638
657
  }
639
658
  /**
640
659
  * 添加缓存
@@ -645,7 +664,7 @@ class Lt extends Be {
645
664
  return X(this.adapter, this.EntityType, n, t);
646
665
  }
647
666
  }
648
- class je extends Lt {
667
+ class qe extends Lt {
649
668
  async get(n) {
650
669
  const t = {
651
670
  where: {
@@ -664,11 +683,11 @@ class je extends Lt {
664
683
  return this.addQueryCache(r);
665
684
  }
666
685
  async count(n) {
667
- const { sql: t, params: s } = Ot(this.adapter, this.metadata, n);
686
+ const { sql: t, params: s } = Ct(this.adapter, this.metadata, n);
668
687
  return (await this.adapter.query(t, s)).results[0].rows[0][0];
669
688
  }
670
689
  async create(n) {
671
- const { sql: t, params: s } = Le(this.metadata, n, this.rxdb.context), r = await this.adapter.query(t, s);
690
+ const { sql: t, params: s } = je(this.metadata, n, this.rxdb.context), r = await this.adapter.query(t, s);
672
691
  return this.addQueryCache(r, !0), n;
673
692
  }
674
693
  async update(n, t) {
@@ -680,10 +699,10 @@ class je extends Lt {
680
699
  t.origin = structuredClone({ ...n }), t.modified = !1, t.removed = !0;
681
700
  };
682
701
  if (t.local === !0) {
683
- const { sql: o, params: i } = It(this.metadata, n);
702
+ const { sql: o, params: i } = Ot(this.metadata, n);
684
703
  return await this.adapter.query(o, i), s(), n;
685
704
  }
686
- const r = T(n);
705
+ const r = w(n);
687
706
  throw new R(`Remove Error${r.name}(${n.id}) not saved local.`);
688
707
  }
689
708
  }
@@ -697,7 +716,7 @@ const jt = (e) => {
697
716
  case 23:
698
717
  return "UPDATE";
699
718
  }
700
- }, 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 = [
719
+ }, qt = "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), vt = [
701
720
  {
702
721
  name: "MemoryVFS",
703
722
  vfsModule: () => import("wa-sqlite/src/examples/MemoryVFS.js").then((e) => e.MemoryVFS),
@@ -796,10 +815,10 @@ const jt = (e) => {
796
815
  }, Pt = async (e) => {
797
816
  const n = xe(e), t = e.async ?? !0;
798
817
  let s;
799
- t ? s = qt : s = Dt;
818
+ t ? s = xt : s = Dt;
800
819
  const [r, o] = await Promise.all([s(), n.vfsModule()]), i = {};
801
820
  e.locateFile ? i.locateFile = e.locateFile : e.wasmPath && (i.locateFile = () => e.wasmPath);
802
- const c = await r(i), a = et(c), l = await o.create(e.vfs, c, n.vfsOptions);
821
+ const c = await r(i), a = tt(c), l = await o.create(e.vfs, c, n.vfsOptions);
803
822
  return a.vfs_register(l, !0), a;
804
823
  };
805
824
  async function kt(e, n, t, s) {
@@ -811,13 +830,13 @@ async function kt(e, n, t, s) {
811
830
  };
812
831
  for await (const l of e.statements(n, t)) {
813
832
  c && (e.reset(l), e.bind_collection(l, c), c = void 0);
814
- const u = [];
815
- for (; await e.step(l) === tt; ) {
816
- const d = e.row(l);
817
- u.push(d);
833
+ const d = [];
834
+ for (; await e.step(l) === nt; ) {
835
+ const u = e.row(l);
836
+ d.push(u);
818
837
  }
819
838
  const p = e.column_names(l);
820
- p.length && o.push({ columns: p, rows: u }), i += e.changes(n);
839
+ p.length && o.push({ columns: p, rows: d }), i += e.changes(n);
821
840
  }
822
841
  return {
823
842
  ...a,
@@ -827,8 +846,8 @@ async function kt(e, n, t, s) {
827
846
  }
828
847
  const Ft = {
829
848
  BALANCED: 16
830
- }, Ne = Ft.BALANCED, Bt = 50 * 1024, Ut = /* @__PURE__ */ new Set(["public$rxdb_change", "public$rxdb_branch", "public$rxdb_migration"]);
831
- class Wt extends Xe {
849
+ }, $e = Ft.BALANCED, Bt = 50 * 1024, Ut = /* @__PURE__ */ new Set(["public$rxdb_change", "public$rxdb_branch", "public$rxdb_migration"]);
850
+ class Wt extends Ye {
832
851
  #n;
833
852
  #e;
834
853
  #a = !1;
@@ -836,7 +855,7 @@ class Wt extends Xe {
836
855
  #c;
837
856
  #s = [];
838
857
  #t;
839
- #r = Ne;
858
+ #r = $e;
840
859
  /**
841
860
  * 初始化数据库
842
861
  *
@@ -845,9 +864,9 @@ class Wt extends Xe {
845
864
  */
846
865
  async init(n, t) {
847
866
  if (this.#a) return;
848
- this.#a = !0, this.#c = new we(1);
867
+ this.#a = !0, this.#c = new Ae(1);
849
868
  const s = await Pt(t), r = `${n}.sqlite`;
850
- this.#e = await s.open_v2(r), this.#n = s, this.#u(s, this.#e), this.#r = t?.batchTimeout ?? Ne;
869
+ this.#e = await s.open_v2(r), this.#n = s, this.#u(s, this.#e), this.#r = t?.batchTimeout ?? $e;
851
870
  const o = t?.cacheSizeKb ?? Bt;
852
871
  await this.execute(`
853
872
  PRAGMA temp_store = memory;
@@ -864,7 +883,7 @@ class Wt extends Xe {
864
883
  */
865
884
  async version() {
866
885
  const n = await this.execute("SELECT sqlite_version()");
867
- return Ye(n, "results[0].rows[0][0]");
886
+ return ze(n, "results[0].rows[0][0]");
868
887
  }
869
888
  /**
870
889
  * 断开数据库连接
@@ -938,7 +957,7 @@ class Wt extends Xe {
938
957
  }
939
958
  }
940
959
  const W = /* @__PURE__ */ new Map(), Kt = 128;
941
- function qe(e, n = "") {
960
+ function De(e, n = "") {
942
961
  const t = `${e}\0${n}`;
943
962
  let s = W.get(t);
944
963
  if (!s) {
@@ -948,9 +967,9 @@ function qe(e, n = "") {
948
967
  return s;
949
968
  }
950
969
  function Ht(e, n) {
951
- e.create_function(n, "regexp", 2, Ae | Se, 0, (t, s) => {
970
+ e.create_function(n, "regexp", 2, Se | be, 0, (t, s) => {
952
971
  try {
953
- const r = qe(e.value_text(s[0])), o = e.value_text(s[1]);
972
+ const r = De(e.value_text(s[0])), o = e.value_text(s[1]);
954
973
  e.result(t, r.test(o) ? 1 : 0);
955
974
  } catch {
956
975
  e.result(t, 0);
@@ -958,30 +977,30 @@ function Ht(e, n) {
958
977
  });
959
978
  }
960
979
  function Vt(e, n) {
961
- e.create_function(n, "regexp_replace", -1, Ae | Se, 0, (t, s) => {
980
+ e.create_function(n, "regexp_replace", -1, Se | be, 0, (t, s) => {
962
981
  if (s.length < 3) {
963
982
  e.result(t, "");
964
983
  return;
965
984
  }
966
985
  try {
967
986
  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]) : "";
968
- e.result(t, o.replace(qe(r, c), i));
987
+ e.result(t, o.replace(De(r, c), i));
969
988
  } catch {
970
989
  e.result(t, e.value_text(s[1]));
971
990
  }
972
991
  });
973
992
  }
974
993
  async function Qt(e, n) {
975
- const { vfs: t, async: s, worker: r, wasmPath: o, locateFile: i, workerInstance: c, sharedWorkerInstance: a, sharedWorker: l } = n, u = {
994
+ const { vfs: t, async: s, worker: r, wasmPath: o, locateFile: i, workerInstance: c, sharedWorkerInstance: a, sharedWorker: l } = n, d = {
976
995
  vfs: t,
977
996
  async: s,
978
997
  worker: r,
979
998
  wasmPath: o,
980
999
  locateFile: i
981
1000
  };
982
- xe(u);
1001
+ xe(d);
983
1002
  let p;
984
- return r && c ? p = Ee(c) : l && a ? p = Ee(a.port) : p = new Wt(), await p.init(e, u), p;
1003
+ return r && c ? p = Ee(c) : l && a ? p = Ee(a.port) : p = new Wt(), await p.init(e, d), p;
985
1004
  }
986
1005
  const Gt = (e, n) => n.map((t) => {
987
1006
  const s = e.rxdb.schemaManager.getEntityMetadata(t.entity, t.namespace);
@@ -1043,28 +1062,28 @@ const Gt = (e, n) => n.map((t) => {
1043
1062
  e.rxdb.dispatchEvent(new ce(s));
1044
1063
  }
1045
1064
  }, Xt = (e, n) => {
1046
- const { tableName: t } = n, [s, r] = ot(t), o = e.rxdb.schemaManager.getEntityTypeByTableName(r, s);
1065
+ const { tableName: t } = n, [s, r] = at(t), o = e.rxdb.schemaManager.getEntityTypeByTableName(r, s);
1047
1066
  if (!o) return;
1048
- const i = e.getRepository(o), c = T(o), a = T(B);
1067
+ const i = e.getRepository(o), c = w(o), a = w(B);
1049
1068
  i.findByRowIds(n.rowIds).then((l) => {
1050
1069
  try {
1051
- let u = l;
1070
+ let d = l;
1052
1071
  if (c === a) {
1053
- if (u = Gt(e, l), n.type === D.SQLITE_INSERT) {
1054
- Jt(e, u);
1055
- const p = u.map((d) => ({
1072
+ if (d = Gt(e, l), n.type === D.SQLITE_INSERT) {
1073
+ Jt(e, d);
1074
+ const p = d.map((u) => ({
1056
1075
  namespace: c.namespace,
1057
1076
  entity: c.name,
1058
- type: d.type,
1059
- id: d.id,
1060
- patch: { ...d },
1077
+ type: u.type,
1078
+ id: u.id,
1079
+ patch: { ...u },
1061
1080
  inversePatch: null,
1062
- recordAt: d.createdAt
1081
+ recordAt: u.createdAt
1063
1082
  }));
1064
1083
  e.rxdb.dispatchEvent(new K(p));
1065
1084
  }
1066
1085
  } else {
1067
- const p = jt(n.type), d = u.map((f) => {
1086
+ const p = jt(n.type), u = d.map((f) => {
1068
1087
  const h = {
1069
1088
  namespace: c.namespace,
1070
1089
  entity: c.name,
@@ -1096,48 +1115,48 @@ const Gt = (e, n) => n.map((t) => {
1096
1115
  switch (p) {
1097
1116
  case "UPDATE":
1098
1117
  e.rxdb.dispatchEvent(
1099
- new Y(d)
1118
+ new Y(u)
1100
1119
  );
1101
1120
  break;
1102
1121
  case "DELETE":
1103
1122
  e.rxdb.dispatchEvent(
1104
- new ce(d)
1123
+ new ce(u)
1105
1124
  );
1106
1125
  break;
1107
1126
  case "INSERT":
1108
1127
  e.rxdb.dispatchEvent(
1109
- new K(d)
1128
+ new K(u)
1110
1129
  );
1111
1130
  break;
1112
1131
  }
1113
1132
  }
1114
- } catch (u) {
1115
- console.error(`[rxdb-adapter-sqlite] Error processing ${t} change event:`, u);
1133
+ } catch (d) {
1134
+ console.error(`[rxdb-adapter-sqlite] Error processing ${t} change event:`, d);
1116
1135
  }
1117
1136
  }).catch((l) => {
1118
1137
  console.error(`[rxdb-adapter-sqlite] Failed to query ${t} change (rowIds: ${n.rowIds}):`, l);
1119
1138
  });
1120
1139
  }, z = (e, n, t) => {
1121
- const { isCount: s, isFindDescendants: r, entityId: o, where: i } = t, c = !o, a = A(n), u = n.relationMap.get("parent").columnName;
1140
+ const { isCount: s, isFindDescendants: r, entityId: o, where: i } = t, c = !o, a = S(n), d = n.relationMap.get("parent").columnName;
1122
1141
  let p = "";
1123
1142
  const f = [`c.__level < ${t.level || 0}`, i && U(i, /* @__PURE__ */ new Map(), n, e)].filter(Boolean).join(" AND ");
1124
1143
  f && (p = `WHERE ${f}`);
1125
1144
  let h = "__children.*";
1126
- s ? c ? h = "count(*)" : h = "count(*)-1" : t.hasChildren && (h += `, EXISTS(SELECT 1 FROM "${a}" __sub WHERE __sub."${u}" = __children.id) AS hasChildren`);
1127
- const _ = [];
1145
+ s ? c ? h = "count(*)" : h = "count(*)-1" : t.hasChildren && (h += `, EXISTS(SELECT 1 FROM "${a}" __sub WHERE __sub."${d}" = __children.id) AS hasChildren`);
1146
+ const y = [];
1128
1147
  let m;
1129
- return c ? m = `"${u}" is null` : (m = "id = ?", _.push(o)), { sql: `WITH RECURSIVE __children AS (
1130
- SELECT *,rowid as ${q}, 0 AS __level
1148
+ return c ? m = `"${d}" is null` : (m = "id = ?", y.push(o)), { sql: `WITH RECURSIVE __children AS (
1149
+ SELECT *,rowid as ${x}, 0 AS __level
1131
1150
  FROM "${a}"
1132
1151
  WHERE ${m}
1133
1152
  UNION ALL
1134
- SELECT children.*,children.rowid as ${q}, c.__level + 1 AS __level
1153
+ SELECT children.*,children.rowid as ${x}, c.__level + 1 AS __level
1135
1154
  FROM "${a}" children
1136
- JOIN __children c ON ${r ? `children."${u}" = c.id` : `children.id = c."${u}"`}
1155
+ JOIN __children c ON ${r ? `children."${d}" = c.id` : `children.id = c."${d}"`}
1137
1156
  ${p}
1138
1157
  )
1139
1158
 
1140
- SELECT ${h} FROM __children ORDER BY __level, id;`, params: _ };
1159
+ SELECT ${h} FROM __children ORDER BY __level, id;`, params: y };
1141
1160
  }, Yt = (e, n, t) => z(e, n, {
1142
1161
  ...t,
1143
1162
  isFindDescendants: !0,
@@ -1147,7 +1166,7 @@ SELECT ${h} FROM __children ORDER BY __level, id;`, params: _ };
1147
1166
  isFindDescendants: !1,
1148
1167
  hasChildren: n.features?.tree?.hasChildren
1149
1168
  }), en = (e, n, t) => z(e, n, { ...t, isFindDescendants: !1, isCount: !0 });
1150
- class tn extends je {
1169
+ class tn extends qe {
1151
1170
  async findDescendants(n) {
1152
1171
  const { sql: t, params: s } = Yt(this.adapter, this.metadata, n), r = await this.adapter.query(t, s);
1153
1172
  return this.addQueryCache(r, !0);
@@ -1166,76 +1185,76 @@ class tn extends je {
1166
1185
  }
1167
1186
  }
1168
1187
  const nn = (e, n) => {
1169
- const t = A(e), s = n.map((r) => I(r.id)).join(",");
1188
+ const t = S(e), s = n.map((r) => T(r.id)).join(",");
1170
1189
  return `DELETE FROM "${t}" WHERE id in (${s});`;
1171
- }, De = (e, n, t) => {
1172
- 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 = [
1190
+ }, ve = (e, n, t) => {
1191
+ const s = S(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 = [
1173
1192
  ...Array.from(e.propertyMap.values()).map((f) => f.columnName),
1174
1193
  ...e.foreignKeyColumnNames || e.foreignKeyNames
1175
- ], u = [], p = [];
1194
+ ], d = [], p = [];
1176
1195
  return n.forEach((f) => {
1177
- const h = be(e, f);
1196
+ const h = Ie(e, f);
1178
1197
  r && (h.createdBy = t.userId), o && (h.updatedBy = t.userId), i && h.createdAt === void 0 && (h.createdAt = a), c && h.updatedAt === void 0 && (h.updatedAt = a);
1179
- const _ = le(e, h), m = [];
1180
- l.forEach((y) => {
1181
- m.push("?"), u.push(_[y] ?? null);
1198
+ const y = le(e, h), m = [];
1199
+ l.forEach((_) => {
1200
+ m.push("?"), d.push(y[_] ?? null);
1182
1201
  }), p.push(`(${m.join(",")})`);
1183
- }), { sql: `INSERT INTO "${s}" (${l.join(",")}) VALUES ${p.join(",")};`, params: u };
1184
- }, $e = async (e, n) => {
1202
+ }), { sql: `INSERT INTO "${s}" (${l.join(",")}) VALUES ${p.join(",")};`, params: d };
1203
+ }, Te = async (e, n) => {
1185
1204
  const t = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Date();
1186
1205
  let r = "";
1187
1206
  const o = /* @__PURE__ */ new Set();
1188
1207
  return n.create.forEach((i, c) => {
1189
1208
  const a = Array.from(i);
1190
1209
  a.forEach((m) => o.add(m));
1191
- const l = T(c), u = De(l, a, e.rxdb.context), p = k(u.sql, u.params), d = a.map((m) => m.id);
1192
- d.forEach((m) => t.set(m, c));
1210
+ const l = w(c), d = ve(l, a, e.rxdb.context), p = k(d.sql, d.params), u = a.map((m) => m.id);
1211
+ u.forEach((m) => t.set(m, c));
1193
1212
  const { sql: f, params: h } = ie(e, l, {
1194
1213
  where: {
1195
1214
  combinator: "and",
1196
- rules: [{ field: "id", operator: "in", value: d }]
1215
+ rules: [{ field: "id", operator: "in", value: u }]
1197
1216
  }
1198
- }), _ = k(f, h);
1199
- r += p + _;
1217
+ }), y = k(f, h);
1218
+ r += p + y;
1200
1219
  }), n.update.forEach((i, c) => {
1201
1220
  const a = Array.from(i);
1202
1221
  a.forEach((f) => o.add(f));
1203
- const l = T(c);
1222
+ const l = w(c);
1204
1223
  a.forEach((f) => {
1205
- const h = F(f), _ = de(l, f, h.patch, {
1224
+ const h = F(f), y = de(l, f, h.patch, {
1206
1225
  ...e.rxdb.context,
1207
1226
  returning: !1,
1208
1227
  updatedAt: s
1209
1228
  });
1210
1229
  t.set(f.id, c);
1211
- const m = k(_.sql, _.params);
1230
+ const m = k(y.sql, y.params);
1212
1231
  r += m;
1213
1232
  });
1214
- const u = a.map((f) => f.id);
1215
- u.forEach((f) => t.set(f, c));
1233
+ const d = a.map((f) => f.id);
1234
+ d.forEach((f) => t.set(f, c));
1216
1235
  const p = ie(e, l, {
1217
1236
  where: {
1218
1237
  combinator: "and",
1219
- rules: [{ field: "id", operator: "in", value: u }]
1238
+ rules: [{ field: "id", operator: "in", value: d }]
1220
1239
  }
1221
- }), d = k(p.sql, p.params);
1222
- r += d;
1223
- }), n.remove.forEach((i, c) => {
1224
- const a = Array.from(i), l = T(c), u = nn(l, a);
1240
+ }), u = k(p.sql, p.params);
1225
1241
  r += u;
1242
+ }), n.remove.forEach((i, c) => {
1243
+ const a = Array.from(i), l = w(c), d = nn(l, a);
1244
+ r += d;
1226
1245
  }), r && (await e.query(r)).results.forEach(({ columns: c, rows: a }) => {
1227
- const l = c.findIndex((d) => d === "id"), u = c.findIndex((d) => d === q), p = e.rxdb.entityManager;
1228
- a.forEach((d) => {
1229
- const f = d[l], h = t.get(f), _ = T(h);
1246
+ const l = c.findIndex((u) => u === "id"), d = c.findIndex((u) => u === x), p = e.rxdb.entityManager;
1247
+ a.forEach((u) => {
1248
+ const f = u[l], h = t.get(f), y = w(h);
1230
1249
  if (p.hasEntityRef(h, f)) {
1231
- const m = p.getEntityRef(h, f), y = oe(_, c, d);
1232
- e.getRepository(h).updateEntity(m, y);
1233
- const N = F(m);
1234
- if (N.local = !0, u !== -1) {
1235
- const $ = BigInt(d[u]);
1250
+ const m = p.getEntityRef(h, f), _ = oe(y, c, u);
1251
+ e.getRepository(h).updateEntity(m, _);
1252
+ const g = F(m);
1253
+ if (g.local = !0, d !== -1) {
1254
+ const $ = BigInt(u[d]);
1236
1255
  e.cacheRowIdEntity($, m);
1237
1256
  }
1238
- N.modified = !1;
1257
+ g.modified = !1;
1239
1258
  }
1240
1259
  });
1241
1260
  }), n.remove.forEach(
@@ -1246,17 +1265,17 @@ const nn = (e, n) => {
1246
1265
  })
1247
1266
  ), Array.from(o);
1248
1267
  }, sn = (e, n) => {
1249
- const t = A(n);
1268
+ const t = S(n);
1250
1269
  let s = `CREATE TABLE "${t}" (`;
1251
1270
  const r = [], o = [], i = [];
1252
1271
  n.propertyMap.forEach((a) => {
1253
1272
  const l = a.columnName;
1254
- let u = `"${l}" ${rt(a)}`;
1255
- 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) && a.default !== void 0) {
1256
- let d = a.default;
1257
- me(a.default) && (d = a.default()), d === "CURRENT_TIMESTAMP" ? u += " DEFAULT(strftime('%FT%H:%M:%fZ'))" : (d = V(d, a), a.type === E.boolean || a.type === E.integer || a.type === E.number ? u += ` DEFAULT ${d}` : u += ` DEFAULT '${d}'`);
1273
+ let d = `"${l}" ${ot(a)}`;
1274
+ 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) {
1275
+ let u = a.default;
1276
+ 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}'`);
1258
1277
  }
1259
- switch (a.nullable || (u += " NOT NULL"), a.type) {
1278
+ switch (a.nullable || (d += " NOT NULL"), a.type) {
1260
1279
  case E.json:
1261
1280
  case E.keyValue:
1262
1281
  case E.stringArray:
@@ -1268,42 +1287,42 @@ const nn = (e, n) => {
1268
1287
  break;
1269
1288
  case E.enum:
1270
1289
  if ("enum" in a && a.enum && a.enum.length > 0) {
1271
- const d = a.enum.map((f) => `'${String(f).replace(/'/g, "''")}'`).join(",");
1272
- o.push(`CHECK (${l} in(${d}))`);
1290
+ const u = a.enum.map((f) => `'${String(f).replace(/'/g, "''")}'`).join(",");
1291
+ a.nullable ? o.push(`CHECK (${l} IN(${u},null))`) : o.push(`CHECK (${l} in(${u}))`);
1273
1292
  }
1274
1293
  break;
1275
1294
  }
1276
1295
  a.unique && r.push(
1277
- `CREATE UNIQUE INDEX ${ye(n, a)} on "${t}"(${l});`
1278
- ), i.push(u);
1296
+ `CREATE UNIQUE INDEX ${_e(n, a)} on "${t}"(${l});`
1297
+ ), i.push(d);
1279
1298
  }), n.relationMap.forEach((a) => {
1280
- if (a.kind === b.ONE_TO_ONE || a.kind === b.MANY_TO_ONE) {
1281
- const l = a.columnName, u = e.rxdb.schemaManager.getEntityMetadata(
1299
+ if (a.kind === I.ONE_TO_ONE || a.kind === I.MANY_TO_ONE) {
1300
+ const l = a.columnName, d = e.rxdb.schemaManager.getEntityMetadata(
1282
1301
  a.mappedEntity,
1283
1302
  a.mappedNamespace
1284
1303
  );
1285
1304
  let p = "TEXT";
1286
- const d = u.propertyMap.get("id");
1287
- d.type === E.integer ? p = "INTEGER" : (d.type === E.uuid || d.type === E.string) && (p = "TEXT");
1305
+ const u = d.propertyMap.get("id");
1306
+ u.type === E.integer ? p = "INTEGER" : (u.type === E.uuid || u.type === E.string) && (p = "TEXT");
1288
1307
  const f = a.onDelete === "SET NULL" || a.onUpdate === "SET NULL";
1289
1308
  let h = `"${l}" ${p}`;
1290
- if (!a.nullable && !f && (h += " NOT NULL"), a.kind === b.MANY_TO_ONE && a.default !== void 0) {
1291
- const _ = a;
1292
- let m = _.default;
1293
- me(_.default) && (m = _.default()), h += ` DEFAULT ${m}`;
1309
+ if (!a.nullable && !f && (h += " NOT NULL"), a.kind === I.MANY_TO_ONE && a.default !== void 0) {
1310
+ const y = a;
1311
+ let m = y.default;
1312
+ me(y.default) && (m = y.default()), h += ` DEFAULT ${m}`;
1294
1313
  }
1295
1314
  if (a.mappedEntity) {
1296
- const _ = e.rxdb.schemaManager.getEntityMetadata(
1315
+ const y = e.rxdb.schemaManager.getEntityMetadata(
1297
1316
  a.mappedEntity,
1298
1317
  a.mappedNamespace
1299
1318
  ), m = P(
1300
- _?.tableName ?? a.mappedEntity,
1319
+ y?.tableName ?? a.mappedEntity,
1301
1320
  a.mappedNamespace
1302
1321
  );
1303
1322
  h += ` REFERENCES ${m}(id)`, a.onDelete && (h += ` ON DELETE ${a.onDelete}`), a.onUpdate && (h += ` ON UPDATE ${a.onUpdate}`);
1304
1323
  }
1305
- i.push(h), (a.unique || a.kind === b.ONE_TO_ONE) && r.push(
1306
- `CREATE UNIQUE INDEX ${ye(n, a)} on "${t}"(${l});`
1324
+ i.push(h), (a.unique || a.kind === I.ONE_TO_ONE) && r.push(
1325
+ `CREATE UNIQUE INDEX ${_e(n, a)} on "${t}"(${l});`
1307
1326
  );
1308
1327
  }
1309
1328
  });
@@ -1317,50 +1336,50 @@ ${a}`).join(","), s += `
1317
1336
  return r.length && (s += `
1318
1337
  ` + r.join(`
1319
1338
  `)), n.indexes && n.indexes.length > 0 && n.indexes.forEach((a) => {
1320
- const l = `idx_${n.name}_${a.name}`, u = a.properties?.map((d) => {
1321
- const f = n.propertyMap.get(d);
1339
+ const l = `idx_${n.name}_${a.name}`, d = a.properties?.map((u) => {
1340
+ const f = n.propertyMap.get(u);
1322
1341
  if (f) return `"${f.columnName}"`;
1323
- const h = n.foreignKeyNames, _ = n.foreignKeyColumnNames;
1324
- if (h && _) {
1325
- const m = h.indexOf(d);
1326
- if (m >= 0) return `"${_[m]}"`;
1342
+ const h = n.foreignKeyNames, y = n.foreignKeyColumnNames;
1343
+ if (h && y) {
1344
+ const m = h.indexOf(u);
1345
+ if (m >= 0) return `"${y[m]}"`;
1327
1346
  }
1328
- return `"${d}"`;
1347
+ return `"${u}"`;
1329
1348
  }).join(", ") || `"${a.name}"`, p = a.unique ? "UNIQUE " : "";
1330
1349
  s += `
1331
- CREATE ${p}INDEX "${l}" ON "${t}"(${u});`;
1350
+ CREATE ${p}INDEX "${l}" ON "${t}"(${d});`;
1332
1351
  }), s;
1333
1352
  }, rn = (e, n) => sn(e, n), pe = (e, n = {}) => {
1334
- const t = A(e), s = T(B), r = A(s), { propertyMap: o, name: i, foreignKeyNames: c, foreignKeyColumnNames: a, namespace: l } = e, u = [];
1335
- for (const [g, C] of o)
1336
- g !== "id" && u.push({ jsName: g, dbColumn: C.columnName });
1353
+ const t = S(e), s = w(B), r = S(s), { propertyMap: o, name: i, foreignKeyNames: c, foreignKeyColumnNames: a, namespace: l } = e, d = [];
1354
+ for (const [N, M] of o)
1355
+ N !== "id" && d.push({ jsName: N, dbColumn: M.columnName });
1337
1356
  const p = a || c;
1338
- for (let g = 0; g < c.length; g++)
1339
- c[g] !== "id" && u.push({ jsName: c[g], dbColumn: p[g] });
1340
- const d = " type, namespace, entity, branchId, transactionId, entityId, inversePatch, patch", { transactionId: f, branchId: h } = n, _ = I(f), m = I(h || "main"), y = `'${l}','${i}',${m},${_}`, N = (g) => g === E.stringArray || g === E.numberArray || g === E.json || g === E.keyValue, $ = (g, C, x) => {
1341
- const Z = o.get(C);
1342
- 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}`;
1343
- }, S = (g, C, x) => o.get(C)?.type === E.boolean ? `CASE WHEN ${x}.${g} = 1 THEN 1 ELSE 0 END` : `${x}.${g}`, L = `
1357
+ for (let N = 0; N < c.length; N++)
1358
+ c[N] !== "id" && d.push({ jsName: c[N], dbColumn: p[N] });
1359
+ const u = " type, namespace, entity, branchId, transactionId, entityId, inversePatch, patch", { transactionId: f, branchId: h } = n, y = T(f), m = T(h || "main"), _ = `'${l}','${i}',${m},${y}`, g = (N) => N === E.stringArray || N === E.numberArray || N === E.json || N === E.keyValue, $ = (N, M, q) => {
1360
+ const Z = o.get(M);
1361
+ return Z?.type === E.boolean ? `CASE WHEN ${q}.${N} = 1 THEN 1 ELSE 0 END` : g(Z?.type) ? `CASE WHEN ${q}.${N} IS NOT NULL THEN json(${q}.${N}) ELSE NULL END` : `${q}.${N}`;
1362
+ }, b = (N, M, q) => o.get(M)?.type === E.boolean ? `CASE WHEN ${q}.${N} = 1 THEN 1 ELSE 0 END` : `${q}.${N}`, L = `
1344
1363
  DROP TRIGGER IF EXISTS ${t}_insert;
1345
1364
  CREATE TRIGGER ${t}_insert AFTER INSERT ON "${t}"
1346
1365
  BEGIN
1347
- INSERT INTO ${r} (${d}) VALUES (
1348
- 'INSERT',${y}, NEW.id, NULL,
1349
- json_object(${u.map((g) => `'${g.jsName}', ${$(g.dbColumn, g.jsName, "NEW")}`).join(`,
1366
+ INSERT INTO ${r} (${u}) VALUES (
1367
+ 'INSERT',${_}, NEW.id, NULL,
1368
+ json_object(${d.map((N) => `'${N.jsName}', ${$(N.dbColumn, N.jsName, "NEW")}`).join(`,
1350
1369
  `)})
1351
1370
  );
1352
- END;`, O = (g) => u.map((C) => {
1353
- const x = N(o.get(C.jsName)?.type) ? 1 : 0;
1354
- 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}`;
1371
+ END;`, O = (N) => d.map((M) => {
1372
+ const q = g(o.get(M.jsName)?.type) ? 1 : 0;
1373
+ return `SELECT '${M.jsName}' AS key, ${b(M.dbColumn, M.jsName, N)} AS value, ${q} AS is_json WHERE OLD.${M.dbColumn} IS NOT NEW.${M.dbColumn}`;
1355
1374
  }).join(`
1356
- UNION `), M = `
1375
+ UNION `), C = `
1357
1376
  DROP TRIGGER IF EXISTS ${t}_update;
1358
1377
  CREATE TRIGGER ${t}_update AFTER UPDATE ON "${t}"
1359
- WHEN ( ${u.map((g) => `OLD.${g.dbColumn} IS NOT NEW.${g.dbColumn}`).join(` OR
1378
+ WHEN ( ${d.map((N) => `OLD.${N.dbColumn} IS NOT NEW.${N.dbColumn}`).join(` OR
1360
1379
  `)} )
1361
1380
  BEGIN
1362
- INSERT INTO ${r} (${d}) VALUES (
1363
- 'UPDATE',${y}, NEW.id,
1381
+ INSERT INTO ${r} (${u}) VALUES (
1382
+ 'UPDATE',${_}, NEW.id,
1364
1383
  (
1365
1384
  SELECT json_group_object(key, CASE WHEN is_json AND value IS NOT NULL THEN json(value) ELSE value END) FROM (
1366
1385
  ${O("OLD")}
@@ -1376,19 +1395,19 @@ CREATE ${p}INDEX "${l}" ON "${t}"(${u});`;
1376
1395
  DROP TRIGGER IF EXISTS ${t}_delete;
1377
1396
  CREATE TRIGGER ${t}_delete AFTER DELETE ON "${t}"
1378
1397
  BEGIN
1379
- INSERT INTO ${r} (${d}) VALUES (
1380
- 'DELETE',${y}, OLD.id,
1381
- json_object(${u.map((g) => `'${g.jsName}', ${$(g.dbColumn, g.jsName, "OLD")}`).join(`,
1398
+ INSERT INTO ${r} (${u}) VALUES (
1399
+ 'DELETE',${_}, OLD.id,
1400
+ json_object(${d.map((N) => `'${N.jsName}', ${$(N.dbColumn, N.jsName, "OLD")}`).join(`,
1382
1401
  `)}), NULL
1383
1402
  );
1384
1403
  END;`;
1385
1404
  return L + `
1386
- ` + M + `
1405
+ ` + C + `
1387
1406
  ` + j;
1388
1407
  }, on = (e, n, t) => {
1389
1408
  let s = "";
1390
1409
  for (let r = 0; r < n.length; r++) {
1391
- const o = n[r], i = T(o);
1410
+ const o = n[r], i = w(o);
1392
1411
  if (s += `
1393
1412
  ` + rn(e, i), i.log !== !1) {
1394
1413
  const c = pe(i);
@@ -1399,11 +1418,11 @@ CREATE ${p}INDEX "${l}" ON "${t}"(${u});`;
1399
1418
  if (t) {
1400
1419
  const r = /* @__PURE__ */ new Map();
1401
1420
  t.forEach((o) => {
1402
- const i = T(o);
1421
+ const i = w(o);
1403
1422
  r.has(i) === !1 && r.set(i, /* @__PURE__ */ new Set()), r.get(i).add(o);
1404
1423
  });
1405
1424
  for (const o of r.keys()) {
1406
- const i = De(
1425
+ const i = ve(
1407
1426
  o,
1408
1427
  Array.from(r.get(o)),
1409
1428
  e.rxdb.context
@@ -1414,106 +1433,108 @@ CREATE ${p}INDEX "${l}" ON "${t}"(${u});`;
1414
1433
  }
1415
1434
  return s;
1416
1435
  }, an = (e) => {
1417
- const n = A(e);
1436
+ const n = S(e);
1418
1437
  return `DROP TRIGGER IF EXISTS ${n}_insert;
1419
1438
  DROP TRIGGER IF EXISTS ${n}_update;
1420
1439
  DROP TRIGGER IF EXISTS ${n}_delete;`;
1421
- }, ve = (e) => {
1440
+ }, Pe = (e) => {
1422
1441
  let n = "";
1423
1442
  return e.rxdb.config.entities.forEach((t) => {
1424
- const s = T(t);
1443
+ const s = w(t);
1425
1444
  s.log !== !1 && (n += an(s));
1426
1445
  }), n;
1427
- }, Pe = (e, n) => {
1446
+ }, ke = (e, n) => {
1428
1447
  const { deletes: t, inserts: s, updates: r } = n, o = { deletes: [], inserts: [], updates: [] }, i = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map();
1429
- for (const [d, f] of t.entries()) {
1430
- const [h, _, m] = ee(d), y = `${h}:${_}`, N = i.get(y);
1431
- N ? N.add(m) : i.set(y, /* @__PURE__ */ new Set([m]));
1432
- let $ = c.get(y);
1433
- $ || ($ = /* @__PURE__ */ new Map(), c.set(y, $)), $.set(m, {
1448
+ for (const [u, f] of t.entries()) {
1449
+ const [h, y, m] = ee(u), _ = `${h}:${y}`, g = i.get(_);
1450
+ g ? g.add(m) : i.set(_, /* @__PURE__ */ new Set([m]));
1451
+ let $ = c.get(_);
1452
+ $ || ($ = /* @__PURE__ */ new Map(), c.set(_, $)), $.set(m, {
1434
1453
  patch: f.patch,
1435
1454
  inversePatch: f.inversePatch
1436
1455
  });
1437
1456
  }
1438
- for (const [d, f] of i) {
1439
- const [h, _] = d.split(":"), m = e.rxdb.schemaManager.getEntityMetadata(_, h), y = A(m), N = m.propertyMap.get("id").type === "integer";
1457
+ for (const [u, f] of i) {
1458
+ const [h, y] = u.split(":"), m = e.rxdb.schemaManager.getEntityMetadata(y, h), _ = S(m), g = m.propertyMap.get("id").type === "integer";
1440
1459
  let $ = "";
1441
- N ? $ = Array.from(f).join(",") : $ = Array.from(f).map((L) => I(L)).join(",");
1442
- const S = `DELETE FROM "${y}" WHERE id in (${$});`;
1460
+ g ? $ = Array.from(f).join(",") : $ = Array.from(f).map((L) => T(L)).join(",");
1461
+ const b = `DELETE FROM "${_}" WHERE id in (${$});`;
1443
1462
  o.deletes.push({
1444
1463
  metadata: m,
1445
1464
  ids: f,
1446
- sql: S,
1447
- changes: c.get(d)
1465
+ sql: b,
1466
+ changes: c.get(u)
1448
1467
  });
1449
1468
  }
1450
1469
  const a = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map();
1451
- for (const [d, f] of s.entries()) {
1452
- const [h, _, m] = ee(d), y = `${h}:${_}`, N = a.get(y), $ = { ...f.patch, id: m };
1453
- N ? N.push($) : a.set(y, [$]);
1454
- let S = l.get(y);
1455
- S || (S = /* @__PURE__ */ new Map(), l.set(y, S)), S.set(m, {
1470
+ for (const [u, f] of s.entries()) {
1471
+ const [h, y, m] = ee(u), _ = `${h}:${y}`, g = a.get(_), $ = { ...f.patch, id: m };
1472
+ g ? g.push($) : a.set(_, [$]);
1473
+ let b = l.get(_);
1474
+ b || (b = /* @__PURE__ */ new Map(), l.set(_, b)), b.set(m, {
1456
1475
  patch: f.patch,
1457
1476
  inversePatch: f.inversePatch
1458
1477
  });
1459
1478
  }
1460
- for (const [d, f] of a) {
1461
- const [h, _] = d.split(":"), m = e.rxdb.schemaManager.getEntityMetadata(_, h);
1462
- let y = "";
1463
- f.forEach((N) => {
1464
- for (const [O, M] of m.propertyMap)
1465
- N[O] === void 0 && M.default !== void 0 && (typeof M.default == "function" ? N[O] = M.default() : N[O] = M.default);
1466
- const { sql: $, params: S } = Le(m, N, {
1479
+ for (const [u, f] of a) {
1480
+ const [h, y] = u.split(":"), m = e.rxdb.schemaManager.getEntityMetadata(y, h);
1481
+ let _ = "";
1482
+ f.forEach((g) => {
1483
+ for (const [O, C] of m.propertyMap)
1484
+ g[O] === void 0 && C.default !== void 0 && (typeof C.default == "function" ? g[O] = C.default() : g[O] = C.default);
1485
+ const { sql: $, params: b } = je(m, g, {
1467
1486
  useReplace: !0
1468
- }), L = k($, S);
1469
- y += L;
1487
+ }), L = k($, b);
1488
+ _ += L;
1470
1489
  }), o.inserts.push({
1471
1490
  metadata: m,
1472
- ids: new Set(f.filter((N) => N != null).map((N) => N.id)),
1473
- sql: y,
1474
- changes: l.get(d)
1491
+ ids: new Set(f.filter((g) => g != null).map((g) => g.id)),
1492
+ sql: _,
1493
+ changes: l.get(u)
1475
1494
  });
1476
1495
  }
1477
- const u = /* @__PURE__ */ new Map(), p = /* @__PURE__ */ new Map();
1478
- for (const [d, f] of r.entries()) {
1479
- const [h, _, m] = ee(d), y = `${h}:${_}`, N = u.get(y), $ = { ...f.patch, id: m };
1480
- N ? N.push($) : u.set(y, [$]);
1481
- let S = p.get(y);
1482
- S || (S = /* @__PURE__ */ new Map(), p.set(y, S));
1483
- const L = e.rxdb.schemaManager.getEntityMetadata(_, h), O = f.patch ? { ...f.patch } : null, M = f.inversePatch ? { ...f.inversePatch } : null;
1496
+ const d = /* @__PURE__ */ new Map(), p = /* @__PURE__ */ new Map();
1497
+ for (const [u, f] of r.entries()) {
1498
+ const [h, y, m] = ee(u), _ = `${h}:${y}`, g = d.get(_), $ = { ...f.patch, id: m };
1499
+ g ? g.push($) : d.set(_, [$]);
1500
+ let b = p.get(_);
1501
+ b || (b = /* @__PURE__ */ new Map(), p.set(_, b));
1502
+ const L = e.rxdb.schemaManager.getEntityMetadata(y, h), O = f.patch ? { ...f.patch } : null, C = f.inversePatch ? { ...f.inversePatch } : null;
1484
1503
  L && (O && Object.keys(O).forEach((j) => {
1485
- const g = L.propertyMap.get(j);
1486
- g && (O[j] = v(O[j], g));
1487
- }), M && Object.keys(M).forEach((j) => {
1488
- const g = L.propertyMap.get(j);
1489
- g && (M[j] = v(M[j], g));
1490
- })), S.set(m, {
1504
+ const N = L.propertyMap.get(j);
1505
+ N && (O[j] = v(O[j], N));
1506
+ }), C && Object.keys(C).forEach((j) => {
1507
+ const N = L.propertyMap.get(j);
1508
+ N && (C[j] = v(C[j], N));
1509
+ })), b.set(m, {
1491
1510
  patch: O,
1492
- inversePatch: M
1511
+ inversePatch: C
1493
1512
  });
1494
1513
  }
1495
- for (const [d, f] of u) {
1496
- const [h, _] = d.split(":"), m = e.rxdb.schemaManager.getEntityMetadata(_, h);
1497
- let y = "";
1498
- f.forEach((N) => {
1514
+ for (const [u, f] of d) {
1515
+ const [h, y] = u.split(":"), m = e.rxdb.schemaManager.getEntityMetadata(y, h);
1516
+ if (!m)
1517
+ throw new Error(`找不到 namespace=${h} entityName=${y} 对应的实体元数据`);
1518
+ let _ = "";
1519
+ f.forEach((g) => {
1499
1520
  let $;
1500
- m.propertyMap.has("updatedAt") && ($ = N.updatedAt);
1501
- const { id: S, ...L } = N, { sql: O, params: M } = de(m, { id: S }, L, {
1521
+ m.propertyMap.has("updatedAt") && ($ = g.updatedAt);
1522
+ const { id: b, ...L } = g, { sql: O, params: C } = de(m, { id: b }, L, {
1502
1523
  updatedAt: $
1503
1524
  });
1504
- y += k(O, M);
1525
+ _ += k(O, C);
1505
1526
  }), o.updates.push({
1506
1527
  metadata: m,
1507
- ids: new Set(f.filter((N) => N != null).map((N) => N.id)),
1508
- sql: y,
1509
- changes: p.get(d)
1528
+ ids: new Set(f.filter((g) => g != null).map((g) => g.id)),
1529
+ sql: _,
1530
+ changes: p.get(u)
1510
1531
  });
1511
1532
  }
1512
1533
  return o;
1513
- }, ke = (e, n) => {
1534
+ }, Fe = (e, n) => {
1514
1535
  let t = "";
1515
1536
  e.rxdb.config.entities.forEach((i) => {
1516
- const c = T(i);
1537
+ const c = w(i);
1517
1538
  if (c.log !== !1)
1518
1539
  try {
1519
1540
  const a = pe(c, {
@@ -1524,7 +1545,7 @@ DROP TRIGGER IF EXISTS ${n}_delete;`;
1524
1545
  console.warn(`Failed to generate trigger SQL for entity ${c.name}:`, a);
1525
1546
  }
1526
1547
  });
1527
- const s = T(H), r = A(s), o = n.replace(/'/g, "''");
1548
+ const s = w(H), r = S(s), o = n.replace(/'/g, "''");
1528
1549
  return t += `
1529
1550
  UPDATE "${r}"
1530
1551
  SET
@@ -1537,76 +1558,81 @@ DROP TRIGGER IF EXISTS ${n}_delete;`;
1537
1558
  ELSE updatedAt
1538
1559
  END
1539
1560
  WHERE id = '${o}' OR activated = 1
1540
- RETURNING rowid as ${q},*;
1561
+ RETURNING rowid as ${x},*;
1541
1562
  `, t;
1542
1563
  }, cn = async (e, n) => {
1543
- const { branchId: t, actions: s } = n, r = s && Pe(e, s);
1564
+ const { branchId: t, actions: s } = n, r = s && ke(e, s);
1544
1565
  let o;
1545
1566
  try {
1546
1567
  await e.transaction(async (a) => {
1547
- const l = ve(e);
1568
+ const l = Pe(e);
1548
1569
  if (l && await a.execute(l), r) {
1549
1570
  if (r.deletes.length)
1550
- for (const u of r.deletes)
1551
- await a.execute(u.sql);
1571
+ for (const d of r.deletes)
1572
+ await a.execute(d.sql);
1552
1573
  if (r.inserts.length)
1553
- for (const u of r.inserts)
1554
- u.successResults = await a.execute(u.sql);
1574
+ for (const d of r.inserts)
1575
+ d.successResults = await a.execute(d.sql);
1555
1576
  if (r.updates.length)
1556
- for (const u of r.updates)
1557
- u.successResults = await a.execute(u.sql);
1577
+ for (const d of r.updates)
1578
+ d.successResults = await a.execute(d.sql);
1558
1579
  }
1559
1580
  s?.updateRxDBChangeSequence !== void 0 && await e.setRxDBChangeSequence(s.updateRxDBChangeSequence);
1560
1581
  }, !1);
1561
- const i = ke(e, t);
1582
+ const i = Fe(e, t);
1562
1583
  o = await e.internalQuery(i);
1563
1584
  const c = (a, l) => {
1564
1585
  if (l.length === 0) return;
1565
- const u = l.map((p) => {
1566
- const d = { ...p };
1586
+ const d = l.map((p) => {
1587
+ const u = { ...p };
1567
1588
  return {
1568
1589
  namespace: a.namespace,
1569
1590
  entity: a.name,
1570
1591
  type: "UPDATE",
1571
1592
  id: p.id,
1572
- patch: d,
1573
- inversePatch: d,
1593
+ patch: u,
1594
+ inversePatch: u,
1574
1595
  recordAt: p.createdAt
1575
1596
  };
1576
1597
  });
1577
- e.rxdb.dispatchEvent(new Y(u));
1598
+ e.rxdb.dispatchEvent(new Y(d));
1578
1599
  };
1579
1600
  if (o) {
1580
- const a = X(e, H, o, !0), l = T(H);
1601
+ const a = X(e, H, o, !0), l = w(H);
1581
1602
  c(l, a);
1582
1603
  }
1583
- r && Fe(e, r);
1604
+ r && Be(e, r);
1584
1605
  } catch (i) {
1585
1606
  throw new R(`switch branch ${t} failed`, { cause: i });
1586
1607
  }
1587
1608
  };
1588
- async function ln(e, n, t = !1) {
1589
- if (await e.transaction(async (s) => {
1609
+ async function ln(e, n, t = !1, s) {
1610
+ if (await e.transaction(async (r) => {
1590
1611
  if (t) {
1591
- const r = ve(e);
1592
- r && await s.execute(r);
1612
+ const o = Pe(e);
1613
+ o && await r.execute(o);
1614
+ }
1615
+ for (const o of n.deletes)
1616
+ await r.execute(o.sql);
1617
+ for (const o of n.inserts)
1618
+ o.successResults = await r.execute(o.sql);
1619
+ for (const o of n.updates)
1620
+ o.successResults = await r.execute(o.sql);
1621
+ if (t && s?.length) {
1622
+ console.log("localChanges", s);
1623
+ const o = un(s);
1624
+ o && await r.execute(o);
1593
1625
  }
1594
- for (const r of n.deletes)
1595
- await s.execute(r.sql);
1596
- for (const r of n.inserts)
1597
- r.successResults = await s.execute(r.sql);
1598
- for (const r of n.updates)
1599
- r.successResults = await s.execute(r.sql);
1600
1626
  }, !1), t) {
1601
- const s = await e.rxdb.versionManager.getCurrentBranch(), r = ke(e, s.id);
1602
- await e.internalQuery(r);
1627
+ const r = await e.rxdb.versionManager.getCurrentBranch(), o = Fe(e, r.id);
1628
+ await e.internalQuery(o);
1603
1629
  }
1604
- Fe(e, n);
1630
+ Be(e, n);
1605
1631
  }
1606
- function Fe(e, n) {
1632
+ function Be(e, n) {
1607
1633
  for (const t of n.deletes) {
1608
1634
  const s = te(t.metadata);
1609
- Ct(e, s, Array.from(t.ids));
1635
+ Mt(e, s, Array.from(t.ids));
1610
1636
  const r = t.metadata, o = Array.from(t.ids).map((i) => {
1611
1637
  const c = t.changes.get(String(i));
1612
1638
  return {
@@ -1652,10 +1678,19 @@ function Fe(e, n) {
1652
1678
  e.rxdb.dispatchEvent(new Y(i));
1653
1679
  }
1654
1680
  }
1655
- const Te = (e, n, t) => {
1681
+ function un(e) {
1682
+ if (!e.length) return "";
1683
+ const n = w(B), t = S(n), s = "type, namespace, entity, branchId, transactionId, entityId, remoteId, inversePatch, patch", r = e.map((o) => {
1684
+ const i = T(o.type), c = T(o.namespace), a = T(o.entity), l = T(o.branchId), d = T(o.transactionId ?? null), p = T(o.entityId), u = o.remoteId != null ? o.remoteId : "NULL", f = o.inversePatch != null ? T(JSON.stringify(o.inversePatch)) : "NULL", h = o.patch != null ? T(JSON.stringify(o.patch)) : "NULL";
1685
+ return `(${i},${c},${a},${l},${d},${p},${u},${f},${h})`;
1686
+ });
1687
+ return `INSERT INTO "${t}" (${s}) VALUES ${r.join(`,
1688
+ `)};`;
1689
+ }
1690
+ const we = (e, n, t) => {
1656
1691
  let s = "";
1657
1692
  return e.rxdb.config.entities.forEach((r) => {
1658
- const o = T(r);
1693
+ const o = w(r);
1659
1694
  if (o.log !== !1) {
1660
1695
  const i = pe(o, {
1661
1696
  branchId: n,
@@ -1666,17 +1701,17 @@ const Te = (e, n, t) => {
1666
1701
  }
1667
1702
  }), s;
1668
1703
  };
1669
- class yn extends He {
1704
+ class Nn extends Ve {
1670
1705
  constructor(n, t) {
1671
1706
  super(n), this.options = t, t.repositories && Object.keys(t.repositories).forEach((r) => {
1672
1707
  this.repository(r, t.repositories[r]);
1673
1708
  });
1674
- const s = ze((r) => {
1709
+ const s = Ze((r) => {
1675
1710
  this.#t || Xt(this, r);
1676
1711
  });
1677
1712
  this.#r().then((r) => {
1678
1713
  r.addEventListener(D.SQLITE_INSERT, s), r.addEventListener(D.SQLITE_UPDATE, s), r.addEventListener(D.SQLITE_DELETE, s);
1679
- }).catch((r) => console.error("[RxDBAdapterSqlite] Failed to register event listeners", r)), this.repository("Repository", je), this.repository("TreeRepository", tn);
1714
+ }).catch((r) => console.error("[RxDBAdapterSqlite] Failed to register event listeners", r)), this.repository("Repository", qe), this.repository("TreeRepository", tn);
1680
1715
  }
1681
1716
  // SQLite 客户端缓存
1682
1717
  #n;
@@ -1690,7 +1725,7 @@ class yn extends He {
1690
1725
  #a = /* @__PURE__ */ new Map();
1691
1726
  #o = /* @__PURE__ */ new WeakMap();
1692
1727
  // 查询任务队列执行器
1693
- #c = new we(1);
1728
+ #c = new Ae(1);
1694
1729
  // sqlite 客户端
1695
1730
  #s;
1696
1731
  // 是否已断开连接
@@ -1698,14 +1733,14 @@ class yn extends He {
1698
1733
  /**
1699
1734
  * 适配器名称
1700
1735
  */
1701
- name = xt;
1736
+ name = qt;
1702
1737
  /**
1703
1738
  * 获取实体仓库
1704
1739
  * @param EntityType 实体类
1705
1740
  */
1706
1741
  getRepository(n) {
1707
1742
  if (!this.repository_cache.has(n)) {
1708
- const t = T(n), s = this.repository_map.get(t.repository);
1743
+ const t = w(n), s = this.repository_map.get(t.repository);
1709
1744
  if (!s)
1710
1745
  throw new R(`Repository '${t.repository}' not found`);
1711
1746
  const r = new s(this, n);
@@ -1759,15 +1794,15 @@ class yn extends He {
1759
1794
  * @param options
1760
1795
  */
1761
1796
  async mutations(n) {
1762
- return this.#e ? await $e(this, n) : await this.transaction(() => $e(this, n));
1797
+ return this.#e ? await Te(this, n) : await this.transaction(() => Te(this, n));
1763
1798
  }
1764
1799
  /**
1765
1800
  * 判断表是否存在
1766
1801
  * @param EntityType
1767
1802
  */
1768
1803
  async isTableExisted(n) {
1769
- const t = T(n), s = A(t), { sql: r, params: o } = nt(s), i = await this.#i(r, o);
1770
- return st(i) === !1;
1804
+ const t = w(n), s = S(t), { sql: r, params: o } = st(s), i = await this.#i(r, o);
1805
+ return rt(i) === !1;
1771
1806
  }
1772
1807
  /**
1773
1808
  * 创建表和初始化数据
@@ -1791,14 +1826,16 @@ class yn extends He {
1791
1826
  * 应用压缩后的变更到本地实体表
1792
1827
  *
1793
1828
  * 将 actions 转换为 SQL 操作实体表。
1829
+ * 当 disableTriggers=true(pull 场景)且提供了 localChanges 时,
1830
+ * 手动将变更记录插入 RxDBChange 表。
1794
1831
  *
1795
1832
  * @param actions - 压缩后的变更操作
1796
- * @param _localChanges - (已废弃)不再使用
1833
+ * @param localChanges - 需要记录到 RxDBChange 表的变更(pull 场景)
1797
1834
  * @param disableTriggers - 是否禁用触发器(用于 pull 等操作,避免创建 RxDBChange)
1798
1835
  */
1799
1836
  async mergeChanges(n, t, s = !1) {
1800
- const r = Pe(this, n);
1801
- await ln(this, r, s);
1837
+ const r = ke(this, n);
1838
+ await ln(this, r, s, t);
1802
1839
  }
1803
1840
  /**
1804
1841
  * 缓存 RowId 与实体映射
@@ -1853,13 +1890,13 @@ class yn extends He {
1853
1890
  async transaction(n, t = !0) {
1854
1891
  await this.rxdb.connect(this.name);
1855
1892
  const s = await this.#r();
1856
- this.#e = !0, this.rxdb.dispatchEvent(un);
1893
+ this.#e = !0, this.rxdb.dispatchEvent(dn);
1857
1894
  let r = "", o = "";
1858
1895
  if (t) {
1859
- const i = Ve(), c = await this.rxdb.versionManager.getCurrentBranch();
1896
+ const i = Qe(), c = await this.rxdb.versionManager.getCurrentBranch();
1860
1897
  if (!c)
1861
1898
  throw new Error("currentBranch is undefined! Cannot start transaction with logging.");
1862
- r = Te(this, c.id, i), o = Te(this, c.id);
1899
+ r = we(this, c.id, i), o = we(this, c.id);
1863
1900
  }
1864
1901
  await s.execute(`
1865
1902
  BEGIN;
@@ -1869,12 +1906,12 @@ class yn extends He {
1869
1906
  try {
1870
1907
  const i = await n(s);
1871
1908
  return await s.execute(`${o}
1872
- COMMIT;`), this.#e = !1, this.rxdb.dispatchEvent(pn), i;
1909
+ COMMIT;`), this.#e = !1, this.rxdb.dispatchEvent(fn), i;
1873
1910
  } catch (i) {
1874
1911
  try {
1875
1912
  await s.execute("ROLLBACK");
1876
1913
  } finally {
1877
- this.#e = !1, this.rxdb.dispatchEvent(dn);
1914
+ this.#e = !1, this.rxdb.dispatchEvent(pn);
1878
1915
  }
1879
1916
  const c = i?.message || "Transaction Error";
1880
1917
  throw new R(c, { cause: i });
@@ -1906,7 +1943,7 @@ COMMIT;`), this.#e = !1, this.rxdb.dispatchEvent(pn), i;
1906
1943
  * @returns
1907
1944
  */
1908
1945
  async getRxDBChangeSequence() {
1909
- const n = _e(B);
1946
+ const n = ye(B);
1910
1947
  return (await this.#i("SELECT seq FROM sqlite_sequence WHERE name = ?", [n])).results[0]?.rows?.[0]?.[0] ?? 0;
1911
1948
  }
1912
1949
  /**
@@ -1914,7 +1951,7 @@ COMMIT;`), this.#e = !1, this.rxdb.dispatchEvent(pn), i;
1914
1951
  * @param sequence 序列值
1915
1952
  */
1916
1953
  async setRxDBChangeSequence(n) {
1917
- const t = _e(B);
1954
+ const t = ye(B);
1918
1955
  await this.#i("UPDATE sqlite_sequence SET seq = ? WHERE name = ?", [n, t]);
1919
1956
  }
1920
1957
  // ============================================
@@ -1941,7 +1978,7 @@ COMMIT;`), this.#e = !1, this.rxdb.dispatchEvent(pn), i;
1941
1978
  return se(/* @__PURE__ */ new Map());
1942
1979
  const s = t.map(() => "?").join(", "), r = `SELECT id, updatedAt FROM "${n}" WHERE id IN (${s})`;
1943
1980
  return re(this.internalQuery(r, t)).pipe(
1944
- Ze((o) => {
1981
+ et((o) => {
1945
1982
  const i = /* @__PURE__ */ new Map();
1946
1983
  if (o.results?.[0]?.rows)
1947
1984
  for (const c of o.results[0].rows)
@@ -2023,16 +2060,16 @@ COMMIT;`), this.#e = !1, this.rxdb.dispatchEvent(pn), i;
2023
2060
  return this.#a.has(n) === !1 && this.#a.set(n, /* @__PURE__ */ new Map()), this.#a.get(n);
2024
2061
  }
2025
2062
  }
2026
- const un = new Ue(), dn = new We(), pn = new Ke();
2063
+ const dn = new We(), pn = new Ke(), fn = new He();
2027
2064
  export {
2028
- q as ROWID,
2029
- yn as RxDBAdapterSqlite,
2065
+ x as ROWID,
2066
+ Nn as RxDBAdapterSqlite,
2030
2067
  R as RxDBAdapterSqliteError,
2031
2068
  Wt as SqliteClient,
2032
- je as SqliteRepository,
2069
+ qe as SqliteRepository,
2033
2070
  vt as WA_SQLITE_VFS_LIST,
2034
2071
  U as buildRuleGroup,
2035
2072
  P as sqliteGetTableName,
2036
- A as sqliteGetTableNameByMetadata,
2073
+ S as sqliteGetTableNameByMetadata,
2037
2074
  Pt as sqliteLoad
2038
2075
  };