@aiao/rxdb-client-generator 0.0.2 → 0.0.3

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