@aiao/rxdb-client-generator 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/RxDBClientGenerator.d.ts +35 -0
- package/RxDBClientGenerator.utils.d.ts +19 -0
- package/generator_all_entity_definition.d.ts +6 -0
- package/generator_entity_definition.d.ts +10 -0
- package/generator_entity_js_file.d.ts +6 -0
- package/generator_entity_properties.d.ts +11 -0
- package/generator_entity_relations.d.ts +22 -0
- package/generator_entity_rules.d.ts +13 -0
- package/generator_repository_methods.d.ts +14 -0
- package/generator_tree_repository_methods.d.ts +14 -0
- package/index.d.ts +1 -0
- package/index.js +652 -0
- package/package.json +18 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# rxdb-client-generator
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { EntityMetadataOptions, EntityType } from '@aiao/rxdb';
|
|
2
|
+
import { Project } from 'ts-morph-browser';
|
|
3
|
+
export interface RxDBClientGeneratorOptions {
|
|
4
|
+
/**
|
|
5
|
+
* 关系查询深度
|
|
6
|
+
* 最小为 1
|
|
7
|
+
* @default 2
|
|
8
|
+
* @example 1 允许查询自己的基本属性 + 自己的关系的基本属性
|
|
9
|
+
* @example 2 允许查询自己的基本属性 + 自己的关系的基本属性 + 自己关系的关系的基本属性
|
|
10
|
+
*/
|
|
11
|
+
relationQueryDeep?: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* RxDB Client 生成器
|
|
15
|
+
*/
|
|
16
|
+
export declare class RxDBClientGenerator {
|
|
17
|
+
project: Project;
|
|
18
|
+
metadataSet: Set<import('type-fest/source/readonly-deep').ReadonlyObjectDeep<import('@aiao/rxdb').EntityMetadataType>>;
|
|
19
|
+
metadataMap: Map<string, import('type-fest/source/readonly-deep').ReadonlyObjectDeep<import('@aiao/rxdb').EntityMetadataType>>;
|
|
20
|
+
config: Required<RxDBClientGeneratorOptions>;
|
|
21
|
+
constructor(options?: RxDBClientGeneratorOptions);
|
|
22
|
+
/**
|
|
23
|
+
* 添加实体配置
|
|
24
|
+
* @param value 实体
|
|
25
|
+
*/
|
|
26
|
+
addEntity(value: EntityMetadataOptions | EntityType): void;
|
|
27
|
+
/**
|
|
28
|
+
* 执行生成器
|
|
29
|
+
*/
|
|
30
|
+
execSync(): void;
|
|
31
|
+
/**
|
|
32
|
+
* 执行生成器
|
|
33
|
+
*/
|
|
34
|
+
exec(): void;
|
|
35
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { EntityMetadata, EntityPropertyMetadata } from '@aiao/rxdb';
|
|
2
|
+
import { OptionalKind, PropertyDeclarationStructure } from 'ts-morph-browser';
|
|
3
|
+
/**
|
|
4
|
+
* 获取属性配置
|
|
5
|
+
*/
|
|
6
|
+
export declare const getPropertyTypeConfig: (property: EntityPropertyMetadata) => Partial<OptionalKind<PropertyDeclarationStructure>> & {
|
|
7
|
+
type: string;
|
|
8
|
+
name: string;
|
|
9
|
+
};
|
|
10
|
+
export declare const getEntityPropertyTsType: (property: EntityPropertyMetadata) => string;
|
|
11
|
+
export declare const getGeneratorEntityConfig: (metadata: EntityMetadata) => {
|
|
12
|
+
entityPath: string;
|
|
13
|
+
fileName: string;
|
|
14
|
+
};
|
|
15
|
+
export declare const getGeneratorEntityDefinitionConfig: (metadata: EntityMetadata) => {
|
|
16
|
+
entityPath: string;
|
|
17
|
+
fileName: string;
|
|
18
|
+
};
|
|
19
|
+
export declare function transitionMetadata(metadata: EntityMetadata): string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EntityMetadata } from '@aiao/rxdb';
|
|
2
|
+
import { SourceFile } from 'ts-morph-browser';
|
|
3
|
+
import { RxDBClientGenerator } from './RxDBClientGenerator';
|
|
4
|
+
/**
|
|
5
|
+
* 生成一个实体的类型文件
|
|
6
|
+
*/
|
|
7
|
+
declare const _default: (generator: RxDBClientGenerator, metadata: EntityMetadata, file: SourceFile) => {
|
|
8
|
+
rxdbNamedImports: Set<string>;
|
|
9
|
+
};
|
|
10
|
+
export default _default;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { EntityMetadata } from '@aiao/rxdb';
|
|
2
|
+
import { OptionalKind, PropertyDeclarationStructure, SourceFile } from 'ts-morph-browser';
|
|
3
|
+
/**
|
|
4
|
+
* 生成实体的属性
|
|
5
|
+
*/
|
|
6
|
+
declare const _default: ({ classProperties, metadata, file }: {
|
|
7
|
+
metadata: EntityMetadata;
|
|
8
|
+
file: SourceFile;
|
|
9
|
+
classProperties: OptionalKind<PropertyDeclarationStructure>[];
|
|
10
|
+
}) => void;
|
|
11
|
+
export default _default;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { EntityMetadata } from '@aiao/rxdb';
|
|
2
|
+
import { OptionalKind, PropertyDeclarationStructure } from 'ts-morph-browser';
|
|
3
|
+
/**
|
|
4
|
+
* 生成实体的关系属性
|
|
5
|
+
*
|
|
6
|
+
* 根据实体元数据中的关系定义,生成相应的TypeScript属性声明:
|
|
7
|
+
* - 对于一对一和多对一关系:生成关系可观察对象属性和ID属性
|
|
8
|
+
* - 对于一对多和多对多关系:生成关系可观察集合属性
|
|
9
|
+
*
|
|
10
|
+
* 生成的属性会添加到类属性数组中,并将所需的导入添加到导入集合中
|
|
11
|
+
*
|
|
12
|
+
* @param options - 生成选项
|
|
13
|
+
* @param options.metadata - 实体元数据,包含关系定义
|
|
14
|
+
* @param options.classProperties - 类属性数组,生成的属性会添加到这里
|
|
15
|
+
* @param options.rxdbNamedImports - rxdb导入集合,用于收集所需的导入
|
|
16
|
+
*/
|
|
17
|
+
declare const _default: ({ classProperties, metadata, rxdbNamedImports }: {
|
|
18
|
+
metadata: EntityMetadata;
|
|
19
|
+
classProperties: OptionalKind<PropertyDeclarationStructure>[];
|
|
20
|
+
rxdbNamedImports: Set<string>;
|
|
21
|
+
}) => void;
|
|
22
|
+
export default _default;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EntityMetadata } from '@aiao/rxdb';
|
|
2
|
+
import { RxDBClientGenerator } from './RxDBClientGenerator';
|
|
3
|
+
interface RuleTypeData {
|
|
4
|
+
rule: string;
|
|
5
|
+
entity: string;
|
|
6
|
+
key: string;
|
|
7
|
+
valueType?: string;
|
|
8
|
+
}
|
|
9
|
+
declare const generator_entity_rules: (generator: RxDBClientGenerator, metadata: EntityMetadata, firstMetadata?: EntityMetadata, ignoreKeys?: string[], result?: RuleTypeData[], patch?: {
|
|
10
|
+
key: string;
|
|
11
|
+
entity: string;
|
|
12
|
+
}[]) => RuleTypeData[];
|
|
13
|
+
export default generator_entity_rules;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { EntityMetadata } from '@aiao/rxdb';
|
|
2
|
+
import { MethodDeclarationStructure, OptionalKind, SourceFile } from 'ts-morph-browser';
|
|
3
|
+
import { RxDBClientGenerator } from './RxDBClientGenerator';
|
|
4
|
+
/**
|
|
5
|
+
* 生成 repository 方法
|
|
6
|
+
*/
|
|
7
|
+
declare const _default: ({ classMethods, rxdbNamedImports, generator, metadata, file }: {
|
|
8
|
+
classMethods: OptionalKind<MethodDeclarationStructure>[];
|
|
9
|
+
rxdbNamedImports: Set<string>;
|
|
10
|
+
generator: RxDBClientGenerator;
|
|
11
|
+
metadata: EntityMetadata;
|
|
12
|
+
file: SourceFile;
|
|
13
|
+
}) => void;
|
|
14
|
+
export default _default;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { EntityMetadata } from '@aiao/rxdb';
|
|
2
|
+
import { MethodDeclarationStructure, OptionalKind, SourceFile } from 'ts-morph-browser';
|
|
3
|
+
import { RxDBClientGenerator } from './RxDBClientGenerator';
|
|
4
|
+
/**
|
|
5
|
+
* 生成 tree repository 方法
|
|
6
|
+
*/
|
|
7
|
+
declare const _default: ({ metadata, classMethods, rxdbNamedImports, generator, file }: {
|
|
8
|
+
generator: RxDBClientGenerator;
|
|
9
|
+
metadata: EntityMetadata;
|
|
10
|
+
classMethods: OptionalKind<MethodDeclarationStructure>[];
|
|
11
|
+
rxdbNamedImports: Set<string>;
|
|
12
|
+
file: SourceFile;
|
|
13
|
+
}) => void;
|
|
14
|
+
export default _default;
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './RxDBClientGenerator';
|
package/index.js
ADDED
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
import { PropertyType as l, RelationKind as y, isEntityInternalName as R, getEntityMetadata as S, transitionMetadata as g } from "@aiao/rxdb";
|
|
2
|
+
import { isNil as _, omit as D, isObject as N, isArray as k, isFunction as A } from "@aiao/utils";
|
|
3
|
+
import { VariableDeclarationKind as j, ScriptTarget as M, Project as x, QuoteKind as F, IndentationText as v } from "ts-morph-browser";
|
|
4
|
+
const K = (n) => {
|
|
5
|
+
const e = E(n), a = !!n.nullable;
|
|
6
|
+
let r = !0;
|
|
7
|
+
!a && Object.hasOwn(n, "default") && (r = !1);
|
|
8
|
+
const i = {
|
|
9
|
+
type: e,
|
|
10
|
+
name: n.name,
|
|
11
|
+
hasQuestionToken: a,
|
|
12
|
+
hasExclamationToken: r,
|
|
13
|
+
isReadonly: n.readonly
|
|
14
|
+
};
|
|
15
|
+
return _(n.default) || (i.initializer = P(n)), i;
|
|
16
|
+
}, E = (n) => {
|
|
17
|
+
let e = "any";
|
|
18
|
+
switch (n.type) {
|
|
19
|
+
case l.uuid:
|
|
20
|
+
e = "UUID";
|
|
21
|
+
break;
|
|
22
|
+
case l.string:
|
|
23
|
+
e = "string";
|
|
24
|
+
break;
|
|
25
|
+
case l.number:
|
|
26
|
+
e = "number";
|
|
27
|
+
break;
|
|
28
|
+
case l.boolean:
|
|
29
|
+
e = "boolean";
|
|
30
|
+
break;
|
|
31
|
+
case l.date:
|
|
32
|
+
e = "Date";
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
return n.nullable && (e += " | null"), e;
|
|
36
|
+
}, P = (n) => {
|
|
37
|
+
switch (n.type) {
|
|
38
|
+
case l.string:
|
|
39
|
+
return `'${String(n.default)}'`;
|
|
40
|
+
case l.date:
|
|
41
|
+
return `new Date(${n.default.getTime()})`;
|
|
42
|
+
default:
|
|
43
|
+
return `${String(n.default)}`;
|
|
44
|
+
}
|
|
45
|
+
}, h = (n, e = /* @__PURE__ */ new Set()) => {
|
|
46
|
+
if (N(n))
|
|
47
|
+
for (const a in n)
|
|
48
|
+
e.add(a), Object.prototype.hasOwnProperty.call(n, a) && h(Reflect.get(n, a), e);
|
|
49
|
+
else if (k(n))
|
|
50
|
+
for (const a of n)
|
|
51
|
+
h(a, e);
|
|
52
|
+
return e;
|
|
53
|
+
}, b = /* @__PURE__ */ new Map();
|
|
54
|
+
Object.keys(l).forEach((n) => {
|
|
55
|
+
const e = l[n];
|
|
56
|
+
b.set(e, n);
|
|
57
|
+
});
|
|
58
|
+
const w = /* @__PURE__ */ new Map();
|
|
59
|
+
Object.keys(y).forEach((n) => {
|
|
60
|
+
const e = y[n];
|
|
61
|
+
w.set(e, n);
|
|
62
|
+
});
|
|
63
|
+
function G(n) {
|
|
64
|
+
const e = D(n, ["propertyMap", "relationMap", "indexMap"]), a = h(e);
|
|
65
|
+
let r = JSON.stringify(e, null, 2);
|
|
66
|
+
return new Set(n.properties.map((t) => t.type)).forEach((t) => {
|
|
67
|
+
const p = b.get(t);
|
|
68
|
+
r = r.replaceAll(`"type": "${t}"`, `"type": PropertyType.${p}`);
|
|
69
|
+
}), n.relations && new Set(n.relations.map((p) => p.kind)).forEach((p) => {
|
|
70
|
+
const o = w.get(p);
|
|
71
|
+
r = r.replaceAll(`"kind": "${p}"`, `"kind": RelationKind.${o}`);
|
|
72
|
+
}), a.forEach((t) => {
|
|
73
|
+
t.includes("-") || t.includes(" ") || (r = r.replaceAll(`"${t}":`, `${t}:`));
|
|
74
|
+
}), r;
|
|
75
|
+
}
|
|
76
|
+
const I = ({
|
|
77
|
+
classProperties: n,
|
|
78
|
+
metadata: e,
|
|
79
|
+
file: a
|
|
80
|
+
}) => {
|
|
81
|
+
const { name: r } = e, i = a.addInterface({
|
|
82
|
+
name: `${r}InitData`,
|
|
83
|
+
isExported: !0,
|
|
84
|
+
docs: ["初始化数据"]
|
|
85
|
+
});
|
|
86
|
+
e.propertyMap.values().filter((t) => R(t.name) === !1).forEach((t) => {
|
|
87
|
+
const p = K(t), { initializer: o, ...s } = p;
|
|
88
|
+
let c = t.displayName || t.name;
|
|
89
|
+
o && (c += `
|
|
90
|
+
@default ${o}`), i.addProperty({
|
|
91
|
+
name: p.name,
|
|
92
|
+
type: p.type,
|
|
93
|
+
hasQuestionToken: !0,
|
|
94
|
+
docs: [c]
|
|
95
|
+
}), n.push({
|
|
96
|
+
...s,
|
|
97
|
+
hasExclamationToken: !1,
|
|
98
|
+
docs: [c]
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
}, Q = ({
|
|
102
|
+
classProperties: n,
|
|
103
|
+
metadata: e,
|
|
104
|
+
rxdbNamedImports: a
|
|
105
|
+
}) => {
|
|
106
|
+
e.relationMap.values().forEach((r) => {
|
|
107
|
+
switch (r.kind) {
|
|
108
|
+
// 处理一对一和多对一关系
|
|
109
|
+
// 这两种关系需要生成:
|
|
110
|
+
// 1. 关系可观察对象属性(name$)- 用于访问关联实体
|
|
111
|
+
// 2. 外键ID属性(nameId)- 存储关联实体的ID
|
|
112
|
+
case y.ONE_TO_ONE:
|
|
113
|
+
case y.MANY_TO_ONE:
|
|
114
|
+
{
|
|
115
|
+
const i = r.nullable ? `RelationEntityObservable<${r.mappedEntity} | null>` : `RelationEntityObservable<${r.mappedEntity}>`, t = r.displayName || r.name;
|
|
116
|
+
n.push({
|
|
117
|
+
name: r.name + "$",
|
|
118
|
+
// 关系属性名使用$后缀,表示可观察对象
|
|
119
|
+
type: i,
|
|
120
|
+
isReadonly: !0,
|
|
121
|
+
// 关系属性是只读的,通过set/remove方法修改
|
|
122
|
+
docs: [t]
|
|
123
|
+
}), n.push({
|
|
124
|
+
name: r.name + "Id",
|
|
125
|
+
// 外键ID属性
|
|
126
|
+
type: "UUID",
|
|
127
|
+
isReadonly: !0,
|
|
128
|
+
docs: [t + " id"]
|
|
129
|
+
}), a.add("RelationEntityObservable");
|
|
130
|
+
}
|
|
131
|
+
break;
|
|
132
|
+
// 处理一对多和多对多关系
|
|
133
|
+
// 这两种关系只需要生成关系可观察集合属性(name$)
|
|
134
|
+
// 不需要外键ID属性,因为外键存储在关联实体或中间表中
|
|
135
|
+
case y.ONE_TO_MANY:
|
|
136
|
+
case y.MANY_TO_MANY:
|
|
137
|
+
{
|
|
138
|
+
const i = r.displayName || r.name, t = `RelationEntitiesObservable<${r.mappedEntity}>`;
|
|
139
|
+
n.push({
|
|
140
|
+
name: r.name + "$",
|
|
141
|
+
// 关系集合属性名使用$后缀
|
|
142
|
+
isReadonly: !0,
|
|
143
|
+
// 关系属性是只读的,通过add/remove方法修改
|
|
144
|
+
type: t,
|
|
145
|
+
docs: [i]
|
|
146
|
+
}), a.add("RelationEntitiesObservable");
|
|
147
|
+
}
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}, C = (n) => Array.from(n.propertyMap.keys()).map((e) => {
|
|
152
|
+
const a = n.propertyMap.get(e);
|
|
153
|
+
let r = "";
|
|
154
|
+
switch (a.type) {
|
|
155
|
+
case l.uuid:
|
|
156
|
+
r = "UUID";
|
|
157
|
+
break;
|
|
158
|
+
case l.string:
|
|
159
|
+
r = "String";
|
|
160
|
+
break;
|
|
161
|
+
case l.number:
|
|
162
|
+
case l.integer:
|
|
163
|
+
r = "Number";
|
|
164
|
+
break;
|
|
165
|
+
case l.boolean:
|
|
166
|
+
r = "Boolean";
|
|
167
|
+
break;
|
|
168
|
+
case l.date:
|
|
169
|
+
r = "Date";
|
|
170
|
+
break;
|
|
171
|
+
default:
|
|
172
|
+
throw new Error("Unknown property type: " + a.type);
|
|
173
|
+
}
|
|
174
|
+
if (!r) throw new Error("ruleName is empty");
|
|
175
|
+
r += "Rules";
|
|
176
|
+
const i = E(a);
|
|
177
|
+
return {
|
|
178
|
+
rule: r,
|
|
179
|
+
entity: n.name,
|
|
180
|
+
key: e,
|
|
181
|
+
valueType: i
|
|
182
|
+
};
|
|
183
|
+
}), $ = (n, e, a, r = [], i = [], t = []) => (a = a ?? e, C(e).forEach(({ rule: p, entity: o, key: s, valueType: c }) => {
|
|
184
|
+
t.length > 0 ? s = t.map((u) => u.key).join(".") + "." + s : c = void 0, i.push({
|
|
185
|
+
rule: p,
|
|
186
|
+
entity: o,
|
|
187
|
+
key: s,
|
|
188
|
+
valueType: c
|
|
189
|
+
});
|
|
190
|
+
}), e.foreignKeyNames.forEach((p) => {
|
|
191
|
+
i.push({
|
|
192
|
+
rule: "UUIDRules",
|
|
193
|
+
entity: e.name,
|
|
194
|
+
key: p
|
|
195
|
+
});
|
|
196
|
+
}), e.relationMap.keys().filter((p) => !r.includes(p)).forEach((p) => {
|
|
197
|
+
const o = e.relationMap.get(p);
|
|
198
|
+
if (!o)
|
|
199
|
+
throw new Error("relation is empty");
|
|
200
|
+
const s = [];
|
|
201
|
+
switch (o.kind) {
|
|
202
|
+
case y.ONE_TO_ONE:
|
|
203
|
+
break;
|
|
204
|
+
case y.ONE_TO_MANY:
|
|
205
|
+
s.push(o.mappedProperty);
|
|
206
|
+
break;
|
|
207
|
+
case y.MANY_TO_ONE:
|
|
208
|
+
break;
|
|
209
|
+
case y.MANY_TO_MANY:
|
|
210
|
+
s.push(o.mappedProperty);
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
const c = n.metadataMap.get(`${o.mappedNamespace}_${o.mappedEntity}`);
|
|
214
|
+
if (!c)
|
|
215
|
+
throw new Error(`generator_entity_rules: metadata "${o.mappedEntity}" not found`);
|
|
216
|
+
if (t.find((m) => m.entity === e.name) || t.length && c === a) return;
|
|
217
|
+
const d = [...t, { key: p, entity: e.name }];
|
|
218
|
+
t.length < n.config.relationQueryDeep && $(n, c, a, s, i, d);
|
|
219
|
+
}), i), U = ({
|
|
220
|
+
classMethods: n,
|
|
221
|
+
rxdbNamedImports: e,
|
|
222
|
+
generator: a,
|
|
223
|
+
metadata: r,
|
|
224
|
+
file: i
|
|
225
|
+
}) => {
|
|
226
|
+
const { name: t } = r, o = $(a, r).map(({ rule: u, entity: d, key: m, valueType: f }) => (e.add(u), f ? (f === "UUID" && e.add("UUID"), `${u}<${d}, '${m}', ${f}>`) : `${u}<${d}, '${m}'>`));
|
|
227
|
+
i.addTypeAlias({
|
|
228
|
+
name: `${t}Rule`,
|
|
229
|
+
type: `
|
|
230
|
+
| ` + Array.from(new Set(o)).join(`
|
|
231
|
+
| `),
|
|
232
|
+
hasDeclareKeyword: !0,
|
|
233
|
+
docs: ["rule"]
|
|
234
|
+
}), i.addTypeAlias({
|
|
235
|
+
name: `${t}RuleGroup`,
|
|
236
|
+
type: `RuleGroup<${t}, keyof ${t}, ${t}Rule>`,
|
|
237
|
+
hasDeclareKeyword: !0,
|
|
238
|
+
docs: ["RuleGroup"],
|
|
239
|
+
isExported: !0
|
|
240
|
+
});
|
|
241
|
+
const s = `${t}OrderByField`, c = Array.from(r.propertyMap.keys());
|
|
242
|
+
i.addTypeAlias({
|
|
243
|
+
name: s,
|
|
244
|
+
type: c.map((u) => `"${u}"`).join(" | "),
|
|
245
|
+
hasDeclareKeyword: !0,
|
|
246
|
+
docs: ["OrderByField"]
|
|
247
|
+
}), n.push({
|
|
248
|
+
name: "get",
|
|
249
|
+
returnType: `Observable<${t}>`,
|
|
250
|
+
docs: [["查询", "@param id 实体的 id", "@param options 查询选项"].join(`
|
|
251
|
+
`)],
|
|
252
|
+
parameters: [
|
|
253
|
+
{
|
|
254
|
+
name: "id",
|
|
255
|
+
type: "string"
|
|
256
|
+
},
|
|
257
|
+
{
|
|
258
|
+
name: "options",
|
|
259
|
+
hasQuestionToken: !0,
|
|
260
|
+
type: "GetOptions"
|
|
261
|
+
}
|
|
262
|
+
],
|
|
263
|
+
isStatic: !0
|
|
264
|
+
}), e.add("GetOptions"), n.push({
|
|
265
|
+
name: "findOneOrFail",
|
|
266
|
+
returnType: `Observable<${t}>`,
|
|
267
|
+
docs: [["查询", "@param where 查询条件", "@param options 查询选项"].join(`
|
|
268
|
+
`)],
|
|
269
|
+
parameters: [
|
|
270
|
+
{
|
|
271
|
+
name: "where",
|
|
272
|
+
type: `${t}RuleGroup`
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
name: "options",
|
|
276
|
+
hasQuestionToken: !0,
|
|
277
|
+
type: `FindOneOrFailOptions<${s}>`
|
|
278
|
+
}
|
|
279
|
+
],
|
|
280
|
+
isStatic: !0
|
|
281
|
+
}), e.add("FindOneOrFailOptions"), n.push({
|
|
282
|
+
name: "find",
|
|
283
|
+
returnType: `Observable<${t}[]>`,
|
|
284
|
+
docs: [["查询", "@param where 查询条件", "@param options 查询选项"].join(`
|
|
285
|
+
`)],
|
|
286
|
+
parameters: [
|
|
287
|
+
{
|
|
288
|
+
name: "where",
|
|
289
|
+
type: `${t}RuleGroup`
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
name: "options",
|
|
293
|
+
hasQuestionToken: !0,
|
|
294
|
+
type: `FindOptions<${s}>`
|
|
295
|
+
}
|
|
296
|
+
],
|
|
297
|
+
isStatic: !0
|
|
298
|
+
}), e.add("FindOptions"), n.push({
|
|
299
|
+
name: "findOne",
|
|
300
|
+
returnType: `Observable<${t} | undefined>`,
|
|
301
|
+
docs: [["查询", "@param where 查询条件", "@param options 查询选项"].join(`
|
|
302
|
+
`)],
|
|
303
|
+
parameters: [
|
|
304
|
+
{
|
|
305
|
+
name: "where",
|
|
306
|
+
type: `${t}RuleGroup`
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
name: "options",
|
|
310
|
+
hasQuestionToken: !0,
|
|
311
|
+
type: `FindOneOptions<${s}>`
|
|
312
|
+
}
|
|
313
|
+
],
|
|
314
|
+
isStatic: !0
|
|
315
|
+
}), e.add("FindOneOptions"), n.push({
|
|
316
|
+
name: "findAll",
|
|
317
|
+
returnType: `Observable<${t}[]>`,
|
|
318
|
+
docs: [["查询", "@param where 查询条件", "@param options 查询选项"].join(`
|
|
319
|
+
`)],
|
|
320
|
+
parameters: [
|
|
321
|
+
{
|
|
322
|
+
name: "where",
|
|
323
|
+
type: `${t}RuleGroup`
|
|
324
|
+
},
|
|
325
|
+
{
|
|
326
|
+
name: "options",
|
|
327
|
+
hasQuestionToken: !0,
|
|
328
|
+
type: `FindAllOptions<${s}>`
|
|
329
|
+
}
|
|
330
|
+
],
|
|
331
|
+
isStatic: !0
|
|
332
|
+
}), e.add("FindAllOptions"), n.push({
|
|
333
|
+
name: "findByCursor",
|
|
334
|
+
returnType: `Observable<${t}[]>`,
|
|
335
|
+
docs: [["查询", "@param where 查询条件", "@param options 查询选项"].join(`
|
|
336
|
+
`)],
|
|
337
|
+
parameters: [
|
|
338
|
+
{
|
|
339
|
+
name: "where",
|
|
340
|
+
type: `${t}RuleGroup`
|
|
341
|
+
},
|
|
342
|
+
{
|
|
343
|
+
name: "options",
|
|
344
|
+
hasQuestionToken: !0,
|
|
345
|
+
type: `FindByCursorOptions<${s}>`
|
|
346
|
+
}
|
|
347
|
+
],
|
|
348
|
+
isStatic: !0
|
|
349
|
+
}), e.add("FindByCursorOptions"), n.push({
|
|
350
|
+
name: "count",
|
|
351
|
+
returnType: "Observable<number>",
|
|
352
|
+
docs: [["查询", "@param where 查询条件", "@param options 查询选项"].join(`
|
|
353
|
+
`)],
|
|
354
|
+
parameters: [
|
|
355
|
+
{
|
|
356
|
+
name: "where",
|
|
357
|
+
type: `${t}RuleGroup`
|
|
358
|
+
},
|
|
359
|
+
{
|
|
360
|
+
name: "options",
|
|
361
|
+
hasQuestionToken: !0,
|
|
362
|
+
type: "CountOptions"
|
|
363
|
+
}
|
|
364
|
+
],
|
|
365
|
+
isStatic: !0
|
|
366
|
+
}), e.add("CountOptions"), n.push({
|
|
367
|
+
name: "save",
|
|
368
|
+
returnType: `Promise<${t}>`,
|
|
369
|
+
docs: [["保存"].join(`
|
|
370
|
+
`)]
|
|
371
|
+
}), n.push({
|
|
372
|
+
name: "remove",
|
|
373
|
+
returnType: `Promise<${t}>`,
|
|
374
|
+
docs: [["删除"].join(`
|
|
375
|
+
`)]
|
|
376
|
+
});
|
|
377
|
+
}, B = ({
|
|
378
|
+
metadata: n,
|
|
379
|
+
classMethods: e,
|
|
380
|
+
rxdbNamedImports: a,
|
|
381
|
+
generator: r,
|
|
382
|
+
file: i
|
|
383
|
+
}) => {
|
|
384
|
+
const { name: t } = n, o = $(r, n).filter(
|
|
385
|
+
(s) => s.key.startsWith("children.") && s.key !== "children.id"
|
|
386
|
+
).map(({ rule: s, entity: c, key: u, valueType: d }) => (a.add(s), d ? (d === "UUID" && a.add("UUID"), `${s}<${c}, '${u}', ${d}>`) : `${s}<${c}, '${u}'>`));
|
|
387
|
+
i.addTypeAlias({
|
|
388
|
+
name: `${t}TreeRule`,
|
|
389
|
+
type: `
|
|
390
|
+
| ` + Array.from(new Set(o)).join(`
|
|
391
|
+
| `),
|
|
392
|
+
hasDeclareKeyword: !0,
|
|
393
|
+
docs: ["TreeRule"]
|
|
394
|
+
}), i.addTypeAlias({
|
|
395
|
+
name: `${t}TreeRuleGroup`,
|
|
396
|
+
type: `RuleGroup<${t}, keyof ${t}, ${t}TreeRule>`,
|
|
397
|
+
hasDeclareKeyword: !0,
|
|
398
|
+
docs: ["TreeRuleGroup"],
|
|
399
|
+
isExported: !0
|
|
400
|
+
}), e.push({
|
|
401
|
+
name: "findDescendants",
|
|
402
|
+
returnType: `Observable<${t}[]>`,
|
|
403
|
+
docs: [["查询子孙实体", "@param options 查询选项"].join(`
|
|
404
|
+
`)],
|
|
405
|
+
parameters: [
|
|
406
|
+
{
|
|
407
|
+
name: "entity",
|
|
408
|
+
type: `${t}`
|
|
409
|
+
},
|
|
410
|
+
{
|
|
411
|
+
name: "where",
|
|
412
|
+
type: `${t}TreeRuleGroup`
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
name: "options",
|
|
416
|
+
type: "FindTreeOptions",
|
|
417
|
+
hasQuestionToken: !0
|
|
418
|
+
}
|
|
419
|
+
],
|
|
420
|
+
isStatic: !0
|
|
421
|
+
}), e.push({
|
|
422
|
+
name: "countDescendants",
|
|
423
|
+
returnType: "Observable<number>",
|
|
424
|
+
docs: [["查询子孙实体数量", "@param options 查询选项"].join(`
|
|
425
|
+
`)],
|
|
426
|
+
parameters: [
|
|
427
|
+
{
|
|
428
|
+
name: "entity",
|
|
429
|
+
type: `${t}`
|
|
430
|
+
},
|
|
431
|
+
{
|
|
432
|
+
name: "where",
|
|
433
|
+
type: `${t}TreeRuleGroup`
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
name: "options",
|
|
437
|
+
type: "FindTreeOptions",
|
|
438
|
+
hasQuestionToken: !0
|
|
439
|
+
}
|
|
440
|
+
],
|
|
441
|
+
isStatic: !0
|
|
442
|
+
}), e.push({
|
|
443
|
+
name: "findAncestors",
|
|
444
|
+
returnType: `Observable<${t}[]>`,
|
|
445
|
+
docs: [["查询祖先实体", "@param options 查询选项"].join(`
|
|
446
|
+
`)],
|
|
447
|
+
parameters: [
|
|
448
|
+
{
|
|
449
|
+
name: "entity",
|
|
450
|
+
type: `${t}`
|
|
451
|
+
},
|
|
452
|
+
{
|
|
453
|
+
name: "where",
|
|
454
|
+
type: `${t}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 查询选项"].join(`
|
|
467
|
+
`)],
|
|
468
|
+
parameters: [
|
|
469
|
+
{
|
|
470
|
+
name: "entity",
|
|
471
|
+
type: `${t}`
|
|
472
|
+
},
|
|
473
|
+
{
|
|
474
|
+
name: "where",
|
|
475
|
+
type: `${t}TreeRuleGroup`
|
|
476
|
+
},
|
|
477
|
+
{
|
|
478
|
+
name: "options",
|
|
479
|
+
type: "FindTreeOptions",
|
|
480
|
+
hasQuestionToken: !0
|
|
481
|
+
}
|
|
482
|
+
],
|
|
483
|
+
isStatic: !0
|
|
484
|
+
}), a.add("FindTreeOptions");
|
|
485
|
+
}, Y = (n, e, a) => {
|
|
486
|
+
const r = /* @__PURE__ */ new Set(), { name: i } = e, t = e.extends[0];
|
|
487
|
+
r.add(t);
|
|
488
|
+
const p = a.addClass({
|
|
489
|
+
name: i,
|
|
490
|
+
isExported: !0,
|
|
491
|
+
extends: t,
|
|
492
|
+
decorators: [],
|
|
493
|
+
hasDeclareKeyword: !0
|
|
494
|
+
});
|
|
495
|
+
p.addJsDoc(`
|
|
496
|
+
${e.displayName}`);
|
|
497
|
+
const o = [], s = [];
|
|
498
|
+
return I({
|
|
499
|
+
classProperties: o,
|
|
500
|
+
file: a,
|
|
501
|
+
metadata: e
|
|
502
|
+
}), Q({
|
|
503
|
+
classProperties: o,
|
|
504
|
+
metadata: e,
|
|
505
|
+
rxdbNamedImports: r
|
|
506
|
+
}), U({
|
|
507
|
+
classMethods: s,
|
|
508
|
+
file: a,
|
|
509
|
+
generator: n,
|
|
510
|
+
metadata: e,
|
|
511
|
+
rxdbNamedImports: r
|
|
512
|
+
}), e.repository === "TreeRepository" && B({
|
|
513
|
+
classMethods: s,
|
|
514
|
+
metadata: e,
|
|
515
|
+
file: a,
|
|
516
|
+
generator: n,
|
|
517
|
+
rxdbNamedImports: r
|
|
518
|
+
}), p.addProperties(
|
|
519
|
+
o.sort((c, u) => c.isReadonly && !u.isReadonly ? -1 : !c.isReadonly && u.isReadonly ? 1 : c.name.localeCompare(u.name))
|
|
520
|
+
), p.addConstructor({
|
|
521
|
+
parameters: [
|
|
522
|
+
{
|
|
523
|
+
name: "initData",
|
|
524
|
+
type: `${i}InitData`,
|
|
525
|
+
hasQuestionToken: !0
|
|
526
|
+
}
|
|
527
|
+
],
|
|
528
|
+
docs: [["初始化数据", "@param initData 初始化数据"].join(`
|
|
529
|
+
`)]
|
|
530
|
+
}), p.addMethods(
|
|
531
|
+
s.sort((c, u) => c.isStatic && !u.isStatic ? -1 : !c.isStatic && u.isStatic ? 1 : c.name.localeCompare(u.name))
|
|
532
|
+
), {
|
|
533
|
+
rxdbNamedImports: r
|
|
534
|
+
};
|
|
535
|
+
}, T = (n) => {
|
|
536
|
+
const { project: e, metadataSet: a } = n, r = e.createSourceFile("types.d.ts"), t = r.addModule({
|
|
537
|
+
name: '"@aiao/rxdb"',
|
|
538
|
+
hasDeclareKeyword: !0,
|
|
539
|
+
docs: ["rxdb"]
|
|
540
|
+
}).addInterface({
|
|
541
|
+
name: "RxDB",
|
|
542
|
+
docs: ["RxDB"]
|
|
543
|
+
}), p = /* @__PURE__ */ new Set(["EntityType", "RuleGroup"]);
|
|
544
|
+
a.forEach((o) => {
|
|
545
|
+
const { rxdbNamedImports: s } = Y(n, o, r);
|
|
546
|
+
s.forEach((d) => p.add(d));
|
|
547
|
+
const { name: c } = o, u = c;
|
|
548
|
+
o.namespace && o.namespace !== "public" ? t.addProperty({
|
|
549
|
+
name: o.namespace,
|
|
550
|
+
type: `{
|
|
551
|
+
${u}: typeof ${u};
|
|
552
|
+
}`,
|
|
553
|
+
docs: [o.displayName]
|
|
554
|
+
}) : t.addProperty({
|
|
555
|
+
name: u,
|
|
556
|
+
type: `typeof ${u}`,
|
|
557
|
+
docs: [o.displayName]
|
|
558
|
+
});
|
|
559
|
+
}), r.addImportDeclaration({
|
|
560
|
+
namedImports: Array.from(p).sort(),
|
|
561
|
+
isTypeOnly: !0,
|
|
562
|
+
moduleSpecifier: "@aiao/rxdb"
|
|
563
|
+
}), r.addImportDeclaration({
|
|
564
|
+
namedImports: ["Observable"],
|
|
565
|
+
isTypeOnly: !0,
|
|
566
|
+
moduleSpecifier: "rxjs"
|
|
567
|
+
}), r.addVariableStatement({
|
|
568
|
+
declarationKind: j.Const,
|
|
569
|
+
hasDeclareKeyword: !0,
|
|
570
|
+
isExported: !0,
|
|
571
|
+
declarations: [
|
|
572
|
+
{
|
|
573
|
+
name: "ENTITIES",
|
|
574
|
+
type: "EntityType[]"
|
|
575
|
+
}
|
|
576
|
+
]
|
|
577
|
+
});
|
|
578
|
+
}, O = (n) => {
|
|
579
|
+
const { project: e, metadataSet: a } = n;
|
|
580
|
+
let r = "";
|
|
581
|
+
const i = /* @__PURE__ */ new Set(["Entity", "__decorateClass"]), t = (o) => {
|
|
582
|
+
for (const s of a.values())
|
|
583
|
+
if (s.relations && s.namespace === o.mappedNamespace && s.name === o.mappedEntity)
|
|
584
|
+
return s.relations.find((c) => c.kind === y.MANY_TO_MANY && c.mappedProperty === o.name);
|
|
585
|
+
};
|
|
586
|
+
a.forEach((o) => {
|
|
587
|
+
o.relations.forEach((s) => {
|
|
588
|
+
if (s.kind === y.MANY_TO_MANY && !t(s))
|
|
589
|
+
throw new Error("mapped relation not found");
|
|
590
|
+
});
|
|
591
|
+
}), a.forEach((o) => {
|
|
592
|
+
const s = o.extends[0], { name: c } = o, u = c, d = G(o);
|
|
593
|
+
s && i.add(s), o.properties.length && i.add("PropertyType"), o.relations.length && i.add("RelationKind"), r += `
|
|
594
|
+
let ${u} = class ${s ? "extends " + s : ""} {};`, r += `
|
|
595
|
+
${u} = __decorateClass(
|
|
596
|
+
[
|
|
597
|
+
Entity(${d})
|
|
598
|
+
],
|
|
599
|
+
${u}
|
|
600
|
+
);`;
|
|
601
|
+
}), r = `import { ${Array.from(i).sort().join(", ")} } from '@aiao/rxdb';` + r;
|
|
602
|
+
const p = Array.from(a.values()).map((o) => o.name).sort().join(", ");
|
|
603
|
+
r += `
|
|
604
|
+
const ENTITIES = [ ${p} ];`, r += `
|
|
605
|
+
export { ENTITIES, ${p} };`, e.createSourceFile("index.js", r);
|
|
606
|
+
};
|
|
607
|
+
class q {
|
|
608
|
+
project;
|
|
609
|
+
metadataSet = /* @__PURE__ */ new Set();
|
|
610
|
+
metadataMap = /* @__PURE__ */ new Map();
|
|
611
|
+
config = {
|
|
612
|
+
relationQueryDeep: 2
|
|
613
|
+
};
|
|
614
|
+
constructor(e) {
|
|
615
|
+
Object.assign(this.config, e), this.config.relationQueryDeep < 1 && (this.config.relationQueryDeep = 1);
|
|
616
|
+
const a = {
|
|
617
|
+
target: M.ESNext
|
|
618
|
+
};
|
|
619
|
+
this.project = new x({
|
|
620
|
+
useInMemoryFileSystem: !0,
|
|
621
|
+
skipAddingFilesFromTsConfig: !0,
|
|
622
|
+
compilerOptions: a,
|
|
623
|
+
manipulationSettings: {
|
|
624
|
+
indentationText: v.TwoSpaces,
|
|
625
|
+
quoteKind: F.Single
|
|
626
|
+
}
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* 添加实体配置
|
|
631
|
+
* @param value 实体
|
|
632
|
+
*/
|
|
633
|
+
addEntity(e) {
|
|
634
|
+
let a = e;
|
|
635
|
+
A(e) ? a = S(e) : a = g(e), this.metadataSet.add(a), this.metadataMap.set(`${a.namespace}_${a.name}`, a);
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* 执行生成器
|
|
639
|
+
*/
|
|
640
|
+
execSync() {
|
|
641
|
+
T(this), O(this), this.project.saveSync();
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* 执行生成器
|
|
645
|
+
*/
|
|
646
|
+
exec() {
|
|
647
|
+
T(this), O(this), this.project.save();
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
export {
|
|
651
|
+
q as RxDBClientGenerator
|
|
652
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aiao/rxdb-client-generator",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./index.js",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@aiao/utils": "0.0.1",
|
|
8
|
+
"@aiao/rxdb": "0.0.1",
|
|
9
|
+
"ts-morph-browser": "^25.0.3"
|
|
10
|
+
},
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./index.js",
|
|
13
|
+
"types": "./index.d.ts"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
}
|
|
18
|
+
}
|