@yandjin-mikro-orm/entity-generator 6.1.4-rc-sti-changes-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/EntityGenerator.d.ts +21 -0
- package/EntityGenerator.js +257 -0
- package/EntitySchemaSourceFile.d.ts +8 -0
- package/EntitySchemaSourceFile.js +181 -0
- package/LICENSE +21 -0
- package/README.md +383 -0
- package/SourceFile.d.ts +31 -0
- package/SourceFile.js +583 -0
- package/index.d.ts +5 -0
- package/index.js +21 -0
- package/index.mjs +4 -0
- package/package.json +70 -0
package/SourceFile.js
ADDED
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SourceFile = void 0;
|
|
4
|
+
const core_1 = require("@yandjin-mikro-orm/core");
|
|
5
|
+
/**
|
|
6
|
+
* @see https://github.com/tc39/proposal-regexp-unicode-property-escapes#other-examples
|
|
7
|
+
*/
|
|
8
|
+
const identifierRegex = /^(?:[$_\p{ID_Start}])(?:[$\u200C\u200D\p{ID_Continue}])*$/u;
|
|
9
|
+
class SourceFile {
|
|
10
|
+
meta;
|
|
11
|
+
namingStrategy;
|
|
12
|
+
platform;
|
|
13
|
+
options;
|
|
14
|
+
coreImports = new Set();
|
|
15
|
+
entityImports = new Set();
|
|
16
|
+
constructor(meta, namingStrategy, platform, options) {
|
|
17
|
+
this.meta = meta;
|
|
18
|
+
this.namingStrategy = namingStrategy;
|
|
19
|
+
this.platform = platform;
|
|
20
|
+
this.options = options;
|
|
21
|
+
}
|
|
22
|
+
generate() {
|
|
23
|
+
let ret = "";
|
|
24
|
+
if (this.meta.embeddable || this.meta.collection) {
|
|
25
|
+
if (this.meta.embeddable) {
|
|
26
|
+
this.coreImports.add("Embeddable");
|
|
27
|
+
ret += `@Embeddable(${this.getEmbeddableDeclOptions()})\n`;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
this.coreImports.add("Entity");
|
|
31
|
+
ret += `@Entity(${this.getEntityDeclOptions()})\n`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
this.meta.indexes.forEach((index) => {
|
|
35
|
+
this.coreImports.add("Index");
|
|
36
|
+
if (index.expression) {
|
|
37
|
+
ret += `@Index({ name: '${index.name}', expression: ${this.quote(index.expression)} })\n`;
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const properties = core_1.Utils.asArray(index.properties).map((prop) => this.quote("" + prop));
|
|
41
|
+
ret += `@Index({ name: '${index.name}', properties: [${properties.join(", ")}] })\n`;
|
|
42
|
+
});
|
|
43
|
+
this.meta.uniques.forEach((index) => {
|
|
44
|
+
this.coreImports.add("Unique");
|
|
45
|
+
if (index.expression) {
|
|
46
|
+
ret += `@Unique({ name: '${index.name}', expression: ${this.quote(index.expression)} })\n`;
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const properties = core_1.Utils.asArray(index.properties).map((prop) => `'${prop}'`);
|
|
50
|
+
ret += `@Unique({ name: '${index.name}', properties: [${properties.join(", ")}] })\n`;
|
|
51
|
+
});
|
|
52
|
+
let classHead = "";
|
|
53
|
+
if (this.meta.className === this.options.customBaseEntityName) {
|
|
54
|
+
this.coreImports.add("Config");
|
|
55
|
+
this.coreImports.add("DefineConfig");
|
|
56
|
+
const defineConfigTypeSettings = {};
|
|
57
|
+
defineConfigTypeSettings.forceObject =
|
|
58
|
+
this.platform.getConfig().get("serialization").forceObject ?? false;
|
|
59
|
+
classHead += `\n${" ".repeat(2)}[Config]?: DefineConfig<${this.serializeObject(defineConfigTypeSettings)}>;\n\n`;
|
|
60
|
+
}
|
|
61
|
+
const enumDefinitions = [];
|
|
62
|
+
const eagerProperties = [];
|
|
63
|
+
const primaryProps = [];
|
|
64
|
+
let classBody = "";
|
|
65
|
+
Object.values(this.meta.properties).forEach((prop) => {
|
|
66
|
+
const decorator = this.getPropertyDecorator(prop, 2);
|
|
67
|
+
const definition = this.getPropertyDefinition(prop, 2);
|
|
68
|
+
classBody += decorator;
|
|
69
|
+
classBody += definition;
|
|
70
|
+
classBody += "\n";
|
|
71
|
+
if (prop.enum) {
|
|
72
|
+
const enumClassName = this.namingStrategy.getClassName(this.meta.collection + "_" + prop.fieldNames[0], "_");
|
|
73
|
+
enumDefinitions.push(this.getEnumClassDefinition(enumClassName, prop.items, 2));
|
|
74
|
+
}
|
|
75
|
+
if (prop.eager) {
|
|
76
|
+
eagerProperties.push(prop);
|
|
77
|
+
}
|
|
78
|
+
if (prop.primary &&
|
|
79
|
+
(!["id", "_id", "uuid"].includes(prop.name) || this.meta.compositePK)) {
|
|
80
|
+
primaryProps.push(prop);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
if (primaryProps.length > 0) {
|
|
84
|
+
this.coreImports.add("PrimaryKeyProp");
|
|
85
|
+
const primaryPropNames = primaryProps.map((prop) => `'${prop.name}'`);
|
|
86
|
+
if (primaryProps.length > 1) {
|
|
87
|
+
classHead += `\n${" ".repeat(2)}[PrimaryKeyProp]?: [${primaryPropNames.join(", ")}];\n`;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
classHead += `\n${" ".repeat(2)}[PrimaryKeyProp]?: ${primaryPropNames[0]};\n`;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (eagerProperties.length > 0) {
|
|
94
|
+
this.coreImports.add("EagerProps");
|
|
95
|
+
const eagerPropertyNames = eagerProperties
|
|
96
|
+
.map((prop) => `'${prop.name}'`)
|
|
97
|
+
.sort();
|
|
98
|
+
classHead += `\n${" ".repeat(2)}[EagerProps]?: ${eagerPropertyNames.join(" | ")};\n`;
|
|
99
|
+
}
|
|
100
|
+
ret += this.getEntityClass(classBody ? `${classHead}\n${classBody}` : classHead);
|
|
101
|
+
ret = `${this.generateImports()}\n\n${ret}`;
|
|
102
|
+
if (enumDefinitions.length) {
|
|
103
|
+
ret += "\n" + enumDefinitions.join("\n");
|
|
104
|
+
}
|
|
105
|
+
return ret;
|
|
106
|
+
}
|
|
107
|
+
generateImports() {
|
|
108
|
+
const imports = [];
|
|
109
|
+
if (this.coreImports.size > 0) {
|
|
110
|
+
imports.push(`import { ${[...this.coreImports].sort().join(", ")} } from '@yandjin-mikro-orm/core';`);
|
|
111
|
+
}
|
|
112
|
+
const entityImportExtension = this.options.esmImport ? ".js" : "";
|
|
113
|
+
const entityImports = [...this.entityImports].filter((e) => e !== this.meta.className);
|
|
114
|
+
entityImports.sort().forEach((entity) => {
|
|
115
|
+
imports.push(`import { ${entity} } from './${this.options.fileName(entity)}${entityImportExtension}';`);
|
|
116
|
+
});
|
|
117
|
+
return imports.join("\n");
|
|
118
|
+
}
|
|
119
|
+
getEntityClass(classBody) {
|
|
120
|
+
let ret = `export `;
|
|
121
|
+
if (this.meta.abstract) {
|
|
122
|
+
ret += `abstract `;
|
|
123
|
+
}
|
|
124
|
+
ret += `class ${this.meta.className}`;
|
|
125
|
+
if (this.meta.extends) {
|
|
126
|
+
this.entityImports.add(this.meta.extends);
|
|
127
|
+
ret += ` extends ${this.meta.extends}`;
|
|
128
|
+
}
|
|
129
|
+
else if (this.options.useCoreBaseEntity) {
|
|
130
|
+
if (this.meta.className === "BaseEntity") {
|
|
131
|
+
this.coreImports.add("BaseEntity as MikroBaseEntity");
|
|
132
|
+
ret += ` extends MikroBaseEntity`;
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
this.coreImports.add("BaseEntity");
|
|
136
|
+
ret += ` extends BaseEntity`;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
ret += ` {\n${classBody}}\n`;
|
|
140
|
+
return ret;
|
|
141
|
+
}
|
|
142
|
+
getBaseName(extension = ".ts") {
|
|
143
|
+
return `${this.options.fileName(this.meta.className)}${extension}`;
|
|
144
|
+
}
|
|
145
|
+
quote(val) {
|
|
146
|
+
/* istanbul ignore next */
|
|
147
|
+
return val.startsWith(`'`) ? `\`${val}\`` : `'${val}'`;
|
|
148
|
+
}
|
|
149
|
+
getPropertyDefinition(prop, padLeft) {
|
|
150
|
+
const padding = " ".repeat(padLeft);
|
|
151
|
+
let hiddenType = "";
|
|
152
|
+
if (prop.hidden) {
|
|
153
|
+
this.coreImports.add("Hidden");
|
|
154
|
+
hiddenType += " & Hidden";
|
|
155
|
+
}
|
|
156
|
+
if ([core_1.ReferenceKind.ONE_TO_MANY, core_1.ReferenceKind.MANY_TO_MANY].includes(prop.kind)) {
|
|
157
|
+
this.coreImports.add("Collection");
|
|
158
|
+
this.entityImports.add(prop.type);
|
|
159
|
+
return `${padding}${prop.name}${hiddenType ? `: Collection<${prop.type}>${hiddenType}` : ""} = new Collection<${prop.type}>(this);\n`;
|
|
160
|
+
}
|
|
161
|
+
const propType = prop.mapToPk
|
|
162
|
+
? (() => {
|
|
163
|
+
const runtimeTypes = prop.columnTypes.map((t) => this.platform.getMappedType(t).runtimeType);
|
|
164
|
+
return runtimeTypes.length === 1
|
|
165
|
+
? runtimeTypes[0]
|
|
166
|
+
: this.serializeObject(runtimeTypes);
|
|
167
|
+
})()
|
|
168
|
+
: prop.type;
|
|
169
|
+
// string defaults are usually things like SQL functions, but can be also enums, for that `useDefault` should be true
|
|
170
|
+
const isEnumOrNonStringDefault = prop.enum || typeof prop.default !== "string";
|
|
171
|
+
const useDefault = prop.default != null && isEnumOrNonStringDefault;
|
|
172
|
+
const optional = prop.nullable ? "?" : useDefault ? "" : "!";
|
|
173
|
+
if (prop.ref) {
|
|
174
|
+
this.coreImports.add("Ref");
|
|
175
|
+
if (typeof prop.kind === "string" && prop.kind !== core_1.ReferenceKind.SCALAR) {
|
|
176
|
+
this.entityImports.add(propType);
|
|
177
|
+
}
|
|
178
|
+
return `${padding}${prop.name}${optional}: Ref<${propType}>${hiddenType};\n`;
|
|
179
|
+
}
|
|
180
|
+
let ret = `${prop.name}${optional}: ${propType}`;
|
|
181
|
+
if (prop.kind === core_1.ReferenceKind.EMBEDDED && prop.array) {
|
|
182
|
+
ret += "[]";
|
|
183
|
+
}
|
|
184
|
+
ret += hiddenType;
|
|
185
|
+
if (useDefault || (optional !== "?" && typeof prop.default === "string")) {
|
|
186
|
+
this.coreImports.add("Opt");
|
|
187
|
+
ret += " & Opt";
|
|
188
|
+
}
|
|
189
|
+
if (!useDefault) {
|
|
190
|
+
return `${padding}${ret};\n`;
|
|
191
|
+
}
|
|
192
|
+
if (prop.enum && typeof prop.default === "string") {
|
|
193
|
+
return `${padding}${ret} = ${propType}.${prop.default.toUpperCase()};\n`;
|
|
194
|
+
}
|
|
195
|
+
return `${padding}${ret} = ${prop.default};\n`;
|
|
196
|
+
}
|
|
197
|
+
getEnumClassDefinition(enumClassName, enumValues, padLeft) {
|
|
198
|
+
const padding = " ".repeat(padLeft);
|
|
199
|
+
let ret = `export enum ${enumClassName} {\n`;
|
|
200
|
+
for (const enumValue of enumValues) {
|
|
201
|
+
ret += `${padding}${enumValue.toUpperCase()} = '${enumValue}',\n`;
|
|
202
|
+
}
|
|
203
|
+
ret += "}\n";
|
|
204
|
+
return ret;
|
|
205
|
+
}
|
|
206
|
+
serializeObject(options, spaces) {
|
|
207
|
+
const sep = typeof spaces === "undefined" ? ", " : `,\n${" ".repeat(spaces)}`;
|
|
208
|
+
const doIndent = typeof spaces !== "undefined";
|
|
209
|
+
if (Array.isArray(options)) {
|
|
210
|
+
return `[${doIndent ? `\n${" ".repeat(spaces)}` : ""}${options.map((val) => `${doIndent ? " ".repeat(spaces) : ""}${this.serializeValue(val, doIndent ? spaces + 2 : undefined)}`).join(sep)}${doIndent ? `\n${" ".repeat(spaces + 2)}` : ""}]`;
|
|
211
|
+
}
|
|
212
|
+
return `{${doIndent ? `\n${" ".repeat(spaces)}` : " "}${Object.entries(options)
|
|
213
|
+
.map(([opt, val]) => {
|
|
214
|
+
return `${doIndent ? " ".repeat(spaces + 2) : ""}${identifierRegex.test(opt) ? opt : JSON.stringify(opt)}: ${this.serializeValue(val, doIndent ? spaces + 2 : undefined)}`;
|
|
215
|
+
})
|
|
216
|
+
.join(sep)}${doIndent ? `,\n${" ".repeat(spaces + 2)}` : " "}}`;
|
|
217
|
+
}
|
|
218
|
+
serializeValue(val, spaces) {
|
|
219
|
+
if (typeof val === "object" && val !== null) {
|
|
220
|
+
return this.serializeObject(val, spaces);
|
|
221
|
+
}
|
|
222
|
+
return val;
|
|
223
|
+
}
|
|
224
|
+
getEntityDeclOptions() {
|
|
225
|
+
const options = {};
|
|
226
|
+
if (this.meta.collection !==
|
|
227
|
+
this.namingStrategy.classToTableName(this.meta.className)) {
|
|
228
|
+
options.tableName = this.quote(this.meta.collection);
|
|
229
|
+
}
|
|
230
|
+
if (this.meta.schema &&
|
|
231
|
+
this.meta.schema !== this.platform.getDefaultSchemaName()) {
|
|
232
|
+
options.schema = this.quote(this.meta.schema);
|
|
233
|
+
}
|
|
234
|
+
if (typeof this.meta.expression === "string") {
|
|
235
|
+
options.expression = this.quote(this.meta.expression);
|
|
236
|
+
}
|
|
237
|
+
else if (typeof this.meta.expression === "function") {
|
|
238
|
+
options.expression = `${this.meta.expression}`;
|
|
239
|
+
}
|
|
240
|
+
if (this.meta.comment) {
|
|
241
|
+
options.comment = this.quote(this.meta.comment);
|
|
242
|
+
}
|
|
243
|
+
if (this.meta.readonly && !this.meta.virtual) {
|
|
244
|
+
options.readonly = this.meta.readonly;
|
|
245
|
+
}
|
|
246
|
+
if (this.meta.virtual) {
|
|
247
|
+
options.virtual = this.meta.virtual;
|
|
248
|
+
}
|
|
249
|
+
return this.getCollectionDecl(options);
|
|
250
|
+
}
|
|
251
|
+
getEmbeddableDeclOptions() {
|
|
252
|
+
const options = {};
|
|
253
|
+
return this.getCollectionDecl(options);
|
|
254
|
+
}
|
|
255
|
+
getCollectionDecl(options) {
|
|
256
|
+
if (this.meta.abstract) {
|
|
257
|
+
options.abstract = true;
|
|
258
|
+
}
|
|
259
|
+
if (this.meta.discriminatorValue) {
|
|
260
|
+
options.discriminatorValue =
|
|
261
|
+
typeof this.meta.discriminatorValue === "string"
|
|
262
|
+
? this.quote(this.meta.discriminatorValue)
|
|
263
|
+
: this.meta.discriminatorValue;
|
|
264
|
+
}
|
|
265
|
+
if (this.meta.discriminatorColumn) {
|
|
266
|
+
options.discriminatorColumn = this.quote(this.meta.discriminatorColumn);
|
|
267
|
+
}
|
|
268
|
+
if (this.meta.discriminatorMap) {
|
|
269
|
+
options.discriminatorMap = Object.fromEntries(Object.entries(this.meta.discriminatorMap).map(([discriminatorValue, className]) => [
|
|
270
|
+
discriminatorValue,
|
|
271
|
+
this.quote(className),
|
|
272
|
+
]));
|
|
273
|
+
}
|
|
274
|
+
if (!core_1.Utils.hasObjectKeys(options)) {
|
|
275
|
+
return "";
|
|
276
|
+
}
|
|
277
|
+
return this.serializeObject(options);
|
|
278
|
+
}
|
|
279
|
+
getPropertyDecorator(prop, padLeft) {
|
|
280
|
+
const padding = " ".repeat(padLeft);
|
|
281
|
+
const options = {};
|
|
282
|
+
let decorator = this.getDecoratorType(prop);
|
|
283
|
+
this.coreImports.add(decorator.substring(1));
|
|
284
|
+
if (prop.kind === core_1.ReferenceKind.MANY_TO_MANY) {
|
|
285
|
+
this.getManyToManyDecoratorOptions(options, prop);
|
|
286
|
+
}
|
|
287
|
+
else if (prop.kind === core_1.ReferenceKind.ONE_TO_MANY) {
|
|
288
|
+
this.getOneToManyDecoratorOptions(options, prop);
|
|
289
|
+
}
|
|
290
|
+
else if (prop.kind === core_1.ReferenceKind.SCALAR ||
|
|
291
|
+
typeof prop.kind === "undefined") {
|
|
292
|
+
this.getScalarPropertyDecoratorOptions(options, prop);
|
|
293
|
+
}
|
|
294
|
+
else if (prop.kind === core_1.ReferenceKind.EMBEDDED) {
|
|
295
|
+
this.getEmbeddedPropertyDeclarationOptions(options, prop);
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
this.getForeignKeyDecoratorOptions(options, prop);
|
|
299
|
+
}
|
|
300
|
+
if (prop.enum) {
|
|
301
|
+
options.items = `() => ${prop.type}`;
|
|
302
|
+
}
|
|
303
|
+
this.getCommonDecoratorOptions(options, prop);
|
|
304
|
+
const indexes = this.getPropertyIndexes(prop, options);
|
|
305
|
+
decorator = [...indexes.sort(), decorator]
|
|
306
|
+
.map((d) => padding + d)
|
|
307
|
+
.join("\n");
|
|
308
|
+
const decoratorArgs = [];
|
|
309
|
+
if (prop.formula) {
|
|
310
|
+
decoratorArgs.push(`${prop.formula}`);
|
|
311
|
+
}
|
|
312
|
+
if (core_1.Utils.hasObjectKeys(options)) {
|
|
313
|
+
decoratorArgs.push(`${this.serializeObject(options)}`);
|
|
314
|
+
}
|
|
315
|
+
return `${decorator}(${decoratorArgs.join(", ")})\n`;
|
|
316
|
+
}
|
|
317
|
+
getPropertyIndexes(prop, options) {
|
|
318
|
+
if (prop.kind === core_1.ReferenceKind.SCALAR) {
|
|
319
|
+
const ret = [];
|
|
320
|
+
if (prop.index) {
|
|
321
|
+
this.coreImports.add("Index");
|
|
322
|
+
ret.push(`@Index({ name: '${prop.index}' })`);
|
|
323
|
+
}
|
|
324
|
+
if (prop.unique) {
|
|
325
|
+
this.coreImports.add("Unique");
|
|
326
|
+
ret.push(`@Unique({ name: '${prop.unique}' })`);
|
|
327
|
+
}
|
|
328
|
+
return ret;
|
|
329
|
+
}
|
|
330
|
+
const processIndex = (type) => {
|
|
331
|
+
if (!prop[type]) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
const defaultName = this.platform.getIndexName(this.meta.collection, prop.fieldNames, type);
|
|
335
|
+
options[type] = defaultName === prop[type] ? "true" : `'${prop[type]}'`;
|
|
336
|
+
const expected = {
|
|
337
|
+
index: this.platform.indexForeignKeys(),
|
|
338
|
+
unique: prop.kind === core_1.ReferenceKind.ONE_TO_ONE,
|
|
339
|
+
};
|
|
340
|
+
if (expected[type] && options[type] === "true") {
|
|
341
|
+
delete options[type];
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
processIndex("index");
|
|
345
|
+
processIndex("unique");
|
|
346
|
+
return [];
|
|
347
|
+
}
|
|
348
|
+
getCommonDecoratorOptions(options, prop) {
|
|
349
|
+
if (this.options.scalarTypeInDecorator &&
|
|
350
|
+
prop.kind === core_1.ReferenceKind.SCALAR &&
|
|
351
|
+
!prop.enum) {
|
|
352
|
+
options.type = this.quote(prop.type);
|
|
353
|
+
}
|
|
354
|
+
if (prop.nullable && !prop.mappedBy) {
|
|
355
|
+
options.nullable = true;
|
|
356
|
+
}
|
|
357
|
+
if (prop.persist === false) {
|
|
358
|
+
options.persist = false;
|
|
359
|
+
}
|
|
360
|
+
["onCreate", "onUpdate", "serializer"]
|
|
361
|
+
.filter((key) => typeof prop[key] === "function")
|
|
362
|
+
.forEach((key) => (options[key] = `${prop[key]}`));
|
|
363
|
+
if (typeof prop.serializedName === "string") {
|
|
364
|
+
options.serializedName = this.quote(prop.serializedName);
|
|
365
|
+
}
|
|
366
|
+
[
|
|
367
|
+
"hidden",
|
|
368
|
+
"version",
|
|
369
|
+
"concurrencyCheck",
|
|
370
|
+
"eager",
|
|
371
|
+
"lazy",
|
|
372
|
+
"orphanRemoval",
|
|
373
|
+
]
|
|
374
|
+
.filter((key) => prop[key])
|
|
375
|
+
.forEach((key) => (options[key] = true));
|
|
376
|
+
if (prop.cascade &&
|
|
377
|
+
(prop.cascade.length !== 1 || prop.cascade[0] !== core_1.Cascade.PERSIST)) {
|
|
378
|
+
this.coreImports.add("Cascade");
|
|
379
|
+
options.cascade = `[${prop.cascade.map((value) => "Cascade." + value.toUpperCase()).join(", ")}]`;
|
|
380
|
+
}
|
|
381
|
+
if (typeof prop.comment === "string") {
|
|
382
|
+
options.comment = this.quote(prop.comment);
|
|
383
|
+
}
|
|
384
|
+
if (prop.default == null) {
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
if (typeof prop.default !== "string") {
|
|
388
|
+
options.default = prop.default;
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
if ([`''`, ""].includes(prop.default)) {
|
|
392
|
+
options.default = `''`;
|
|
393
|
+
}
|
|
394
|
+
else if (prop.defaultRaw === this.quote(prop.default)) {
|
|
395
|
+
options.default = this.quote(prop.default);
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
options.defaultRaw = `\`${prop.default}\``;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
getScalarPropertyDecoratorOptions(options, prop) {
|
|
402
|
+
if (prop.fieldNames[0] !== this.namingStrategy.propertyToColumnName(prop.name)) {
|
|
403
|
+
options.fieldName = `'${prop.fieldNames[0]}'`;
|
|
404
|
+
}
|
|
405
|
+
// for enum properties, we don't need a column type or the property length
|
|
406
|
+
// in the decorator so return early.
|
|
407
|
+
if (prop.enum) {
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
let t = prop.type;
|
|
411
|
+
if (t === "Date") {
|
|
412
|
+
t = "datetime";
|
|
413
|
+
}
|
|
414
|
+
const mappedType1 = this.platform.getMappedType(t);
|
|
415
|
+
const mappedType2 = this.platform.getMappedType(prop.columnTypes[0]);
|
|
416
|
+
const columnType1 = mappedType1.getColumnType({ ...prop, autoincrement: false }, this.platform);
|
|
417
|
+
const columnType2 = mappedType2.getColumnType({ ...prop, autoincrement: false }, this.platform);
|
|
418
|
+
if (columnType1 !== columnType2 ||
|
|
419
|
+
[mappedType1, mappedType2].some((t) => t instanceof core_1.UnknownType)) {
|
|
420
|
+
options.columnType = this.quote(columnType2);
|
|
421
|
+
}
|
|
422
|
+
const assign = (key) => {
|
|
423
|
+
if (prop[key] != null) {
|
|
424
|
+
options[key] = prop[key];
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
if (!(mappedType2 instanceof core_1.DateType) && !options.columnType) {
|
|
428
|
+
assign("length");
|
|
429
|
+
}
|
|
430
|
+
// those are already included in the `columnType` in most cases, and when that option is present, they would be ignored anyway
|
|
431
|
+
/* istanbul ignore next */
|
|
432
|
+
if (mappedType2 instanceof core_1.DecimalType && !options.columnType) {
|
|
433
|
+
assign("precision");
|
|
434
|
+
assign("scale");
|
|
435
|
+
}
|
|
436
|
+
if (prop.autoincrement) {
|
|
437
|
+
if (!prop.primary ||
|
|
438
|
+
!["number", "bigint"].includes(t) ||
|
|
439
|
+
this.meta.getPrimaryProps().length !== 1) {
|
|
440
|
+
options.autoincrement = true;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
if (prop.primary &&
|
|
445
|
+
["number", "bigint"].includes(t) &&
|
|
446
|
+
this.meta.getPrimaryProps().length === 1) {
|
|
447
|
+
options.autoincrement = false;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if (prop.generated) {
|
|
451
|
+
options.generated =
|
|
452
|
+
typeof prop.generated === "string"
|
|
453
|
+
? this.quote(prop.generated)
|
|
454
|
+
: `${prop.generated}`;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
getManyToManyDecoratorOptions(options, prop) {
|
|
458
|
+
this.entityImports.add(prop.type);
|
|
459
|
+
options.entity = `() => ${prop.type}`;
|
|
460
|
+
if (prop.mappedBy) {
|
|
461
|
+
options.mappedBy = this.quote(prop.mappedBy);
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (prop.pivotTable !==
|
|
465
|
+
this.namingStrategy.joinTableName(this.meta.collection, prop.type, prop.name)) {
|
|
466
|
+
options.pivotTable = this.quote(prop.pivotTable);
|
|
467
|
+
}
|
|
468
|
+
if (prop.pivotEntity && prop.pivotEntity !== prop.pivotTable) {
|
|
469
|
+
this.entityImports.add(prop.pivotEntity);
|
|
470
|
+
options.pivotEntity = `() => ${prop.pivotEntity}`;
|
|
471
|
+
}
|
|
472
|
+
if (prop.joinColumns.length === 1) {
|
|
473
|
+
options.joinColumn = this.quote(prop.joinColumns[0]);
|
|
474
|
+
}
|
|
475
|
+
else {
|
|
476
|
+
options.joinColumns = `[${prop.joinColumns.map(this.quote).join(", ")}]`;
|
|
477
|
+
}
|
|
478
|
+
if (prop.inverseJoinColumns.length === 1) {
|
|
479
|
+
options.inverseJoinColumn = this.quote(prop.inverseJoinColumns[0]);
|
|
480
|
+
}
|
|
481
|
+
else {
|
|
482
|
+
options.inverseJoinColumns = `[${prop.inverseJoinColumns.map(this.quote).join(", ")}]`;
|
|
483
|
+
}
|
|
484
|
+
if (prop.fixedOrder) {
|
|
485
|
+
options.fixedOrder = true;
|
|
486
|
+
if (prop.fixedOrderColumn &&
|
|
487
|
+
prop.fixedOrderColumn !== this.namingStrategy.referenceColumnName()) {
|
|
488
|
+
options.fixedOrderColumn = this.quote(prop.fixedOrderColumn);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
getOneToManyDecoratorOptions(options, prop) {
|
|
493
|
+
this.entityImports.add(prop.type);
|
|
494
|
+
options.entity = `() => ${prop.type}`;
|
|
495
|
+
options.mappedBy = this.quote(prop.mappedBy);
|
|
496
|
+
}
|
|
497
|
+
getEmbeddedPropertyDeclarationOptions(options, prop) {
|
|
498
|
+
this.coreImports.add("Embedded");
|
|
499
|
+
this.entityImports.add(prop.type);
|
|
500
|
+
options.entity = `() => ${prop.type}`;
|
|
501
|
+
if (prop.array) {
|
|
502
|
+
options.array = true;
|
|
503
|
+
}
|
|
504
|
+
if (prop.object) {
|
|
505
|
+
options.object = true;
|
|
506
|
+
}
|
|
507
|
+
if (prop.prefix === false || typeof prop.prefix === "string") {
|
|
508
|
+
options.prefix = prop.prefix;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
getForeignKeyDecoratorOptions(options, prop) {
|
|
512
|
+
const parts = prop.referencedTableName.split(".", 2);
|
|
513
|
+
const className = this.namingStrategy.getEntityName(...parts.reverse());
|
|
514
|
+
this.entityImports.add(className);
|
|
515
|
+
options.entity = `() => ${className}`;
|
|
516
|
+
if (prop.ref) {
|
|
517
|
+
options.ref = true;
|
|
518
|
+
}
|
|
519
|
+
if (prop.mapToPk) {
|
|
520
|
+
options.mapToPk = true;
|
|
521
|
+
}
|
|
522
|
+
if (prop.mappedBy) {
|
|
523
|
+
options.mappedBy = this.quote(prop.mappedBy);
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
if (prop.fieldNames.length === 1) {
|
|
527
|
+
if (prop.fieldNames[0] !==
|
|
528
|
+
this.namingStrategy.joinKeyColumnName(prop.name, prop.referencedColumnNames[0])) {
|
|
529
|
+
options.fieldName = this.quote(prop.fieldNames[0]);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
if (prop.fieldNames.length > 1 &&
|
|
534
|
+
prop.fieldNames.some((fieldName, i) => fieldName !==
|
|
535
|
+
this.namingStrategy.joinKeyColumnName(prop.name, prop.referencedColumnNames[i]))) {
|
|
536
|
+
options.fieldNames = prop.fieldNames.map((fieldName) => this.quote(fieldName));
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
if (!["no action", "restrict"].includes(prop.updateRule.toLowerCase())) {
|
|
540
|
+
options.updateRule = this.quote(prop.updateRule);
|
|
541
|
+
}
|
|
542
|
+
if (!["no action", "restrict"].includes(prop.deleteRule.toLowerCase())) {
|
|
543
|
+
options.deleteRule = this.quote(prop.deleteRule);
|
|
544
|
+
}
|
|
545
|
+
if (prop.primary) {
|
|
546
|
+
options.primary = true;
|
|
547
|
+
}
|
|
548
|
+
if (prop.generated) {
|
|
549
|
+
options.generated =
|
|
550
|
+
typeof prop.generated === "string"
|
|
551
|
+
? this.quote(prop.generated)
|
|
552
|
+
: `${prop.generated}`;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
getDecoratorType(prop) {
|
|
556
|
+
if (prop.kind === core_1.ReferenceKind.ONE_TO_ONE) {
|
|
557
|
+
return "@OneToOne";
|
|
558
|
+
}
|
|
559
|
+
if (prop.kind === core_1.ReferenceKind.MANY_TO_ONE) {
|
|
560
|
+
return "@ManyToOne";
|
|
561
|
+
}
|
|
562
|
+
if (prop.kind === core_1.ReferenceKind.ONE_TO_MANY) {
|
|
563
|
+
return "@OneToMany";
|
|
564
|
+
}
|
|
565
|
+
if (prop.kind === core_1.ReferenceKind.MANY_TO_MANY) {
|
|
566
|
+
return "@ManyToMany";
|
|
567
|
+
}
|
|
568
|
+
if (prop.kind === core_1.ReferenceKind.EMBEDDED) {
|
|
569
|
+
return "@Embedded";
|
|
570
|
+
}
|
|
571
|
+
if (prop.primary) {
|
|
572
|
+
return "@PrimaryKey";
|
|
573
|
+
}
|
|
574
|
+
if (prop.enum) {
|
|
575
|
+
return "@Enum";
|
|
576
|
+
}
|
|
577
|
+
if (prop.formula) {
|
|
578
|
+
return "@Formula";
|
|
579
|
+
}
|
|
580
|
+
return "@Property";
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
exports.SourceFile = SourceFile;
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
/**
|
|
18
|
+
* @packageDocumentation
|
|
19
|
+
* @module entity-generator
|
|
20
|
+
*/
|
|
21
|
+
__exportStar(require("./EntityGenerator"), exports);
|
package/index.mjs
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yandjin-mikro-orm/entity-generator",
|
|
3
|
+
"version": "6.1.4-rc-sti-changes-1",
|
|
4
|
+
"description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"module": "index.mjs",
|
|
7
|
+
"typings": "index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
"./package.json": "./package.json",
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./index.d.ts",
|
|
13
|
+
"default": "./index.mjs"
|
|
14
|
+
},
|
|
15
|
+
"require": "./index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+ssh://git@github.com/mikro-orm/mikro-orm.git"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"orm",
|
|
24
|
+
"mongo",
|
|
25
|
+
"mongodb",
|
|
26
|
+
"mysql",
|
|
27
|
+
"mariadb",
|
|
28
|
+
"postgresql",
|
|
29
|
+
"sqlite",
|
|
30
|
+
"sqlite3",
|
|
31
|
+
"ts",
|
|
32
|
+
"typescript",
|
|
33
|
+
"js",
|
|
34
|
+
"javascript",
|
|
35
|
+
"entity",
|
|
36
|
+
"ddd",
|
|
37
|
+
"mikro-orm",
|
|
38
|
+
"unit-of-work",
|
|
39
|
+
"data-mapper",
|
|
40
|
+
"identity-map"
|
|
41
|
+
],
|
|
42
|
+
"author": "Martin Adámek",
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/mikro-orm/mikro-orm/issues"
|
|
46
|
+
},
|
|
47
|
+
"homepage": "https://mikro-orm.io",
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">= 18.12.0"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "yarn clean && yarn compile && yarn copy && yarn run -T gen-esm-wrapper index.js index.mjs",
|
|
53
|
+
"clean": "yarn run -T rimraf ./dist",
|
|
54
|
+
"compile": "yarn run -T tsc -p tsconfig.build.json",
|
|
55
|
+
"copy": "node ../../scripts/copy.mjs"
|
|
56
|
+
},
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public"
|
|
59
|
+
},
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"@yandjin-mikro-orm/knex": "6.1.4-rc-sti-changes-1",
|
|
62
|
+
"fs-extra": "11.2.0"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@yandjin-mikro-orm/core": "^6.1.4-rc-sti-changes-1"
|
|
66
|
+
},
|
|
67
|
+
"peerDependencies": {
|
|
68
|
+
"@yandjin-mikro-orm/core": "^6.0.0"
|
|
69
|
+
}
|
|
70
|
+
}
|