@aiao/rxdb 0.0.2 → 0.0.4

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 (89) hide show
  1. package/LICENSE +21 -0
  2. package/{RxDB.d.ts → dist/RxDB.d.ts} +39 -11
  3. package/dist/RxDB.d.ts.map +1 -0
  4. package/{RxDBError.d.ts → dist/RxDBError.d.ts} +2 -0
  5. package/dist/RxDBError.d.ts.map +1 -0
  6. package/{entity → dist/entity}/@Entity.d.ts +4 -9
  7. package/dist/entity/@Entity.d.ts.map +1 -0
  8. package/{entity → dist/entity}/@TreeEntity.d.ts +5 -4
  9. package/dist/entity/@TreeEntity.d.ts.map +1 -0
  10. package/{entity → dist/entity}/EntityBase.d.ts +4 -1
  11. package/dist/entity/EntityBase.d.ts.map +1 -0
  12. package/{entity → dist/entity}/EntityManager.d.ts +33 -9
  13. package/dist/entity/EntityManager.d.ts.map +1 -0
  14. package/{entity → dist/entity}/EntityStatus.d.ts +26 -19
  15. package/dist/entity/EntityStatus.d.ts.map +1 -0
  16. package/dist/entity/TreeEntityBase.d.ts +15 -0
  17. package/dist/entity/TreeEntityBase.d.ts.map +1 -0
  18. package/{entity → dist/entity}/entity-metadata-options.interface.d.ts +67 -72
  19. package/dist/entity/entity-metadata-options.interface.d.ts.map +1 -0
  20. package/{entity → dist/entity}/entity-metadata.interface.d.ts +5 -14
  21. package/dist/entity/entity-metadata.interface.d.ts.map +1 -0
  22. package/{entity → dist/entity}/entity.interface.d.ts +10 -1
  23. package/dist/entity/entity.interface.d.ts.map +1 -0
  24. package/{entity → dist/entity}/entity.utils.d.ts +9 -2
  25. package/dist/entity/entity.utils.d.ts.map +1 -0
  26. package/{entity → dist/entity}/entity_proxy_helper.d.ts +3 -2
  27. package/dist/entity/entity_proxy_helper.d.ts.map +1 -0
  28. package/{entity → dist/entity}/entity_relation_helper.d.ts +4 -3
  29. package/dist/entity/entity_relation_helper.d.ts.map +1 -0
  30. package/{entity → dist/entity}/generate_many_to_many_entity.d.ts +5 -4
  31. package/dist/entity/generate_many_to_many_entity.d.ts.map +1 -0
  32. package/{entity → dist/entity}/transition-metadata.d.ts +6 -5
  33. package/dist/entity/transition-metadata.d.ts.map +1 -0
  34. package/dist/index.d.ts +38 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +3386 -0
  37. package/{repository → dist/repository}/Repository.d.ts +14 -20
  38. package/dist/repository/Repository.d.ts.map +1 -0
  39. package/{repository → dist/repository}/RepositoryBase.d.ts +6 -5
  40. package/dist/repository/RepositoryBase.d.ts.map +1 -0
  41. package/dist/repository/TreeRepository.d.ts +29 -0
  42. package/dist/repository/TreeRepository.d.ts.map +1 -0
  43. package/dist/repository/query-options.interface.d.ts +47 -0
  44. package/dist/repository/query-options.interface.d.ts.map +1 -0
  45. package/{repository → dist/repository}/query.interface.d.ts +18 -10
  46. package/dist/repository/query.interface.d.ts.map +1 -0
  47. package/dist/repository/query.utils.d.ts +27 -0
  48. package/dist/repository/query.utils.d.ts.map +1 -0
  49. package/dist/repository/relation-query.interface.d.ts +63 -0
  50. package/dist/repository/relation-query.interface.d.ts.map +1 -0
  51. package/{repository → dist/repository}/repository.interface.d.ts +9 -18
  52. package/dist/repository/repository.interface.d.ts.map +1 -0
  53. package/dist/repository/tree-repository.interface.d.ts +66 -0
  54. package/dist/repository/tree-repository.interface.d.ts.map +1 -0
  55. package/{rxdb-adapter.d.ts → dist/rxdb-adapter.d.ts} +30 -6
  56. package/dist/rxdb-adapter.d.ts.map +1 -0
  57. package/{rxdb-events.d.ts → dist/rxdb-events.d.ts} +9 -82
  58. package/dist/rxdb-events.d.ts.map +1 -0
  59. package/{rxdb-plugin.d.ts → dist/rxdb-plugin.d.ts} +2 -1
  60. package/dist/rxdb-plugin.d.ts.map +1 -0
  61. package/{rxdb-utils.d.ts → dist/rxdb-utils.d.ts} +15 -3
  62. package/dist/rxdb-utils.d.ts.map +1 -0
  63. package/{rxdb.interface.d.ts → dist/rxdb.interface.d.ts} +3 -10
  64. package/dist/rxdb.interface.d.ts.map +1 -0
  65. package/{rxdb.private.d.ts → dist/rxdb.private.d.ts} +14 -14
  66. package/dist/rxdb.private.d.ts.map +1 -0
  67. package/{schema → dist/schema}/SchemaManager.d.ts +8 -4
  68. package/dist/schema/SchemaManager.d.ts.map +1 -0
  69. package/dist/system/VersionManager.d.ts +35 -0
  70. package/dist/system/VersionManager.d.ts.map +1 -0
  71. package/dist/system/branch.d.ts +17 -0
  72. package/dist/system/branch.d.ts.map +1 -0
  73. package/dist/system/change.d.ts +78 -0
  74. package/dist/system/change.d.ts.map +1 -0
  75. package/dist/system/migration.d.ts +27 -0
  76. package/dist/system/migration.d.ts.map +1 -0
  77. package/dist/system/types.d.ts +335 -0
  78. package/dist/system/types.d.ts.map +1 -0
  79. package/package.json +26 -12
  80. package/entity/TreeEntityBase.d.ts +0 -7
  81. package/index.d.ts +0 -26
  82. package/index.js +0 -1779
  83. package/repository/TreeRepository.d.ts +0 -28
  84. package/repository/query-options.interface.d.ts +0 -53
  85. package/repository/query.utils.d.ts +0 -19
  86. package/repository/tree-query-options.interface.d.ts +0 -9
  87. package/repository/tree-repository.interface.d.ts +0 -26
  88. package/system/change.d.ts +0 -13
  89. package/system/migration.d.ts +0 -9
package/index.js DELETED
@@ -1,1779 +0,0 @@
1
- import { isFunction as oe, startCase as ce, get as le, isEqual as J, isSymbol as de, omitBy as he, deepFreeze as pe, isPromise as ue } from "@aiao/utils";
2
- import { v7 as fe } from "uuid";
3
- import { BehaviorSubject as N, filter as Y, distinctUntilChanged as I, switchMap as f, shareReplay as A, map as D, tap as b, firstValueFrom as S, of as j, defer as q } from "rxjs";
4
- class v extends Error {
5
- }
6
- class T {
7
- constructor(e) {
8
- this.entity = e;
9
- }
10
- }
11
- class U extends T {
12
- constructor(e, t) {
13
- super(e), this.error = t;
14
- }
15
- }
16
- const Ee = "ENTITY_NEW";
17
- class ye extends T {
18
- type = Ee;
19
- }
20
- const V = "ENTITY_CREATE", me = `${V}_SUCCESS`, _e = `${V}_ERROR`;
21
- class ge extends T {
22
- type = V;
23
- }
24
- class be extends T {
25
- type = me;
26
- }
27
- class ve extends U {
28
- type = _e;
29
- }
30
- const K = "ENTITY_UPDATE", Te = `${K}_SUCCESS`, Ae = `${K}_ERROR`;
31
- class Oe extends T {
32
- type = K;
33
- }
34
- class Re extends T {
35
- type = Te;
36
- }
37
- class we extends U {
38
- type = Ae;
39
- }
40
- const W = "ENTITY_REMOVE", Ne = `${W}_SUCCESS`, xe = `${W}_ERROR`;
41
- class Me extends T {
42
- type = W;
43
- }
44
- class Se extends T {
45
- type = Ne;
46
- }
47
- class Ce extends U {
48
- type = xe;
49
- }
50
- const Ie = "ENTITY_PATCH";
51
- class De extends T {
52
- constructor(e, t, s, n, i, a) {
53
- super(e), this.patch = t, this.inversePatch = s, this.recordAt = n, this.timeStamp = i, this.updatedBy = a;
54
- }
55
- type = Ie;
56
- }
57
- const F = "TRANSACTION_BEGIN", z = "TRANSACTION_COMMIT", H = "TRANSACTION_ROLLBACK";
58
- class mt {
59
- type = F;
60
- }
61
- class _t {
62
- type = z;
63
- }
64
- class gt {
65
- type = H;
66
- }
67
- const ee = "DATABASE_DELETE", Be = "DATABASE_INSERT", $e = "DATABASE_UPDATE";
68
- class G {
69
- constructor(e, t) {
70
- this.EntityType = e, this.data = t;
71
- }
72
- }
73
- class bt extends G {
74
- type = ee;
75
- }
76
- class vt extends G {
77
- type = Be;
78
- }
79
- class Tt extends G {
80
- type = $e;
81
- }
82
- const te = Symbol("ɵProxy"), L = Symbol("ɵMetadata"), se = Symbol("ɵStatus"), B = Symbol("ɵStatusCheck"), ne = Symbol("ɵEntityManager"), re = Symbol("ɵEntityDeleteCheck"), x = Symbol("ɵUpdateBy"), Pe = "SYSTEM", Ye = "USER", je = (r, e) => e.sync.local?.adapter === r, Le = (r) => [F, z, H].includes(r.type), O = (r) => r[L] || r.constructor[L], m = (r) => r[se], At = (r, e, t, s) => {
83
- let n = s > 1 ? void 0 : s ? Object.getOwnPropertyDescriptor(e, t) : e;
84
- for (let i = r.length - 1, a; i >= 0; i--)
85
- (a = r[i]) && (n = a(n) || n);
86
- return n;
87
- }, ke = () => fe(), Ue = [
88
- "id",
89
- "createdAt",
90
- "updatedAt",
91
- "removedAt",
92
- "createdBy",
93
- "updatedBy",
94
- "removedBy"
95
- ], Ve = ["rev"], Ot = (r) => !!(Ue.includes(r) || Ve.includes(r) || r.startsWith("_")), u = (r, e, t) => Object.defineProperty(r, e, {
96
- value: t,
97
- enumerable: !1,
98
- configurable: !1,
99
- writable: !1
100
- }), Q = (r, e, t) => Object.defineProperty(r, e, {
101
- value: t,
102
- enumerable: !1,
103
- configurable: !1,
104
- writable: !0
105
- }), w = (r, e, t) => {
106
- let s;
107
- const n = {
108
- get: () => (s = t(), n.get = () => s, s),
109
- enumerable: !1,
110
- configurable: !1
111
- };
112
- return Object.defineProperty(r, e, n);
113
- }, Ke = (r, e) => {
114
- const t = {};
115
- let s = !1;
116
- r.defaultValueProperties.forEach((n) => {
117
- e[n.name] === void 0 && (s = !0, oe(n.default) ? t[n.name] = n.default() : t[n.name] = n.default);
118
- }), s && Object.assign(e, t);
119
- }, We = (r, e, t) => {
120
- const { propertyMap: s, foreignKeyNames: n } = O(e);
121
- Object.keys(t).forEach((i) => {
122
- s.has(i) ? r.propertyMap.get(i)?.readonly !== !0 && (e[i] = t[i]) : n.includes(i) && (e[i] = t[i]);
123
- });
124
- };
125
- var E = /* @__PURE__ */ ((r) => (r.uuid = "uuid", r.string = "string", r.number = "number", r.integer = "integer", r.boolean = "boolean", r.date = "date", r.json = "json", r))(E || {}), p = /* @__PURE__ */ ((r) => (r.ONE_TO_ONE = "1:1", r.ONE_TO_MANY = "1:m", r.MANY_TO_ONE = "m:1", r.MANY_TO_MANY = "m:n", r))(p || {}), Fe = /* @__PURE__ */ ((r) => (r.Full = "full", r.Filter = "filter", r.None = "None", r))(Fe || {});
126
- const ze = (r, e) => {
127
- const t = [];
128
- t.push(r);
129
- const s = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), a = { ...r };
130
- if (a.repository = a.repository || "Repository", a.namespace = a.namespace || "public", a.properties = a.properties || [], a.relations = a.relations || [], a.indexes = a.indexes || [], e) {
131
- let o = e, c = r;
132
- for (; o !== Object.prototype && c; )
133
- o = Object.getPrototypeOf(o), c = O(o), c && t.push(c);
134
- const l = t.map((d) => d.name);
135
- l.shift(), a.extends = l;
136
- } else
137
- a.extends = r.extends || [];
138
- return a.displayName || (a.displayName = ce(a.name).replace(" ", "")), a.relations.forEach((o) => {
139
- o.mappedNamespace = o.mappedNamespace || "public";
140
- }), t.reverse().forEach((o) => {
141
- o.properties?.forEach((c) => {
142
- s.set(c.name, c);
143
- }), o.relations?.forEach((c) => {
144
- if (c.mappedNamespace = c.mappedNamespace || "public", c.mappedEntity === o.name && c.mappedNamespace === o.namespace) {
145
- const l = { ...c };
146
- l.mappedEntity = a.name, l.mappedNamespace = a.namespace, n.set(c.name, l);
147
- } else
148
- n.set(c.name, c);
149
- }), o.indexes?.forEach((c) => i.set(c.name, c));
150
- }), u(a, "propertyMap", s), u(a, "relationMap", n), u(a, "indexMap", i), w(
151
- a,
152
- "defaultValueProperties",
153
- () => Array.from(s.values()).filter((o) => o.default !== void 0)
154
- ), w(
155
- a,
156
- "foreignKeyRelations",
157
- () => Array.from(a.relationMap.values()).filter(
158
- (o) => o.kind === p.MANY_TO_ONE || o.kind === p.ONE_TO_ONE
159
- )
160
- ), w(a, "foreignKeyRelationMap", () => {
161
- const o = /* @__PURE__ */ new Map();
162
- return a.foreignKeyRelations.forEach((c) => o.set(c.name, c)), o;
163
- }), w(a, "foreignKeyNames", () => a.foreignKeyRelations.map((o) => `${o.name}Id`)), u(
164
- a,
165
- "isForeignKey",
166
- (o) => o && o.endsWith("Id") && a.foreignKeyNames.includes(o)
167
- ), a;
168
- }, R = (r) => (e) => {
169
- const t = ze(r, e);
170
- if (u(e, L, t), r.abstract) return e;
171
- const s = e;
172
- return class extends s {
173
- /**
174
- * 实体构造函数
175
- * 处理实体初始化、默认值填充和代理包装
176
- *
177
- * 构造函数执行以下步骤:
178
- * 1. 调用原始类的构造函数
179
- * 2. 获取实体管理器
180
- * 3. 填充默认值(基于元数据中定义的默认值)
181
- * 4. 填充初始值(如果提供了初始化数据)
182
- * 5. 创建并返回实体代理(用于变更跟踪)
183
- */
184
- constructor(n) {
185
- super(n);
186
- const i = this[ne];
187
- if (!i) throw new v("need init rxdb");
188
- return Ke(t, this), n && We(t, this, n), i[te](this);
189
- }
190
- };
191
- }, Rt = (r) => {
192
- const e = r.features || {};
193
- return R({
194
- ...r,
195
- features: {
196
- ...e,
197
- treeType: e.treeType || "adjacency-list"
198
- },
199
- repository: r.repository || "TreeRepository"
200
- });
201
- };
202
- var He = Object.getOwnPropertyDescriptor, Ge = (r, e, t, s) => {
203
- for (var n = s > 1 ? void 0 : s ? He(e, t) : e, i = r.length - 1, a; i >= 0; i--)
204
- (a = r[i]) && (n = a(n) || n);
205
- return n;
206
- };
207
- let $ = class {
208
- /**
209
- * 实体唯一标识符
210
- * 自动生成的UUID,作为主键
211
- */
212
- id;
213
- /**
214
- * 创建时间
215
- * 实体创建时自动设置为当前时间
216
- */
217
- createdAt;
218
- /**
219
- * 更新时间
220
- * 实体创建或更新时自动设置为当前时间
221
- */
222
- updatedAt;
223
- /**
224
- * 删除时间
225
- * 实体被软删除时设置为当前时间,否则为null
226
- */
227
- removedAt;
228
- /**
229
- * 创建者ID
230
- * 记录创建此实体的用户ID
231
- */
232
- createdBy;
233
- /**
234
- * 更新者ID
235
- * 记录最后更新此实体的用户ID
236
- */
237
- updatedBy;
238
- /**
239
- * 删除者ID
240
- * 记录删除此实体的用户ID
241
- */
242
- removedBy;
243
- /**
244
- * 实体基类构造函数
245
- * 子类会通过 Entity 装饰器增强此构造函数
246
- *
247
- * @param initData - 可选的初始化数据
248
- */
249
- // eslint-disable-next-line @typescript-eslint/no-empty-function
250
- constructor(r) {
251
- }
252
- };
253
- $ = Ge([
254
- R({
255
- name: "EntityBase",
256
- abstract: !0,
257
- // 标记为抽象类,不会直接创建此类的实例
258
- properties: [
259
- {
260
- name: "id",
261
- type: E.uuid,
262
- primary: !0,
263
- // 主键
264
- unique: !0,
265
- // 唯一索引
266
- readonly: !0,
267
- // 创建后不可修改
268
- default: () => ke()
269
- // 默认生成UUID
270
- },
271
- {
272
- name: "createdAt",
273
- type: E.date,
274
- readonly: !0,
275
- default: () => /* @__PURE__ */ new Date()
276
- },
277
- {
278
- name: "updatedAt",
279
- type: E.date,
280
- readonly: !0,
281
- default: () => /* @__PURE__ */ new Date()
282
- },
283
- {
284
- name: "removedAt",
285
- type: E.date,
286
- readonly: !0,
287
- nullable: !0
288
- },
289
- {
290
- name: "createdBy",
291
- type: E.string,
292
- readonly: !0,
293
- nullable: !0
294
- },
295
- {
296
- name: "updatedBy",
297
- type: E.string,
298
- readonly: !0,
299
- nullable: !0
300
- },
301
- {
302
- name: "removedBy",
303
- type: E.string,
304
- readonly: !0,
305
- nullable: !0
306
- }
307
- ]
308
- })
309
- ], $);
310
- var Je = Object.getOwnPropertyDescriptor, qe = (r, e, t, s) => {
311
- for (var n = s > 1 ? void 0 : s ? Je(e, t) : e, i = r.length - 1, a; i >= 0; i--)
312
- (a = r[i]) && (n = a(n) || n);
313
- return n;
314
- };
315
- let X = class extends $ {
316
- };
317
- X = qe([
318
- R({
319
- name: "TreeAdjacencyListEntityBase",
320
- abstract: !0,
321
- // 标记为抽象类,不会直接创建此类的实例
322
- relations: [
323
- {
324
- name: "children",
325
- // 子节点关系名称
326
- kind: p.ONE_TO_MANY,
327
- // 一对多关系
328
- mappedEntity: "TreeAdjacencyListEntityBase",
329
- // 关联到同一实体类型
330
- mappedProperty: "parent"
331
- // 映射到子实体的 parent 属性
332
- },
333
- {
334
- name: "parent",
335
- // 父节点关系名称
336
- kind: p.MANY_TO_ONE,
337
- // 多对一关系
338
- mappedEntity: "TreeAdjacencyListEntityBase",
339
- // 关联到同一实体类型
340
- nullable: !0
341
- // 允许根节点没有父节点
342
- }
343
- ]
344
- })
345
- ], X);
346
- const Qe = (r) => !!r.combinator, Xe = (r, e) => {
347
- const t = e[r.field], { operator: s, value: n } = r;
348
- switch (s) {
349
- case "in":
350
- return n.includes(t);
351
- case "notIn":
352
- return n.includes(t) === !1;
353
- case "between":
354
- return t >= n[0] && t <= n[1];
355
- case "notBetween":
356
- return t < n[0] || t > n[1];
357
- case "contains":
358
- return `${t}`.includes(`${n}`);
359
- case "notContain":
360
- return `${t}`.includes(`${n}`) === !1;
361
- case "beginWith":
362
- return `${t}`.startsWith(`${n}`);
363
- case "notBeginWith":
364
- return `${t}`.startsWith(`${n}`) === !1;
365
- case "endWith":
366
- return `${t}`.endsWith(`${n}`);
367
- case "notEndWith":
368
- return `${t}`.endsWith(`${n}`) === !1;
369
- case "=":
370
- return J(n, t);
371
- case "!=":
372
- return J(n, t) === !1;
373
- case ">":
374
- return t > n;
375
- case ">=":
376
- return t >= n;
377
- case "<":
378
- return t < n;
379
- case "<=":
380
- return t <= n;
381
- default:
382
- throw new v(`Unknown operator: ${s}`);
383
- }
384
- }, k = (r, e) => {
385
- if (Qe(r)) {
386
- const { combinator: t, rules: s } = r;
387
- return t === "and" ? s.every((n) => k(n, e)) : s.some((n) => k(n, e));
388
- } else
389
- return Xe(r, e);
390
- }, wt = (r, e) => k(e, r), Ze = (r, e, t) => {
391
- const { field: s, order: n } = t, i = le(e, s);
392
- if (n === "asc") {
393
- if (r.some((c) => i < Reflect.get(c, s))) return !0;
394
- if (r.every((c) => i > Reflect.get(c, s))) return !1;
395
- } else {
396
- if (r.some((c) => i > Reflect.get(c, s))) return !0;
397
- if (r.every((c) => i < Reflect.get(c, s))) return !1;
398
- }
399
- }, Nt = (r, e, t) => {
400
- if (t.length === 0) return !1;
401
- for (let s = 0; s < t.length; s++) {
402
- const n = t[s], i = Ze(r, e, n);
403
- if (i === void 0) {
404
- if (s < t.length - 1) {
405
- const { field: a } = n, o = Reflect.get(e, a);
406
- r = r.filter((c) => Reflect.get(c, a) === o);
407
- }
408
- } else
409
- return i;
410
- }
411
- };
412
- var et = /* @__PURE__ */ ((r) => (r.Synced = "Synced", r.Syncing = "Syncing", r.Never = "Never", r))(et || {});
413
- class xt {
414
- constructor(e, t) {
415
- this.rxdb = e, this.EntityClass = t;
416
- }
417
- /**
418
- * 获取实体实例
419
- * @param data 实体数据
420
- */
421
- createEntityRef(e) {
422
- return this.rxdb.em.createEntityRef(this.EntityClass, e);
423
- }
424
- /**
425
- * 获取实体实例
426
- * @param id 实体的 ID
427
- */
428
- getEntityRef(e) {
429
- return this.rxdb.em.getEntityRef(this.EntityClass, e);
430
- }
431
- /**
432
- * 获取实体实例
433
- * @param id 实体的 ID
434
- */
435
- hasEntityRef(e) {
436
- return this.rxdb.em.hasEntityRef(this.EntityClass, e);
437
- }
438
- /**
439
- * 更新实体
440
- */
441
- updateEntity(e, t) {
442
- e[x] = Pe;
443
- const s = m(e);
444
- s.origin = { ...t }, Object.assign(e, t), e[x] = null;
445
- }
446
- }
447
- class ae {
448
- constructor(e, t) {
449
- this.rxdb = e, this.EntityType = t;
450
- const s = O(t);
451
- this.sync = s.sync || e.options.sync;
452
- const { local: n, remote: i } = this.sync;
453
- n && this.localAdapterSub.next(n.adapter), i && this.remoteAdapterSub.next(i.adapter);
454
- }
455
- /**
456
- * 本地适配器
457
- */
458
- localAdapterSub = new N("");
459
- localAdapter$ = this.localAdapterSub.asObservable().pipe(
460
- Y(Boolean),
461
- I(),
462
- f((e) => this.rxdb.getAdapter(e)),
463
- A(1)
464
- );
465
- /**
466
- * 本地仓库
467
- */
468
- local$ = this.localAdapter$.pipe(
469
- D((e) => e.getRepository(this.EntityType)),
470
- A(1)
471
- );
472
- /**
473
- * 远程适配器
474
- */
475
- remoteAdapterSub = new N("");
476
- remoteAdapter$ = this.remoteAdapterSub.asObservable().pipe(
477
- Y(Boolean),
478
- I(),
479
- f((e) => this.rxdb.getAdapter(e)),
480
- A(1)
481
- );
482
- /**
483
- * 远程仓库
484
- */
485
- remote$ = this.remoteAdapter$.pipe(
486
- D((e) => e.getRepository(this.EntityType)),
487
- A(1)
488
- );
489
- /**
490
- * 同步配置
491
- * TODO: 待实现同步功能
492
- */
493
- sync;
494
- /**
495
- * 根据 id 获取实体
496
- * @param id
497
- * @param options
498
- */
499
- get(e, t) {
500
- return this.local$.pipe(
501
- f((s) => s.get(e, t)),
502
- b(this._setLocalEntityStatus)
503
- );
504
- }
505
- /**
506
- * 查找一个实体
507
- * @param where
508
- * @param options
509
- */
510
- findOne(e, t) {
511
- return this.local$.pipe(
512
- f((s) => s.findOne(e, t)),
513
- b((s) => s && this._setLocalEntityStatus(s))
514
- );
515
- }
516
- /**
517
- * 查找一个实体,查不到就抛出错误
518
- * @param where
519
- * @param options
520
- */
521
- findOneOrFail(e, t) {
522
- return this.local$.pipe(
523
- f((s) => s.findOneOrFail(e, t)),
524
- b(this._setLocalEntityStatus)
525
- );
526
- }
527
- /**
528
- * 查询多个实体
529
- * @param where
530
- * @param options
531
- */
532
- find(e, t) {
533
- return this.local$.pipe(
534
- f((s) => s.find(e, t)),
535
- b(this._setLocalEntitiesStatus)
536
- );
537
- }
538
- /**
539
- * 查询所有实体
540
- * @param where
541
- * @param options
542
- */
543
- findAll(e, t) {
544
- return this.local$.pipe(
545
- f((s) => s.findAll(e, t)),
546
- b(this._setLocalEntitiesStatus)
547
- );
548
- }
549
- /**
550
- * 通过指针查询实体
551
- * @param where
552
- * @param options
553
- */
554
- findByCursor(e, t) {
555
- return this.local$.pipe(
556
- f((s) => s.findByCursor(e, t)),
557
- b(this._setLocalEntitiesStatus)
558
- );
559
- }
560
- /**
561
- * 查询实体数量
562
- * @param where
563
- * @param options
564
- */
565
- count(e, t) {
566
- return this.local$.pipe(f((s) => s.count(e, t)));
567
- }
568
- /**
569
- * 事务保存
570
- * @param entities 需要保存的实体
571
- */
572
- async transactionSave(e) {
573
- return e = Array.from(new Set(e)), (await S(this.localAdapter$)).transaction(async () => {
574
- let s;
575
- for (let n = 0; n < e.length; n++) {
576
- const i = e[n], a = m(i);
577
- a.modified && (a.local ? s = await this.rxdb.em.update(i) : s = await this.rxdb.em.create(i));
578
- }
579
- return s;
580
- });
581
- }
582
- /**
583
- * 创建实体
584
- * @param entity
585
- */
586
- create(e) {
587
- const t = this.local$.pipe(
588
- f((s) => s.create(e)),
589
- b(this._setLocalEntityStatus)
590
- );
591
- return S(t);
592
- }
593
- /**
594
- * 更新实体
595
- * @param entity
596
- * @param patch
597
- */
598
- update(e, t) {
599
- const s = this.local$.pipe(
600
- f((n) => n.update(e, t)),
601
- b(this._setLocalEntityStatus)
602
- );
603
- return S(s);
604
- }
605
- /**
606
- * 删除实体
607
- * @param entity
608
- */
609
- remove(e) {
610
- const t = this.local$.pipe(f((s) => s.remove(e)));
611
- return S(t);
612
- }
613
- /**
614
- * 设置状态 local
615
- */
616
- _setLocalEntityStatus = (e) => m(e).local = !0;
617
- _setLocalEntitiesStatus = (e) => e.forEach(this._setLocalEntityStatus);
618
- }
619
- class tt extends ae {
620
- /**
621
- * 查询子孙
622
- */
623
- findDescendants(e, t, s) {
624
- return this.local$.pipe(
625
- f((n) => n.findDescendants(e, t, s)),
626
- b(this._setLocalEntitiesStatus)
627
- );
628
- }
629
- /**
630
- * 查询子孙数量
631
- */
632
- countDescendants(e, t, s) {
633
- return this.local$.pipe(f((n) => n.countDescendants(e, t, s)));
634
- }
635
- /**
636
- * 查询祖先
637
- */
638
- findAncestors(e, t, s) {
639
- return this.local$.pipe(
640
- f((n) => n.findAncestors(e, t, s)),
641
- b(this._setLocalEntitiesStatus)
642
- );
643
- }
644
- /**
645
- * 查询祖先数量
646
- */
647
- countAncestors(e, t, s) {
648
- return this.local$.pipe(f((n) => n.countAncestors(e, t, s)));
649
- }
650
- }
651
- const st = (r) => {
652
- const e = m(r), t = r;
653
- let s = [];
654
- const n = () => {
655
- let o = {}, c = {}, l = null;
656
- const d = () => {
657
- l && (e.addEntityPatch(c, o, l), l = null, o = {}, c = {});
658
- };
659
- s.forEach(({ prop: h, value: y, oldValue: _, updatedBy: g }) => {
660
- o[h] = o[h] ?? _, c[h] = y, l === null ? l = g : l !== g && d();
661
- }), d(), e[B] = !1, s = [], t[x] = null;
662
- };
663
- let i;
664
- const a = {
665
- // get: (target, prop, receiver) => {
666
- // const value = Reflect.get(target, prop, receiver);
667
- // return value;
668
- // },
669
- /**
670
- * 拦截属性设置操作
671
- * 当属性值发生变化时,记录变更并安排检查
672
- */
673
- set: (o, c, l, d) => {
674
- if (o[c] === l) return !0;
675
- if (de(c) === !1) {
676
- const h = o[x] || Ye;
677
- s.push({ updatedBy: h, prop: c, value: l, oldValue: o[c] }), e[B] = !0, e.modified = !0, clearTimeout(i), i = setTimeout(() => n(), 0);
678
- }
679
- return Reflect.set(o, c, l, d);
680
- }
681
- };
682
- return new Proxy(r, a);
683
- }, nt = (r, e, t) => {
684
- const s = t.rxdb.schema.getEntityType(r.mappedEntity, r.mappedNamespace);
685
- if (!s) throw new v(`mapped entity not found: ${r.mappedEntity}`);
686
- const n = r.name + "Id", i = r.name + "$";
687
- let a;
688
- switch (r.kind) {
689
- /**
690
- * 多对一, 一对一关系处理
691
- * 创建一个可观察对象,通过外键ID获取关联实体
692
- * 提供set和remove方法操作关系
693
- */
694
- case p.MANY_TO_ONE:
695
- case p.ONE_TO_ONE:
696
- a = function() {
697
- const o = this, c = m(o), l = new N(o[n]), d = l.asObservable().pipe(
698
- f((h) => h ? t.getRepository(s).get(h) : j(null)),
699
- A(1)
700
- );
701
- return u(d, "set", (h) => {
702
- h || d.remove(), Reflect.get(o, n) != h.id && (c.addRelationEntity(r, h), l.next(h.id), Reflect.set(o, n, h.id));
703
- }), u(d, "remove", () => {
704
- Reflect.get(o, n) != null && (l.next(null), c.removeRelationEntity(r, e), Reflect.set(o, n, null));
705
- }), Object.freeze(d);
706
- };
707
- break;
708
- /**
709
- * 一对多关系处理
710
- * 创建一个可观察对象,查询所有关联到当前实体的实体集合
711
- * 提供add和remove方法操作关系集合
712
- */
713
- case p.ONE_TO_MANY:
714
- a = function() {
715
- const o = r.mappedProperty + "Id", c = r.mappedProperty + "$", l = this, d = m(l), h = t.getRepository(s).findAll({
716
- combinator: "and",
717
- rules: [
718
- {
719
- field: o,
720
- operator: "=",
721
- value: l.id
722
- }
723
- ]
724
- }), y = t.getRepository(s).count({
725
- combinator: "and",
726
- rules: [
727
- {
728
- field: o,
729
- operator: "=",
730
- value: l.id
731
- }
732
- ]
733
- });
734
- return u(h, "count$", y), u(h, "add", (..._) => {
735
- _.forEach((g) => {
736
- d.addRelationEntity(r, g), g[c].set(l);
737
- });
738
- }), u(h, "remove", (..._) => {
739
- _.forEach((g) => {
740
- d.removeRelationEntity(r, g), g[c].remove();
741
- });
742
- }), Object.freeze(h);
743
- };
744
- break;
745
- /**
746
- * 多对多关系处理
747
- * 通过中间表(junction entity)实现多对多关系
748
- * 创建一个可观察对象,查询通过中间表关联到当前实体的实体集合
749
- * 提供add和remove方法操作关系集合
750
- */
751
- case p.MANY_TO_MANY:
752
- a = function() {
753
- const o = this, c = m(o), l = r.name + "Id";
754
- if (!r.junctionEntityType) throw new v("junction entity not found");
755
- const d = t.getRepository(r.junctionEntityType).findAll({
756
- combinator: "and",
757
- rules: [
758
- {
759
- field: r.mappedProperty + "Id",
760
- operator: "=",
761
- value: o.id
762
- }
763
- ]
764
- }).pipe(
765
- f((y) => {
766
- const _ = y.map((g) => g[l]);
767
- return t.getRepository(s).findAll({
768
- combinator: "and",
769
- rules: [
770
- {
771
- field: "id",
772
- operator: "in",
773
- value: _
774
- }
775
- ]
776
- });
777
- })
778
- ), h = t.getRepository(r.junctionEntityType).count({
779
- combinator: "and",
780
- rules: [
781
- {
782
- field: r.mappedProperty + "Id",
783
- operator: "=",
784
- value: o.id
785
- }
786
- ]
787
- });
788
- return u(d, "count$", h), u(d, "add", (...y) => {
789
- y.forEach((_) => c.addRelationEntity(r, _));
790
- }), u(d, "remove", (...y) => {
791
- y.forEach((_) => {
792
- c.removeRelationEntity(r, _);
793
- });
794
- }), Object.freeze(d);
795
- };
796
- break;
797
- }
798
- Object.defineProperty(e.prototype, i, {
799
- get: a,
800
- enumerable: !1,
801
- configurable: !1
802
- });
803
- };
804
- class rt {
805
- /**
806
- * 创建实体状态实例
807
- *
808
- * @constructor
809
- * @param rxdb - RxDB 实例
810
- * @param data - 实体状态初始数据
811
- */
812
- constructor(e, t) {
813
- this.rxdb = e, u(this, "target", t.target), this._local = t.local || !1, this._remote = t.remote || !1, this._modified = t.modified || !1, this._origin = { ...t.target }, this._patches = t.patches || [], [
814
- "_rowId",
815
- "_local",
816
- "_modified",
817
- "_origin",
818
- "_patches",
819
- "_remote",
820
- "_removed",
821
- "_snapshots",
822
- "patches$",
823
- "rxdb",
824
- B
825
- ].forEach(
826
- (s) => Object.defineProperty(this, s, {
827
- enumerable: !1,
828
- configurable: !1
829
- })
830
- );
831
- }
832
- /**
833
- * 按关系类型存储关联实体
834
- * 用于跟踪和管理实体的各种关系
835
- */
836
- #e = /* @__PURE__ */ new Map();
837
- /**
838
- * 实体变更记录的可观察对象
839
- * 用于发布实体变更通知
840
- */
841
- #t = new N([]);
842
- /**
843
- * 实体状态快照数组
844
- * 用于存储实体在不同时间点的完整状态
845
- */
846
- #s = [];
847
- /**
848
- * 标识实体是否存在于远程数据库
849
- */
850
- _remote;
851
- /**
852
- * 标识实体是否存在于本地数据库
853
- */
854
- _local;
855
- /**
856
- * 标识实体是否已被修改
857
- */
858
- _modified;
859
- /**
860
- * 标识实体是否已被删除
861
- */
862
- _removed = !1;
863
- /**
864
- * 实体在数据库中的行ID
865
- */
866
- _rowId;
867
- /**
868
- * 实体变更记录数组
869
- */
870
- _patches;
871
- /**
872
- * 实体原始对象
873
- */
874
- target;
875
- /**
876
- * 实体代理对象
877
- * 用于拦截属性访问和修改
878
- */
879
- proxyTarget;
880
- /**
881
- * 实体变更记录的可观察流
882
- * 用于订阅实体变更
883
- */
884
- patches$ = this.#t.asObservable();
885
- /**
886
- * 状态检查标记
887
- * 用于内部状态验证
888
- */
889
- [B] = !1;
890
- /**
891
- * 实体原始数据
892
- * 用于比较变更和恢复
893
- */
894
- _origin;
895
- /**
896
- * 是否已修改
897
- */
898
- get modified() {
899
- return this._modified;
900
- }
901
- set modified(e) {
902
- this._modified !== e && (this._modified = e);
903
- }
904
- /**
905
- * 是否已删除
906
- */
907
- get removed() {
908
- return this._removed;
909
- }
910
- set removed(e) {
911
- this._removed !== e && (this._removed = e);
912
- }
913
- /**
914
- * 是否是远程数据
915
- */
916
- get remote() {
917
- return this._remote;
918
- }
919
- set remote(e) {
920
- this._remote !== e && (this._remote = e);
921
- }
922
- /**
923
- * 是否是本地数据
924
- */
925
- get local() {
926
- return this._local;
927
- }
928
- set local(e) {
929
- this._local !== e && (this._local = e);
930
- }
931
- /**
932
- * 获取变化
933
- */
934
- get patches() {
935
- return this._patches;
936
- }
937
- /**
938
- * 设置原始值
939
- */
940
- set origin(e) {
941
- this._origin = e;
942
- }
943
- get origin() {
944
- return this._origin;
945
- }
946
- /**
947
- * 设置 rowId
948
- */
949
- set rowId(e) {
950
- this._rowId !== e && (this._rowId = e, this.rxdb.em.updateEntityRowId(this.proxyTarget, e));
951
- }
952
- get rowId() {
953
- return this._rowId;
954
- }
955
- /**
956
- * 添加实体变更记录
957
- * 记录实体的变更并发布变更事件
958
- *
959
- * @param patch - 变更内容
960
- * @param inversePatch - 逆向变更内容,用于撤销操作
961
- * @param updatedBy - 更新者信息
962
- * @param [recordAt=Date.now()] - 记录时间戳
963
- */
964
- addEntityPatch(e, t, s, n = Date.now()) {
965
- const i = performance.now(), a = { patch: e, inversePatch: t, recordAt: n, timeStamp: i, updatedBy: s };
966
- this._patches.length === 0 && this.#s.push(Object.freeze(structuredClone(this.origin))), w(a, "snapshot", () => {
967
- const c = this._patches.findIndex((h) => h === a), l = this.#s[c];
968
- if (l) return l;
969
- let d;
970
- for (let h = 0; h <= c; h++) {
971
- const y = this.#s[h];
972
- y ? d = y : (d = Object.freeze({ ...d, ...a.patch }), this.#s[h] = d);
973
- }
974
- return d;
975
- }), this._patches.push(a), this.#t.next(this._patches);
976
- const o = new De(this.proxyTarget, e, t, n, i, s);
977
- this.rxdb.dispatchEvent(o);
978
- }
979
- /**
980
- * 获取需要保存的关联实体
981
- * 根据关系类型的优先级排序,返回需要保存的实体数组
982
- *
983
- * @returns 需要保存的实体数组
984
- */
985
- getNeedSaveRelationEntities() {
986
- const e = /* @__PURE__ */ new Set([this.proxyTarget]);
987
- return this.#e.forEach(
988
- (t) => t.forEach((s) => {
989
- m(s).modified === !0 && e.add(s);
990
- })
991
- ), Array.from(e);
992
- }
993
- /**
994
- * 添加关系实体
995
- * 根据关系类型处理实体间的关联
996
- *
997
- * @param relation - 关系元数据
998
- * @param entity - 要关联的实体
999
- */
1000
- addRelationEntity(e, t) {
1001
- const s = this.getRelationCache(e.kind);
1002
- switch (e.kind) {
1003
- case p.MANY_TO_MANY:
1004
- {
1005
- const n = e.junctionEntityType, i = e.name + "Id", a = e.mappedProperty + "Id", o = (h) => h instanceof n && h[i] === t.id && h[a] === this.target.id;
1006
- if (s.values().find(o)) return;
1007
- const l = m(t).getRelationCache(p.MANY_TO_MANY);
1008
- let d = l.values().find(o);
1009
- d || (d = new n({
1010
- [i]: t.id,
1011
- [a]: this.target.id
1012
- })), s.add(t), s.add(d), l.add(this.proxyTarget), l.add(d);
1013
- }
1014
- break;
1015
- default:
1016
- s.add(t);
1017
- }
1018
- }
1019
- /**
1020
- * 删除关系实体
1021
- * 根据关系类型移除实体间的关联
1022
- *
1023
- * @param relation - 关系元数据
1024
- * @param entity - 要移除关联的实体
1025
- */
1026
- removeRelationEntity(e, t) {
1027
- const s = this.getRelationCache(e.kind);
1028
- switch (e.kind) {
1029
- case p.MANY_TO_MANY:
1030
- {
1031
- const n = e.junctionEntityType, i = e.name + "Id", a = e.mappedProperty + "Id", o = s.values().find((c) => c instanceof n && c[i] === this.target.id && c[a] === t.id);
1032
- o && s.delete(o);
1033
- }
1034
- break;
1035
- }
1036
- s.delete(t);
1037
- }
1038
- /**
1039
- * 获取指定关系类型的实体缓存
1040
- * 如果缓存不存在则创建新的缓存
1041
- *
1042
- * @param kind - 关系类型
1043
- */
1044
- getRelationCache(e) {
1045
- let t = this.#e.get(e);
1046
- return t ? this.#e.get(e) : (t = /* @__PURE__ */ new Set(), this.#e.set(e, t), t);
1047
- }
1048
- }
1049
- class at {
1050
- /**
1051
- * 创建实体管理器实例
1052
- *
1053
- * 构造函数会:
1054
- * 1. 为所有注册的实体类型添加静态和实例方法
1055
- * 2. 设置实体关系处理
1056
- * 3. 配置实体代理和删除检查
1057
- *
1058
- * @param rxdb - RxDB实例,提供数据库访问和事件分发
1059
- */
1060
- constructor(e) {
1061
- this.rxdb = e, this.rxdb.options.entities.forEach((t) => {
1062
- const s = O(t);
1063
- u(t.prototype, ne, this), ["get", "findOneOrFail", "find", "findOne", "findAll", "findByCursor", "count"].forEach((n) => {
1064
- u(t, n, (i, a) => this.#n(t)[n](i, a));
1065
- }), s.repository === "TreeRepository" && ["findDescendants", "countDescendants", "findAncestors", "countAncestors"].forEach((n) => {
1066
- u(
1067
- t,
1068
- n,
1069
- (i, a, o) => this.#n(t)[n](i, a, o)
1070
- );
1071
- }), Object.keys(Z).forEach(
1072
- (n) => u(t.prototype, n, Z[n](this, t))
1073
- ), s.relationMap.forEach((n) => nt(n, t, this));
1074
- }), u(this, te, (t) => {
1075
- const s = this.#i(t, { local: !1, remote: !1, modified: !0 });
1076
- this.addEntityCache(s);
1077
- const n = new ye(s);
1078
- return this.rxdb.dispatchEvent(n), s;
1079
- }), u(this, re, (t) => {
1080
- const { EntityType: s, data: n } = t;
1081
- if (n.rowId) {
1082
- const a = this.#a(s).get(n.rowId), o = a && this.#r(s).get(a);
1083
- if (o) {
1084
- const c = m(o);
1085
- c.removed = !0;
1086
- }
1087
- }
1088
- });
1089
- }
1090
- /**
1091
- * 实体仓库映射
1092
- * 存储每个实体类型对应的仓库实例
1093
- */
1094
- #e = /* @__PURE__ */ new Map();
1095
- /**
1096
- * 实体缓存映射
1097
- * 按实体类型存储实体实例,键为实体ID
1098
- * 用于实现实体标识映射和缓存
1099
- */
1100
- #t = /* @__PURE__ */ new Map();
1101
- /**
1102
- * 行ID映射
1103
- * 存储数据库行ID到实体UUID的映射关系
1104
- * 用于在数据库操作中快速查找实体
1105
- */
1106
- #s = /* @__PURE__ */ new Map();
1107
- /**
1108
- * 创建实体引用
1109
- * 根据提供的数据创建或获取实体实例,并设置其状态
1110
- * 如果实体已存在于缓存中,则返回缓存的实例
1111
- *
1112
- * @template T 实体类型
1113
- * @param EntityType - 实体类型
1114
- * @param data - 实体数据,必须包含id
1115
- * @param [status] - 可选的实体状态配置
1116
- * @returns 创建或获取的实体实例
1117
- */
1118
- createEntityRef(e, t, s) {
1119
- const n = this.#r(e);
1120
- if (n.has(t.id) === !1) {
1121
- const i = Object.create(e.prototype);
1122
- Object.assign(i, t);
1123
- const a = this.#i(i, s);
1124
- n.set(i.id, a);
1125
- }
1126
- return n.get(t.id);
1127
- }
1128
- /**
1129
- * 获取实体引用
1130
- * 从缓存中获取指定ID的实体实例
1131
- *
1132
- * @template T 实体类型
1133
- * @param EntityType - 实体类型
1134
- * @param id - 实体ID
1135
- * @returns 实体实例,如果不存在则返回undefined
1136
- */
1137
- getEntityRef(e, t) {
1138
- return this.#r(e)?.get(t);
1139
- }
1140
- /**
1141
- * 检查实体是否在缓存中
1142
- * 判断指定ID的实体是否已在缓存中初始化
1143
- *
1144
- * @template T 实体类型
1145
- * @param EntityType - 实体类型
1146
- * @param id - 实体ID
1147
- * @returns 如果实体在缓存中存在则返回true
1148
- */
1149
- hasEntityRef(e, t) {
1150
- return this.#r(e)?.has(t) === !0;
1151
- }
1152
- /**
1153
- * 添加实体到缓存
1154
- * 将实体实例添加到对应类型的缓存映射中
1155
- *
1156
- * @template T 实体类型
1157
- * @param entity - 要缓存的实体实例
1158
- */
1159
- addEntityCache(e) {
1160
- this.#r(e.constructor)?.set(e.id, e);
1161
- }
1162
- /**
1163
- * 从缓存中删除实体
1164
- * 将实体实例从缓存映射中移除
1165
- *
1166
- * @template T 实体类型
1167
- * @param entity - 要移除的实体实例
1168
- */
1169
- removeEntityCache(e) {
1170
- this.#r(e.constructor)?.delete(e.id);
1171
- }
1172
- /**
1173
- * 更新实体的行ID映射
1174
- * 建立数据库行ID到实体UUID的映射关系
1175
- *
1176
- * @template T 实体类型
1177
- * @param entity - 实体实例
1178
- * @param rowId - 数据库行ID
1179
- */
1180
- updateEntityRowId(e, t) {
1181
- this.#a(e.constructor).set(t, e.id);
1182
- }
1183
- /**
1184
- * 保存实体
1185
- * 根据实体状态决定是创建新实体还是更新现有实体
1186
- * 如果有关联实体需要保存,会使用事务进行批量保存
1187
- *
1188
- * @template T 实体类型
1189
- * @param data - 要保存的实体实例
1190
- * @returns 保存后的实体实例
1191
- */
1192
- async save(e) {
1193
- const t = e.constructor, s = m(e), n = s.getNeedSaveRelationEntities();
1194
- return n.length > 1 ? this.#n(t).transactionSave(n) : s.modified ? s.local ? this.update(e) : this.create(e) : e;
1195
- }
1196
- /**
1197
- * 删除实体
1198
- * 从数据库中删除实体,并分发相关事件
1199
- *
1200
- * @template T 实体类型
1201
- * @param data - 要删除的实体实例
1202
- * @returns 删除后的实体实例
1203
- * @throws 如果删除失败,抛出错误
1204
- */
1205
- async remove(e) {
1206
- const t = e.constructor;
1207
- this.rxdb.dispatchEvent(new Me(e));
1208
- try {
1209
- const s = await this.#n(t).remove(e);
1210
- return this.rxdb.dispatchEvent(new Se(s)), s;
1211
- } catch (s) {
1212
- throw this.rxdb.dispatchEvent(new Ce(e, s)), s;
1213
- }
1214
- }
1215
- /**
1216
- * 创建实体
1217
- * 在数据库中创建新实体,并分发相关事件
1218
- *
1219
- * @template T 实体类型
1220
- * @param data - 要创建的实体实例
1221
- * @returns 创建后的实体实例
1222
- * @throws 如果创建失败,抛出错误
1223
- */
1224
- async create(e) {
1225
- const t = e.constructor;
1226
- this.rxdb.dispatchEvent(new ge(e));
1227
- try {
1228
- const s = await this.#n(t).create(e);
1229
- return this.rxdb.dispatchEvent(new be(s)), s;
1230
- } catch (s) {
1231
- throw this.rxdb.dispatchEvent(new ve(e, s)), s;
1232
- }
1233
- }
1234
- /**
1235
- * 更新实体
1236
- * 在数据库中更新实体,只更新已修改的属性,并分发相关事件
1237
- *
1238
- * @template T 实体类型
1239
- * @param data - 要更新的实体实例
1240
- * @returns 更新后的实体实例
1241
- * @throws 如果更新失败,抛出错误
1242
- */
1243
- async update(e) {
1244
- const t = e.constructor, s = m(e);
1245
- if (s.modified) {
1246
- const n = he(s.target, (i, a) => i === s.origin[a]);
1247
- this.rxdb.dispatchEvent(new Oe(e));
1248
- try {
1249
- const i = await this.#n(t).update(e, n);
1250
- return this.rxdb.dispatchEvent(new Re(i)), i;
1251
- } catch (i) {
1252
- throw this.rxdb.dispatchEvent(new we(e, i)), i;
1253
- }
1254
- }
1255
- return e;
1256
- }
1257
- /**
1258
- * 获取实体仓库
1259
- * 获取指定实体类型的仓库实例
1260
- *
1261
- * @template T 实体类型
1262
- * @param EntityType - 实体类型
1263
- * @returns 实体仓库实例
1264
- */
1265
- getRepository(e) {
1266
- return this.#n(e);
1267
- }
1268
- /**
1269
- * 获取或创建实体仓库
1270
- * 根据实体元数据中的repository配置创建对应类型的仓库
1271
- *
1272
- * @template T 实体类型
1273
- * @template RT 仓库类型
1274
- * @param EntityType - 实体类型
1275
- * @returns 实体仓库实例
1276
- * @throws {RxDBError} 如果仓库类型无效
1277
- */
1278
- #n(e) {
1279
- if (!this.#e.has(e)) {
1280
- const t = O(e);
1281
- let s;
1282
- switch (t.repository) {
1283
- case "Repository":
1284
- s = new ae(this.rxdb, e);
1285
- break;
1286
- case "TreeRepository":
1287
- s = new tt(this.rxdb, e);
1288
- break;
1289
- default:
1290
- throw new v("repository must be 'Repository' or 'TreeRepository'");
1291
- }
1292
- return this.#e.set(e, s), s;
1293
- }
1294
- return this.#e.get(e);
1295
- }
1296
- /**
1297
- * 获取实体缓存映射
1298
- * 获取指定实体类型的缓存映射,如果不存在则创建
1299
- *
1300
- * @template T 实体类型
1301
- * @param EntityType - 实体类型
1302
- * @returns 实体缓存映射
1303
- */
1304
- #r(e) {
1305
- return this.#t.has(e) === !1 && this.#t.set(e, /* @__PURE__ */ new Map()), this.#t.get(e);
1306
- }
1307
- /**
1308
- * 获取行ID缓存映射
1309
- * 获取指定实体类型的行ID映射,如果不存在则创建
1310
- *
1311
- * @template T 实体类型
1312
- * @param EntityType - 实体类型
1313
- * @returns 行ID到UUID的映射
1314
- */
1315
- #a(e) {
1316
- return this.#s.has(e) === !1 && this.#s.set(e, /* @__PURE__ */ new Map()), this.#s.get(e);
1317
- }
1318
- /**
1319
- * 初始化实体
1320
- * 设置实体的状态和代理,使其可以跟踪变更
1321
- *
1322
- * @template T 实体类型
1323
- * @param entity - 要初始化的实体实例
1324
- * @param [status] - 可选的实体状态配置
1325
- * @returns 初始化后的代理实体实例
1326
- */
1327
- #i(e, t) {
1328
- Q(e, x, "");
1329
- const s = new rt(this.rxdb, { target: e, ...t });
1330
- Q(e, se, s);
1331
- const n = st(e);
1332
- return u(s, "proxyTarget", n), n;
1333
- }
1334
- }
1335
- const Z = {
1336
- save: (r) => function() {
1337
- return r.save(this);
1338
- },
1339
- remove: (r) => function() {
1340
- return r.remove(this);
1341
- }
1342
- }, it = (r) => {
1343
- const [{ metadata: e, relation: t }, { metadata: s, relation: n }] = r, i = Array.from(/* @__PURE__ */ new Set([e.namespace, s.namespace])).sort().join("_") || void 0;
1344
- return {
1345
- name: Array.from(/* @__PURE__ */ new Set([e.name, s.name])).sort().join("_"),
1346
- namespace: i,
1347
- relations: [
1348
- {
1349
- name: t.name,
1350
- kind: p.MANY_TO_ONE,
1351
- mappedNamespace: s.namespace,
1352
- mappedEntity: s.name
1353
- },
1354
- {
1355
- name: n.name,
1356
- kind: p.MANY_TO_ONE,
1357
- mappedNamespace: e.namespace,
1358
- mappedEntity: e.name
1359
- }
1360
- ]
1361
- };
1362
- };
1363
- var ot = Object.getOwnPropertyDescriptor, ct = (r, e, t, s) => {
1364
- for (var n = s > 1 ? void 0 : s ? ot(e, t) : e, i = r.length - 1, a; i >= 0; i--)
1365
- (a = r[i]) && (n = a(n) || n);
1366
- return n;
1367
- };
1368
- let P = class {
1369
- id;
1370
- namespace;
1371
- entity;
1372
- entityId;
1373
- type;
1374
- inversePatch;
1375
- patch;
1376
- changedAt;
1377
- };
1378
- P = ct([
1379
- R({
1380
- name: "RxDBChange",
1381
- log: !1,
1382
- properties: [
1383
- {
1384
- name: "id",
1385
- type: E.integer,
1386
- primary: !0
1387
- },
1388
- {
1389
- name: "namespace",
1390
- type: E.string,
1391
- readonly: !0,
1392
- nullable: !0
1393
- },
1394
- {
1395
- name: "entity",
1396
- type: E.string,
1397
- readonly: !0
1398
- },
1399
- {
1400
- name: "entityId",
1401
- type: E.uuid,
1402
- readonly: !0
1403
- },
1404
- {
1405
- name: "type",
1406
- type: E.string,
1407
- readonly: !0
1408
- },
1409
- {
1410
- name: "inversePatch",
1411
- type: E.json,
1412
- readonly: !0,
1413
- nullable: !0
1414
- },
1415
- {
1416
- name: "patch",
1417
- type: E.json,
1418
- readonly: !0,
1419
- nullable: !0
1420
- },
1421
- {
1422
- name: "changedAt",
1423
- type: E.date,
1424
- default: "CURRENT_TIMESTAMP",
1425
- readonly: !0,
1426
- nullable: !0
1427
- }
1428
- ]
1429
- })
1430
- ], P);
1431
- var lt = Object.getOwnPropertyDescriptor, dt = (r, e, t, s) => {
1432
- for (var n = s > 1 ? void 0 : s ? lt(e, t) : e, i = r.length - 1, a; i >= 0; i--)
1433
- (a = r[i]) && (n = a(n) || n);
1434
- return n;
1435
- };
1436
- let M = class {
1437
- id;
1438
- name;
1439
- executedAt;
1440
- };
1441
- M = dt([
1442
- R({
1443
- name: "RxDBMigration",
1444
- log: !1,
1445
- properties: [
1446
- {
1447
- name: "id",
1448
- type: E.integer,
1449
- primary: !0
1450
- },
1451
- {
1452
- name: "name",
1453
- type: E.string,
1454
- readonly: !0
1455
- },
1456
- {
1457
- name: "executedAt",
1458
- type: E.date,
1459
- readonly: !0,
1460
- default: () => /* @__PURE__ */ new Date()
1461
- }
1462
- ]
1463
- })
1464
- ], M);
1465
- var ht = Object.getOwnPropertyDescriptor, pt = (r, e, t, s) => {
1466
- for (var n = s > 1 ? void 0 : s ? ht(e, t) : e, i = r.length - 1, a; i >= 0; i--)
1467
- (a = r[i]) && (n = a(n) || n);
1468
- return n;
1469
- };
1470
- const C = (r, e) => e + ":" + r;
1471
- class ut {
1472
- constructor(e) {
1473
- this.rxdb = e;
1474
- const { entities: t } = this.rxdb.options;
1475
- [P, M, ...t].forEach((s) => {
1476
- const n = O(s), { name: i, namespace: a } = n, o = C(i, a);
1477
- this.#e.set(o, n), this.#t.set(o, s);
1478
- }), this.#e.forEach((s) => {
1479
- const { relations: n } = s;
1480
- n.forEach((i) => {
1481
- if (i.kind === p.MANY_TO_MANY) {
1482
- const a = this.findMappedRelation(s, i);
1483
- if (!a) throw new v("mapped relation not found");
1484
- const o = it([{ metadata: s, relation: i }, a]), c = C(o.name, o.namespace);
1485
- if (this.#e.has(c) === !1) {
1486
- let l = class extends $ {
1487
- };
1488
- l = pt([
1489
- R(o)
1490
- ], l);
1491
- const d = O(l);
1492
- this.#e.set(c, d), this.#t.set(c, l), this.rxdb.options.entities.push(l);
1493
- }
1494
- i.junctionEntityType = this.#t.get(c);
1495
- }
1496
- });
1497
- }), this.#e.forEach((s, n) => {
1498
- const { name: i, namespace: a } = s;
1499
- let o = e;
1500
- a && Object.hasOwn(o, a) === !1 && (u(o, a, {}), o = o[a]), u(o, i, this.#t.get(n));
1501
- }), Object.freeze(this);
1502
- }
1503
- #e = /* @__PURE__ */ new Map();
1504
- #t = /* @__PURE__ */ new Map();
1505
- /**
1506
- * 查找当前关系属性的映射关系
1507
- * @param relation 当前关系属性
1508
- */
1509
- findMappedRelation(e, t) {
1510
- for (const s of this.#e.values())
1511
- if (s.relations && s.name === t.mappedEntity && s.namespace === t.mappedNamespace) {
1512
- let n;
1513
- switch (t.kind) {
1514
- case p.MANY_TO_MANY:
1515
- n = (a) => a.kind === p.MANY_TO_MANY && a.name === t.mappedProperty && a.mappedProperty === t.name;
1516
- break;
1517
- case p.ONE_TO_MANY:
1518
- n = (a) => a.kind === p.MANY_TO_ONE && a.name === t.mappedProperty;
1519
- break;
1520
- case p.MANY_TO_ONE:
1521
- n = (a) => a.kind === p.ONE_TO_MANY && a.mappedProperty === t.name;
1522
- break;
1523
- case p.ONE_TO_ONE:
1524
- n = (a) => a.kind === p.ONE_TO_ONE && a.mappedEntity === e.name;
1525
- break;
1526
- }
1527
- const i = s.relations.find(n);
1528
- if (i)
1529
- return {
1530
- metadata: s,
1531
- relation: i
1532
- };
1533
- }
1534
- }
1535
- /**
1536
- * 通过查询条件的字段找出关系数组
1537
- * @param field 查询条件的字段
1538
- */
1539
- getFieldRelations(e, t) {
1540
- if (t.includes(".") === !1) throw new v(`field '${t}' 必须是关属性查询`);
1541
- const s = [], n = t.split("."), i = n.length - 1;
1542
- let a = e, o, c = !1, l = "";
1543
- return n.length === 2 && n[1] == "id" && (c = a.foreignKeyRelationMap.has(n[0]), l = n[0] + "Id"), n.forEach((d, h) => {
1544
- if (h === i) {
1545
- if (a.propertyMap.has(d) === !1) throw new v(`property '${d}' not found`);
1546
- o = a.propertyMap.get(d);
1547
- } else {
1548
- if (a.relationMap.has(d) === !1) throw new v(`relation '${d}' not found`);
1549
- const y = a.relationMap.get(d);
1550
- s.push({ relation: y, metadata: a }), a = this.getEntityMetadata(y.mappedEntity, y.mappedNamespace);
1551
- }
1552
- }), {
1553
- property: o,
1554
- propertyName: l || o.name,
1555
- isForeignKey: c,
1556
- relations: s
1557
- };
1558
- }
1559
- /**
1560
- * 获取实体元数据
1561
- */
1562
- getEntityMetadata(e, t) {
1563
- const s = C(e, t);
1564
- return this.#e.get(s);
1565
- }
1566
- /**
1567
- * 获取实体元数据
1568
- */
1569
- getEntityType(e, t) {
1570
- const s = C(e, t);
1571
- return this.#t.get(s);
1572
- }
1573
- }
1574
- class Mt {
1575
- // 插件
1576
- #e = /* @__PURE__ */ new WeakSet();
1577
- // events
1578
- #t = /* @__PURE__ */ new Map();
1579
- #s = [];
1580
- #n = !1;
1581
- // 适配器
1582
- #r = /* @__PURE__ */ new Map();
1583
- #a = /* @__PURE__ */ new Map();
1584
- #i = new N(0);
1585
- #l = this.#i.asObservable().pipe(I(), A(1));
1586
- // 上下文
1587
- #c = {};
1588
- /**
1589
- * Schema 管理
1590
- */
1591
- schema;
1592
- /**
1593
- * Entity 管理
1594
- */
1595
- em;
1596
- /**
1597
- * 设置环境上下文
1598
- */
1599
- get context() {
1600
- return this.#c;
1601
- }
1602
- set context(e) {
1603
- this.#c = e;
1604
- }
1605
- options;
1606
- constructor(e) {
1607
- this.options = Object.freeze(
1608
- Object.assign(
1609
- {
1610
- relationQueryDeep: 2
1611
- },
1612
- e
1613
- )
1614
- ), this.schema = new ut(this), this.em = new at(this), this.context = { ...this.options.context }, this.addEventListener(F, () => {
1615
- this.#n = !0;
1616
- }), this.addEventListener(z, () => {
1617
- this.#n = !1, this.#s.forEach((s) => this.dispatchEvent(s)), this.#s = [];
1618
- }), this.addEventListener(H, () => {
1619
- this.#n = !1, this.#s = [];
1620
- });
1621
- const t = this.em;
1622
- this.addEventListener(ee, (s) => t[re](s)), pe(this.options);
1623
- }
1624
- /**
1625
- * 注册适配器
1626
- */
1627
- adapter(e, t) {
1628
- return this.#r.set(e, t), this.#i.next(this.#r.size), this;
1629
- }
1630
- /**
1631
- * 获取适配器
1632
- */
1633
- getAdapter(e) {
1634
- return this.#a.has(e) ? this.#a.get(e) : this.#l.pipe(
1635
- D(() => this.#r.get(e)),
1636
- Y(Boolean),
1637
- I(),
1638
- f((t) => {
1639
- if (!this.#a.has(e)) {
1640
- const s = t(this);
1641
- ue(s) ? this.#a.set(e, q(() => s).pipe(A(1))) : this.#a.set(e, j(s).pipe(A(1)));
1642
- }
1643
- return this.#a.get(e);
1644
- })
1645
- );
1646
- }
1647
- /**
1648
- * 安装插件
1649
- */
1650
- use(e, t) {
1651
- if (this.#e.has(e))
1652
- console.warn("plugin already installed");
1653
- else {
1654
- this.#e.add(e);
1655
- const s = e(this, t);
1656
- this[s.name] = s;
1657
- }
1658
- return this;
1659
- }
1660
- /**
1661
- * 连接适配器
1662
- */
1663
- connect(e) {
1664
- return this.getAdapter(e).pipe(
1665
- f((t) => t.connect()),
1666
- f((t) => je(e, this.options) ? q(async () => {
1667
- if (!await t.isTableExisted(M)) {
1668
- await t.createTable(M), await t.createTable(P);
1669
- for (let n = 0; n < this.options.entities.length; n++) {
1670
- const i = this.options.entities[n];
1671
- try {
1672
- await t.createTable(i);
1673
- } catch (a) {
1674
- console.error(a);
1675
- }
1676
- }
1677
- }
1678
- }).pipe(D(() => t)) : j(t))
1679
- );
1680
- }
1681
- addEventListener(e, t) {
1682
- this.#o(e).add(t);
1683
- }
1684
- removeEventListener(e, t) {
1685
- this.#o(e).delete(t);
1686
- }
1687
- dispatchEvent(e) {
1688
- this.#n && Le(e) === !1 ? this.#s.push(e) : this.#o(e.type).forEach((t) => t.call(this, e));
1689
- }
1690
- #o(e) {
1691
- if (this.#t.has(e))
1692
- return this.#t.get(e);
1693
- {
1694
- const s = /* @__PURE__ */ new Set();
1695
- return this.#t.set(e, s), s;
1696
- }
1697
- }
1698
- }
1699
- class ie {
1700
- constructor(e) {
1701
- this.rxdb = e;
1702
- }
1703
- }
1704
- class St extends ie {
1705
- }
1706
- class Ct extends ie {
1707
- }
1708
- class It {
1709
- constructor(e) {
1710
- this.rxdb = e;
1711
- }
1712
- }
1713
- export {
1714
- ee as DATABASE_DELETE,
1715
- Be as DATABASE_INSERT,
1716
- $e as DATABASE_UPDATE,
1717
- _e as ENTITY_CREATE_ERROR_EVENT,
1718
- V as ENTITY_CREATE_EVENT,
1719
- me as ENTITY_CREATE_SUCCESS_EVENT,
1720
- Ee as ENTITY_NEW_EVENT,
1721
- Ie as ENTITY_PATCH_EVENT,
1722
- xe as ENTITY_REMOVE_ERROR_EVENT,
1723
- W as ENTITY_REMOVE_EVENT,
1724
- Ne as ENTITY_REMOVE_SUCCESS_EVENT,
1725
- Ae as ENTITY_UPDATE_ERROR_EVENT,
1726
- K as ENTITY_UPDATE_EVENT,
1727
- Te as ENTITY_UPDATE_SUCCESS_EVENT,
1728
- R as Entity,
1729
- $ as EntityBase,
1730
- ve as EntityCreateErrorEvent,
1731
- ge as EntityCreateEvent,
1732
- be as EntityCreateSuccessEvent,
1733
- ye as EntityNewEvent,
1734
- De as EntityPatchEvent,
1735
- Ce as EntityRemoveErrorEvent,
1736
- Me as EntityRemoveEvent,
1737
- Se as EntityRemoveSuccessEvent,
1738
- we as EntityUpdateErrorEvent,
1739
- Oe as EntityUpdateEvent,
1740
- Re as EntityUpdateSuccessEvent,
1741
- E as PropertyType,
1742
- p as RelationKind,
1743
- xt as RepositoryBase,
1744
- Mt as RxDB,
1745
- ie as RxDBAdapterBase,
1746
- bt as RxDBAdapterDeleteChangeEvent,
1747
- vt as RxDBAdapterInsertChangeEvent,
1748
- St as RxDBAdapterLocalBase,
1749
- Ct as RxDBAdapterRemoteBase,
1750
- Tt as RxDBAdapterUpdateChangeEvent,
1751
- P as RxDBChange,
1752
- v as RxDBError,
1753
- M as RxDBMigration,
1754
- It as RxDBPluginBase,
1755
- et as SyncStatus,
1756
- Fe as SyncType,
1757
- F as TRANSACTION_BEGIN,
1758
- z as TRANSACTION_COMMIT,
1759
- H as TRANSACTION_ROLLBACK,
1760
- mt as TransactionBeginEvent,
1761
- _t as TransactionCommitEvent,
1762
- gt as TransactionRollbackEvent,
1763
- X as TreeAdjacencyListEntityBase,
1764
- Rt as TreeEntity,
1765
- At as __decorateClass,
1766
- Ke as fillDefaultValue,
1767
- We as fillInitValue,
1768
- O as getEntityMetadata,
1769
- m as getEntityStatus,
1770
- Nt as isEntityEffectOrderBy,
1771
- Ot as isEntityInternalName,
1772
- wt as isEntityMatchRuleGroup,
1773
- Qe as isRuleGroup,
1774
- u as setSafeObjectKey,
1775
- w as setSafeObjectKeyLazyInitOnce,
1776
- Q as setSafeObjectWritableKey,
1777
- ze as transitionMetadata,
1778
- ke as uuid
1779
- };