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