@aiao/rxdb-client-generator 0.0.5 → 0.0.6

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.
@@ -1,863 +0,0 @@
1
- import { PropertyType as u, RelationKind as h, getEntityMetadata as k, getEntityMetadataOptions as C, transitionMetadata as b, ENTITY_METADATA_MAP as j } from "@aiao/rxdb";
2
- import { isNil as v, omit as F, isObject as B, isArray as K, capitalizeFirst as g, unionBy as Y, isFunction as U } from "@aiao/utils";
3
- const G = (s) => {
4
- const e = N(s), t = !!s.nullable;
5
- let n = !0;
6
- !t && Object.hasOwn(s, "default") && (n = !1);
7
- const r = {
8
- type: e,
9
- name: s.name,
10
- hasQuestionToken: t,
11
- hasExclamationToken: n,
12
- isReadonly: s.readonly
13
- };
14
- return v(s.default) || (r.initializer = J(s)), r;
15
- }, N = (s) => {
16
- let e = "any";
17
- switch (s.type) {
18
- case u.uuid:
19
- e = "UUID";
20
- break;
21
- case u.string:
22
- e = "string";
23
- break;
24
- case u.number:
25
- e = "number";
26
- break;
27
- case u.boolean:
28
- e = "boolean";
29
- break;
30
- case u.date:
31
- e = "Date";
32
- break;
33
- }
34
- return s.nullable && (e += " | null"), e;
35
- }, J = (s) => {
36
- switch (s.type) {
37
- case u.string:
38
- return `'${String(s.default)}'`;
39
- case u.date:
40
- if (s.default)
41
- return "new Date()";
42
- break;
43
- }
44
- return "";
45
- }, _ = (s, e = /* @__PURE__ */ new Set()) => {
46
- if (B(s))
47
- for (const t in s)
48
- e.add(t), Object.prototype.hasOwnProperty.call(s, t) && _(Reflect.get(s, t), e);
49
- else if (K(s))
50
- for (const t of s)
51
- _(t, e);
52
- return e;
53
- }, x = /* @__PURE__ */ new Map();
54
- Object.keys(u).forEach((s) => {
55
- const e = u[s];
56
- x.set(e, s);
57
- });
58
- const I = /* @__PURE__ */ new Map();
59
- Object.keys(h).forEach((s) => {
60
- const e = h[s];
61
- I.set(e, s);
62
- });
63
- function Q(s) {
64
- const e = F(s, ["propertyMap", "relationMap", "indexMap"]), t = _(e);
65
- let n = JSON.stringify(e, null, 2);
66
- return new Set(s.properties.map((i) => i.type)).forEach((i) => {
67
- const o = x.get(i);
68
- n = n.replaceAll(`"type": "${i}"`, `"type": PropertyType.${o}`);
69
- }), s.relations && new Set(s.relations.map((o) => o.kind)).forEach((o) => {
70
- const a = I.get(o);
71
- n = n.replaceAll(`"kind": "${o}"`, `"kind": RelationKind.${a}`);
72
- }), t.forEach((i) => {
73
- i.includes("-") || i.includes(" ") || (n = n.replaceAll(`"${i}":`, `${i}:`));
74
- }), n;
75
- }
76
- const L = ({
77
- classProperties: s,
78
- metadata: e,
79
- file: t,
80
- rxdbNamedImports: n
81
- }) => {
82
- const { name: r } = e, i = t.addInterface({
83
- name: `${r}InitData`,
84
- isExported: !0,
85
- docs: ["初始化数据"]
86
- });
87
- Array.from(e.properties.values()).forEach((o) => {
88
- const a = G(o), { initializer: c, ...l } = a, d = [];
89
- switch (d.push(o.displayName || o.name), c && d.push(`@default ${c}`), a.type) {
90
- case "UUID":
91
- n.add("UUID");
92
- break;
93
- }
94
- i.addProperty({
95
- name: a.name,
96
- type: a.type,
97
- hasQuestionToken: !0,
98
- docs: d
99
- }), s.push({
100
- ...l,
101
- hasExclamationToken: !1,
102
- docs: d
103
- });
104
- });
105
- }, S = (s) => {
106
- let e = "UUID";
107
- switch (s.propertyMap.get("id").type) {
108
- case u.uuid:
109
- e = "UUID";
110
- break;
111
- case u.string:
112
- e = "string";
113
- break;
114
- case u.integer:
115
- e = "number";
116
- break;
117
- }
118
- return e;
119
- }, H = ({
120
- classProperties: s,
121
- metadata: e,
122
- rxdbNamedImports: t,
123
- generator: n
124
- }) => {
125
- Array.from(e.relationMap.values()).forEach((r) => {
126
- switch (r.kind) {
127
- // 处理一对一和多对一关系
128
- // 这两种关系需要生成:
129
- // 1. 关系可观察对象属性(name$)- 用于访问关联实体
130
- // 2. 外键ID属性(nameId)- 存储关联实体的ID
131
- case h.ONE_TO_ONE:
132
- case h.MANY_TO_ONE:
133
- {
134
- const i = r.nullable ? `RelationEntityObservable<${r.mappedEntity} | null>` : `RelationEntityObservable<${r.mappedEntity}>`, o = r.displayName || r.name;
135
- s.push({
136
- name: r.name + "$",
137
- // 关系属性名使用$后缀,表示可观察对象
138
- type: i,
139
- isReadonly: !0,
140
- // 关系属性是只读的,通过set/remove方法修改
141
- docs: [o]
142
- });
143
- const a = n.getMetadata(r.mappedEntity, r.mappedNamespace), c = S(a);
144
- s.push({
145
- name: r.name + "Id",
146
- // 外键ID属性
147
- type: c,
148
- isReadonly: !0,
149
- hasQuestionToken: r.nullable,
150
- docs: [o + " id"]
151
- }), t.add("RelationEntityObservable");
152
- }
153
- break;
154
- // 处理一对多和多对多关系
155
- // 这两种关系只需要生成关系可观察集合属性(name$)
156
- // 不需要外键ID属性,因为外键存储在关联实体或中间表中
157
- case h.ONE_TO_MANY:
158
- case h.MANY_TO_MANY:
159
- {
160
- const i = r.displayName || r.name, o = `RelationEntitiesObservable<${r.mappedEntity}>`;
161
- s.push({
162
- name: r.name + "$",
163
- // 关系集合属性名使用$后缀
164
- isReadonly: !0,
165
- // 关系属性是只读的,通过add/remove方法修改
166
- type: o,
167
- docs: [i]
168
- }), t.add("RelationEntitiesObservable");
169
- }
170
- break;
171
- }
172
- });
173
- }, V = (s) => {
174
- const e = [];
175
- return Array.from(s.propertyMap.keys()).forEach((t) => {
176
- const n = s.propertyMap.get(t);
177
- let r = "";
178
- switch (n.type) {
179
- case u.uuid:
180
- r = "UUID";
181
- break;
182
- case u.string:
183
- r = "String";
184
- break;
185
- case u.number:
186
- case u.integer:
187
- r = "Number";
188
- break;
189
- case u.boolean:
190
- r = "Boolean";
191
- break;
192
- case u.date:
193
- r = "Date";
194
- break;
195
- }
196
- if (r) {
197
- r += "Rules";
198
- const i = N(n);
199
- e.push({
200
- rule: r,
201
- entity: s.name,
202
- key: t,
203
- valueType: i
204
- });
205
- }
206
- }), e;
207
- }, D = (s, e, t, n = [], r = [], i = []) => (t = t ?? e, V(e).forEach(({ rule: o, entity: a, key: c, valueType: l }) => {
208
- i.length > 0 ? c = i.map((d) => d.key).join(".") + "." + c : l = void 0, r.push({
209
- rule: o,
210
- entity: a,
211
- key: c,
212
- valueType: l
213
- });
214
- }), e.foreignKeyNames.forEach((o) => {
215
- r.push({
216
- rule: "UUIDRules",
217
- entity: e.name,
218
- key: o
219
- });
220
- }), Array.from(e.relationMap.keys()).filter((o) => !n.includes(o)).forEach((o) => {
221
- const a = e.relationMap.get(o);
222
- if (!a)
223
- throw new Error("relation is empty");
224
- const c = [];
225
- switch (a.kind) {
226
- case h.ONE_TO_ONE:
227
- break;
228
- case h.ONE_TO_MANY:
229
- c.push(a.mappedProperty);
230
- break;
231
- case h.MANY_TO_ONE:
232
- break;
233
- case h.MANY_TO_MANY:
234
- c.push(a.mappedProperty);
235
- break;
236
- }
237
- const l = s.getMetadata(a.mappedEntity, a.mappedNamespace);
238
- if (!l)
239
- throw new Error(`generator_entity_rules: metadata "${a.mappedEntity}" not found`);
240
- if (i.find(($) => $.entity === e.name) || i.length && l === t) return;
241
- const y = [...i, { key: o, entity: e.name }];
242
- i.length < s.config.relationQueryDeep && D(s, l, t, c, r, y);
243
- }), r), M = (s, e) => s.map(({ rule: t, entity: n, key: r, valueType: i }) => (e.add(t), i ? r.includes(".") ? (e.add(`Relation${t}`), `Relation${t}<'${r}', ${i}>`) : `${t}<${n}, '${r}', ${i}>` : r.includes(".") ? (e.add(`Relation${t}`), `Relation${t}<'${r}'>`) : `${t}<${n}, '${r}'>`)), z = ({
244
- classMethods: s,
245
- rxdbNamedImports: e,
246
- generator: t,
247
- metadata: n,
248
- file: r,
249
- staticTypesInterface: i,
250
- idType: o
251
- }) => {
252
- const { name: a } = n, c = D(t, n), l = M(c, e), d = `
253
- |` + c.map((f) => `'${f.key}'`).join(`
254
- |`);
255
- r.addTypeAlias({
256
- name: `${a}Rule`,
257
- type: Array.from(new Set(l)).join(`
258
- | `),
259
- hasDeclareKeyword: !0,
260
- docs: ["rule"]
261
- }), r.addTypeAlias({
262
- name: `${a}RuleGroup`,
263
- type: `RuleGroupCustom<typeof ${a}, ${d},
264
- ${a}Rule>`,
265
- hasDeclareKeyword: !0,
266
- docs: ["RuleGroupCustom"],
267
- isExported: !0
268
- });
269
- const y = `${a}OrderByField`, $ = Array.from(n.propertyMap.keys());
270
- r.addTypeAlias({
271
- name: y,
272
- type: $.map((f) => `"${f}"`).join(" | "),
273
- hasDeclareKeyword: !0,
274
- docs: ["OrderByField"]
275
- });
276
- const p = ({
277
- method: f,
278
- options: T,
279
- returnType: E,
280
- metHodDoc: P
281
- }) => {
282
- s.push({
283
- name: f,
284
- returnType: `Observable<${E}>`,
285
- docs: [P || `${f} 查询`, "@param options 查询选项"],
286
- parameters: [
287
- {
288
- name: "options",
289
- type: T
290
- }
291
- ],
292
- isStatic: !0
293
- }), i.addProperty({
294
- name: `${f}Options`,
295
- type: T,
296
- docs: ["查询选项"]
297
- });
298
- const A = g(`${f}Options`);
299
- T.includes(A) && e.add(g(`${f}Options`));
300
- };
301
- p({
302
- method: "get",
303
- options: `${o}`,
304
- returnType: a
305
- });
306
- const m = (f) => ({
307
- method: f,
308
- options: `${g(f)}Options<typeof ${a},${a}RuleGroup,${y}>`
309
- });
310
- p({
311
- ...m("findOneOrFail"),
312
- returnType: a
313
- }), p({
314
- ...m("find"),
315
- returnType: `${a}[]`
316
- }), p({
317
- ...m("findOne"),
318
- returnType: `${a} | undefined`
319
- }), p({
320
- ...m("findAll"),
321
- returnType: `${a}[]`
322
- }), p({
323
- method: "count",
324
- options: `${g("count")}Options<typeof ${a},${a}RuleGroup>`,
325
- returnType: "number"
326
- }), s.push({
327
- name: "save",
328
- returnType: `Promise<${a}>`,
329
- docs: ["保存"]
330
- }), s.push({
331
- name: "remove",
332
- returnType: `Promise<${a}>`,
333
- docs: ["删除"]
334
- }), s.push({
335
- name: "reset",
336
- returnType: "void",
337
- docs: ["重置数据"]
338
- });
339
- }, q = ({
340
- metadata: s,
341
- classMethods: e,
342
- rxdbNamedImports: t,
343
- generator: n,
344
- file: r,
345
- staticTypesInterface: i
346
- }) => {
347
- const { name: o } = s, a = D(n, s).filter(
348
- (p) => p.key.startsWith("children.") && p.key !== "children.id"
349
- ), c = M(a, t), l = a.map((p) => `'${p.key}'`).join(" | ");
350
- r.addTypeAlias({
351
- name: `${o}TreeRule`,
352
- type: Array.from(new Set(c)).join(`
353
- | `),
354
- hasDeclareKeyword: !0,
355
- docs: ["TreeRule"]
356
- }), r.addTypeAlias({
357
- name: `${o}TreeRuleGroup`,
358
- type: `RuleGroupCustom<typeof ${o}, ${l}, ${o}TreeRule>`,
359
- hasDeclareKeyword: !0,
360
- docs: ["TreeRuleGroup"],
361
- isExported: !0
362
- });
363
- const d = ({
364
- method: p,
365
- options: m,
366
- returnType: f,
367
- metHodDoc: T,
368
- optionsIsRequired: E
369
- }) => {
370
- e.push({
371
- name: p,
372
- returnType: `Observable<${f}>`,
373
- docs: [T || `${p} 查询`, "@param options 查询选项"],
374
- parameters: [
375
- {
376
- name: "options",
377
- type: m,
378
- hasQuestionToken: !E
379
- }
380
- ],
381
- isStatic: !0
382
- }), i.addProperty({
383
- name: "entity",
384
- type: `${o}`,
385
- docs: ["查询的实体"]
386
- }), i.addProperty({
387
- name: `${p}Options`,
388
- type: m,
389
- docs: ["查询选项"]
390
- });
391
- };
392
- t.add(g("FindTreeRootOptions")), t.add(g("FindTreeOptions"));
393
- const y = `FindTreeRootOptions<typeof ${o},${o}TreeRuleGroup>`;
394
- d({
395
- method: "findRoot",
396
- options: y,
397
- returnType: `${o}[]`,
398
- metHodDoc: "查询子孙实体",
399
- optionsIsRequired: !1
400
- }), d({
401
- method: "countRoot",
402
- options: y,
403
- returnType: "number",
404
- metHodDoc: "查询子孙实体数量"
405
- });
406
- const $ = `FindTreeOptions<typeof ${o},${o}TreeRuleGroup>`;
407
- d({
408
- method: "findDescendants",
409
- options: $,
410
- returnType: `${o}[]`,
411
- metHodDoc: "查询子孙实体"
412
- }), d({
413
- method: "countDescendants",
414
- options: $,
415
- returnType: "number",
416
- metHodDoc: "查询子孙实体数量"
417
- }), d({
418
- method: "findAncestors",
419
- options: $,
420
- returnType: `${o}[]`,
421
- metHodDoc: "查询祖先实体"
422
- }), d({
423
- method: "countAncestors",
424
- options: $,
425
- returnType: "number",
426
- metHodDoc: "查询祖先实体数量"
427
- });
428
- }, W = (s, e, t) => {
429
- const n = /* @__PURE__ */ new Set(), { name: r } = e, i = e.extends[0] || "", o = ["IEntityStaticType"];
430
- i.includes("TreeAdjacencyListEntityBase") ? o.push("ITreeEntity") : i.includes("EntityBase") && o.push("IEntity"), n.add(i);
431
- const a = t.addClass({
432
- name: r,
433
- isExported: !0,
434
- extends: i,
435
- implements: o.sort(),
436
- decorators: [],
437
- hasDeclareKeyword: !0
438
- });
439
- a.addJsDoc(`${e.displayName}`);
440
- const c = [];
441
- c.push({
442
- type: `${r}StaticTypes`,
443
- name: "[ENTITY_STATIC_TYPES]",
444
- isStatic: !1
445
- }), n.add("ENTITY_STATIC_TYPES");
446
- const l = t.addInterface({
447
- name: `${r}StaticTypes`,
448
- docs: ["静态类型"],
449
- isExported: !0
450
- }), d = S(e);
451
- l.addProperty({
452
- name: "idType",
453
- type: d,
454
- docs: ["id 类型"]
455
- });
456
- const y = [];
457
- L({
458
- classProperties: c,
459
- file: t,
460
- metadata: e,
461
- rxdbNamedImports: n
462
- }), H({
463
- classProperties: c,
464
- metadata: e,
465
- rxdbNamedImports: n,
466
- generator: s
467
- }), z({
468
- classMethods: y,
469
- file: t,
470
- generator: s,
471
- metadata: e,
472
- rxdbNamedImports: n,
473
- staticTypesInterface: l,
474
- idType: d
475
- }), e.repository === "TreeRepository" && q({
476
- classMethods: y,
477
- metadata: e,
478
- file: t,
479
- generator: s,
480
- rxdbNamedImports: n,
481
- staticTypesInterface: l
482
- }), a.addProperties(
483
- c.sort((p, m) => p.isReadonly && !m.isReadonly ? -1 : !p.isReadonly && m.isReadonly ? 1 : p.name.localeCompare(m.name))
484
- ), a.addConstructor({
485
- parameters: [
486
- {
487
- name: "initData",
488
- type: `${r}InitData`,
489
- hasQuestionToken: !0
490
- }
491
- ],
492
- docs: ["初始化数据", "@param initData 初始化数据"]
493
- });
494
- const $ = Y(y, (p) => p.name).sort(
495
- (p, m) => p.isStatic && !m.isStatic ? -1 : !p.isStatic && m.isStatic ? 1 : p.name.localeCompare(m.name)
496
- );
497
- return a.addMethods($), {
498
- rxdbNamedImports: n
499
- };
500
- };
501
- var w = /* @__PURE__ */ ((s) => (s.Var = "var", s.Let = "let", s.Const = "const", s))(w || {});
502
- class X {
503
- files = /* @__PURE__ */ new Map();
504
- createSourceFile(e, t) {
505
- const n = new O(e);
506
- return t && n.setContent(t), this.files.set(e, n), n;
507
- }
508
- addSourceFileAtPath(e) {
509
- const t = new O(e);
510
- return this.files.set(e, t), t;
511
- }
512
- getSourceFiles() {
513
- return Array.from(this.files.values());
514
- }
515
- }
516
- class O {
517
- filePath;
518
- classes = [];
519
- interfaces = [];
520
- modules = [];
521
- typeAliases = [];
522
- imports = [];
523
- variables = [];
524
- fileContent = "";
525
- constructor(e) {
526
- this.filePath = e;
527
- }
528
- addClass(e) {
529
- const t = new ee(e);
530
- return this.classes.push(t), t;
531
- }
532
- addInterface(e) {
533
- let t = this.interfaces.find((n) => n.name === e.name);
534
- return t || (t = new Z(e), this.interfaces.push(t)), t;
535
- }
536
- addModule(e) {
537
- return this.modules.push(e), {
538
- addInterface: (t) => {
539
- const n = this.modules[this.modules.length - 1];
540
- return n.interfaces || (n.interfaces = []), n.interfaces.push(t), {
541
- addProperty: (r) => {
542
- const i = this.modules[this.modules.length - 1], o = i.interfaces[i.interfaces.length - 1];
543
- o.properties || (o.properties = []), o.properties.push(r);
544
- }
545
- };
546
- }
547
- };
548
- }
549
- addTypeAlias(e) {
550
- return this.typeAliases.push(e), e;
551
- }
552
- addImportDeclaration(e) {
553
- this.imports.push(e);
554
- }
555
- addVariableStatement(e) {
556
- this.variables.push(e);
557
- }
558
- getClasses() {
559
- return this.classes;
560
- }
561
- getFilePath() {
562
- return this.filePath;
563
- }
564
- getText() {
565
- if (this.fileContent)
566
- return this.fileContent;
567
- let e = "";
568
- return this.imports.forEach((t) => {
569
- t.namedImports && t.namedImports.length > 0 && (e += `import { ${t.namedImports.join(", ")} } from '${t.moduleSpecifier}';
570
- `);
571
- }), this.imports.length > 0 && (e += `
572
- `), this.typeAliases.forEach((t) => {
573
- t.docs && t.docs.length > 0 && (e += this.renderJsDoc(t.docs, 0)), t.isExported && (e += "export "), t.hasDeclareKeyword && (e += "declare "), e += `type ${t.name} = ${t.type};
574
-
575
- `;
576
- }), this.modules.forEach((t) => {
577
- t.docs && t.docs.length > 0 && (e += this.renderJsDoc(t.docs, 0)), t.hasDeclareKeyword && (e += "declare "), e += `module ${t.name} {
578
- `, t.interfaces && t.interfaces.length > 0 && t.interfaces.forEach((n) => {
579
- e += this.renderInterfaceBlock(n, 2, !1);
580
- }), e += `}
581
-
582
- `;
583
- }), this.interfaces.forEach((t) => {
584
- e += this.renderInterfaceBlock(t, 0, !0) + `
585
- `;
586
- }), this.classes.forEach((t) => {
587
- const n = t.getName();
588
- if (n) {
589
- t.jsDoc && t.jsDoc.length > 0 ? e += this.renderJsDoc(t.jsDoc) : e += this.renderJsDoc([n]), e += `export declare class ${n}`;
590
- const r = t.getBaseClass();
591
- if (r) {
592
- const o = r.getName();
593
- o && (e += ` extends ${o}`);
594
- }
595
- const i = t.getImplements();
596
- if (i.length > 0 && (e += ` implements ${i.map((o) => o.getText()).join(", ")}`), e += ` {
597
- `, t.properties && t.properties.length > 0 && t.properties.forEach((o) => {
598
- e += this.renderProperty(o, 2, !1);
599
- }), t.constructorData) {
600
- const o = t.constructorData;
601
- o.docs && o.docs.length > 0 && (e += this.renderJsDoc(o.docs, 2)), e += ` constructor(${this.renderParameterList(o.parameters)});
602
- `;
603
- }
604
- t.methods && t.methods.length > 0 && t.methods.forEach((o) => {
605
- o.docs && o.docs.length > 0 && (e += this.renderJsDoc(o.docs, 2)), e += " ", o.isStatic && (e += "static "), e += `${o.name}(${this.renderParameterList(o.parameters)})`, o.returnType && (e += `: ${o.returnType}`), e += `;
606
- `;
607
- }), e += `}
608
-
609
- `;
610
- }
611
- }), this.variables.forEach((t) => {
612
- t.isExported && (e += "export "), t.hasDeclareKeyword && (e += "declare "), e += `${t.declarationKind} ${this.renderVariableDeclarationList(t.declarations)};
613
-
614
- `;
615
- }), e;
616
- }
617
- setContent(e) {
618
- this.fileContent = e;
619
- }
620
- async save() {
621
- console.log(`SourceFile ${this.filePath} saved`);
622
- }
623
- saveSync() {
624
- console.log(`SourceFile ${this.filePath} saved synchronously`);
625
- }
626
- // Helpers: rendering
627
- // 通用参数渲染函数
628
- renderParameter(e, t) {
629
- let n = "";
630
- return t > 0 && (n += ", "), n += e.name, e.hasQuestionToken && (n += "?"), e.type && (n += `: ${e.type}`), n;
631
- }
632
- // 通用参数列表渲染函数
633
- renderParameterList(e) {
634
- return !e || e.length === 0 ? "" : e.map((t, n) => this.renderParameter(t, n)).join("");
635
- }
636
- // 通用变量声明渲染函数
637
- renderVariableDeclaration(e, t) {
638
- let n = "";
639
- return t > 0 && (n += ", "), n += e.name, e.type && (n += `: ${e.type}`), e.initializer && (n += ` = ${e.initializer}`), n;
640
- }
641
- // 通用变量声明列表渲染函数
642
- renderVariableDeclarationList(e) {
643
- return !e || e.length === 0 ? "" : e.map((t, n) => this.renderVariableDeclaration(t, n)).join("");
644
- }
645
- // 通用JSDoc渲染函数
646
- renderJsDoc(e, t = 0) {
647
- if (!e || e.length === 0) return "";
648
- const n = " ".repeat(t);
649
- return `${n}/**
650
- ${n} * ${e.join(`
651
- ${n} * `)}
652
- ${n} */
653
- `;
654
- }
655
- // 通用属性渲染函数
656
- renderProperty(e, t, n = !0) {
657
- let r = "";
658
- return e.docs && e.docs.length > 0 && (r += this.renderJsDoc(e.docs, t)), r += " ".repeat(t), e.isStatic && (r += "static "), r += e.name, e.hasQuestionToken && (r += "?"), e.type && (r += `: ${e.type}`), !n && e.initializer && (r += ` = ${e.initializer}`), r += `;
659
- `, r;
660
- }
661
- // 接口属性渲染(调用通用函数)
662
- renderInterfaceProperty(e, t) {
663
- return this.renderProperty(e, t);
664
- }
665
- renderInterfaceBlock(e, t, n) {
666
- const r = " ".repeat(t);
667
- let i = "";
668
- return e.docs && e.docs.length > 0 && (i += this.renderJsDoc(e.docs, t)), n && e.isExported ? i += `${r}export ` : i += r, i += `interface ${e.name}`, e.extends && e.extends.length > 0 && (i += ` extends ${e.extends.join(", ")}`), i += ` {
669
- `, e.properties && e.properties.length > 0 && e.properties.forEach((o) => {
670
- i += this.renderInterfaceProperty(o, t + 2);
671
- }), i += `${r}}
672
- `, i;
673
- }
674
- }
675
- class Z {
676
- name;
677
- extends;
678
- docs;
679
- isExported;
680
- properties = [];
681
- constructor(e) {
682
- Object.assign(this, e);
683
- }
684
- addProperty(e) {
685
- this.properties.some((t) => t.name === e.name) || this.properties.push(e);
686
- }
687
- }
688
- class ee {
689
- structure;
690
- decorators = [];
691
- properties = [];
692
- methods = [];
693
- constructorData;
694
- jsDoc = [];
695
- constructor(e) {
696
- this.structure = e;
697
- }
698
- addJsDoc(e) {
699
- this.jsDoc.push(e);
700
- }
701
- addProperties(e) {
702
- this.properties.push(...e);
703
- }
704
- addConstructor(e) {
705
- this.constructorData = e;
706
- }
707
- addMethods(e) {
708
- this.methods.push(...e);
709
- }
710
- getDecorators() {
711
- return this.decorators;
712
- }
713
- getName() {
714
- return this.structure.name;
715
- }
716
- getBaseClass() {
717
- return this.structure.extends ? {
718
- getName: () => this.structure.extends,
719
- getText: () => this.structure.extends,
720
- getDecorators: () => [],
721
- getImplements: () => [],
722
- getBaseClass: () => {
723
- }
724
- } : void 0;
725
- }
726
- getImplements() {
727
- return (this.structure.implements || []).map((e) => ({
728
- getName: () => e,
729
- getText: () => e,
730
- getDecorators: () => [],
731
- getImplements: () => [],
732
- getBaseClass: () => {
733
- }
734
- }));
735
- }
736
- getText() {
737
- return this.structure.name || "";
738
- }
739
- }
740
- const te = (s) => {
741
- const { project: e, metadataSet: t } = s, n = e.createSourceFile("index.d.ts"), i = n.addModule({
742
- name: '"@aiao/rxdb"',
743
- hasDeclareKeyword: !0,
744
- docs: ["rxdb"]
745
- }).addInterface({
746
- name: "RxDB",
747
- docs: ["RxDB"]
748
- }), o = /* @__PURE__ */ new Set([
749
- "EntityType",
750
- "IEntity",
751
- "IEntityStaticType",
752
- "ITreeEntity",
753
- "RuleGroupCustom",
754
- "UUID"
755
- ]);
756
- t.forEach((a) => {
757
- const { rxdbNamedImports: c } = W(s, a, n);
758
- c.forEach((y) => o.add(y));
759
- const { name: l } = a, d = l;
760
- a.namespace && a.namespace !== "public" ? i.addProperty({
761
- name: a.namespace,
762
- type: `{
763
- ${d}: typeof ${d};
764
- }`,
765
- docs: [a.displayName]
766
- }) : i.addProperty({
767
- name: d,
768
- type: `typeof ${d}`,
769
- docs: [a.displayName]
770
- });
771
- }), n.addImportDeclaration({
772
- namedImports: Array.from(o).filter(Boolean).sort(),
773
- isTypeOnly: !0,
774
- moduleSpecifier: "@aiao/rxdb"
775
- }), n.addImportDeclaration({
776
- namedImports: ["Observable"],
777
- isTypeOnly: !0,
778
- moduleSpecifier: "rxjs"
779
- }), n.addVariableStatement({
780
- declarationKind: w.Const,
781
- hasDeclareKeyword: !0,
782
- isExported: !0,
783
- declarations: [
784
- {
785
- name: "ENTITIES",
786
- type: "EntityType[]"
787
- }
788
- ]
789
- });
790
- }, ne = (s) => {
791
- const { project: e, metadataSet: t } = s;
792
- let n = "";
793
- const r = /* @__PURE__ */ new Set(["Entity", "__decorateClass"]), i = (a) => {
794
- for (const c of t.values())
795
- if (c.relations && c.namespace === a.mappedNamespace && c.name === a.mappedEntity)
796
- return c.relations.find((l) => l.kind === h.MANY_TO_MANY && l.mappedProperty === a.name);
797
- };
798
- t.forEach((a) => {
799
- a.relations.forEach((c) => {
800
- if (c.kind === h.MANY_TO_MANY && !i(c))
801
- throw new Error("mapped relation not found");
802
- });
803
- }), t.forEach((a) => {
804
- const c = a.extends[0], { name: l } = a, d = l, y = Q(a);
805
- c && r.add(c), a.properties.length && r.add("PropertyType"), a.relations.length && r.add("RelationKind"), n += `
806
- let ${d} = class ${c ? "extends " + c : ""} {};`, n += `
807
- ${d} = __decorateClass(
808
- [
809
- Entity(${y})
810
- ],
811
- ${d}
812
- );`;
813
- }), n = `import { ${Array.from(r).sort().join(", ")} } from '@aiao/rxdb';` + n;
814
- const o = Array.from(t.values()).map((a) => a.name).sort().join(", ");
815
- n += `
816
- const ENTITIES = [ ${o} ];`, n += `
817
- export { ENTITIES, ${o} };`, e.createSourceFile("index.js", n);
818
- }, R = (s, e = "public") => `${e}_${s}`;
819
- class oe {
820
- metadataMap = /* @__PURE__ */ new Map();
821
- project;
822
- metadataSet = /* @__PURE__ */ new Set();
823
- config = {
824
- relationQueryDeep: 3
825
- };
826
- constructor(e) {
827
- Object.assign(this.config, e), this.config.relationQueryDeep < 1 && (this.config.relationQueryDeep = 1);
828
- }
829
- /**
830
- * 添加实体配置
831
- * @param value 实体
832
- */
833
- addEntity(e, t) {
834
- let n;
835
- if (U(e))
836
- n = k(e);
837
- else {
838
- const r = e;
839
- t = t || r.extends?.length && C(r.extends[0]) || [], n = b(e, t);
840
- }
841
- this.metadataSet.add(n), this.metadataMap.set(`${n.namespace}_${n.name}`, n);
842
- }
843
- getMetadata(e, t) {
844
- return this.metadataMap.get(R(e, t));
845
- }
846
- /**
847
- * 执行生成器
848
- */
849
- exec() {
850
- this.project = new X(), Object.values(j).forEach(
851
- (e) => e.forEach((t) => {
852
- const n = b(t);
853
- this.metadataMap.set(R(t.name, t.namespace), n);
854
- })
855
- ), te(this), ne(this);
856
- }
857
- getSourceFiles() {
858
- return this.project.getSourceFiles();
859
- }
860
- }
861
- export {
862
- oe as R
863
- };