@aiao/rxdb-client-generator 0.0.3 → 0.0.4

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