@aiao/rxdb-plugin-tree 0.0.0-stage → 0.0.28
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/LICENSE +21 -0
- package/README.md +111 -2
- package/dist/constants-DRludxr-.js +41 -0
- package/dist/constants.d.ts +49 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/entity/tree-entity-base.d.ts +44 -0
- package/dist/entity/tree-entity-base.d.ts.map +1 -0
- package/dist/entity/tree-entity.decorator.d.ts +7 -0
- package/dist/entity/tree-entity.decorator.d.ts.map +1 -0
- package/dist/entity/tree-entity.interface.d.ts +36 -0
- package/dist/entity/tree-entity.interface.d.ts.map +1 -0
- package/dist/entity/tree-metadata.interface.d.ts +34 -0
- package/dist/entity/tree-metadata.interface.d.ts.map +1 -0
- package/dist/generator/TreeRepositoryGenerator.d.ts +37 -0
- package/dist/generator/TreeRepositoryGenerator.d.ts.map +1 -0
- package/dist/generator/index.d.ts +17 -0
- package/dist/generator/index.d.ts.map +1 -0
- package/dist/generator.js +87 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +564 -0
- package/dist/plugin.d.ts +84 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/query/merge-update-tree.d.ts +80 -0
- package/dist/query/merge-update-tree.d.ts.map +1 -0
- package/dist/query/merge_create.d.ts +9 -0
- package/dist/query/merge_create.d.ts.map +1 -0
- package/dist/query/merge_remove.d.ts +9 -0
- package/dist/query/merge_remove.d.ts.map +1 -0
- package/dist/query/merge_update.d.ts +9 -0
- package/dist/query/merge_update.d.ts.map +1 -0
- package/dist/query/query-tree.utils.d.ts +87 -0
- package/dist/query/query-tree.utils.d.ts.map +1 -0
- package/dist/query/tree-helper.d.ts +143 -0
- package/dist/query/tree-helper.d.ts.map +1 -0
- package/dist/query/tree-query.interface.d.ts +53 -0
- package/dist/query/tree-query.interface.d.ts.map +1 -0
- package/dist/repository/TreeRepository.d.ts +56 -0
- package/dist/repository/TreeRepository.d.ts.map +1 -0
- package/dist/repository/tree-level.utils.d.ts +9 -0
- package/dist/repository/tree-level.utils.d.ts.map +1 -0
- package/dist/repository/tree-repository.interface.d.ts +70 -0
- package/dist/repository/tree-repository.interface.d.ts.map +1 -0
- package/package.json +63 -3
- package/src/constants.ts +97 -0
- package/src/entity/tree-entity-base.ts +100 -0
- package/src/entity/tree-entity.decorator.ts +37 -0
- package/src/entity/tree-entity.interface.ts +43 -0
- package/src/entity/tree-metadata.interface.ts +35 -0
- package/src/generator/TreeRepositoryGenerator.ts +153 -0
- package/src/generator/index.ts +17 -0
- package/src/index.ts +23 -0
- package/src/plugin.ts +111 -0
- package/src/query/merge-update-tree.ts +558 -0
- package/src/query/merge_create.ts +125 -0
- package/src/query/merge_remove.ts +164 -0
- package/src/query/merge_update.ts +132 -0
- package/src/query/query-tree.utils.ts +204 -0
- package/src/query/tree-helper.ts +342 -0
- package/src/query/tree-query.interface.ts +66 -0
- package/src/repository/TreeRepository.ts +133 -0
- package/src/repository/tree-level.utils.ts +14 -0
- package/src/repository/tree-repository.interface.ts +76 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
import { n as e, r as t, t as n } from "./constants-DRludxr-.js";
|
|
2
|
+
import { Entity as r, EntityBase as i, Repository as a, RxDBPluginBase as o, SyncType as s, applyExternalEntityUpdate as c, assertOptionalNonNegativeSafeInteger as l, getEntityId as u, getFingerprintByEntities as d, getFingerprintPrimitive as f, isEntityMatchWhere as p, isStaleEntityEvent as m, isStaleEntityRemoveEvent as h, prepareIncrementalUpdate as g, queryNeedRefreshCreate as _, queryNeedRefreshRemove as v, queryNeedRefreshUpdate as y } from "@aiao/rxdb";
|
|
3
|
+
import { switchMap as b, tap as x } from "rxjs";
|
|
4
|
+
//#region \0@oxc-project+runtime@0.152.0/helpers/esm/decorate.js
|
|
5
|
+
function S(e, t, n, r) {
|
|
6
|
+
var i = arguments.length, a = i < 3 ? t : r === null ? r = Object.getOwnPropertyDescriptor(t, n) : r, o;
|
|
7
|
+
if (typeof Reflect == "object" && typeof Reflect.decorate == "function") a = Reflect.decorate(e, t, n, r);
|
|
8
|
+
else for (var s = e.length - 1; s >= 0; s--) (o = e[s]) && (a = (i < 3 ? o(a) : i > 3 ? o(t, n, a) : o(t, n)) || a);
|
|
9
|
+
return i > 3 && a && Object.defineProperty(t, n, a), a;
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/entity/tree-entity-base.ts
|
|
13
|
+
var C = class extends i {
|
|
14
|
+
parentId;
|
|
15
|
+
};
|
|
16
|
+
C = S([r(n)], C);
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/entity/tree-entity.decorator.ts
|
|
19
|
+
var w = (e) => {
|
|
20
|
+
let t = e.features?.tree ?? {};
|
|
21
|
+
return r({
|
|
22
|
+
...e,
|
|
23
|
+
features: {
|
|
24
|
+
...e.features,
|
|
25
|
+
tree: {
|
|
26
|
+
...t,
|
|
27
|
+
type: t.type ?? "adjacency-list",
|
|
28
|
+
hasChildren: t.hasChildren ?? !1
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
repository: e.repository ?? "TreeRepository"
|
|
32
|
+
});
|
|
33
|
+
}, T = (e) => e ? Reflect.get(e, "parentId") ?? null : null, E = (e) => "parentId" in e.patch && T(e.patch) !== T(e.inversePatch);
|
|
34
|
+
function* D(e, t, n = {}) {
|
|
35
|
+
let { maxLevel: r, maxDepth: i } = n, a = /* @__PURE__ */ new Set(), o = T(e), s = 1;
|
|
36
|
+
for (; o !== null && !a.has(o) && (i === void 0 || a.size < i);) {
|
|
37
|
+
a.add(o);
|
|
38
|
+
let e = t.get(o);
|
|
39
|
+
if (!e || r !== void 0 && s > r) break;
|
|
40
|
+
yield {
|
|
41
|
+
entity: e,
|
|
42
|
+
level: s
|
|
43
|
+
}, o = T(e), s++;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function O(e, t, n, r) {
|
|
47
|
+
if (t == null) {
|
|
48
|
+
let t = /* @__PURE__ */ new Set(), i = e, a = 0;
|
|
49
|
+
for (; r === void 0 || a <= r;) {
|
|
50
|
+
let e = T(i);
|
|
51
|
+
if (e === null) return !0;
|
|
52
|
+
if (t.has(e)) return !1;
|
|
53
|
+
t.add(e);
|
|
54
|
+
let r = n.get(e);
|
|
55
|
+
if (!r) return !1;
|
|
56
|
+
i = r, a++;
|
|
57
|
+
}
|
|
58
|
+
return !1;
|
|
59
|
+
}
|
|
60
|
+
let i = /* @__PURE__ */ new Set(), a = T(e), o = 1;
|
|
61
|
+
for (; a !== null && !i.has(a);) {
|
|
62
|
+
if (a === t) return r === void 0 || o <= r;
|
|
63
|
+
i.add(a), o++;
|
|
64
|
+
let e = n.get(a);
|
|
65
|
+
if (e) a = T(e);
|
|
66
|
+
else break;
|
|
67
|
+
}
|
|
68
|
+
return !1;
|
|
69
|
+
}
|
|
70
|
+
function k(e, t, n, r) {
|
|
71
|
+
if (t == null) return !1;
|
|
72
|
+
let i = n.get(t);
|
|
73
|
+
if (!i) return !1;
|
|
74
|
+
for (let { entity: t } of D(i, n, { maxLevel: r })) if (Reflect.get(t, "id") === Reflect.get(e, "id")) return !0;
|
|
75
|
+
return !1;
|
|
76
|
+
}
|
|
77
|
+
function* A(...e) {
|
|
78
|
+
for (let t of e) yield* t;
|
|
79
|
+
}
|
|
80
|
+
function j(e, t) {
|
|
81
|
+
let n = /* @__PURE__ */ new Map();
|
|
82
|
+
for (let r of e) {
|
|
83
|
+
let e = t(r);
|
|
84
|
+
e != null && n.set(e, r);
|
|
85
|
+
}
|
|
86
|
+
return n;
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/query/merge_create.ts
|
|
90
|
+
var M = (e, t) => {
|
|
91
|
+
let n = e.options.where, r = t.filter((e) => !n || p(e.patch, n));
|
|
92
|
+
if (r.length === 0) return;
|
|
93
|
+
let i = r.map((t) => e.serialize(t));
|
|
94
|
+
switch (e.type) {
|
|
95
|
+
case "findDescendants": {
|
|
96
|
+
let t = e.options, n = j(A(e.resultEntitySet, i), (e) => e.id), r = i.filter((e) => e.id === t.entityId || O(e, t.entityId, n, t.level));
|
|
97
|
+
if (r.length === 0) return;
|
|
98
|
+
r.forEach((t) => e.resultEntitySet.add(t)), e.next(Array.from(e.resultEntitySet.values()), !0);
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
case "findAncestors": {
|
|
102
|
+
let t = e.options, n = j(A(e.resultEntitySet, i), (e) => e.id), r = i.filter((e) => e.id === t.entityId || k(e, t.entityId, n, t.level));
|
|
103
|
+
if (r.length === 0) return;
|
|
104
|
+
r.forEach((t) => e.resultEntitySet.add(t)), e.next(Array.from(e.resultEntitySet.values()), !0);
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}, N = (t, n) => {
|
|
109
|
+
if (!e.has(t.type)) return;
|
|
110
|
+
if (t.result === void 0) {
|
|
111
|
+
t.refresh();
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (n.some((e) => m(t.rxdb, e))) {
|
|
115
|
+
t.refresh();
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
let r = [], i = [];
|
|
119
|
+
switch (t.type) {
|
|
120
|
+
case "findDescendants":
|
|
121
|
+
case "findAncestors":
|
|
122
|
+
i.push(["match_where", "not_match_relation_where"]), r.push(["match_relation_where"]);
|
|
123
|
+
break;
|
|
124
|
+
case "countDescendants":
|
|
125
|
+
case "countAncestors": r.push(["match_where"], ["match_relation_where"]);
|
|
126
|
+
}
|
|
127
|
+
let a = _(t, n, r, i);
|
|
128
|
+
a.refresh ? t.refresh() : a.recalculate && M(t, a.current_entities);
|
|
129
|
+
}, P = (e, t) => {
|
|
130
|
+
let n = new Set(t.map((e) => e.id));
|
|
131
|
+
switch (e.type) {
|
|
132
|
+
case "findDescendants": {
|
|
133
|
+
let t = Array.from(e.resultEntitySet.values()), r = j(t, (e) => e.id), i = /* @__PURE__ */ new Map(), a = (e) => {
|
|
134
|
+
let t = i.get(e.id);
|
|
135
|
+
if (t !== void 0) return t;
|
|
136
|
+
let a = [e.id], o = n.has(e.id);
|
|
137
|
+
if (!o) for (let { entity: t } of D(e, r)) {
|
|
138
|
+
let e = i.get(t.id);
|
|
139
|
+
if (e !== void 0) {
|
|
140
|
+
o = e;
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
if (n.has(t.id)) {
|
|
144
|
+
o = !0;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
a.push(t.id);
|
|
148
|
+
}
|
|
149
|
+
for (let e of a) i.set(e, o);
|
|
150
|
+
return o;
|
|
151
|
+
}, o = t.filter((e) => !a(e));
|
|
152
|
+
if (o.length === t.length) return;
|
|
153
|
+
e.next(o, !0);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
case "findAncestors": {
|
|
157
|
+
let t = Array.from(e.resultEntitySet.values()), r = t.filter((e) => !n.has(e.id));
|
|
158
|
+
if (r.length === t.length) return;
|
|
159
|
+
let { entityId: i } = e.options;
|
|
160
|
+
if (i == null) {
|
|
161
|
+
e.next(r, !0);
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
let a = j(r, (e) => e.id), o = [], s = a.get(i);
|
|
165
|
+
if (s) {
|
|
166
|
+
o.push(s);
|
|
167
|
+
for (let { entity: e } of D(s, a)) o.push(e);
|
|
168
|
+
}
|
|
169
|
+
e.next(o, !0);
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}, F = (t, n) => {
|
|
174
|
+
if (!e.has(t.type)) return;
|
|
175
|
+
if (t.result === void 0) {
|
|
176
|
+
t.refresh();
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
let r = n.filter((e) => !h(t.rxdb, e));
|
|
180
|
+
if (r.length === 0) return;
|
|
181
|
+
let i = [], a = [];
|
|
182
|
+
switch (t.type) {
|
|
183
|
+
case "findAncestors":
|
|
184
|
+
a.push(["result_contains"]), i.push(["match_relation_where"]);
|
|
185
|
+
break;
|
|
186
|
+
case "findDescendants":
|
|
187
|
+
a.push(["result_contains"], ["match_where"]), i.push(["match_relation_where"]);
|
|
188
|
+
break;
|
|
189
|
+
case "countDescendants":
|
|
190
|
+
case "countAncestors": i.push(["match_where"], ["match_relation_where"]);
|
|
191
|
+
}
|
|
192
|
+
let o = v(t, r, i, a);
|
|
193
|
+
o.refresh ? t.refresh() : o.recalculate && P(t, o.current_entities);
|
|
194
|
+
}, I = (e, t) => {
|
|
195
|
+
let n = u(t);
|
|
196
|
+
if (n !== void 0) return e.ids.has(n) ? !0 : !e.complete && void 0;
|
|
197
|
+
}, L = class {
|
|
198
|
+
cache;
|
|
199
|
+
oldResultMap;
|
|
200
|
+
constructor(e, t) {
|
|
201
|
+
this.cache = e, this.oldResultMap = t;
|
|
202
|
+
}
|
|
203
|
+
resolveParentEntity(e) {
|
|
204
|
+
let t = this.cache.getSerializedUpdate(e);
|
|
205
|
+
return t ? (this.oldResultMap.set(e, t), t) : this.oldResultMap.get(e);
|
|
206
|
+
}
|
|
207
|
+
isEntityDescendant(e, t) {
|
|
208
|
+
if (t == null) {
|
|
209
|
+
let t = T(e), n = 0, r = /* @__PURE__ */ new Set();
|
|
210
|
+
for (; t !== null && !r.has(t);) {
|
|
211
|
+
n++, r.add(t);
|
|
212
|
+
let e = this.resolveParentEntity(t);
|
|
213
|
+
if (e) t = T(e);
|
|
214
|
+
else break;
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
isDescendant: !0,
|
|
218
|
+
level: n
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
let n = T(e), r = /* @__PURE__ */ new Set(), i = 0;
|
|
222
|
+
for (; n !== null && !r.has(n);) {
|
|
223
|
+
if (i++, n === t) return {
|
|
224
|
+
isDescendant: !0,
|
|
225
|
+
level: i
|
|
226
|
+
};
|
|
227
|
+
r.add(n);
|
|
228
|
+
let e = this.resolveParentEntity(n);
|
|
229
|
+
if (e) n = T(e);
|
|
230
|
+
else break;
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
isDescendant: !1,
|
|
234
|
+
level: 0
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
isEntityAncestor(e, t, n) {
|
|
238
|
+
let r = u(t);
|
|
239
|
+
if (r === void 0 || r === u(e)) return !1;
|
|
240
|
+
let i = T(e), a = /* @__PURE__ */ new Set(), o = 0;
|
|
241
|
+
for (; i !== null && !a.has(i);) {
|
|
242
|
+
if (o++, n !== void 0 && o > n) return !1;
|
|
243
|
+
if (i === r) return !0;
|
|
244
|
+
a.add(i);
|
|
245
|
+
let e = this.resolveParentEntity(i);
|
|
246
|
+
if (e) i = T(e);
|
|
247
|
+
else break;
|
|
248
|
+
}
|
|
249
|
+
return !1;
|
|
250
|
+
}
|
|
251
|
+
isEntityDescendantForCount(e, t) {
|
|
252
|
+
if (!e) return;
|
|
253
|
+
if (t == null) return !0;
|
|
254
|
+
let n = T(e), r = /* @__PURE__ */ new Set();
|
|
255
|
+
for (; n !== null && !r.has(n);) {
|
|
256
|
+
if (n === t) return !0;
|
|
257
|
+
r.add(n);
|
|
258
|
+
let e = this.cache.getSerializedUpdate(n);
|
|
259
|
+
if (e) n = T(e);
|
|
260
|
+
else return;
|
|
261
|
+
}
|
|
262
|
+
return !1;
|
|
263
|
+
}
|
|
264
|
+
descendantDepthForCount(e, t) {
|
|
265
|
+
if (!e) return;
|
|
266
|
+
let n = t == null, r = T(e), i = /* @__PURE__ */ new Set(), a = 0;
|
|
267
|
+
for (; r !== null && !i.has(r);) {
|
|
268
|
+
if (a++, !n && r === t) return {
|
|
269
|
+
isDescendant: !0,
|
|
270
|
+
depth: a
|
|
271
|
+
};
|
|
272
|
+
i.add(r);
|
|
273
|
+
let e = this.cache.getSerializedUpdate(r);
|
|
274
|
+
if (!e) return;
|
|
275
|
+
r = T(e);
|
|
276
|
+
}
|
|
277
|
+
return n ? {
|
|
278
|
+
isDescendant: !0,
|
|
279
|
+
depth: a
|
|
280
|
+
} : {
|
|
281
|
+
isDescendant: !1,
|
|
282
|
+
depth: a
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
collectAncestorIdsForCount(e, t) {
|
|
286
|
+
let n = /* @__PURE__ */ new Set(), r = T(e), i = 0;
|
|
287
|
+
for (; r !== null && !n.has(r);) {
|
|
288
|
+
if (i++, t !== void 0 && i > t) return {
|
|
289
|
+
ids: n,
|
|
290
|
+
complete: !0
|
|
291
|
+
};
|
|
292
|
+
n.add(r);
|
|
293
|
+
let e = this.cache.getSerializedUpdate(r);
|
|
294
|
+
if (!e) return {
|
|
295
|
+
ids: n,
|
|
296
|
+
complete: !1
|
|
297
|
+
};
|
|
298
|
+
r = T(e);
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
ids: n,
|
|
302
|
+
complete: !0
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
}, R = (e, t, n, r) => {
|
|
306
|
+
let i = e.options, a = i.entityId, o = i.level, s = Array.from(e.resultEntitySet.values()), l = /* @__PURE__ */ new Map();
|
|
307
|
+
s.forEach((e) => {
|
|
308
|
+
let t = u(e);
|
|
309
|
+
t !== void 0 && l.set(t, e);
|
|
310
|
+
});
|
|
311
|
+
let d = new Set(l.keys()), f = new L(r, l), p = !1, m = /* @__PURE__ */ new Set(), h = (e, t) => a == null ? T(t) === null : e === a, g = (e, t) => {
|
|
312
|
+
if (!n.updatedIds.has(e) || h(e, r.getSerializedUpdate(e) ?? t)) return "keep";
|
|
313
|
+
if (n.newlyUnmatchedIds.has(e)) return "drop";
|
|
314
|
+
if (!n.matchNowIds.has(e)) return "keep";
|
|
315
|
+
let i = r.getData(e);
|
|
316
|
+
if (!i || !E(i)) return "keep";
|
|
317
|
+
let s = r.getSerializedUpdate(e);
|
|
318
|
+
if (!s) return "refresh";
|
|
319
|
+
let { isDescendant: c, level: l } = f.isEntityDescendant(s, a);
|
|
320
|
+
return c && (o === void 0 || l <= o) ? "keep" : "drop";
|
|
321
|
+
}, _ = !1, v = s.filter((e) => {
|
|
322
|
+
let t = u(e);
|
|
323
|
+
if (t === void 0) return !0;
|
|
324
|
+
let n = g(t, e);
|
|
325
|
+
return n === "refresh" ? (_ = !0, !0) : n === "keep" || (m.add(t), p = !0, !1);
|
|
326
|
+
});
|
|
327
|
+
if (_) {
|
|
328
|
+
e.refresh();
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
let y = v.map((e) => {
|
|
332
|
+
let t = u(e);
|
|
333
|
+
if (t !== void 0 && n.updatedIds.has(t)) {
|
|
334
|
+
let n = r.getData(t);
|
|
335
|
+
n && (c(e, n.patch), p = !0);
|
|
336
|
+
}
|
|
337
|
+
return e;
|
|
338
|
+
}), b = y;
|
|
339
|
+
if (m.size > 0) {
|
|
340
|
+
let e = /* @__PURE__ */ new Map();
|
|
341
|
+
y.forEach((t) => {
|
|
342
|
+
let n = u(t);
|
|
343
|
+
n !== void 0 && e.set(n, t);
|
|
344
|
+
});
|
|
345
|
+
let t = (t) => r.getSerializedUpdate(t) ?? e.get(t) ?? l.get(t), n = /* @__PURE__ */ new Map(), i = (e) => {
|
|
346
|
+
let r = u(e), i = r === void 0 ? void 0 : n.get(r);
|
|
347
|
+
if (i !== void 0) return i;
|
|
348
|
+
let o = r === void 0 ? [] : [r], s = !1, c = T(e), l = /* @__PURE__ */ new Set();
|
|
349
|
+
for (; c !== null && !l.has(c);) {
|
|
350
|
+
let e = n.get(c);
|
|
351
|
+
if (e !== void 0) {
|
|
352
|
+
s = e;
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
if (m.has(c)) {
|
|
356
|
+
s = !0;
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
if (c === a) break;
|
|
360
|
+
l.add(c), o.push(c);
|
|
361
|
+
let r = t(c);
|
|
362
|
+
if (!r) break;
|
|
363
|
+
c = T(r);
|
|
364
|
+
}
|
|
365
|
+
for (let e of o) n.set(e, s);
|
|
366
|
+
return s;
|
|
367
|
+
};
|
|
368
|
+
b = y.filter((e) => {
|
|
369
|
+
let t = u(e);
|
|
370
|
+
return t !== void 0 && !h(t, e) && i(e) ? (p = !0, !1) : !0;
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
let x = t.filter((e) => {
|
|
374
|
+
let t = e.id;
|
|
375
|
+
return !n.matchNowIds.has(t) || d.has(t) ? !1 : n.newlyMatchedIds.has(t) || E(e);
|
|
376
|
+
});
|
|
377
|
+
for (let t of x) {
|
|
378
|
+
let n = r.getSerializedUpdate(t.id);
|
|
379
|
+
if (!n) continue;
|
|
380
|
+
let { isDescendant: i, level: s } = f.isEntityDescendant(n, a);
|
|
381
|
+
if (i && (o === void 0 || s <= o)) {
|
|
382
|
+
e.refresh();
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
p && e.next(b, !0);
|
|
387
|
+
}, z = (e, t, n, r) => {
|
|
388
|
+
let i = e.options, a = i.entityId, o = i.level, s = Array.from(e.resultEntitySet.values()), l = /* @__PURE__ */ new Map();
|
|
389
|
+
s.forEach((e) => {
|
|
390
|
+
let t = u(e);
|
|
391
|
+
t !== void 0 && l.set(t, e);
|
|
392
|
+
});
|
|
393
|
+
let d = new Set(l.keys()), f = new L(r, l), p = () => a == null ? void 0 : r.getSerializedUpdate(a) || l.get(a);
|
|
394
|
+
if (t.some(E)) {
|
|
395
|
+
e.refresh();
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
let m = p(), h = (e) => {
|
|
399
|
+
if (e === a) return !1;
|
|
400
|
+
if (d.has(e)) return !0;
|
|
401
|
+
let t = r.getSerializedUpdate(e);
|
|
402
|
+
return !!m && !!t && f.isEntityAncestor(m, t, o);
|
|
403
|
+
};
|
|
404
|
+
if ([...n.newlyMatchedIds, ...n.newlyUnmatchedIds].some(h)) {
|
|
405
|
+
e.refresh();
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
let g = !1, _ = s.map((e) => {
|
|
409
|
+
let t = u(e);
|
|
410
|
+
if (t !== void 0 && n.updatedIds.has(t)) {
|
|
411
|
+
let n = r.getData(t);
|
|
412
|
+
n && (c(e, n.patch), g = !0);
|
|
413
|
+
}
|
|
414
|
+
return e;
|
|
415
|
+
});
|
|
416
|
+
if (m) {
|
|
417
|
+
let e = u(m);
|
|
418
|
+
e !== void 0 && (_.some((t) => u(t) === e) || (g = !0, _.push(m)));
|
|
419
|
+
}
|
|
420
|
+
g && e.next(_, !0);
|
|
421
|
+
}, B = (e, t, n, r) => {
|
|
422
|
+
let i = e.options, a = i.entityId, { level: o } = i, s = new L(r, /* @__PURE__ */ new Map()), c = (e) => {
|
|
423
|
+
if (o === void 0) return s.isEntityDescendantForCount(e, a);
|
|
424
|
+
let t = s.descendantDepthForCount(e, a);
|
|
425
|
+
if (t !== void 0) return t.isDescendant && t.depth <= o;
|
|
426
|
+
}, l = (e) => a == null && !!e && T(e) === null;
|
|
427
|
+
t.some((e) => {
|
|
428
|
+
let t = e.id, i = n.matchBeforeIds.has(t) !== n.matchNowIds.has(t), a = E(e);
|
|
429
|
+
if (!i && !a) return !1;
|
|
430
|
+
let o = r.getSerializedBefore(t, e.inversePatch), s = r.getSerializedUpdate(t);
|
|
431
|
+
if (l(o) && l(s)) return !1;
|
|
432
|
+
let u = c(o), d = a ? c(s) : u;
|
|
433
|
+
return u !== !1 || d !== !1;
|
|
434
|
+
}) && e.refresh();
|
|
435
|
+
}, V = (e, t, n, r) => {
|
|
436
|
+
let i = e.options, a = i.entityId, { level: o } = i;
|
|
437
|
+
if (a == null) {
|
|
438
|
+
e.refresh();
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
let s = r.getData(a);
|
|
442
|
+
if (!s) {
|
|
443
|
+
e.refresh();
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
let c = new L(r, /* @__PURE__ */ new Map()), l = r.getSerializedBefore(s.id, s.inversePatch), u = r.getSerializedUpdate(s.id);
|
|
447
|
+
if (!l || !u) {
|
|
448
|
+
e.refresh();
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
if (T(l) !== T(u)) {
|
|
452
|
+
e.refresh();
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
let d = c.collectAncestorIdsForCount(u, o);
|
|
456
|
+
t.some((e) => {
|
|
457
|
+
let t = e.id;
|
|
458
|
+
if (t === a || n.matchBeforeIds.has(t) === n.matchNowIds.has(t) && !E(e)) return !1;
|
|
459
|
+
let i = r.getSerializedBefore(t, e.inversePatch), o = I(d, i), s = I(d, r.getSerializedUpdate(t));
|
|
460
|
+
return o !== !1 || s !== !1;
|
|
461
|
+
}) && e.refresh();
|
|
462
|
+
}, H = (e, t) => {
|
|
463
|
+
let { cache: n, classification: r } = g(e, t);
|
|
464
|
+
switch (e.type) {
|
|
465
|
+
case "findDescendants":
|
|
466
|
+
R(e, t, r, n);
|
|
467
|
+
break;
|
|
468
|
+
case "findAncestors":
|
|
469
|
+
z(e, t, r, n);
|
|
470
|
+
break;
|
|
471
|
+
case "countDescendants":
|
|
472
|
+
B(e, t, r, n);
|
|
473
|
+
break;
|
|
474
|
+
case "countAncestors": V(e, t, r, n);
|
|
475
|
+
}
|
|
476
|
+
}, U = (t, n) => {
|
|
477
|
+
if (!e.has(t.type)) return;
|
|
478
|
+
if (t.result === void 0) {
|
|
479
|
+
t.refresh();
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
if (t.type === "findAncestors" && n.some(E)) {
|
|
483
|
+
t.refresh();
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
let r = [], i = [];
|
|
487
|
+
switch (t.type) {
|
|
488
|
+
case "findDescendants":
|
|
489
|
+
i.push(["result_contains", "not_match_relation_where"], ["match_where", "not_match_relation_where"], ["match_where_before", "not_match_relation_where"]), r.push(["match_relation_where"]);
|
|
490
|
+
break;
|
|
491
|
+
case "findAncestors":
|
|
492
|
+
i.push(["result_contains", "not_match_relation_where"], ["match_where", "not_match_relation_where"]), r.push(["match_relation_where"]);
|
|
493
|
+
break;
|
|
494
|
+
case "countDescendants":
|
|
495
|
+
case "countAncestors": i.push(["match_where", "not_match_relation_where"], ["match_where_before", "not_match_relation_where"]), r.push(["match_relation_where"]);
|
|
496
|
+
}
|
|
497
|
+
let a = y(t, n, r, i);
|
|
498
|
+
a.refresh ? t.refresh() : a.recalculate && H(t, n);
|
|
499
|
+
}, W = (e) => l(e, `tree query 'level' must be a non-negative integer, received: ${String(e)}`), G = class extends a {
|
|
500
|
+
static _STATIC_METHODS = [...super._STATIC_METHODS, ...t];
|
|
501
|
+
constructor(t, n) {
|
|
502
|
+
super(t, n), e.forEach((e) => {
|
|
503
|
+
this.queryManager.registerMergeCreateFn(e, N), this.queryManager.registerMergeUpdateFn(e, U), this.queryManager.registerMergeRemoveFn(e, F);
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
findDescendants(e) {
|
|
507
|
+
return e = this.#e(e), this.queryManager.createTask({
|
|
508
|
+
options: {
|
|
509
|
+
type: "findDescendants",
|
|
510
|
+
options: e
|
|
511
|
+
},
|
|
512
|
+
runner: () => this.primary$.pipe(b((t) => t.findDescendants(e)), x(this._setLocals)),
|
|
513
|
+
getFingerprint: d
|
|
514
|
+
}).result$;
|
|
515
|
+
}
|
|
516
|
+
countDescendants(e) {
|
|
517
|
+
return e = this.#e(e), this.queryManager.createTask({
|
|
518
|
+
options: {
|
|
519
|
+
type: "countDescendants",
|
|
520
|
+
options: e
|
|
521
|
+
},
|
|
522
|
+
runner: () => this.primary$.pipe(b((t) => t.countDescendants(e))),
|
|
523
|
+
getFingerprint: f
|
|
524
|
+
}).result$;
|
|
525
|
+
}
|
|
526
|
+
findAncestors(e) {
|
|
527
|
+
return e = this.#e(e), this.queryManager.createTask({
|
|
528
|
+
options: {
|
|
529
|
+
type: "findAncestors",
|
|
530
|
+
options: e
|
|
531
|
+
},
|
|
532
|
+
runner: () => this.primary$.pipe(b((t) => t.findAncestors(e)), x(this._setLocals)),
|
|
533
|
+
getFingerprint: d
|
|
534
|
+
}).result$;
|
|
535
|
+
}
|
|
536
|
+
countAncestors(e) {
|
|
537
|
+
return e = this.#e(e), this.queryManager.createTask({
|
|
538
|
+
options: {
|
|
539
|
+
type: "countAncestors",
|
|
540
|
+
options: e
|
|
541
|
+
},
|
|
542
|
+
runner: () => this.primary$.pipe(b((t) => t.countAncestors(e))),
|
|
543
|
+
getFingerprint: f
|
|
544
|
+
}).result$;
|
|
545
|
+
}
|
|
546
|
+
#e(e) {
|
|
547
|
+
return {
|
|
548
|
+
...e,
|
|
549
|
+
level: W(e.level),
|
|
550
|
+
entityId: e.entityId ?? null
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
}, K = class extends o {
|
|
554
|
+
lifecycle = "scoped";
|
|
555
|
+
name = "tree";
|
|
556
|
+
install(e) {
|
|
557
|
+
this.rxdb.repository("TreeRepository", {
|
|
558
|
+
class: G,
|
|
559
|
+
unsupportedSyncTypes: { [s.QueryCache]: "树查询依赖本地完整的祖先链,而缓存只覆盖查过的 where 命中的行,递归会在缺口处静默截断。改用 SyncType.Full / Filter,或把该实体换成普通 Repository。" }
|
|
560
|
+
}, e);
|
|
561
|
+
}
|
|
562
|
+
}, q = (e) => new K(e);
|
|
563
|
+
//#endregion
|
|
564
|
+
export { K as RxDBPluginTree, n as TREE_ADJACENCY_LIST_ENTITY_BASE_OPTIONS, e as TREE_QUERY_TYPES, t as TREE_QUERY_TYPE_LIST, C as TreeAdjacencyListEntityBase, w as TreeEntity, G as TreeRepository, W as assertTreeLevel, q as rxDBPluginTree };
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { IRxDBPlugin, Plugin, RxDBPluginBase } from '@aiao/rxdb';
|
|
2
|
+
import { LifecycleScope } from '@aiao/utils';
|
|
3
|
+
import { EntityMetadataTreeFeatures } from './entity/tree-metadata.interface.js';
|
|
4
|
+
import { TreeRepository } from './repository/TreeRepository.js';
|
|
5
|
+
/**
|
|
6
|
+
* {@link rxDBPluginTree} 的选项。
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* 目前无可配项:树的所有行为都由实体元数据(`features.tree`)和查询选项决定,
|
|
10
|
+
* 没有需要在装配期敲定的开关。保留这个类型是为了让 `Plugin<…>` 的形参具名 ——
|
|
11
|
+
* 将来加选项时是加字段,不是改签名。
|
|
12
|
+
*/
|
|
13
|
+
type RxDBPluginTreeOptions = object;
|
|
14
|
+
/**
|
|
15
|
+
* 树形结构插件。
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* 只做一件事:把 {@link TreeRepository} 登记到 `rxdb` 的仓储注册表,并声明它撑不住
|
|
19
|
+
* `SyncType.QueryCache`。登记是 **scoped** 的 —— 连接断开时随 scope 一起撤销,
|
|
20
|
+
* 不会把指向已拆纪元的仓储留在 `rxdb` 上。
|
|
21
|
+
*
|
|
22
|
+
* 四个树查询(`findDescendants` / `countDescendants` / `findAncestors` / `countAncestors`)
|
|
23
|
+
* 与它们的增量 merge 都挂在 `TreeRepository` 上,不装本插件的应用连 `QueryOptions`
|
|
24
|
+
* 里都没有这四支。
|
|
25
|
+
*
|
|
26
|
+
* 一般不直接 `new`,用工厂 {@link rxDBPluginTree} 交给 `.use()`。
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import { rxDBPluginTree } from '@aiao/rxdb-plugin-tree';
|
|
31
|
+
*
|
|
32
|
+
* const rxdb = new RxDB({ entities: [Category] }).use(rxDBPluginTree);
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare class RxDBPluginTree extends RxDBPluginBase implements IRxDBPlugin {
|
|
36
|
+
readonly lifecycle: "scoped";
|
|
37
|
+
name: Uncapitalize<string>;
|
|
38
|
+
install(scope: LifecycleScope): void;
|
|
39
|
+
}
|
|
40
|
+
declare module '@aiao/rxdb' {
|
|
41
|
+
interface RxDB {
|
|
42
|
+
tree: RxDBPluginTree;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 把 `TreeRepository` 登进门面轴注册表
|
|
46
|
+
*
|
|
47
|
+
* @remarks
|
|
48
|
+
* 与上面 `install()` 里的 `rxdb.repository('TreeRepository', …)` 成对:
|
|
49
|
+
* 那一处是运行期登记,这一处是类型登记。少了它,`@Entity({ repository: 'TreeRepository' })`
|
|
50
|
+
* 只能落到 `RxDBRepositoryName` 的 `(string & {})` 那一支,补全里一个字都没有。
|
|
51
|
+
*/
|
|
52
|
+
interface RxDBRepositories {
|
|
53
|
+
TreeRepository: typeof TreeRepository;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 扩展 EntityMetadataFeatures,添加 tree 字段
|
|
57
|
+
*/
|
|
58
|
+
interface EntityMetadataFeatures {
|
|
59
|
+
/**
|
|
60
|
+
* 树形特性配置
|
|
61
|
+
* 由 rxdb-plugin-tree 插件提供
|
|
62
|
+
*/
|
|
63
|
+
tree?: EntityMetadataTreeFeatures;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* {@link RxDBPluginTree} 的工厂,交给 `RxDB.use()`。
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* 注册树实体(继承 `TreeAdjacencyListEntityBase` 或 `@TreeEntity` 标注)的应用**必须**
|
|
71
|
+
* 装它:没装时 `repository: 'TreeRepository'` 在注册期找不到仓储,实体初始化直接抛错
|
|
72
|
+
* (契约见 `__tests__/contracts/missing-plugin-error.spec.ts`),不会静默降级成普通仓储。
|
|
73
|
+
*
|
|
74
|
+
* @param db - 宿主 `RxDB` 实例,由 `use()` 注入
|
|
75
|
+
* @returns 插件实例
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const rxdb = new RxDB({ entities: [Category] }).use(rxDBPluginTree);
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare const rxDBPluginTree: Plugin<RxDBPluginTreeOptions>;
|
|
83
|
+
export {};
|
|
84
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,EAAQ,cAAc,EAAY,MAAM,YAAY,CAAC;AACjF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,0BAA0B,EAAE,MAAM,qCAAqC,CAAC;AACjF,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAEhE;;;;;;;GAOG;AACH,KAAK,qBAAqB,GAAG,MAAM,CAAC;AAEpC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,cAAe,SAAQ,cAAe,YAAW,WAAW;IACvE,QAAQ,CAAC,SAAS,EAAG,QAAQ,CAAU;IACvC,IAAI,EAAE,YAAY,CAAC,MAAM,CAAC,CAAU;IAEpC,OAAO,CAAC,KAAK,EAAE,cAAc;CAkB9B;AAED,OAAO,QAAQ,YAAY,CAAC;IAC1B,UAAU,IAAI;QACZ,IAAI,EAAE,cAAc,CAAC;KACtB;IACD;;;;;;;OAOG;IACH,UAAU,gBAAgB;QACxB,cAAc,EAAE,OAAO,cAAc,CAAC;KACvC;IACD;;OAEG;IACH,UAAU,sBAAsB;QAC9B;;;WAGG;QACH,IAAI,CAAC,EAAE,0BAA0B,CAAC;KACnC;CACF;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,qBAAqB,CAAwC,CAAC"}
|