@mikro-orm/entity-generator 7.0.0-dev.22 → 7.0.0-dev.220
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/DefineEntitySourceFile.d.ts +5 -0
- package/DefineEntitySourceFile.js +115 -0
- package/EntityGenerator.d.ts +2 -1
- package/EntityGenerator.js +111 -34
- package/EntitySchemaSourceFile.d.ts +3 -2
- package/EntitySchemaSourceFile.js +47 -36
- package/NativeEnumSourceFile.d.ts +16 -0
- package/NativeEnumSourceFile.js +47 -0
- package/README.md +3 -2
- package/SourceFile.d.ts +4 -1
- package/SourceFile.js +149 -51
- package/package.json +6 -6
- package/tsconfig.build.tsbuildinfo +1 -0
package/SourceFile.js
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
import { Cascade, Config, DecimalType, ReferenceKind, SCALAR_TYPES, UnknownType, Utils, } from '@mikro-orm/core';
|
|
1
|
+
import { Cascade, Config, DecimalType, ReferenceKind, SCALAR_TYPES, UnknownType, Utils, inspect, } from '@mikro-orm/core';
|
|
2
2
|
import { parse, relative } from 'node:path';
|
|
3
|
-
import { inspect } from 'node:util';
|
|
4
3
|
import { POSSIBLE_TYPE_IMPORTS } from './CoreImportsHelper.js';
|
|
5
4
|
/**
|
|
6
5
|
* @see https://github.com/tc39/proposal-regexp-unicode-property-escapes#other-examples
|
|
7
6
|
*/
|
|
8
7
|
export const identifierRegex = /^(?:[$_\p{ID_Start}])(?:[$\u200C\u200D\p{ID_Continue}])*$/u;
|
|
9
|
-
const primitivesAndLibs = [...SCALAR_TYPES, '
|
|
8
|
+
const primitivesAndLibs = [...SCALAR_TYPES, 'unknown', 'object', 'any'];
|
|
10
9
|
export class SourceFile {
|
|
11
10
|
meta;
|
|
12
11
|
namingStrategy;
|
|
13
12
|
platform;
|
|
14
13
|
options;
|
|
15
14
|
coreImports = new Set();
|
|
15
|
+
decoratorImports = new Set();
|
|
16
16
|
entityImports = new Set();
|
|
17
|
+
enumImports = new Map();
|
|
17
18
|
constructor(meta, namingStrategy, platform, options) {
|
|
18
19
|
this.meta = meta;
|
|
19
20
|
this.namingStrategy = namingStrategy;
|
|
@@ -25,24 +26,24 @@ export class SourceFile {
|
|
|
25
26
|
if (this.meta.embeddable || this.meta.collection) {
|
|
26
27
|
if (this.meta.embeddable) {
|
|
27
28
|
const options = this.getEmbeddableDeclOptions();
|
|
28
|
-
ret += `@${this.
|
|
29
|
+
ret += `@${this.referenceDecoratorImport('Embeddable')}(${Utils.hasObjectKeys(options) ? this.serializeObject(options) : ''})\n`;
|
|
29
30
|
}
|
|
30
31
|
else {
|
|
31
32
|
const options = this.getEntityDeclOptions();
|
|
32
|
-
ret += `@${this.
|
|
33
|
+
ret += `@${this.referenceDecoratorImport('Entity')}(${Utils.hasObjectKeys(options) ? this.serializeObject(options) : ''})\n`;
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
36
|
for (const index of this.meta.indexes) {
|
|
36
37
|
if (index.properties?.length === 1 && typeof this.meta.properties[index.properties[0]] !== 'undefined') {
|
|
37
38
|
continue;
|
|
38
39
|
}
|
|
39
|
-
ret += `@${this.
|
|
40
|
+
ret += `@${this.referenceDecoratorImport('Index')}(${this.serializeObject(this.getIndexOptions(index))})\n`;
|
|
40
41
|
}
|
|
41
42
|
for (const index of this.meta.uniques) {
|
|
42
43
|
if (index.properties?.length === 1 && typeof this.meta.properties[index.properties[0]] !== 'undefined') {
|
|
43
44
|
continue;
|
|
44
45
|
}
|
|
45
|
-
ret += `@${this.
|
|
46
|
+
ret += `@${this.referenceDecoratorImport('Unique')}(${this.serializeObject(this.getUniqueOptions(index))})\n`;
|
|
46
47
|
}
|
|
47
48
|
let classHead = '';
|
|
48
49
|
if (this.meta.className === this.options.customBaseEntityName) {
|
|
@@ -65,7 +66,10 @@ export class SourceFile {
|
|
|
65
66
|
classBody += definition;
|
|
66
67
|
classBody += '\n';
|
|
67
68
|
if (prop.enum) {
|
|
68
|
-
|
|
69
|
+
const def = this.getEnumClassDefinition(prop, 2);
|
|
70
|
+
if (def.length) {
|
|
71
|
+
enumDefinitions.push(def);
|
|
72
|
+
}
|
|
69
73
|
}
|
|
70
74
|
if (prop.eager) {
|
|
71
75
|
eagerProperties.push(prop);
|
|
@@ -99,9 +103,12 @@ export class SourceFile {
|
|
|
99
103
|
if (typeof index.name === 'string') {
|
|
100
104
|
indexOpt.name = this.quote(index.name);
|
|
101
105
|
}
|
|
102
|
-
if (index.expression) {
|
|
106
|
+
if (typeof index.expression === 'string') {
|
|
103
107
|
indexOpt.expression = this.quote(index.expression);
|
|
104
108
|
}
|
|
109
|
+
else if (typeof index.expression === 'function') {
|
|
110
|
+
indexOpt.expression = `${index.expression}`.replace(')=>`', ') => `');
|
|
111
|
+
}
|
|
105
112
|
if (isAtEntityLevel && index.properties) {
|
|
106
113
|
indexOpt.properties = Utils.asArray(index.properties).map(prop => this.quote('' + prop));
|
|
107
114
|
}
|
|
@@ -112,9 +119,12 @@ export class SourceFile {
|
|
|
112
119
|
if (typeof index.name === 'string') {
|
|
113
120
|
uniqueOpt.name = this.quote(index.name);
|
|
114
121
|
}
|
|
115
|
-
if (index.expression) {
|
|
122
|
+
if (typeof index.expression === 'string') {
|
|
116
123
|
uniqueOpt.expression = this.quote(index.expression);
|
|
117
124
|
}
|
|
125
|
+
else if (typeof index.expression === 'function') {
|
|
126
|
+
uniqueOpt.expression = `${index.expression}`.replace(')=>`', ') => `');
|
|
127
|
+
}
|
|
118
128
|
if (isAtEntityLevel && index.properties) {
|
|
119
129
|
uniqueOpt.properties = Utils.asArray(index.properties).map(prop => this.quote('' + prop));
|
|
120
130
|
}
|
|
@@ -135,37 +145,61 @@ export class SourceFile {
|
|
|
135
145
|
return ret;
|
|
136
146
|
}).join(', '))} } from '@mikro-orm/core';`);
|
|
137
147
|
}
|
|
148
|
+
if (this.decoratorImports.size > 0) {
|
|
149
|
+
const type = this.options.decorators;
|
|
150
|
+
imports.add(`import { ${([...this.decoratorImports].sort().map(t => {
|
|
151
|
+
let ret = t;
|
|
152
|
+
if (this.options.coreImportsPrefix) {
|
|
153
|
+
const resolvedIdentifier = `${this.options.coreImportsPrefix}${t}`;
|
|
154
|
+
ret += ` as ${resolvedIdentifier}`;
|
|
155
|
+
}
|
|
156
|
+
return ret;
|
|
157
|
+
}).join(', '))} } from '@mikro-orm/decorators/${type}';`);
|
|
158
|
+
}
|
|
138
159
|
const extension = this.options.esmImport ? '.js' : '';
|
|
139
160
|
const { dir, base } = parse(`${this.options.path ?? '.'}/${this.getBaseName()}`);
|
|
140
161
|
const basePath = relative(dir, this.options.path ?? '.') || '.';
|
|
141
162
|
(this.options.extraImports?.(basePath, base) ?? []).forEach(v => this.entityImports.add(v));
|
|
142
163
|
const entityImports = [...this.entityImports].filter(e => e !== this.meta.className);
|
|
143
|
-
|
|
164
|
+
const importMap = new Map();
|
|
165
|
+
for (const entity of entityImports) {
|
|
144
166
|
const file = this.options.onImport?.(entity, basePath, extension, base) ?? {
|
|
145
167
|
path: `${basePath}/${this.options.fileName(entity)}${extension}`,
|
|
146
168
|
name: entity,
|
|
147
169
|
};
|
|
148
170
|
if (file.path === '') {
|
|
149
171
|
if (file.name === '') {
|
|
150
|
-
|
|
172
|
+
continue;
|
|
151
173
|
}
|
|
152
|
-
|
|
153
|
-
|
|
174
|
+
importMap.set(file.path, `import ${this.quote(file.name)};`);
|
|
175
|
+
continue;
|
|
154
176
|
}
|
|
155
177
|
if (file.name === '') {
|
|
156
|
-
|
|
157
|
-
|
|
178
|
+
importMap.set(file.path, `import * as ${entity} from ${this.quote(file.path)};`);
|
|
179
|
+
continue;
|
|
158
180
|
}
|
|
159
181
|
if (file.name === 'default') {
|
|
160
|
-
|
|
161
|
-
|
|
182
|
+
importMap.set(file.path, `import ${entity} from ${this.quote(file.path)};`);
|
|
183
|
+
continue;
|
|
162
184
|
}
|
|
163
185
|
if (file.name === entity) {
|
|
164
|
-
|
|
165
|
-
|
|
186
|
+
importMap.set(file.path, `import { ${entity} } from ${this.quote(file.path)};`);
|
|
187
|
+
continue;
|
|
166
188
|
}
|
|
167
|
-
|
|
168
|
-
}
|
|
189
|
+
importMap.set(file.path, `import { ${identifierRegex.test(file.name) ? file.name : this.quote(file.name)} as ${entity} } from ${this.quote(file.path)};`);
|
|
190
|
+
}
|
|
191
|
+
if (this.enumImports.size) {
|
|
192
|
+
for (const [name, exports] of this.enumImports.entries()) {
|
|
193
|
+
const file = this.options.onImport?.(name, basePath, extension, base) ?? {
|
|
194
|
+
path: `${basePath}/${this.options.fileName(name)}${extension}`,
|
|
195
|
+
name,
|
|
196
|
+
};
|
|
197
|
+
importMap.set(file.path, `import { ${exports.join(', ')} } from ${this.quote(file.path)};`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
for (const key of [...importMap.keys()].sort()) {
|
|
201
|
+
imports.add(importMap.get(key));
|
|
202
|
+
}
|
|
169
203
|
return Array.from(imports.values()).join('\n');
|
|
170
204
|
}
|
|
171
205
|
getEntityClass(classBody) {
|
|
@@ -175,8 +209,8 @@ export class SourceFile {
|
|
|
175
209
|
}
|
|
176
210
|
ret += `class ${this.meta.className}`;
|
|
177
211
|
if (this.meta.extends) {
|
|
178
|
-
this.entityImports.add(this.meta.extends);
|
|
179
|
-
ret += ` extends ${this.meta.extends}`;
|
|
212
|
+
this.entityImports.add(Utils.className(this.meta.extends));
|
|
213
|
+
ret += ` extends ${Utils.className(this.meta.extends)}`;
|
|
180
214
|
}
|
|
181
215
|
else if (this.options.useCoreBaseEntity) {
|
|
182
216
|
ret += ` extends ${this.referenceCoreImport('BaseEntity')}`;
|
|
@@ -195,6 +229,7 @@ export class SourceFile {
|
|
|
195
229
|
getPropertyDefinition(prop, padLeft) {
|
|
196
230
|
const padding = ' '.repeat(padLeft);
|
|
197
231
|
const propName = identifierRegex.test(prop.name) ? prop.name : this.quote(prop.name);
|
|
232
|
+
const enumMode = this.options.enumMode;
|
|
198
233
|
let hiddenType = '';
|
|
199
234
|
if (prop.hidden) {
|
|
200
235
|
hiddenType += ` & ${this.referenceCoreImport('Hidden')}`;
|
|
@@ -212,7 +247,11 @@ export class SourceFile {
|
|
|
212
247
|
: (() => {
|
|
213
248
|
if (isScalar) {
|
|
214
249
|
if (prop.enum) {
|
|
215
|
-
|
|
250
|
+
const method = enumMode === 'ts-enum' ? 'getEnumClassName' : 'getEnumTypeName';
|
|
251
|
+
if (prop.nativeEnumName) {
|
|
252
|
+
return this.namingStrategy[method](prop.nativeEnumName, undefined, this.meta.schema);
|
|
253
|
+
}
|
|
254
|
+
return this.namingStrategy[method](prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
216
255
|
}
|
|
217
256
|
breakdownOfIType = this.breakdownOfIType(prop);
|
|
218
257
|
if (typeof breakdownOfIType !== 'undefined') {
|
|
@@ -247,8 +286,14 @@ export class SourceFile {
|
|
|
247
286
|
return `${padding}${ret};\n`;
|
|
248
287
|
}
|
|
249
288
|
if (prop.enum && typeof prop.default === 'string') {
|
|
289
|
+
if (enumMode === 'union-type') {
|
|
290
|
+
return `${padding}${ret} = ${this.quote(prop.default)};\n`;
|
|
291
|
+
}
|
|
292
|
+
const enumClassName = prop.nativeEnumName
|
|
293
|
+
? this.namingStrategy.getEnumClassName(prop.nativeEnumName, undefined, this.meta.schema)
|
|
294
|
+
: this.namingStrategy.getEnumClassName(prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
250
295
|
const enumVal = this.namingStrategy.enumValueToEnumProperty(prop.default, prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
251
|
-
return `${padding}${ret} = ${
|
|
296
|
+
return `${padding}${ret} = ${enumClassName}${identifierRegex.test(enumVal) ? `.${enumVal}` : `[${this.quote(enumVal)}]`};\n`;
|
|
252
297
|
}
|
|
253
298
|
if (prop.fieldNames?.length > 1) {
|
|
254
299
|
// TODO: Composite FKs with default values require additions to default/defaultRaw that are not yet supported.
|
|
@@ -264,15 +309,51 @@ export class SourceFile {
|
|
|
264
309
|
return `${padding}${ret} = ${prop.ref ? this.referenceCoreImport('ref') : this.referenceCoreImport('rel')}(${propType}, ${defaultVal});\n`;
|
|
265
310
|
}
|
|
266
311
|
getEnumClassDefinition(prop, padLeft) {
|
|
312
|
+
const enumMode = this.options.enumMode;
|
|
313
|
+
if (prop.nativeEnumName) {
|
|
314
|
+
const imports = [];
|
|
315
|
+
if (enumMode !== 'union-type') {
|
|
316
|
+
imports.push(prop.runtimeType);
|
|
317
|
+
}
|
|
318
|
+
if (!this.options.inferEntityType && enumMode !== 'ts-enum') {
|
|
319
|
+
const enumTypeName = this.namingStrategy.getEnumTypeName(prop.nativeEnumName, undefined, this.meta.schema);
|
|
320
|
+
imports.push(enumTypeName);
|
|
321
|
+
}
|
|
322
|
+
this.enumImports.set(prop.runtimeType, imports);
|
|
323
|
+
return '';
|
|
324
|
+
}
|
|
267
325
|
const enumClassName = this.namingStrategy.getEnumClassName(prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
326
|
+
const enumTypeName = this.namingStrategy.getEnumTypeName(prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
268
327
|
const padding = ' '.repeat(padLeft);
|
|
269
|
-
let ret = `export enum ${enumClassName} {\n`;
|
|
270
328
|
const enumValues = prop.items;
|
|
329
|
+
if (enumMode === 'union-type') {
|
|
330
|
+
return `export type ${enumTypeName} = ${enumValues.map(item => this.quote(item)).join(' | ')};\n`;
|
|
331
|
+
}
|
|
332
|
+
let ret = '';
|
|
333
|
+
if (enumMode === 'dictionary') {
|
|
334
|
+
ret += `export const ${enumClassName} = {\n`;
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
ret += `export enum ${enumClassName} {\n`;
|
|
338
|
+
}
|
|
271
339
|
for (const enumValue of enumValues) {
|
|
272
340
|
const enumName = this.namingStrategy.enumValueToEnumProperty(enumValue, prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
273
|
-
|
|
341
|
+
if (enumMode === 'dictionary') {
|
|
342
|
+
ret += `${padding}${identifierRegex.test(enumName) ? enumName : this.quote(enumName)}: ${this.quote(enumValue)},\n`;
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
ret += `${padding}${identifierRegex.test(enumName) ? enumName : this.quote(enumName)} = ${this.quote(enumValue)},\n`;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (enumMode === 'dictionary') {
|
|
349
|
+
ret += '} as const;\n';
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
ret += '}\n';
|
|
353
|
+
}
|
|
354
|
+
if (enumMode === 'dictionary') {
|
|
355
|
+
ret += `\nexport type ${enumTypeName} = (typeof ${enumClassName})[keyof typeof ${enumClassName}];\n`;
|
|
274
356
|
}
|
|
275
|
-
ret += '}\n';
|
|
276
357
|
return ret;
|
|
277
358
|
}
|
|
278
359
|
serializeObject(options, wordwrap, spaces, level = 0) {
|
|
@@ -302,8 +383,8 @@ export class SourceFile {
|
|
|
302
383
|
}
|
|
303
384
|
getEntityDeclOptions() {
|
|
304
385
|
const options = {};
|
|
305
|
-
if (this.meta.
|
|
306
|
-
options.tableName = this.quote(this.meta.
|
|
386
|
+
if (this.meta.tableName !== this.namingStrategy.classToTableName(this.meta.className)) {
|
|
387
|
+
options.tableName = this.quote(this.meta.tableName);
|
|
307
388
|
}
|
|
308
389
|
if (this.meta.schema && this.meta.schema !== this.platform.getDefaultSchemaName()) {
|
|
309
390
|
options.schema = this.quote(this.meta.schema);
|
|
@@ -312,7 +393,7 @@ export class SourceFile {
|
|
|
312
393
|
options.expression = this.quote(this.meta.expression);
|
|
313
394
|
}
|
|
314
395
|
else if (typeof this.meta.expression === 'function') {
|
|
315
|
-
options.expression =
|
|
396
|
+
options.expression = this.meta.expression.toString();
|
|
316
397
|
}
|
|
317
398
|
if (this.meta.repositoryClass) {
|
|
318
399
|
this.entityImports.add(this.meta.repositoryClass);
|
|
@@ -345,14 +426,14 @@ export class SourceFile {
|
|
|
345
426
|
}
|
|
346
427
|
if (this.meta.discriminatorMap) {
|
|
347
428
|
options.discriminatorMap = Object.fromEntries(Object.entries(this.meta.discriminatorMap)
|
|
348
|
-
.map(([discriminatorValue,
|
|
429
|
+
.map(([discriminatorValue, cls]) => [discriminatorValue, this.quote(Utils.className(cls))]));
|
|
349
430
|
}
|
|
350
431
|
return options;
|
|
351
432
|
}
|
|
352
433
|
getPropertyDecorator(prop, padLeft) {
|
|
353
434
|
const padding = ' '.repeat(padLeft);
|
|
354
435
|
const options = {};
|
|
355
|
-
let decorator = `@${this.
|
|
436
|
+
let decorator = `@${this.referenceDecoratorImport(this.getDecoratorType(prop))}`;
|
|
356
437
|
if (prop.kind === ReferenceKind.MANY_TO_MANY) {
|
|
357
438
|
this.getManyToManyDecoratorOptions(options, prop);
|
|
358
439
|
}
|
|
@@ -373,7 +454,7 @@ export class SourceFile {
|
|
|
373
454
|
decorator = [...indexes.sort(), decorator].map(d => padding + d).join('\n');
|
|
374
455
|
const decoratorArgs = [];
|
|
375
456
|
if (prop.formula) {
|
|
376
|
-
decoratorArgs.push(
|
|
457
|
+
decoratorArgs.push(prop.formula.toString());
|
|
377
458
|
}
|
|
378
459
|
if (Utils.hasObjectKeys(options)) {
|
|
379
460
|
decoratorArgs.push(`${this.serializeObject(options)}`);
|
|
@@ -402,26 +483,26 @@ export class SourceFile {
|
|
|
402
483
|
let propIndexIsNonTrivialIndex = false;
|
|
403
484
|
const nonTrivialIndexes = this.meta.indexes.filter(i => i.properties?.length === 1 && i.properties[0] === prop.name);
|
|
404
485
|
for (const i of nonTrivialIndexes) {
|
|
405
|
-
ret.push(`@${this.
|
|
486
|
+
ret.push(`@${this.referenceDecoratorImport('Index')}(${this.serializeObject(this.getIndexOptions(i, false))})`);
|
|
406
487
|
if (prop.index === i.name) {
|
|
407
488
|
propIndexIsNonTrivialIndex = true;
|
|
408
489
|
delete options.index;
|
|
409
490
|
}
|
|
410
491
|
}
|
|
411
492
|
if (prop.index && !options.index && !propIndexIsNonTrivialIndex) {
|
|
412
|
-
ret.push(`@${this.
|
|
493
|
+
ret.push(`@${this.referenceDecoratorImport('Index')}(${typeof prop.index === 'string' ? `{ name: ${this.quote(prop.index)} }` : ''})`);
|
|
413
494
|
}
|
|
414
495
|
let propIndexIsNonTrivialUnique = false;
|
|
415
496
|
const nonTrivialUnique = this.meta.uniques.filter(i => i.properties?.length === 1 && i.properties[0] === prop.name);
|
|
416
497
|
for (const i of nonTrivialUnique) {
|
|
417
|
-
ret.push(`@${this.
|
|
498
|
+
ret.push(`@${this.referenceDecoratorImport('Unique')}(${this.serializeObject(this.getUniqueOptions(i, false))})`);
|
|
418
499
|
if (prop.unique === i.name) {
|
|
419
500
|
propIndexIsNonTrivialUnique = true;
|
|
420
501
|
delete options.unique;
|
|
421
502
|
}
|
|
422
503
|
}
|
|
423
504
|
if (prop.unique && !options.unique && !propIndexIsNonTrivialUnique) {
|
|
424
|
-
ret.push(`@${this.
|
|
505
|
+
ret.push(`@${this.referenceDecoratorImport('Unique')}(${typeof prop.unique === 'string' ? `{ name: ${this.quote(prop.unique)} }` : ''})`);
|
|
425
506
|
}
|
|
426
507
|
return ret;
|
|
427
508
|
}
|
|
@@ -432,7 +513,7 @@ export class SourceFile {
|
|
|
432
513
|
if (prop.primary && (prop.enum || !(typeof prop.kind === 'undefined' || prop.kind === ReferenceKind.SCALAR))) {
|
|
433
514
|
options.primary = true;
|
|
434
515
|
}
|
|
435
|
-
['persist', 'hydrate'
|
|
516
|
+
['persist', 'hydrate']
|
|
436
517
|
.filter(key => prop[key] === false)
|
|
437
518
|
.forEach(key => options[key] = false);
|
|
438
519
|
['onCreate', 'onUpdate', 'serializer']
|
|
@@ -531,12 +612,23 @@ export class SourceFile {
|
|
|
531
612
|
this.propTypeBreakdowns.set(prop, r);
|
|
532
613
|
return r;
|
|
533
614
|
}
|
|
534
|
-
getScalarPropertyDecoratorOptions(options, prop) {
|
|
615
|
+
getScalarPropertyDecoratorOptions(options, prop, quote = true) {
|
|
535
616
|
if (prop.fieldNames[0] !== this.namingStrategy.propertyToColumnName(prop.name)) {
|
|
536
617
|
options.fieldName = this.quote(prop.fieldNames[0]);
|
|
537
618
|
}
|
|
538
619
|
if (prop.enum) {
|
|
539
|
-
options.
|
|
620
|
+
if (this.options.enumMode === 'union-type') {
|
|
621
|
+
options.items = `[${prop.items.map(item => this.quote(item)).join(', ')}]`;
|
|
622
|
+
}
|
|
623
|
+
else if (prop.nativeEnumName) {
|
|
624
|
+
const enumClassName = this.namingStrategy.getEnumClassName(prop.nativeEnumName, undefined, this.meta.schema);
|
|
625
|
+
options.items = `() => ${enumClassName}`;
|
|
626
|
+
options.nativeEnumName = this.quote(prop.nativeEnumName);
|
|
627
|
+
}
|
|
628
|
+
else {
|
|
629
|
+
const enumClassName = this.namingStrategy.getEnumClassName(prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
630
|
+
options.items = `() => ${enumClassName}`;
|
|
631
|
+
}
|
|
540
632
|
}
|
|
541
633
|
// For enum properties, we don't need a column type
|
|
542
634
|
// or the property length or other information in the decorator.
|
|
@@ -565,7 +657,7 @@ export class SourceFile {
|
|
|
565
657
|
return ((useDefault && !hasUsableNullDefault) || (prop.optional && !prop.nullable));
|
|
566
658
|
})() // also when there is the "| Opt" type modifier (because reflect-metadata can't extract the base)
|
|
567
659
|
) {
|
|
568
|
-
options.type = this.quote(prop.type);
|
|
660
|
+
options.type = quote ? this.quote(prop.type) : prop.type;
|
|
569
661
|
}
|
|
570
662
|
}
|
|
571
663
|
const columnTypeFromMappedRuntimeType = mappedRuntimeType.getColumnType({ ...prop, autoincrement: false }, this.platform);
|
|
@@ -592,7 +684,7 @@ export class SourceFile {
|
|
|
592
684
|
assign('length');
|
|
593
685
|
}
|
|
594
686
|
// those are already included in the `columnType` in most cases, and when that option is present, they would be ignored anyway
|
|
595
|
-
/* v8 ignore next
|
|
687
|
+
/* v8 ignore next */
|
|
596
688
|
if (mappedColumnType instanceof DecimalType && !options.columnType) {
|
|
597
689
|
assign('precision');
|
|
598
690
|
assign('scale');
|
|
@@ -626,12 +718,12 @@ export class SourceFile {
|
|
|
626
718
|
options.mappedBy = this.quote(prop.mappedBy);
|
|
627
719
|
return;
|
|
628
720
|
}
|
|
629
|
-
if (prop.pivotTable !== this.namingStrategy.joinTableName(this.meta.collection, prop.type, prop.name)) {
|
|
721
|
+
if (prop.pivotTable !== this.namingStrategy.joinTableName(this.meta.collection, prop.type, prop.name, this.meta.tableName)) {
|
|
630
722
|
options.pivotTable = this.quote(prop.pivotTable);
|
|
631
723
|
}
|
|
632
|
-
if (prop.pivotEntity && prop.pivotEntity !== prop.pivotTable) {
|
|
633
|
-
this.entityImports.add(prop.pivotEntity);
|
|
634
|
-
options.pivotEntity = `() => ${prop.pivotEntity}`;
|
|
724
|
+
if (prop.pivotEntity && Utils.className(prop.pivotEntity) !== prop.pivotTable) {
|
|
725
|
+
this.entityImports.add(Utils.className(prop.pivotEntity));
|
|
726
|
+
options.pivotEntity = `() => ${Utils.className(prop.pivotEntity)}`;
|
|
635
727
|
}
|
|
636
728
|
if (prop.joinColumns.length === 1) {
|
|
637
729
|
options.joinColumn = this.quote(prop.joinColumns[0]);
|
|
@@ -666,7 +758,7 @@ export class SourceFile {
|
|
|
666
758
|
if (prop.array) {
|
|
667
759
|
options.array = true;
|
|
668
760
|
}
|
|
669
|
-
if (prop.object) {
|
|
761
|
+
if (prop.object && !prop.array) {
|
|
670
762
|
options.object = true;
|
|
671
763
|
}
|
|
672
764
|
if (prop.prefix === false || typeof prop.prefix === 'string') {
|
|
@@ -699,10 +791,10 @@ export class SourceFile {
|
|
|
699
791
|
if (prop.ownColumns && prop.ownColumns.length !== prop.fieldNames.length) {
|
|
700
792
|
options.referencedColumnNames = prop.referencedColumnNames.map(fieldName => this.quote(fieldName));
|
|
701
793
|
}
|
|
702
|
-
if (
|
|
794
|
+
if (prop.updateRule) {
|
|
703
795
|
options.updateRule = this.quote(prop.updateRule);
|
|
704
796
|
}
|
|
705
|
-
if (
|
|
797
|
+
if (prop.deleteRule) {
|
|
706
798
|
options.deleteRule = this.quote(prop.deleteRule);
|
|
707
799
|
}
|
|
708
800
|
if (prop.primary) {
|
|
@@ -749,4 +841,10 @@ export class SourceFile {
|
|
|
749
841
|
? `${this.options.coreImportsPrefix}${identifier}`
|
|
750
842
|
: identifier;
|
|
751
843
|
}
|
|
844
|
+
referenceDecoratorImport(identifier) {
|
|
845
|
+
this.decoratorImports.add(identifier);
|
|
846
|
+
return this.options.coreImportsPrefix
|
|
847
|
+
? `${this.options.coreImportsPrefix}${identifier}`
|
|
848
|
+
: identifier;
|
|
849
|
+
}
|
|
752
850
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/entity-generator",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "7.0.0-dev.
|
|
4
|
+
"version": "7.0.0-dev.220",
|
|
5
5
|
"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.",
|
|
6
6
|
"exports": {
|
|
7
7
|
"./package.json": "./package.json",
|
|
@@ -38,10 +38,10 @@
|
|
|
38
38
|
},
|
|
39
39
|
"homepage": "https://mikro-orm.io",
|
|
40
40
|
"engines": {
|
|
41
|
-
"node": ">= 22.
|
|
41
|
+
"node": ">= 22.17.0"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
|
-
"build": "yarn
|
|
44
|
+
"build": "yarn compile && yarn copy",
|
|
45
45
|
"clean": "yarn run -T rimraf ./dist",
|
|
46
46
|
"compile": "yarn run -T tsc -p tsconfig.build.json",
|
|
47
47
|
"copy": "node ../../scripts/copy.mjs"
|
|
@@ -50,12 +50,12 @@
|
|
|
50
50
|
"access": "public"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@mikro-orm/
|
|
53
|
+
"@mikro-orm/sql": "7.0.0-dev.220"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@mikro-orm/core": "^6.4
|
|
56
|
+
"@mikro-orm/core": "^6.6.4"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
|
-
"@mikro-orm/core": "7.0.0-dev.
|
|
59
|
+
"@mikro-orm/core": "7.0.0-dev.220"
|
|
60
60
|
}
|
|
61
61
|
}
|