@aiao/rxdb-adapter-sqlite 0.0.2 → 0.0.3

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.
Files changed (2) hide show
  1. package/index.js +48 -49
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -1,29 +1,29 @@
1
- import { RxDBError as me, PropertyType as m, RelationKind as I, getEntityMetadata as q, RxDBChange as Ee, RepositoryBase as ye, TRANSACTION_BEGIN as $e, TRANSACTION_COMMIT as be, TRANSACTION_ROLLBACK as Ne, getEntityStatus as re, isEntityMatchRuleGroup as R, isEntityEffectOrderBy as we, TransactionBeginEvent as Te, TransactionCommitEvent as ge, TransactionRollbackEvent as Ie, RxDBAdapterLocalBase as Oe, RxDBAdapterUpdateChangeEvent as Ae, RxDBAdapterInsertChangeEvent as qe, RxDBAdapterDeleteChangeEvent as Se } from "@aiao/rxdb";
1
+ import { RxDBError as me, PropertyType as y, RelationKind as I, getEntityMetadata as q, RxDBChange as Ee, RepositoryBase as ye, TRANSACTION_BEGIN as $e, TRANSACTION_COMMIT as be, TRANSACTION_ROLLBACK as Ne, getEntityStatus as re, isEntityMatchRuleGroup as R, isEntityEffectOrderBy as we, TransactionBeginEvent as Te, TransactionCommitEvent as ge, TransactionRollbackEvent as Ie, RxDBAdapterLocalBase as Oe, RxDBAdapterUpdateChangeEvent as Ae, RxDBAdapterInsertChangeEvent as qe, RxDBAdapterDeleteChangeEvent as Se } from "@aiao/rxdb";
2
2
  import { wrap as Re } from "comlink";
3
3
  import { Subject as ve, auditTime as xe, ReplaySubject as U, BehaviorSubject as ae, Observable as ie, shareReplay as L, switchMap as w, take as Q, takeUntil as K, map as f, firstValueFrom as O, tap as A, fromEvent as Ce, defer as ke, filter as Z, catchError as Le, of as Me, from as ee } from "rxjs";
4
4
  import { pickBy as oe, isFunction as je, get as De, traverseObjectKeys as Be } from "@aiao/utils";
5
5
  import { Factory as Fe, SQLITE_ROW as We } from "@journeyapps/wa-sqlite";
6
6
  const Pe = (r) => `SELECT * FROM sqlite_master WHERE type='table' AND name='${r}' limit 1;`, Ve = (r) => !!r.sql && (r.results.length === 0 || r.results[0].rows.length === 0), S = "_rowid", te = (r) => {
7
7
  switch (r.type) {
8
- case m.uuid:
9
- case m.string:
10
- case m.json:
8
+ case y.uuid:
9
+ case y.string:
10
+ case y.json:
11
11
  return "TEXT";
12
- case m.number:
12
+ case y.number:
13
13
  return "REAL";
14
- case m.integer:
15
- case m.boolean:
14
+ case y.integer:
15
+ case y.boolean:
16
16
  return "INTEGER";
17
- case m.date:
17
+ default:
18
18
  return "TEXT";
19
19
  }
20
20
  }, Ue = (r, e) => {
21
21
  if (r === null && e.nullable) return null;
22
22
  if (r !== void 0)
23
23
  switch (e.type) {
24
- case m.boolean:
24
+ case y.boolean:
25
25
  return r ? 1 : 0;
26
- case m.date:
26
+ case y.date:
27
27
  return r instanceof Date ? r.toISOString() : r;
28
28
  default:
29
29
  return r;
@@ -32,17 +32,17 @@ const Pe = (r) => `SELECT * FROM sqlite_master WHERE type='table' AND name='${r}
32
32
  if (r === null && e.nullable) return null;
33
33
  if (r !== void 0)
34
34
  switch (e.type) {
35
- case m.boolean:
35
+ case y.boolean:
36
36
  return !!r;
37
- case m.date:
37
+ case y.date:
38
38
  return r;
39
- case m.json:
39
+ case y.json:
40
40
  return r ? JSON.parse(r) : null;
41
41
  default:
42
42
  return r;
43
43
  }
44
- }, ce = (r, e) => `${e}$${r}`, y = (r) => ce(r.name, r.namespace), le = (r, e) => `idx_${r.name}_${e.name}`;
45
- class E extends me {
44
+ }, ce = (r, e) => `${e}$${r}`, E = (r) => ce(r.name, r.namespace), le = (r, e) => `idx_${r.name}_${e.name}`;
45
+ class m extends me {
46
46
  }
47
47
  const H = (r, e) => {
48
48
  const t = {};
@@ -64,7 +64,7 @@ const H = (r, e) => {
64
64
  }), s;
65
65
  }, Je = (r, e) => {
66
66
  let t = "";
67
- const s = y(e);
67
+ const s = E(e);
68
68
  return Array.from(e.relationMap.values()).forEach((n) => {
69
69
  switch (n.kind) {
70
70
  case I.ONE_TO_ONE:
@@ -83,21 +83,21 @@ ALTER TABLE ${s} ADD COLUMN "${a}" TEXT`, n.nullable || (t += " NOT NULL"), n.ma
83
83
  }
84
84
  }), t;
85
85
  }, Ye = (r, e) => {
86
- const t = y(e);
86
+ const t = E(e);
87
87
  let s = `CREATE TABLE ${t} (`;
88
88
  const n = [], a = [], o = [];
89
89
  e.propertyMap.forEach((i) => {
90
90
  let l = "";
91
- if (l += `"${i.name}"`, i.primary ? i.type === m.integer ? l += " INTEGER PRIMARY KEY AUTOINCREMENT" : l += ` ${te(i)} PRIMARY KEY` : l += ` ${te(i)}`, !i.nullable) l += " NOT NULL";
91
+ if (l += `"${i.name}"`, i.primary ? i.type === y.integer ? l += " INTEGER PRIMARY KEY AUTOINCREMENT" : l += ` ${te(i)} PRIMARY KEY` : l += ` ${te(i)}`, !i.nullable) l += " NOT NULL";
92
92
  else if (Reflect.get(i, "default") !== void 0) {
93
93
  let u = i.default;
94
94
  je(i.default) && (u = i.default()), l += ` DEFAULT ${String(u)}`;
95
95
  }
96
96
  switch (i.type) {
97
- case m.json:
97
+ case y.json:
98
98
  a.push(`CHECK ( JSON_VALID(${i.name})=1 )`);
99
99
  break;
100
- case m.boolean:
100
+ case y.boolean:
101
101
  a.push(`CHECK (${i.name} in(0,1))`);
102
102
  break;
103
103
  }
@@ -111,7 +111,7 @@ ALTER TABLE ${s} ADD COLUMN "${a}" TEXT`, n.nullable || (t += " NOT NULL"), n.ma
111
111
  ${i}`).join(","), s += `
112
112
  );`;
113
113
  else
114
- throw new E("columns is empty!");
114
+ throw new m("columns is empty!");
115
115
  return n.length && (s += `
116
116
  ` + n.join(`
117
117
  `)), s;
@@ -119,7 +119,7 @@ ${i}`).join(","), s += `
119
119
  let t = "";
120
120
  return t += Ye(r, e), t += Je(r, e), t;
121
121
  }, ze = (r, e) => {
122
- const t = y(e), s = q(Ee), n = y(s), { propertyMap: a, name: o, foreignKeyNames: c, namespace: i } = e, l = [...a.keys(), ...c].filter((d) => ["id", "updatedAt"].includes(d) === !1), u = "namespace, entity, type, entityId, inversePatch, patch", h = `CREATE TRIGGER ${t}_insert AFTER INSERT ON ${t}
122
+ const t = E(e), s = q(Ee), n = E(s), { propertyMap: a, name: o, foreignKeyNames: c, namespace: i } = e, l = [...a.keys(), ...c].filter((d) => ["id", "updatedAt"].includes(d) === !1), u = "namespace, entity, type, entityId, inversePatch, patch", h = `CREATE TRIGGER ${t}_insert AFTER INSERT ON ${t}
123
123
  BEGIN
124
124
  INSERT INTO ${n} (${u}) VALUES (
125
125
  '${i}', '${o}', 'INSERT', NEW.id, NULL,
@@ -245,10 +245,10 @@ const ue = "sqlite", Xe = () => import("@journeyapps/wa-sqlite/dist/wa-sqlite-as
245
245
  }
246
246
  ], de = (r) => {
247
247
  const e = et.find((a) => a.name === r.vfs), { vfs: t, async: s, worker: n } = r;
248
- if (!e) throw new E(`vfs ${t} not found`);
249
- if (s !== s) throw new E(`vfs ${t} not support async: ${s}`);
250
- if (n && !e.worker) throw new E(`vfs ${t} not support worker`);
251
- if (!n && !e.jsContext) throw new E(`vfs ${t} only support worker`);
248
+ if (!e) throw new m(`vfs ${t} not found`);
249
+ if (s !== s) throw new m(`vfs ${t} not support async: ${s}`);
250
+ if (n && !e.worker) throw new m(`vfs ${t} not support worker`);
251
+ if (!n && !e.jsContext) throw new m(`vfs ${t} only support worker`);
252
252
  return e;
253
253
  }, tt = async (r) => {
254
254
  const e = de(r), t = r.async ?? !0;
@@ -297,7 +297,6 @@ class nt {
297
297
  this.changeChanel = new BroadcastChannel(o), this.#e.update_hook(
298
298
  this.#t,
299
299
  (c, i, l, u) => {
300
- if (!l) return;
301
300
  const h = {
302
301
  type: c,
303
302
  dbName: i,
@@ -322,13 +321,13 @@ class nt {
322
321
  }
323
322
  }
324
323
  const rt = (r, e, t) => {
325
- const s = y(r), n = [e.id];
324
+ const s = E(r), n = [e.id];
326
325
  return {
327
326
  sql: `DELETE FROM ${s} WHERE id = ?;`,
328
327
  params: n
329
328
  };
330
329
  }, at = (r, e, t) => {
331
- const s = y(r), n = Ke(r, e);
330
+ const s = E(r), n = Ke(r, e);
332
331
  t?.userId && (n.createdBy = t.userId, n.updatedBy = t.userId), n.createdAt = /* @__PURE__ */ new Date(), n.updatedAt = n.createdAt;
333
332
  const a = Object.keys(n), o = H(r, n), c = Array(a.length).fill("?").join(","), i = Object.values(o);
334
333
  return {
@@ -338,7 +337,7 @@ const rt = (r, e, t) => {
338
337
  }, it = (r, e, t) => {
339
338
  const s = {};
340
339
  s.removedAt = /* @__PURE__ */ new Date(), t?.userId && (s.removedBy = t.userId);
341
- const n = y(r), o = Object.keys(s).map((l) => `${l} = ?`).join(","), c = H(r, s), i = [...Object.values(c), e.id];
340
+ const n = E(r), o = Object.keys(s).map((l) => `${l} = ?`).join(","), c = H(r, s), i = [...Object.values(c), e.id];
342
341
  return {
343
342
  sql: `UPDATE ${n} SET ${o} WHERE id = ? and removedAt is null and removedBy is null RETURNING rowid as ${S},*;`,
344
343
  params: i
@@ -348,7 +347,7 @@ const rt = (r, e, t) => {
348
347
  n.updatedAt = /* @__PURE__ */ new Date(), s?.userId && (n.updatedBy = s.userId);
349
348
  const a = Object.keys(n), o = H(r, n), c = a.map((h) => `${h} = ?`).join(","), i = [...Object.values(o), e.id];
350
349
  return {
351
- sql: `UPDATE ${y(r)} SET ${c} WHERE id = ? RETURNING rowid as ${S},*;`,
350
+ sql: `UPDATE ${E(r)} SET ${c} WHERE id = ? RETURNING rowid as ${S},*;`,
352
351
  params: i
353
352
  };
354
353
  }, J = (r) => r?.map((e) => `${e.field} ${e.order}`).join(", "), v = (r, e = /* @__PURE__ */ new Map()) => {
@@ -426,7 +425,7 @@ const rt = (r, e, t) => {
426
425
  const fe = p.replace(`.${B}`, ""), F = (b) => d.length === 1 ? b.name : `${fe}_${b.name}`, G = (b) => (s.has(b) || s.set(b, []), s.get(b));
427
426
  d.forEach(({ metadata: b, relation: T }, z) => {
428
427
  const k = r.rxdb.schema.findMappedRelation(b, T);
429
- if (!k) throw new E("mappedRelation not found");
428
+ if (!k) throw new m("mappedRelation not found");
430
429
  const P = () => {
431
430
  const g = G(k.metadata), $ = F(T), x = i($);
432
431
  return {
@@ -483,7 +482,7 @@ const rt = (r, e, t) => {
483
482
  });
484
483
  let l = "";
485
484
  for (const [u, h] of s.entries()) {
486
- const _ = y(u);
485
+ const _ = E(u);
487
486
  for (const p of h)
488
487
  l += ` LEFT JOIN ${_} '${p.joinTableName}' ON ${p.on}`;
489
488
  }
@@ -494,21 +493,21 @@ const rt = (r, e, t) => {
494
493
  return a && (o += a), t && (o += ` where ${t}`), n && (o += ` order by ${n}`), s && (o += ` limit ${s}`), o += ";", o;
495
494
  }, ht = (r, e, t, s) => {
496
495
  t = structuredClone(t);
497
- const { joinSQL: n, fieldAliasMap: a } = j(r, e, t), o = y(e), c = v(t, a), i = J(s?.orderBy);
496
+ const { joinSQL: n, fieldAliasMap: a } = j(r, e, t), o = E(e), c = v(t, a), i = J(s?.orderBy);
498
497
  return {
499
498
  sql: Y({ tableName: o, where: c, orderBy: i, join: n }),
500
499
  params: []
501
500
  };
502
501
  }, _t = (r, e, t, s) => {
503
502
  t = structuredClone(t);
504
- const { joinSQL: n, fieldAliasMap: a } = j(r, e, t), o = y(e), c = v(t, a), i = J(s?.orderBy);
503
+ const { joinSQL: n, fieldAliasMap: a } = j(r, e, t), o = E(e), c = v(t, a), i = J(s?.orderBy);
505
504
  return {
506
505
  sql: Y({ tableName: o, where: c, orderBy: i, join: n }),
507
506
  params: []
508
507
  };
509
508
  }, ft = (r, e, t, s) => {
510
509
  t = structuredClone(t);
511
- const { joinSQL: n, fieldAliasMap: a } = j(r, e, t), o = y(e), c = v(t, a);
510
+ const { joinSQL: n, fieldAliasMap: a } = j(r, e, t), o = E(e), c = v(t, a);
512
511
  let i = `SELECT count(_.rowid) as count FROM ${o} _`;
513
512
  return n && (i += n), c && (i += ` where ${c}`), {
514
513
  sql: i,
@@ -516,7 +515,7 @@ const rt = (r, e, t) => {
516
515
  };
517
516
  }, V = (r, e, t, s) => {
518
517
  t = structuredClone(t);
519
- const { joinSQL: n, fieldAliasMap: a } = j(r, e, t), o = y(e), c = v(t, a), i = J(s?.orderBy);
518
+ const { joinSQL: n, fieldAliasMap: a } = j(r, e, t), o = E(e), c = v(t, a), i = J(s?.orderBy);
520
519
  return {
521
520
  sql: Y({ tableName: o, where: c, orderBy: i, join: n, limit: 1 }),
522
521
  params: []
@@ -641,7 +640,7 @@ class _e extends pt {
641
640
  return this.#s("get", a, s, t), a.result$.pipe(
642
641
  f((o) => this.#t(a, o)),
643
642
  f((o) => {
644
- if (o.length === 0) throw new E("not found");
643
+ if (o.length === 0) throw new m("not found");
645
644
  return o[0];
646
645
  })
647
646
  );
@@ -659,7 +658,7 @@ class _e extends pt {
659
658
  f((a) => this.#t(n, a)),
660
659
  f((a) => {
661
660
  if (a.length === 0)
662
- throw new E("not found");
661
+ throw new m("not found");
663
662
  return a[0];
664
663
  })
665
664
  );
@@ -673,7 +672,7 @@ class _e extends pt {
673
672
  return this.#s("findAll", n, e, t), n.result$.pipe(f((a) => this.#t(n, a)));
674
673
  }
675
674
  findByCursor(e, t) {
676
- throw new E("Method not implemented.");
675
+ throw new m("Method not implemented.");
677
676
  }
678
677
  count(e, t) {
679
678
  const s = ft(this.adapter, this.metadata, e), n = this.create_query_task(s);
@@ -706,7 +705,7 @@ class _e extends pt {
706
705
  return O(c);
707
706
  }
708
707
  const n = q(e);
709
- throw new E(`Remove Error${n.name}(${e.id}) not saved local.`);
708
+ throw new m(`Remove Error${n.name}(${e.id}) not saved local.`);
710
709
  }
711
710
  /**
712
711
  * 根据 type 计算主动更新缓存策略
@@ -728,7 +727,7 @@ class _e extends pt {
728
727
  this.#l(n, s, t);
729
728
  break;
730
729
  default:
731
- throw new E("unknown type");
730
+ throw new m("unknown type");
732
731
  }
733
732
  }
734
733
  }
@@ -807,7 +806,7 @@ class _e extends pt {
807
806
  }
808
807
  }
809
808
  const D = (r, e, t, s) => {
810
- const n = s?.level || 0, a = y(r), o = n > 0 ? `c.level < ${n}` : "";
809
+ const n = s?.level || 0, a = E(r), o = n > 0 ? `c.level < ${n}` : "";
811
810
  let c = "";
812
811
  const i = [o, v(t)].filter(Boolean).join(" AND ");
813
812
  return i && (c = `WHERE ${i}`), {
@@ -864,7 +863,7 @@ class bt extends _e {
864
863
  this.#n(n, s, t);
865
864
  break;
866
865
  default:
867
- throw new E("unknown type");
866
+ throw new m("unknown type");
868
867
  }
869
868
  }
870
869
  }
@@ -982,7 +981,7 @@ class Rt extends Oe {
982
981
  A(() => setTimeout(() => this.#n.delete(e.key), 0)),
983
982
  Le((s) => {
984
983
  const n = s.message || "执行错误";
985
- return t.error(new E(`${n} ${e.sql} ${JSON.stringify(e.bindings)}`)), Me({ error: s });
984
+ return t.error(new m(`${n} ${e.sql} ${JSON.stringify(e.bindings)}`)), Me({ error: s });
986
985
  })
987
986
  );
988
987
  }),
@@ -1032,7 +1031,7 @@ class Rt extends Oe {
1032
1031
  s = new bt(this, this.#a, e);
1033
1032
  break;
1034
1033
  default:
1035
- throw new E("Unsupported repository type: " + t.repository);
1034
+ throw new m("Unsupported repository type: " + t.repository);
1036
1035
  }
1037
1036
  return this.#t.set(e, s), s;
1038
1037
  }
@@ -1050,7 +1049,7 @@ class Rt extends Oe {
1050
1049
  * @param EntityType
1051
1050
  */
1052
1051
  isTableExisted$(e) {
1053
- const t = q(e), s = y(t);
1052
+ const t = q(e), s = E(t);
1054
1053
  return this.#d(Pe(s)).pipe(f((n) => Ve(n) === !1));
1055
1054
  }
1056
1055
  /**
@@ -1097,7 +1096,7 @@ class Rt extends Oe {
1097
1096
  } catch (s) {
1098
1097
  await t.execute("ROLLBACK"), this.rxdb.dispatchEvent(Tt);
1099
1098
  const n = s?.message || "Transaction Error";
1100
- throw new E(n);
1099
+ throw new m(n);
1101
1100
  }
1102
1101
  })
1103
1102
  );
@@ -1118,7 +1117,7 @@ class Rt extends Oe {
1118
1117
  const Nt = new Te(), wt = new ge(), Tt = new Ie();
1119
1118
  export {
1120
1119
  Rt as RxDBAdapterSqlite,
1121
- E as RxdbAdapterSqliteError,
1120
+ m as RxdbAdapterSqliteError,
1122
1121
  nt as SqliteClient,
1123
1122
  et as WA_SQLITE_VFS_LIST,
1124
1123
  tt as sqliteLoad
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@aiao/rxdb-adapter-sqlite",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "dependencies": {
7
- "@aiao/rxdb": "0.0.2",
8
- "@aiao/utils": "0.0.2",
7
+ "@aiao/rxdb": "0.0.3",
8
+ "@aiao/utils": "0.0.3",
9
9
  "rxjs": "~7.8.0",
10
10
  "@journeyapps/wa-sqlite": "^1.2.4",
11
11
  "comlink": "^4.4.2"