@aiao/rxdb-client-generator 0.0.5 → 0.0.7
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/README.md +32 -0
- package/dist/RxDBClientGenerator-DmFzgpuy.js +1056 -0
- package/dist/build-client-lib-BGlzuRcG.js +166 -0
- package/dist/{analyze_file.d.ts → cli/analyze-file.d.ts} +2 -2
- package/dist/cli/analyze-file.d.ts.map +1 -0
- package/dist/{build_rxdb_client_lib.d.ts → cli/build-client-lib.d.ts} +1 -1
- package/dist/cli/build-client-lib.d.ts.map +1 -0
- package/dist/cli/cli.d.ts.map +1 -0
- package/dist/{cli.interface.d.ts → cli/cli.interface.d.ts} +1 -1
- package/dist/cli/cli.interface.d.ts.map +1 -0
- package/dist/cli/find-files.d.ts +8 -0
- package/dist/cli/find-files.d.ts.map +1 -0
- package/dist/cli.js +34 -55
- package/dist/core/RxDBClientGenerator.d.ts +71 -0
- package/dist/core/RxDBClientGenerator.d.ts.map +1 -0
- package/dist/core/RxDBClientGenerator.utils.d.ts +36 -0
- package/dist/core/RxDBClientGenerator.utils.d.ts.map +1 -0
- package/dist/core/metadata.utils.d.ts +4 -0
- package/dist/core/metadata.utils.d.ts.map +1 -0
- package/dist/{ts_morph_browser.d.ts → core/ts-morph-browser.d.ts} +63 -85
- package/dist/core/ts-morph-browser.d.ts.map +1 -0
- package/dist/generators/RepositoryGenerator.interface.d.ts +83 -0
- package/dist/generators/RepositoryGenerator.interface.d.ts.map +1 -0
- package/dist/generators/RepositoryGeneratorBase.d.ts +64 -0
- package/dist/generators/RepositoryGeneratorBase.d.ts.map +1 -0
- package/dist/generators/TreeRepositoryGenerator.d.ts +11 -0
- package/dist/generators/TreeRepositoryGenerator.d.ts.map +1 -0
- package/dist/generators/entity-definition.d.ts +10 -0
- package/dist/generators/entity-definition.d.ts.map +1 -0
- package/dist/{generator_entity_properties.d.ts → generators/entity-properties.d.ts} +4 -5
- package/dist/generators/entity-properties.d.ts.map +1 -0
- package/dist/{generator_entity_relations.d.ts → generators/entity-relations.d.ts} +5 -6
- package/dist/generators/entity-relations.d.ts.map +1 -0
- package/dist/generators/entity-rules.d.ts +16 -0
- package/dist/generators/entity-rules.d.ts.map +1 -0
- package/dist/generators/utils.d.ts +3 -0
- package/dist/generators/utils.d.ts.map +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/plugins/vite.d.ts +4 -0
- package/dist/plugins/vite.d.ts.map +1 -0
- package/dist/vite.js +6 -6
- package/package.json +5 -5
- package/dist/RxDBClientGenerator-B3NS1KdI.js +0 -863
- package/dist/RxDBClientGenerator.d.ts +0 -34
- package/dist/RxDBClientGenerator.d.ts.map +0 -1
- package/dist/RxDBClientGenerator.utils.d.ts +0 -20
- package/dist/RxDBClientGenerator.utils.d.ts.map +0 -1
- package/dist/analyze_file.d.ts.map +0 -1
- package/dist/build_rxdb_client_lib-D1VubBOB.js +0 -93
- package/dist/build_rxdb_client_lib.d.ts.map +0 -1
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.interface.d.ts.map +0 -1
- package/dist/find_files.d.ts +0 -3
- package/dist/find_files.d.ts.map +0 -1
- package/dist/generator_all_entity_definition.d.ts +0 -7
- package/dist/generator_all_entity_definition.d.ts.map +0 -1
- package/dist/generator_entity_definition.d.ts +0 -11
- package/dist/generator_entity_definition.d.ts.map +0 -1
- package/dist/generator_entity_js_file.d.ts +0 -7
- package/dist/generator_entity_js_file.d.ts.map +0 -1
- package/dist/generator_entity_properties.d.ts.map +0 -1
- package/dist/generator_entity_relations.d.ts.map +0 -1
- package/dist/generator_entity_rules.d.ts +0 -14
- package/dist/generator_entity_rules.d.ts.map +0 -1
- package/dist/generator_repository_methods.d.ts +0 -19
- package/dist/generator_repository_methods.d.ts.map +0 -1
- package/dist/generator_tree_repository_methods.d.ts +0 -16
- package/dist/generator_tree_repository_methods.d.ts.map +0 -1
- package/dist/generator_utils.d.ts +0 -3
- package/dist/generator_utils.d.ts.map +0 -1
- package/dist/ts_morph_browser.d.ts.map +0 -1
- package/dist/vite.d.ts +0 -4
- package/dist/vite.d.ts.map +0 -1
- /package/dist/{cli.d.ts → cli/cli.d.ts} +0 -0
|
@@ -0,0 +1,1056 @@
|
|
|
1
|
+
import { PropertyType as d, RelationKind as f, ENTITY_BASE_METADATA_OPTIONS as S, TREE_ADJACENCY_LIST_ENTITY_BASE_OPTIONS as C, getEntityMetadata as B, transitionMetadata as N } from "@aiao/rxdb";
|
|
2
|
+
import { omit as K, isObject as G, isArray as F, isNil as V, unionBy as v, capitalizeFirst as E, isFunction as U } from "@aiao/utils";
|
|
3
|
+
const Y = (r, e) => {
|
|
4
|
+
const t = T(r, e), s = !!r.nullable;
|
|
5
|
+
let a = !0;
|
|
6
|
+
!s && Object.hasOwn(r, "default") && (a = !1);
|
|
7
|
+
const o = {
|
|
8
|
+
type: t,
|
|
9
|
+
name: r.name,
|
|
10
|
+
hasQuestionToken: s,
|
|
11
|
+
hasExclamationToken: a,
|
|
12
|
+
isReadonly: r.readonly
|
|
13
|
+
};
|
|
14
|
+
return V(r.default) || (o.initializer = L(r)), o;
|
|
15
|
+
}, I = (r, e) => `${e.name}${r.name.charAt(0).toUpperCase() + r.name.slice(1)}KeyValue`, J = (r, e) => r.type !== d.keyValue || !("properties" in r) || !r.properties || r.properties.length === 0 ? "KeyValue" : I(r, e), H = (r, e) => r.type !== d.keyValue || !("properties" in r) || !r.properties || r.properties.length === 0 ? [] : r.properties.map((t) => ({
|
|
16
|
+
name: t.name,
|
|
17
|
+
type: T(t, e),
|
|
18
|
+
hasQuestionToken: t.nullable || !1,
|
|
19
|
+
docs: t.displayName ? [t.displayName] : void 0
|
|
20
|
+
})), T = (r, e) => {
|
|
21
|
+
let t = "any";
|
|
22
|
+
switch (r.type) {
|
|
23
|
+
case d.uuid:
|
|
24
|
+
t = "UUID";
|
|
25
|
+
break;
|
|
26
|
+
case d.string:
|
|
27
|
+
t = "string";
|
|
28
|
+
break;
|
|
29
|
+
case d.number:
|
|
30
|
+
t = "number";
|
|
31
|
+
break;
|
|
32
|
+
case d.integer:
|
|
33
|
+
t = "number";
|
|
34
|
+
break;
|
|
35
|
+
case d.boolean:
|
|
36
|
+
t = "boolean";
|
|
37
|
+
break;
|
|
38
|
+
case d.date:
|
|
39
|
+
t = "Date";
|
|
40
|
+
break;
|
|
41
|
+
case d.stringArray:
|
|
42
|
+
t = "string[]";
|
|
43
|
+
break;
|
|
44
|
+
case d.numberArray:
|
|
45
|
+
t = "number[]";
|
|
46
|
+
break;
|
|
47
|
+
case d.keyValue:
|
|
48
|
+
t = J(r, e);
|
|
49
|
+
break;
|
|
50
|
+
case d.json:
|
|
51
|
+
t = "Record<string, any>";
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
return r.nullable && (t += " | null"), t;
|
|
55
|
+
}, L = (r) => {
|
|
56
|
+
switch (r.type) {
|
|
57
|
+
case d.string:
|
|
58
|
+
return `'${String(r.default)}'`;
|
|
59
|
+
case d.date:
|
|
60
|
+
if (r.default)
|
|
61
|
+
return "new Date()";
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
return "";
|
|
65
|
+
}, D = (r, e = /* @__PURE__ */ new Set()) => {
|
|
66
|
+
if (G(r))
|
|
67
|
+
for (const t in r)
|
|
68
|
+
e.add(t), Object.prototype.hasOwnProperty.call(r, t) && D(Reflect.get(r, t), e);
|
|
69
|
+
else if (F(r))
|
|
70
|
+
for (const t of r)
|
|
71
|
+
D(t, e);
|
|
72
|
+
return e;
|
|
73
|
+
}, x = /* @__PURE__ */ new Map();
|
|
74
|
+
Object.keys(d).forEach((r) => {
|
|
75
|
+
const e = d[r];
|
|
76
|
+
x.set(e, r);
|
|
77
|
+
});
|
|
78
|
+
const w = /* @__PURE__ */ new Map();
|
|
79
|
+
Object.keys(f).forEach((r) => {
|
|
80
|
+
const e = f[r];
|
|
81
|
+
w.set(e, r);
|
|
82
|
+
});
|
|
83
|
+
function Q(r) {
|
|
84
|
+
const e = K(r, ["propertyMap", "relationMap", "indexMap"]), t = D(e);
|
|
85
|
+
let s = JSON.stringify(e, null, 2);
|
|
86
|
+
return new Set(r.properties.map((o) => o.type).filter(Boolean)).forEach((o) => {
|
|
87
|
+
const n = x.get(o);
|
|
88
|
+
s = s.replaceAll(`"type": "${o}"`, `"type": PropertyType.${n}`);
|
|
89
|
+
}), r.relations && new Set(r.relations.map((n) => n.kind)).forEach((n) => {
|
|
90
|
+
const i = w.get(n);
|
|
91
|
+
s = s.replaceAll(`"kind": "${n}"`, `"kind": RelationKind.${i}`);
|
|
92
|
+
}), t.forEach((o) => {
|
|
93
|
+
o.includes("-") || o.includes(" ") || (s = s.replaceAll(`"${o}":`, `${o}:`));
|
|
94
|
+
}), s;
|
|
95
|
+
}
|
|
96
|
+
const q = ({
|
|
97
|
+
classProperties: r,
|
|
98
|
+
metadata: e,
|
|
99
|
+
file: t,
|
|
100
|
+
rxdbNamedImports: s
|
|
101
|
+
}) => {
|
|
102
|
+
const a = t.addInterface({
|
|
103
|
+
name: `${e.name}InitData`,
|
|
104
|
+
isExported: !0,
|
|
105
|
+
docs: ["初始化数据"]
|
|
106
|
+
}), o = (n, i = !1) => {
|
|
107
|
+
if (n.type === d.keyValue && "properties" in n && n.properties && n.properties.length > 0) {
|
|
108
|
+
const y = I(n, e), g = t.addInterface({
|
|
109
|
+
name: y,
|
|
110
|
+
isExported: !0,
|
|
111
|
+
docs: [n.displayName || `${n.name} keyValue 类型`]
|
|
112
|
+
});
|
|
113
|
+
H(n, e).forEach((m) => {
|
|
114
|
+
g.addProperty(m);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
const c = Y(n, e), { initializer: u, ...h } = c, l = [];
|
|
118
|
+
switch (l.push(n.displayName || n.name), u && l.push(`@default ${u}`), c.type) {
|
|
119
|
+
case "UUID":
|
|
120
|
+
s.add("UUID");
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
i || a.addProperty({
|
|
124
|
+
name: c.name,
|
|
125
|
+
type: c.type,
|
|
126
|
+
hasQuestionToken: !0,
|
|
127
|
+
docs: l
|
|
128
|
+
}), r.push({
|
|
129
|
+
...h,
|
|
130
|
+
hasExclamationToken: !1,
|
|
131
|
+
isReadonly: n.readonly || !1,
|
|
132
|
+
docs: l
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
Array.from(e.properties.values()).forEach((n) => {
|
|
136
|
+
o(n);
|
|
137
|
+
}), Array.from(e.computedPropertyMap.values()).forEach((n) => {
|
|
138
|
+
let i = e.features || {};
|
|
139
|
+
e.repository === "GraphRepository" || e.extends.includes("GraphRepository") ? i = e.features?.graph || {} : (e.repository === "TreeRepository" || e.extends.includes("TreeRepository")) && (i = e.features?.tree || {}), i[n.name] && o(n, !0);
|
|
140
|
+
});
|
|
141
|
+
}, z = (r) => {
|
|
142
|
+
const e = r.propertyMap.get("id");
|
|
143
|
+
let t = "UUID";
|
|
144
|
+
switch (e?.type) {
|
|
145
|
+
case d.uuid:
|
|
146
|
+
t = "UUID";
|
|
147
|
+
break;
|
|
148
|
+
case d.string:
|
|
149
|
+
t = "string";
|
|
150
|
+
break;
|
|
151
|
+
case d.integer:
|
|
152
|
+
t = "number";
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
return t;
|
|
156
|
+
}, W = ({
|
|
157
|
+
classProperties: r,
|
|
158
|
+
metadata: e,
|
|
159
|
+
rxdbNamedImports: t,
|
|
160
|
+
generator: s
|
|
161
|
+
}) => {
|
|
162
|
+
Array.from(e.relationMap.values()).forEach((a) => {
|
|
163
|
+
switch (a.kind) {
|
|
164
|
+
// 处理一对一和多对一关系
|
|
165
|
+
// 这两种关系需要生成:
|
|
166
|
+
// 1. 关系可观察对象属性(name$)- 用于访问关联实体
|
|
167
|
+
// 2. 外键ID属性(nameId)- 存储关联实体的ID
|
|
168
|
+
case f.ONE_TO_ONE:
|
|
169
|
+
case f.MANY_TO_ONE:
|
|
170
|
+
{
|
|
171
|
+
const o = `RelationEntityObservable<typeof ${a.mappedEntity}>`, n = a.displayName || a.name;
|
|
172
|
+
r.push({
|
|
173
|
+
name: a.name + "$",
|
|
174
|
+
// 关系属性名使用$后缀,表示可观察对象
|
|
175
|
+
type: o,
|
|
176
|
+
isReadonly: !0,
|
|
177
|
+
// 关系属性是只读的,通过set/remove方法修改
|
|
178
|
+
docs: [n]
|
|
179
|
+
});
|
|
180
|
+
const i = s.getMetadata(a.mappedEntity, a.mappedNamespace), c = z(i);
|
|
181
|
+
r.push({
|
|
182
|
+
name: a.name + "Id",
|
|
183
|
+
// 外键ID属性
|
|
184
|
+
// 当关系 nullable 时,id 既可能不存在(undefined),也可能为 null(根节点),或为有效 id
|
|
185
|
+
// 使用 ?: 与 "| null" 组合表达三种状态
|
|
186
|
+
type: a.nullable ? `${c} | null` : c,
|
|
187
|
+
isReadonly: !1,
|
|
188
|
+
hasQuestionToken: a.nullable,
|
|
189
|
+
docs: [n + " id"]
|
|
190
|
+
}), t.add("RelationEntityObservable");
|
|
191
|
+
}
|
|
192
|
+
break;
|
|
193
|
+
// 处理一对多和多对多关系
|
|
194
|
+
// 这两种关系只需要生成关系可观察集合属性(name$)
|
|
195
|
+
// 不需要外键ID属性,因为外键存储在关联实体或中间表中
|
|
196
|
+
case f.ONE_TO_MANY:
|
|
197
|
+
case f.MANY_TO_MANY:
|
|
198
|
+
{
|
|
199
|
+
const o = a.displayName || a.name, n = `RelationEntitiesObservable<${a.mappedEntity}>`;
|
|
200
|
+
r.push({
|
|
201
|
+
name: a.name + "$",
|
|
202
|
+
// 关系集合属性名使用$后缀
|
|
203
|
+
isReadonly: !0,
|
|
204
|
+
// 关系属性是只读的,通过add/remove方法修改
|
|
205
|
+
type: n,
|
|
206
|
+
docs: [o]
|
|
207
|
+
}), t.add("RelationEntitiesObservable");
|
|
208
|
+
}
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}, Z = (r, e, t) => {
|
|
213
|
+
const s = /* @__PURE__ */ new Set(), { name: a } = e, o = e.extends[0] || "", n = [];
|
|
214
|
+
o.includes("TreeAdjacencyListEntityBase") ? n.push("ITreeEntity") : o.includes("EntityBase") && n.push("IEntity"), s.add(o);
|
|
215
|
+
const i = t.addClass({
|
|
216
|
+
name: a,
|
|
217
|
+
isExported: !0,
|
|
218
|
+
extends: o,
|
|
219
|
+
implements: n.sort(),
|
|
220
|
+
decorators: [],
|
|
221
|
+
hasDeclareKeyword: !0
|
|
222
|
+
});
|
|
223
|
+
i.addJsDoc(`${e.displayName}`);
|
|
224
|
+
const c = [];
|
|
225
|
+
c.push({
|
|
226
|
+
type: `${a}StaticTypes`,
|
|
227
|
+
name: "[ENTITY_STATIC_TYPES]",
|
|
228
|
+
isStatic: !0
|
|
229
|
+
}), s.add("ENTITY_STATIC_TYPES");
|
|
230
|
+
const u = t.addInterface({
|
|
231
|
+
name: `${a}StaticTypes`,
|
|
232
|
+
docs: ["静态类型"],
|
|
233
|
+
isExported: !0
|
|
234
|
+
}), h = [];
|
|
235
|
+
q({
|
|
236
|
+
classProperties: c,
|
|
237
|
+
file: t,
|
|
238
|
+
metadata: e,
|
|
239
|
+
rxdbNamedImports: s
|
|
240
|
+
}), W({
|
|
241
|
+
classProperties: c,
|
|
242
|
+
metadata: e,
|
|
243
|
+
rxdbNamedImports: s,
|
|
244
|
+
generator: r
|
|
245
|
+
});
|
|
246
|
+
const l = e.repository || "Repository", y = r.getRepositoryGenerator(l);
|
|
247
|
+
if (y) {
|
|
248
|
+
if (l !== "Repository") {
|
|
249
|
+
const p = r.getRepositoryGenerator("Repository");
|
|
250
|
+
p && p.generate({
|
|
251
|
+
metadata: e,
|
|
252
|
+
classProperties: c,
|
|
253
|
+
classMethods: h,
|
|
254
|
+
rxdbNamedImports: s,
|
|
255
|
+
generator: r,
|
|
256
|
+
file: t,
|
|
257
|
+
staticTypesInterface: u
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
y.generate({
|
|
261
|
+
metadata: e,
|
|
262
|
+
classProperties: c,
|
|
263
|
+
classMethods: h,
|
|
264
|
+
rxdbNamedImports: s,
|
|
265
|
+
generator: r,
|
|
266
|
+
file: t,
|
|
267
|
+
staticTypesInterface: u
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
i.addProperties(
|
|
271
|
+
c.sort((p, m) => p.isStatic && !m.isStatic ? -1 : !p.isStatic && m.isStatic ? 1 : p.isReadonly && !m.isReadonly ? -1 : !p.isReadonly && m.isReadonly ? 1 : p.name.localeCompare(m.name))
|
|
272
|
+
), i.addConstructor({
|
|
273
|
+
parameters: [
|
|
274
|
+
{
|
|
275
|
+
name: "initData",
|
|
276
|
+
type: `${a}InitData`,
|
|
277
|
+
hasQuestionToken: !0
|
|
278
|
+
}
|
|
279
|
+
],
|
|
280
|
+
docs: ["初始化数据", "@param initData 初始化数据"]
|
|
281
|
+
});
|
|
282
|
+
const g = v(h, (p) => p.name).sort(
|
|
283
|
+
(p, m) => p.isStatic && !m.isStatic ? -1 : !p.isStatic && m.isStatic ? 1 : p.name.localeCompare(m.name)
|
|
284
|
+
);
|
|
285
|
+
return i.addMethods(g), {
|
|
286
|
+
rxdbNamedImports: s
|
|
287
|
+
};
|
|
288
|
+
}, X = {
|
|
289
|
+
[d.uuid]: "UUID",
|
|
290
|
+
[d.string]: "String",
|
|
291
|
+
[d.number]: "Number",
|
|
292
|
+
[d.integer]: "Number",
|
|
293
|
+
[d.boolean]: "Boolean",
|
|
294
|
+
[d.date]: "Date",
|
|
295
|
+
[d.stringArray]: "StringArray",
|
|
296
|
+
[d.numberArray]: "NumberArray",
|
|
297
|
+
[d.keyValue]: "KeyValue"
|
|
298
|
+
}, R = (r) => X[r] ?? null, O = (r, e, t) => {
|
|
299
|
+
const s = r.length > 0 ? r.map((a) => a.key).join(".") + "." : "";
|
|
300
|
+
return t ? `${s}${e}.${t}` : `${s}${e}`;
|
|
301
|
+
}, ee = (r, e, t) => {
|
|
302
|
+
let s;
|
|
303
|
+
switch (r.type) {
|
|
304
|
+
case d.stringArray:
|
|
305
|
+
s = "string";
|
|
306
|
+
break;
|
|
307
|
+
case d.numberArray:
|
|
308
|
+
s = "number";
|
|
309
|
+
break;
|
|
310
|
+
case d.keyValue:
|
|
311
|
+
s = `Partial<${I(r, e)}>`;
|
|
312
|
+
break;
|
|
313
|
+
default:
|
|
314
|
+
t.length > 0 && (s = T(r, e));
|
|
315
|
+
}
|
|
316
|
+
return s;
|
|
317
|
+
}, b = (r, e, t, s, a) => ({
|
|
318
|
+
rule: `${r}Rules`,
|
|
319
|
+
entity: e,
|
|
320
|
+
key: t,
|
|
321
|
+
valueType: s,
|
|
322
|
+
subRuleGroup: a
|
|
323
|
+
}), te = (r, e, t, s, a) => {
|
|
324
|
+
r.type !== d.keyValue || !("properties" in r) || !r.properties || r.properties.forEach((o) => {
|
|
325
|
+
const n = R(o.type);
|
|
326
|
+
if (!n) return;
|
|
327
|
+
const i = O(t, s, o.name), c = T(o, e);
|
|
328
|
+
a.push(b(n, e.name, i, c));
|
|
329
|
+
});
|
|
330
|
+
}, se = (r, e = []) => {
|
|
331
|
+
const t = [];
|
|
332
|
+
return Array.from(r.propertyMap.keys()).forEach((s) => {
|
|
333
|
+
const a = r.propertyMap.get(s), o = R(a.type);
|
|
334
|
+
if (o) {
|
|
335
|
+
const n = ee(a, r, e), i = O(e, s);
|
|
336
|
+
t.push(b(o, r.name, i, n));
|
|
337
|
+
}
|
|
338
|
+
te(a, r, e, s, t);
|
|
339
|
+
}), t;
|
|
340
|
+
}, re = (r, e, t) => {
|
|
341
|
+
e.foreignKeyNames.forEach((s) => {
|
|
342
|
+
const a = s.replace(/Id$/, ""), o = e.foreignKeyRelationMap.get(a);
|
|
343
|
+
if (!o) return;
|
|
344
|
+
const i = r.getMetadata(o.mappedEntity, o.mappedNamespace)?.propertyMap.get("id")?.type, c = i && R(i) || "UUID";
|
|
345
|
+
t.push(b(c, e.name, s));
|
|
346
|
+
});
|
|
347
|
+
}, ne = (r) => {
|
|
348
|
+
switch (r.kind) {
|
|
349
|
+
case f.ONE_TO_MANY:
|
|
350
|
+
case f.MANY_TO_MANY:
|
|
351
|
+
return r.mappedProperty ? [r.mappedProperty] : [];
|
|
352
|
+
default:
|
|
353
|
+
return [];
|
|
354
|
+
}
|
|
355
|
+
}, oe = (r, e, t, s) => !!(r.some((o) => o.entity === s.name) || r.length > 0 && e === t), ie = (r, e, t, s, a, o) => {
|
|
356
|
+
Array.from(e.relationMap.keys()).filter((n) => !s.includes(n)).forEach((n) => {
|
|
357
|
+
const i = e.relationMap.get(n);
|
|
358
|
+
if (!i)
|
|
359
|
+
throw new Error("relation is empty");
|
|
360
|
+
const c = r.getMetadata(i.mappedEntity, i.mappedNamespace);
|
|
361
|
+
if (!c)
|
|
362
|
+
throw new Error(`generator_entity_rules: metadata "${i.mappedEntity}" not found`);
|
|
363
|
+
if (oe(o, c, t, e))
|
|
364
|
+
return;
|
|
365
|
+
if (o.length === 0) {
|
|
366
|
+
const l = `${c.name}RuleGroup`;
|
|
367
|
+
a.push(b("RelationExists", e.name, n, void 0, l));
|
|
368
|
+
}
|
|
369
|
+
if (o.length >= r.config.relationQueryDeep)
|
|
370
|
+
return;
|
|
371
|
+
const u = ne(i), h = [...o, { key: n, entity: e.name }];
|
|
372
|
+
M(r, c, t, u, a, h);
|
|
373
|
+
});
|
|
374
|
+
}, M = (r, e, t, s = [], a = [], o = []) => (t = t ?? e, a.push(...se(e, o)), re(r, e, a), ie(r, e, t, s, a, o), a), _ = (r, e) => r.map(({ rule: t, entity: s, key: a, valueType: o, subRuleGroup: n }) => (e.add(t), n ? `${t}<'${a}', ${n}>` : o ? a.includes(".") ? (e.add(`Relation${t}`), `Relation${t}<'${a}', ${o}>`) : `${t}<${s}, '${a}', ${o}>` : a.includes(".") ? (e.add(`Relation${t}`), `Relation${t}<'${a}'>`) : `${t}<${s}, '${a}'>`));
|
|
375
|
+
class ae {
|
|
376
|
+
generate(e) {
|
|
377
|
+
this.generateProperties(e), this.generateMethods(e);
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* 生成器钩子:生成特定的 Repository 属性
|
|
381
|
+
* 子类可选择性覆盖此方法,生成特定的计算属性、索引等
|
|
382
|
+
*
|
|
383
|
+
* @example
|
|
384
|
+
* ```typescript
|
|
385
|
+
* // Tree Generator 可能需要生成树级别缓存属性
|
|
386
|
+
* protected generateProperties(context: GeneratorContext): void {
|
|
387
|
+
* context.classProperties.push({
|
|
388
|
+
* name: 'maxDepth',
|
|
389
|
+
* type: 'number',
|
|
390
|
+
* isReadonly: true
|
|
391
|
+
* });
|
|
392
|
+
* }
|
|
393
|
+
* ```
|
|
394
|
+
*/
|
|
395
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
396
|
+
generateProperties(e) {
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* 共享工具:添加静态查询方法
|
|
400
|
+
*/
|
|
401
|
+
addStaticMethod(e, t) {
|
|
402
|
+
const { classMethods: s, staticTypesInterface: a, rxdbNamedImports: o } = e, n = [t.metHodDoc || `${t.method} 查询`, "@param options 查询选项"];
|
|
403
|
+
t.example && n.push("@example", t.example), s.push({
|
|
404
|
+
name: t.method,
|
|
405
|
+
returnType: `Observable<${t.returnType}>`,
|
|
406
|
+
docs: n,
|
|
407
|
+
parameters: [
|
|
408
|
+
{
|
|
409
|
+
name: "options",
|
|
410
|
+
type: t.options,
|
|
411
|
+
hasQuestionToken: !t.optionsIsRequired
|
|
412
|
+
}
|
|
413
|
+
],
|
|
414
|
+
isStatic: !0
|
|
415
|
+
}), a.addProperty({
|
|
416
|
+
name: `${t.method}Options`,
|
|
417
|
+
type: t.options,
|
|
418
|
+
docs: ["查询选项"]
|
|
419
|
+
});
|
|
420
|
+
const i = t.options.match(/\b([A-Z]\w+Options)\b/g);
|
|
421
|
+
i && i.forEach((c) => {
|
|
422
|
+
!/^[a-z]/.test(c) && c.match(/^(Find|Count|Get)/) && o.add(c);
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* 共享工具:添加实例方法
|
|
427
|
+
*/
|
|
428
|
+
addInstanceMethod(e, t) {
|
|
429
|
+
e.classMethods.push({
|
|
430
|
+
name: t.name,
|
|
431
|
+
returnType: t.returnType,
|
|
432
|
+
docs: t.docs
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* 共享工具:获取 ID 类型
|
|
437
|
+
*/
|
|
438
|
+
getIdType(e) {
|
|
439
|
+
const t = e.propertyMap.get("id");
|
|
440
|
+
if (!t) return "UUID";
|
|
441
|
+
const { type: s } = t;
|
|
442
|
+
return s === "uuid" ? "UUID" : s === "string" ? "string" : s === "number" || s === "integer" ? "number" : "UUID";
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
class j extends ae {
|
|
446
|
+
name = "Repository";
|
|
447
|
+
generateMethods(e) {
|
|
448
|
+
const { metadata: t, file: s, rxdbNamedImports: a, staticTypesInterface: o } = e, { name: n } = t, i = this.getIdType(t);
|
|
449
|
+
o.addProperty({
|
|
450
|
+
name: "idType",
|
|
451
|
+
type: i,
|
|
452
|
+
docs: ["id 类型"]
|
|
453
|
+
});
|
|
454
|
+
const c = M(e.generator, t), u = _(c, a), h = `
|
|
455
|
+
|` + c.map((p) => `'${p.key}'`).join(`
|
|
456
|
+
|`);
|
|
457
|
+
s.addTypeAlias({
|
|
458
|
+
name: `${n}Rule`,
|
|
459
|
+
type: Array.from(new Set(u)).join(`
|
|
460
|
+
| `),
|
|
461
|
+
hasDeclareKeyword: !0,
|
|
462
|
+
docs: ["rule"]
|
|
463
|
+
}), s.addTypeAlias({
|
|
464
|
+
name: `${n}RuleGroup`,
|
|
465
|
+
// 关键:逗号后不加空格,直接换行,避免行尾空格
|
|
466
|
+
type: `RuleGroupBase<typeof ${n},${h},
|
|
467
|
+
${n}Rule>`,
|
|
468
|
+
hasDeclareKeyword: !0,
|
|
469
|
+
docs: ["RuleGroupBase"],
|
|
470
|
+
isExported: !0
|
|
471
|
+
});
|
|
472
|
+
const l = `${n}OrderByField`, y = Array.from(t.propertyMap.keys());
|
|
473
|
+
s.addTypeAlias({
|
|
474
|
+
name: l,
|
|
475
|
+
type: y.map((p) => `"${p}"`).join(" | "),
|
|
476
|
+
hasDeclareKeyword: !0,
|
|
477
|
+
docs: ["OrderByField"]
|
|
478
|
+
});
|
|
479
|
+
const g = (p) => ({
|
|
480
|
+
method: p,
|
|
481
|
+
options: `${E(p)}Options<typeof ${n},${n}RuleGroup,${l}>`
|
|
482
|
+
});
|
|
483
|
+
this.addStaticMethod(e, {
|
|
484
|
+
method: "get",
|
|
485
|
+
options: `${i}`,
|
|
486
|
+
returnType: n,
|
|
487
|
+
metHodDoc: "根据 ID 获取单个实体",
|
|
488
|
+
example: `${n}.get('123').subscribe(entity => console.log(entity));`,
|
|
489
|
+
optionsIsRequired: !0
|
|
490
|
+
}), this.addStaticMethod(e, {
|
|
491
|
+
...g("findOneOrFail"),
|
|
492
|
+
returnType: n,
|
|
493
|
+
metHodDoc: "查询单个实体,未找到时抛出错误",
|
|
494
|
+
example: `${n}.findOneOrFail({ where: { combinator: 'and', rules: [] } }).subscribe(entity => console.log(entity));`,
|
|
495
|
+
optionsIsRequired: !0
|
|
496
|
+
}), this.addStaticMethod(e, {
|
|
497
|
+
...g("find"),
|
|
498
|
+
returnType: `${n}[]`,
|
|
499
|
+
metHodDoc: "查询多个实体",
|
|
500
|
+
example: `${n}.find({ where: { combinator: 'and', rules: [] }] }).subscribe(list => console.log(list));`,
|
|
501
|
+
optionsIsRequired: !0
|
|
502
|
+
}), this.addStaticMethod(e, {
|
|
503
|
+
...g("findOne"),
|
|
504
|
+
returnType: `${n} | undefined`,
|
|
505
|
+
metHodDoc: "查询单个实体,未找到时返回 undefined",
|
|
506
|
+
example: `${n}.findOne({ where: { combinator: 'and', rules: [] } }).subscribe(entity => console.log(entity));`,
|
|
507
|
+
optionsIsRequired: !0
|
|
508
|
+
}), this.addStaticMethod(e, {
|
|
509
|
+
...g("findAll"),
|
|
510
|
+
returnType: `${n}[]`,
|
|
511
|
+
metHodDoc: "查询所有实体",
|
|
512
|
+
example: `${n}.findAll({ where: { combinator: 'and', rules: [] } }).subscribe(list => console.log(list));`,
|
|
513
|
+
optionsIsRequired: !0
|
|
514
|
+
}), this.addStaticMethod(e, {
|
|
515
|
+
...g("findByCursor"),
|
|
516
|
+
returnType: `${n}[]`,
|
|
517
|
+
metHodDoc: "游标分页查询",
|
|
518
|
+
example: `${n}.findByCursor({ where: { combinator: 'and', rules: [] } }).subscribe(list => console.log(list));`,
|
|
519
|
+
optionsIsRequired: !0
|
|
520
|
+
}), this.addStaticMethod(e, {
|
|
521
|
+
method: "count",
|
|
522
|
+
options: `${E("count")}Options<typeof ${n},${n}RuleGroup>`,
|
|
523
|
+
returnType: "number",
|
|
524
|
+
metHodDoc: "统计实体数量",
|
|
525
|
+
example: `${n}.count({ where: { combinator: 'and', rules: [] } }).subscribe(total => console.log(total));`,
|
|
526
|
+
optionsIsRequired: !0
|
|
527
|
+
}), this.addInstanceMethod(e, {
|
|
528
|
+
name: "save",
|
|
529
|
+
returnType: `Promise<${n}>`,
|
|
530
|
+
docs: ["保存"]
|
|
531
|
+
}), this.addInstanceMethod(e, {
|
|
532
|
+
name: "remove",
|
|
533
|
+
returnType: `Promise<${n}>`,
|
|
534
|
+
docs: ["删除"]
|
|
535
|
+
}), this.addInstanceMethod(e, {
|
|
536
|
+
name: "reset",
|
|
537
|
+
returnType: "void",
|
|
538
|
+
docs: ["重置数据"]
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
class ce extends j {
|
|
543
|
+
name = "TreeRepository";
|
|
544
|
+
generateMethods(e) {
|
|
545
|
+
const { metadata: t, rxdbNamedImports: s, generator: a, file: o, staticTypesInterface: n } = e, { name: i } = t, c = M(a, t).filter(
|
|
546
|
+
(y) => y.key.startsWith("children.") && y.key !== "children.id"
|
|
547
|
+
), u = _(c, s), h = c.map((y) => `'${y.key}'`).join(" | ");
|
|
548
|
+
o.addTypeAlias({
|
|
549
|
+
name: `${i}TreeRule`,
|
|
550
|
+
type: Array.from(new Set(u)).join(`
|
|
551
|
+
| `),
|
|
552
|
+
hasDeclareKeyword: !0,
|
|
553
|
+
docs: ["TreeRule"]
|
|
554
|
+
}), o.addTypeAlias({
|
|
555
|
+
name: `${i}TreeRuleGroup`,
|
|
556
|
+
type: `RuleGroupBase<typeof ${i}, ${h}, ${i}TreeRule>`,
|
|
557
|
+
hasDeclareKeyword: !0,
|
|
558
|
+
docs: ["TreeRuleGroup"],
|
|
559
|
+
isExported: !0
|
|
560
|
+
}), n.addProperty({
|
|
561
|
+
name: "entity",
|
|
562
|
+
type: `${i}`,
|
|
563
|
+
docs: ["查询的实体"]
|
|
564
|
+
}), s.add(E("FindTreeOptions"));
|
|
565
|
+
const l = `FindTreeOptions<typeof ${i},${i}TreeRuleGroup>`;
|
|
566
|
+
this.addStaticMethod(e, {
|
|
567
|
+
method: "findDescendants",
|
|
568
|
+
options: l,
|
|
569
|
+
returnType: `${i}[]`,
|
|
570
|
+
metHodDoc: "查询子孙实体(包含自身)",
|
|
571
|
+
example: `// 查询某节点的所有后代
|
|
572
|
+
${i}.findDescendants({ entityId: root.id }).subscribe(list => console.log(list));
|
|
573
|
+
|
|
574
|
+
// 仅查询直接子节点(level 1)
|
|
575
|
+
${i}.findDescendants({ entityId: root.id, level: 1 }).subscribe(children => console.log(children));`
|
|
576
|
+
}), this.addStaticMethod(e, {
|
|
577
|
+
method: "countDescendants",
|
|
578
|
+
options: l,
|
|
579
|
+
returnType: "number",
|
|
580
|
+
metHodDoc: "统计子孙实体数量(不包含自身)",
|
|
581
|
+
example: `// 统计某节点下的后代数量
|
|
582
|
+
${i}.countDescendants({ entityId: root.id }).subscribe(count => console.log(count));`
|
|
583
|
+
}), this.addStaticMethod(e, {
|
|
584
|
+
method: "findAncestors",
|
|
585
|
+
options: l,
|
|
586
|
+
returnType: `${i}[]`,
|
|
587
|
+
metHodDoc: "查询祖先实体(包含自身)",
|
|
588
|
+
example: `// 查询某节点的所有祖先(面包屑导航)
|
|
589
|
+
${i}.findAncestors({ entityId: grand.id }).subscribe(ancestors => console.log(ancestors));
|
|
590
|
+
|
|
591
|
+
// 仅查询直接父节点(level 1)
|
|
592
|
+
${i}.findAncestors({ entityId: grand.id, level: 1 }).subscribe(parents => console.log(parents));`
|
|
593
|
+
}), this.addStaticMethod(e, {
|
|
594
|
+
method: "countAncestors",
|
|
595
|
+
options: l,
|
|
596
|
+
returnType: "number",
|
|
597
|
+
metHodDoc: "统计祖先实体数量(不包含自身)",
|
|
598
|
+
example: `// 统计某节点的祖先层级深度
|
|
599
|
+
${i}.countAncestors({ entityId: grand.id }).subscribe(depth => console.log(depth));`
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
var k = /* @__PURE__ */ ((r) => (r.Var = "var", r.Let = "let", r.Const = "const", r))(k || {});
|
|
604
|
+
class $ {
|
|
605
|
+
/**
|
|
606
|
+
* 渲染单个参数
|
|
607
|
+
*/
|
|
608
|
+
static renderParameter(e, t) {
|
|
609
|
+
const s = [];
|
|
610
|
+
return t > 0 && s.push(", "), s.push(e.name), e.hasQuestionToken && s.push("?"), e.type && s.push(`: ${e.type}`), s.join("");
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* 渲染参数列表
|
|
614
|
+
*/
|
|
615
|
+
static renderParameterList(e) {
|
|
616
|
+
return e?.length ? e.map((t, s) => this.renderParameter(t, s)).join("") : "";
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* 渲染单个变量声明
|
|
620
|
+
*/
|
|
621
|
+
static renderVariableDeclaration(e, t) {
|
|
622
|
+
const s = [];
|
|
623
|
+
return t > 0 && s.push(", "), s.push(e.name), e.type && s.push(`: ${e.type}`), e.initializer && s.push(` = ${e.initializer}`), s.join("");
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* 渲染变量声明列表
|
|
627
|
+
*/
|
|
628
|
+
static renderVariableDeclarationList(e) {
|
|
629
|
+
return e?.length ? e.map((t, s) => this.renderVariableDeclaration(t, s)).join("") : "";
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* 渲染 JSDoc 注释
|
|
633
|
+
*/
|
|
634
|
+
static renderJsDoc(e, t = 0) {
|
|
635
|
+
if (!e?.length) return "";
|
|
636
|
+
const s = " ".repeat(t), a = e.map((o) => `${s} * ${o}`).join(`
|
|
637
|
+
`);
|
|
638
|
+
return `${s}/**
|
|
639
|
+
${a}
|
|
640
|
+
${s} */
|
|
641
|
+
`;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* 渲染属性
|
|
645
|
+
*/
|
|
646
|
+
static renderProperty(e, t, s = !0) {
|
|
647
|
+
const a = " ".repeat(t), o = [];
|
|
648
|
+
return e.docs?.length && o.push(this.renderJsDoc(e.docs, t)), o.push(a), e.isStatic && o.push("static "), e.isReadonly && o.push("readonly "), o.push(e.name), e.hasQuestionToken && o.push("?"), e.type && o.push(`: ${e.type}`), !s && e.initializer && o.push(` = ${e.initializer}`), o.push(`;
|
|
649
|
+
`), o.join("");
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* 渲染接口块
|
|
653
|
+
*/
|
|
654
|
+
static renderInterfaceBlock(e, t, s) {
|
|
655
|
+
const a = " ".repeat(t), o = [];
|
|
656
|
+
return e.docs?.length && o.push(this.renderJsDoc(e.docs, t)), o.push(a), s && e.isExported && o.push("export "), o.push(`interface ${e.name}`), e.extends?.length && o.push(` extends ${e.extends.join(", ")}`), o.push(` {
|
|
657
|
+
`), e.properties?.length && e.properties.forEach((n) => {
|
|
658
|
+
o.push(this.renderProperty(n, t + 2));
|
|
659
|
+
}), o.push(`${a}}
|
|
660
|
+
`), o.join("");
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* 渲染导入声明
|
|
664
|
+
*/
|
|
665
|
+
static renderImports(e) {
|
|
666
|
+
if (!e.length) return "";
|
|
667
|
+
const t = e.filter((s) => s.namedImports?.length).map((s) => `import { ${s.namedImports.join(", ")} } from '${s.moduleSpecifier}';`);
|
|
668
|
+
return t.length ? t.join(`
|
|
669
|
+
`) + `
|
|
670
|
+
|
|
671
|
+
` : "";
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* 渲染类型别名
|
|
675
|
+
*/
|
|
676
|
+
static renderTypeAliases(e) {
|
|
677
|
+
return e.length ? e.map((t) => {
|
|
678
|
+
const s = [];
|
|
679
|
+
return t.docs?.length && s.push(this.renderJsDoc(t.docs, 0)), t.isExported && s.push("export "), t.hasDeclareKeyword && s.push("declare "), s.push(`type ${t.name} = ${t.type};
|
|
680
|
+
|
|
681
|
+
`), s.join("");
|
|
682
|
+
}).join("") : "";
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* 渲染模块声明
|
|
686
|
+
*/
|
|
687
|
+
static renderModules(e) {
|
|
688
|
+
return e.length ? e.map((t) => {
|
|
689
|
+
const s = [];
|
|
690
|
+
return t.docs?.length && s.push(this.renderJsDoc(t.docs, 0)), t.hasDeclareKeyword && s.push("declare "), s.push(`module ${t.name} {
|
|
691
|
+
`), t.interfaces?.length && t.interfaces.forEach((a) => {
|
|
692
|
+
s.push(this.renderInterfaceBlock(a, 2, !1));
|
|
693
|
+
}), s.push(`}
|
|
694
|
+
|
|
695
|
+
`), s.join("");
|
|
696
|
+
}).join("") : "";
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* 渲染类声明
|
|
700
|
+
*/
|
|
701
|
+
static renderClass(e) {
|
|
702
|
+
const t = e.getName();
|
|
703
|
+
if (!t) return "";
|
|
704
|
+
const s = [], a = e.jsDoc?.length ? e.jsDoc : [t];
|
|
705
|
+
s.push(this.renderJsDoc(a)), s.push(`export declare class ${t}`);
|
|
706
|
+
const o = e.getBaseClass();
|
|
707
|
+
if (o) {
|
|
708
|
+
const i = o.getName();
|
|
709
|
+
i && s.push(` extends ${i}`);
|
|
710
|
+
}
|
|
711
|
+
const n = e.getImplements();
|
|
712
|
+
if (n.length) {
|
|
713
|
+
const i = n.map((c) => c.getText()).join(", ");
|
|
714
|
+
s.push(` implements ${i}`);
|
|
715
|
+
}
|
|
716
|
+
if (s.push(` {
|
|
717
|
+
`), e.properties?.length && e.properties.forEach((i) => {
|
|
718
|
+
s.push(this.renderProperty(i, 2, !1));
|
|
719
|
+
}), e.constructorData) {
|
|
720
|
+
const { parameters: i, docs: c } = e.constructorData;
|
|
721
|
+
c?.length && s.push(this.renderJsDoc(c, 2)), s.push(` constructor(${this.renderParameterList(i)});
|
|
722
|
+
`);
|
|
723
|
+
}
|
|
724
|
+
return e.methods?.length && e.methods.forEach((i) => {
|
|
725
|
+
i.docs?.length && s.push(this.renderJsDoc(i.docs, 2));
|
|
726
|
+
const c = [" "];
|
|
727
|
+
i.isStatic && c.push("static "), c.push(`${i.name}(${this.renderParameterList(i.parameters)})`), i.returnType && c.push(`: ${i.returnType}`), c.push(`;
|
|
728
|
+
`), s.push(c.join(""));
|
|
729
|
+
}), s.push(`}
|
|
730
|
+
|
|
731
|
+
`), s.join("");
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* 渲染变量声明
|
|
735
|
+
*/
|
|
736
|
+
static renderVariables(e) {
|
|
737
|
+
return e.length ? e.map((t) => {
|
|
738
|
+
const s = [];
|
|
739
|
+
return t.isExported && s.push("export "), t.hasDeclareKeyword && s.push("declare "), s.push(`${t.declarationKind} `), s.push(this.renderVariableDeclarationList(t.declarations)), s.push(`;
|
|
740
|
+
|
|
741
|
+
`), s.join("");
|
|
742
|
+
}).join("") : "";
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
class de {
|
|
746
|
+
name;
|
|
747
|
+
extends;
|
|
748
|
+
docs;
|
|
749
|
+
isExported;
|
|
750
|
+
properties = [];
|
|
751
|
+
constructor(e) {
|
|
752
|
+
Object.assign(this, e), this.properties || (this.properties = []);
|
|
753
|
+
}
|
|
754
|
+
addProperty(e) {
|
|
755
|
+
this.properties.some((t) => t.name === e.name) || this.properties.push(e);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
class pe {
|
|
759
|
+
structure;
|
|
760
|
+
decorators = [];
|
|
761
|
+
properties = [];
|
|
762
|
+
methods = [];
|
|
763
|
+
constructorData;
|
|
764
|
+
jsDoc = [];
|
|
765
|
+
constructor(e) {
|
|
766
|
+
this.structure = e;
|
|
767
|
+
}
|
|
768
|
+
addJsDoc(e) {
|
|
769
|
+
this.jsDoc.push(e);
|
|
770
|
+
}
|
|
771
|
+
addProperties(e) {
|
|
772
|
+
this.properties.push(...e);
|
|
773
|
+
}
|
|
774
|
+
addConstructor(e) {
|
|
775
|
+
this.constructorData = e;
|
|
776
|
+
}
|
|
777
|
+
addMethods(e) {
|
|
778
|
+
this.methods.push(...e);
|
|
779
|
+
}
|
|
780
|
+
getDecorators() {
|
|
781
|
+
return this.decorators;
|
|
782
|
+
}
|
|
783
|
+
getName() {
|
|
784
|
+
return this.structure.name;
|
|
785
|
+
}
|
|
786
|
+
getBaseClass() {
|
|
787
|
+
if (this.structure.extends)
|
|
788
|
+
return {
|
|
789
|
+
getName: () => this.structure.extends,
|
|
790
|
+
getText: () => this.structure.extends,
|
|
791
|
+
getDecorators: () => [],
|
|
792
|
+
getImplements: () => [],
|
|
793
|
+
getBaseClass: () => {
|
|
794
|
+
}
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
getImplements() {
|
|
798
|
+
return (this.structure.implements || []).map((e) => ({
|
|
799
|
+
getName: () => e,
|
|
800
|
+
getText: () => e,
|
|
801
|
+
getDecorators: () => [],
|
|
802
|
+
getImplements: () => [],
|
|
803
|
+
getBaseClass: () => {
|
|
804
|
+
}
|
|
805
|
+
}));
|
|
806
|
+
}
|
|
807
|
+
getText() {
|
|
808
|
+
return this.structure.name || "";
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
class A {
|
|
812
|
+
filePath;
|
|
813
|
+
classes = [];
|
|
814
|
+
interfaces = [];
|
|
815
|
+
modules = [];
|
|
816
|
+
typeAliases = [];
|
|
817
|
+
imports = [];
|
|
818
|
+
variables = [];
|
|
819
|
+
fileContent = "";
|
|
820
|
+
constructor(e) {
|
|
821
|
+
this.filePath = e;
|
|
822
|
+
}
|
|
823
|
+
addClass(e) {
|
|
824
|
+
const t = new pe(e);
|
|
825
|
+
return e.properties && t.addProperties(e.properties), e.methods && t.addMethods(e.methods), e.docs && e.docs.forEach((s) => t.addJsDoc(s)), this.classes.push(t), t;
|
|
826
|
+
}
|
|
827
|
+
addInterface(e) {
|
|
828
|
+
let t = this.interfaces.find((s) => s.name === e.name);
|
|
829
|
+
return t || (t = new de(e), this.interfaces.push(t)), t;
|
|
830
|
+
}
|
|
831
|
+
addModule(e) {
|
|
832
|
+
return this.modules.push(e), {
|
|
833
|
+
addInterface: (t) => {
|
|
834
|
+
const s = this.modules[this.modules.length - 1];
|
|
835
|
+
return s.interfaces || (s.interfaces = []), s.interfaces.push(t), {
|
|
836
|
+
addProperty: (a) => {
|
|
837
|
+
const o = this.modules[this.modules.length - 1], n = o.interfaces[o.interfaces.length - 1];
|
|
838
|
+
n.properties || (n.properties = []), n.properties.push(a);
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
}
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
addTypeAlias(e) {
|
|
845
|
+
return this.typeAliases.push(e), e;
|
|
846
|
+
}
|
|
847
|
+
addImportDeclaration(e) {
|
|
848
|
+
this.imports.push(e);
|
|
849
|
+
}
|
|
850
|
+
addVariableStatement(e) {
|
|
851
|
+
this.variables.push(e);
|
|
852
|
+
}
|
|
853
|
+
getClasses() {
|
|
854
|
+
return this.classes;
|
|
855
|
+
}
|
|
856
|
+
getFilePath() {
|
|
857
|
+
return this.filePath;
|
|
858
|
+
}
|
|
859
|
+
getText() {
|
|
860
|
+
return this.fileContent ? this.fileContent : [
|
|
861
|
+
$.renderImports(this.imports),
|
|
862
|
+
$.renderTypeAliases(this.typeAliases),
|
|
863
|
+
$.renderModules(this.modules),
|
|
864
|
+
this.interfaces.map((t) => $.renderInterfaceBlock(t, 0, !0) + `
|
|
865
|
+
`).join(""),
|
|
866
|
+
this.classes.map((t) => $.renderClass(t)).join(""),
|
|
867
|
+
$.renderVariables(this.variables)
|
|
868
|
+
].join("");
|
|
869
|
+
}
|
|
870
|
+
setContent(e) {
|
|
871
|
+
this.fileContent = e;
|
|
872
|
+
}
|
|
873
|
+
async save() {
|
|
874
|
+
console.log(`SourceFile ${this.filePath} saved`);
|
|
875
|
+
}
|
|
876
|
+
saveSync() {
|
|
877
|
+
console.log(`SourceFile ${this.filePath} saved synchronously`);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
class le {
|
|
881
|
+
files = /* @__PURE__ */ new Map();
|
|
882
|
+
createSourceFile(e, t) {
|
|
883
|
+
const s = new A(e);
|
|
884
|
+
return t && s.setContent(t), this.files.set(e, s), s;
|
|
885
|
+
}
|
|
886
|
+
addSourceFileAtPath(e) {
|
|
887
|
+
const t = new A(e);
|
|
888
|
+
return this.files.set(e, t), t;
|
|
889
|
+
}
|
|
890
|
+
getSourceFiles() {
|
|
891
|
+
return Array.from(this.files.values());
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
const P = (r, e = "public") => `${e}_${r}`;
|
|
895
|
+
class ye {
|
|
896
|
+
/**
|
|
897
|
+
* Repository 生成器注册表
|
|
898
|
+
* 用于支持可扩展的 Repository 类型
|
|
899
|
+
*/
|
|
900
|
+
repositoryGenerators = /* @__PURE__ */ new Map();
|
|
901
|
+
entityMetadataOptionsMap = /* @__PURE__ */ new Map([
|
|
902
|
+
["EntityBase", [S]],
|
|
903
|
+
["TreeEntityBase", [C, S]]
|
|
904
|
+
]);
|
|
905
|
+
metadataOptionsSet = /* @__PURE__ */ new Set();
|
|
906
|
+
metadataSet = /* @__PURE__ */ new Set();
|
|
907
|
+
metadataMap = /* @__PURE__ */ new Map();
|
|
908
|
+
project;
|
|
909
|
+
config = {
|
|
910
|
+
relationQueryDeep: 3
|
|
911
|
+
};
|
|
912
|
+
constructor(e) {
|
|
913
|
+
Object.assign(this.config, e), this.config.relationQueryDeep < 1 && (this.config.relationQueryDeep = 1), this.registerRepositoryGenerator(new j()), this.registerRepositoryGenerator(new ce());
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* 添加实体配置
|
|
917
|
+
* @param value 实体
|
|
918
|
+
*/
|
|
919
|
+
addEntity(e, t) {
|
|
920
|
+
let s;
|
|
921
|
+
if (U(e))
|
|
922
|
+
s = B(e);
|
|
923
|
+
else {
|
|
924
|
+
const a = e;
|
|
925
|
+
t = t || a.extends?.length && this.entityMetadataOptionsMap.get(a.extends[0]) || [], s = N(e, t);
|
|
926
|
+
}
|
|
927
|
+
this.metadataSet.add(s), this.metadataMap.set(`${s.namespace}_${s.name}`, s);
|
|
928
|
+
}
|
|
929
|
+
getMetadata(e, t) {
|
|
930
|
+
return this.metadataMap.get(P(e, t));
|
|
931
|
+
}
|
|
932
|
+
registerAbstractMetadata(e, t) {
|
|
933
|
+
this.entityMetadataOptionsMap.set(e, t), t.forEach((s) => {
|
|
934
|
+
const a = N(s);
|
|
935
|
+
this.metadataMap.set(P(s.name, s.namespace), a);
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
/**
|
|
939
|
+
* 注册 Repository 生成器
|
|
940
|
+
* 用于扩展支持自定义 Repository 类型
|
|
941
|
+
*
|
|
942
|
+
* @param generator Repository 生成器实例
|
|
943
|
+
* @example
|
|
944
|
+
* ```typescript
|
|
945
|
+
* const generator = new RxDBClientGenerator();
|
|
946
|
+
* generator.registerRepositoryGenerator(new GeoRepositoryGenerator());
|
|
947
|
+
* ```
|
|
948
|
+
*/
|
|
949
|
+
registerRepositoryGenerator(e) {
|
|
950
|
+
this.repositoryGenerators.set(e.name, e);
|
|
951
|
+
}
|
|
952
|
+
/**
|
|
953
|
+
* 获取 Repository 生成器
|
|
954
|
+
* @param name Repository 名称
|
|
955
|
+
* @returns 对应的生成器,如果不存在则返回 undefined
|
|
956
|
+
*/
|
|
957
|
+
getRepositoryGenerator(e) {
|
|
958
|
+
return this.repositoryGenerators.get(e);
|
|
959
|
+
}
|
|
960
|
+
/**
|
|
961
|
+
* 执行生成器
|
|
962
|
+
*/
|
|
963
|
+
exec() {
|
|
964
|
+
this.project = new le(), this.generateAllEntityDefinition(), this.generateEntityJsFile();
|
|
965
|
+
}
|
|
966
|
+
getSourceFiles() {
|
|
967
|
+
return this.project.getSourceFiles();
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* 生成实体的 js 文件
|
|
971
|
+
* 公开此方法以便测试使用
|
|
972
|
+
*/
|
|
973
|
+
generateEntityJsFile() {
|
|
974
|
+
const { project: e, metadataSet: t } = this;
|
|
975
|
+
let s = "";
|
|
976
|
+
const a = /* @__PURE__ */ new Set(["Entity", "__decorateClass"]), o = (i) => {
|
|
977
|
+
const c = this.getMetadata(i.mappedEntity, i.mappedNamespace);
|
|
978
|
+
if (c?.relations)
|
|
979
|
+
return c.relations.find(
|
|
980
|
+
(u) => u.kind === f.MANY_TO_MANY && u.name === i.mappedProperty && u.mappedProperty === i.name
|
|
981
|
+
);
|
|
982
|
+
};
|
|
983
|
+
t.forEach((i) => {
|
|
984
|
+
i.relations.forEach((c) => {
|
|
985
|
+
if (c.kind === f.MANY_TO_MANY && !o(c))
|
|
986
|
+
throw new Error("mapped relation not found");
|
|
987
|
+
});
|
|
988
|
+
}), t.forEach((i) => {
|
|
989
|
+
const c = i.extends[0], { name: u } = i, h = u, l = Q(i);
|
|
990
|
+
c && a.add(c), i.properties.length && a.add("PropertyType"), i.relations.length && a.add("RelationKind"), s += `
|
|
991
|
+
let ${h} = class ${c ? "extends " + c : ""} {};`, s += `
|
|
992
|
+
${h} = __decorateClass(
|
|
993
|
+
[
|
|
994
|
+
Entity(${l})
|
|
995
|
+
],
|
|
996
|
+
${h}
|
|
997
|
+
);`;
|
|
998
|
+
}), s = `import { ${Array.from(a).sort().join(", ")} } from '@aiao/rxdb';` + s;
|
|
999
|
+
const n = Array.from(t.values()).map((i) => i.name).sort().join(", ");
|
|
1000
|
+
s += `
|
|
1001
|
+
const ENTITIES = [ ${n} ];`, s += `
|
|
1002
|
+
export { ENTITIES, ${n} };`, e.createSourceFile("index.js", s);
|
|
1003
|
+
}
|
|
1004
|
+
/**
|
|
1005
|
+
* 生成所有 entity 的定义到 index.d.ts 文件里
|
|
1006
|
+
* @private
|
|
1007
|
+
*/
|
|
1008
|
+
generateAllEntityDefinition() {
|
|
1009
|
+
const { project: e, metadataSet: t } = this, s = e.createSourceFile("index.d.ts"), o = s.addModule({
|
|
1010
|
+
name: '"@aiao/rxdb"',
|
|
1011
|
+
hasDeclareKeyword: !0,
|
|
1012
|
+
docs: ["rxdb"]
|
|
1013
|
+
}).addInterface({
|
|
1014
|
+
name: "RxDB",
|
|
1015
|
+
docs: ["RxDB"]
|
|
1016
|
+
}), n = /* @__PURE__ */ new Set(["EntityType", "IEntity", "ITreeEntity", "RuleGroupBase", "UUID"]);
|
|
1017
|
+
t.forEach((i) => {
|
|
1018
|
+
const { rxdbNamedImports: c } = Z(this, i, s);
|
|
1019
|
+
c.forEach((l) => n.add(l));
|
|
1020
|
+
const { name: u } = i, h = u;
|
|
1021
|
+
i.namespace && i.namespace !== "public" ? o.addProperty({
|
|
1022
|
+
name: i.namespace,
|
|
1023
|
+
type: `{
|
|
1024
|
+
${h}: typeof ${h};
|
|
1025
|
+
}`,
|
|
1026
|
+
docs: [i.displayName]
|
|
1027
|
+
}) : o.addProperty({
|
|
1028
|
+
name: h,
|
|
1029
|
+
type: `typeof ${h}`,
|
|
1030
|
+
docs: [i.displayName]
|
|
1031
|
+
});
|
|
1032
|
+
}), s.addImportDeclaration({
|
|
1033
|
+
namedImports: Array.from(n).filter(Boolean).sort(),
|
|
1034
|
+
isTypeOnly: !0,
|
|
1035
|
+
moduleSpecifier: "@aiao/rxdb"
|
|
1036
|
+
}), s.addImportDeclaration({
|
|
1037
|
+
namedImports: ["Observable"],
|
|
1038
|
+
isTypeOnly: !0,
|
|
1039
|
+
moduleSpecifier: "rxjs"
|
|
1040
|
+
}), s.addVariableStatement({
|
|
1041
|
+
declarationKind: k.Const,
|
|
1042
|
+
hasDeclareKeyword: !0,
|
|
1043
|
+
isExported: !0,
|
|
1044
|
+
declarations: [
|
|
1045
|
+
{
|
|
1046
|
+
name: "ENTITIES",
|
|
1047
|
+
type: "EntityType[]"
|
|
1048
|
+
}
|
|
1049
|
+
]
|
|
1050
|
+
});
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
export {
|
|
1054
|
+
ae as R,
|
|
1055
|
+
ye as a
|
|
1056
|
+
};
|