@mikro-orm/entity-generator 7.0.0-dev.3 → 7.0.0-dev.300
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 +11 -4
- package/SourceFile.js +218 -54
- package/package.json +6 -6
- package/tsconfig.build.tsbuildinfo +1 -0
package/SourceFile.js
CHANGED
|
@@ -1,18 +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
3
|
import { POSSIBLE_TYPE_IMPORTS } from './CoreImportsHelper.js';
|
|
4
4
|
/**
|
|
5
5
|
* @see https://github.com/tc39/proposal-regexp-unicode-property-escapes#other-examples
|
|
6
6
|
*/
|
|
7
7
|
export const identifierRegex = /^(?:[$_\p{ID_Start}])(?:[$\u200C\u200D\p{ID_Continue}])*$/u;
|
|
8
|
-
const primitivesAndLibs = [...SCALAR_TYPES, '
|
|
8
|
+
const primitivesAndLibs = [...SCALAR_TYPES, 'unknown', 'object', 'any'];
|
|
9
9
|
export class SourceFile {
|
|
10
10
|
meta;
|
|
11
11
|
namingStrategy;
|
|
12
12
|
platform;
|
|
13
13
|
options;
|
|
14
14
|
coreImports = new Set();
|
|
15
|
+
decoratorImports = new Set();
|
|
15
16
|
entityImports = new Set();
|
|
17
|
+
enumImports = new Map();
|
|
16
18
|
constructor(meta, namingStrategy, platform, options) {
|
|
17
19
|
this.meta = meta;
|
|
18
20
|
this.namingStrategy = namingStrategy;
|
|
@@ -24,24 +26,24 @@ export class SourceFile {
|
|
|
24
26
|
if (this.meta.embeddable || this.meta.collection) {
|
|
25
27
|
if (this.meta.embeddable) {
|
|
26
28
|
const options = this.getEmbeddableDeclOptions();
|
|
27
|
-
ret += `@${this.
|
|
29
|
+
ret += `@${this.referenceDecoratorImport('Embeddable')}(${Utils.hasObjectKeys(options) ? this.serializeObject(options) : ''})\n`;
|
|
28
30
|
}
|
|
29
31
|
else {
|
|
30
32
|
const options = this.getEntityDeclOptions();
|
|
31
|
-
ret += `@${this.
|
|
33
|
+
ret += `@${this.referenceDecoratorImport('Entity')}(${Utils.hasObjectKeys(options) ? this.serializeObject(options) : ''})\n`;
|
|
32
34
|
}
|
|
33
35
|
}
|
|
34
36
|
for (const index of this.meta.indexes) {
|
|
35
37
|
if (index.properties?.length === 1 && typeof this.meta.properties[index.properties[0]] !== 'undefined') {
|
|
36
38
|
continue;
|
|
37
39
|
}
|
|
38
|
-
ret += `@${this.
|
|
40
|
+
ret += `@${this.referenceDecoratorImport('Index')}(${this.serializeObject(this.getIndexOptions(index))})\n`;
|
|
39
41
|
}
|
|
40
42
|
for (const index of this.meta.uniques) {
|
|
41
43
|
if (index.properties?.length === 1 && typeof this.meta.properties[index.properties[0]] !== 'undefined') {
|
|
42
44
|
continue;
|
|
43
45
|
}
|
|
44
|
-
ret += `@${this.
|
|
46
|
+
ret += `@${this.referenceDecoratorImport('Unique')}(${this.serializeObject(this.getUniqueOptions(index))})\n`;
|
|
45
47
|
}
|
|
46
48
|
let classHead = '';
|
|
47
49
|
if (this.meta.className === this.options.customBaseEntityName) {
|
|
@@ -64,7 +66,10 @@ export class SourceFile {
|
|
|
64
66
|
classBody += definition;
|
|
65
67
|
classBody += '\n';
|
|
66
68
|
if (prop.enum) {
|
|
67
|
-
|
|
69
|
+
const def = this.getEnumClassDefinition(prop, 2);
|
|
70
|
+
if (def.length) {
|
|
71
|
+
enumDefinitions.push(def);
|
|
72
|
+
}
|
|
68
73
|
}
|
|
69
74
|
if (prop.eager) {
|
|
70
75
|
eagerProperties.push(prop);
|
|
@@ -93,17 +98,68 @@ export class SourceFile {
|
|
|
93
98
|
}
|
|
94
99
|
return ret;
|
|
95
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Convert index column options to quoted output format.
|
|
103
|
+
*/
|
|
104
|
+
getColumnOptions(columns) {
|
|
105
|
+
if (!columns?.length) {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
return columns.map(col => {
|
|
109
|
+
const colOpt = { name: this.quote(col.name) };
|
|
110
|
+
if (col.sort) {
|
|
111
|
+
colOpt.sort = this.quote(col.sort.toUpperCase());
|
|
112
|
+
}
|
|
113
|
+
if (col.nulls) {
|
|
114
|
+
colOpt.nulls = this.quote(col.nulls.toUpperCase());
|
|
115
|
+
}
|
|
116
|
+
if (col.length != null) {
|
|
117
|
+
colOpt.length = col.length;
|
|
118
|
+
}
|
|
119
|
+
if (col.collation) {
|
|
120
|
+
colOpt.collation = this.quote(col.collation);
|
|
121
|
+
}
|
|
122
|
+
return colOpt;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
96
125
|
getIndexOptions(index, isAtEntityLevel = true) {
|
|
97
126
|
const indexOpt = {};
|
|
98
127
|
if (typeof index.name === 'string') {
|
|
99
128
|
indexOpt.name = this.quote(index.name);
|
|
100
129
|
}
|
|
101
|
-
if (index.expression) {
|
|
130
|
+
if (typeof index.expression === 'string') {
|
|
102
131
|
indexOpt.expression = this.quote(index.expression);
|
|
103
132
|
}
|
|
133
|
+
else if (typeof index.expression === 'function') {
|
|
134
|
+
indexOpt.expression = `${index.expression}`.replace(')=>`', ') => `');
|
|
135
|
+
}
|
|
104
136
|
if (isAtEntityLevel && index.properties) {
|
|
105
137
|
indexOpt.properties = Utils.asArray(index.properties).map(prop => this.quote('' + prop));
|
|
106
138
|
}
|
|
139
|
+
// Index type (e.g., 'fulltext', 'spatial', 'btree', 'hash')
|
|
140
|
+
if (index.type) {
|
|
141
|
+
indexOpt.type = this.quote(index.type);
|
|
142
|
+
}
|
|
143
|
+
// Advanced index options
|
|
144
|
+
const columns = this.getColumnOptions(index.columns);
|
|
145
|
+
if (columns) {
|
|
146
|
+
indexOpt.columns = columns;
|
|
147
|
+
}
|
|
148
|
+
if (index.include) {
|
|
149
|
+
indexOpt.include = Utils.asArray(index.include).map(prop => this.quote('' + prop));
|
|
150
|
+
}
|
|
151
|
+
if (index.fillFactor != null) {
|
|
152
|
+
indexOpt.fillFactor = index.fillFactor;
|
|
153
|
+
}
|
|
154
|
+
if (index.invisible) {
|
|
155
|
+
indexOpt.invisible = true;
|
|
156
|
+
}
|
|
157
|
+
if (index.disabled) {
|
|
158
|
+
indexOpt.disabled = true;
|
|
159
|
+
}
|
|
160
|
+
if (index.clustered) {
|
|
161
|
+
indexOpt.clustered = true;
|
|
162
|
+
}
|
|
107
163
|
return indexOpt;
|
|
108
164
|
}
|
|
109
165
|
getUniqueOptions(index, isAtEntityLevel = true) {
|
|
@@ -111,15 +167,31 @@ export class SourceFile {
|
|
|
111
167
|
if (typeof index.name === 'string') {
|
|
112
168
|
uniqueOpt.name = this.quote(index.name);
|
|
113
169
|
}
|
|
114
|
-
if (index.expression) {
|
|
170
|
+
if (typeof index.expression === 'string') {
|
|
115
171
|
uniqueOpt.expression = this.quote(index.expression);
|
|
116
172
|
}
|
|
173
|
+
else if (typeof index.expression === 'function') {
|
|
174
|
+
uniqueOpt.expression = `${index.expression}`.replace(')=>`', ') => `');
|
|
175
|
+
}
|
|
117
176
|
if (isAtEntityLevel && index.properties) {
|
|
118
177
|
uniqueOpt.properties = Utils.asArray(index.properties).map(prop => this.quote('' + prop));
|
|
119
178
|
}
|
|
120
179
|
if (index.deferMode) {
|
|
121
180
|
uniqueOpt.deferMode = `${this.referenceCoreImport('DeferMode')}.INITIALLY_${index.deferMode.toUpperCase()}`;
|
|
122
181
|
}
|
|
182
|
+
const columns = this.getColumnOptions(index.columns);
|
|
183
|
+
if (columns) {
|
|
184
|
+
uniqueOpt.columns = columns;
|
|
185
|
+
}
|
|
186
|
+
if (index.include) {
|
|
187
|
+
uniqueOpt.include = Utils.asArray(index.include).map(prop => this.quote('' + prop));
|
|
188
|
+
}
|
|
189
|
+
if (index.fillFactor != null) {
|
|
190
|
+
uniqueOpt.fillFactor = index.fillFactor;
|
|
191
|
+
}
|
|
192
|
+
if (index.disabled) {
|
|
193
|
+
uniqueOpt.disabled = true;
|
|
194
|
+
}
|
|
123
195
|
return uniqueOpt;
|
|
124
196
|
}
|
|
125
197
|
generateImports() {
|
|
@@ -134,37 +206,61 @@ export class SourceFile {
|
|
|
134
206
|
return ret;
|
|
135
207
|
}).join(', '))} } from '@mikro-orm/core';`);
|
|
136
208
|
}
|
|
209
|
+
if (this.decoratorImports.size > 0) {
|
|
210
|
+
const type = this.options.decorators;
|
|
211
|
+
imports.add(`import { ${([...this.decoratorImports].sort().map(t => {
|
|
212
|
+
let ret = t;
|
|
213
|
+
if (this.options.coreImportsPrefix) {
|
|
214
|
+
const resolvedIdentifier = `${this.options.coreImportsPrefix}${t}`;
|
|
215
|
+
ret += ` as ${resolvedIdentifier}`;
|
|
216
|
+
}
|
|
217
|
+
return ret;
|
|
218
|
+
}).join(', '))} } from '@mikro-orm/decorators/${type}';`);
|
|
219
|
+
}
|
|
137
220
|
const extension = this.options.esmImport ? '.js' : '';
|
|
138
221
|
const { dir, base } = parse(`${this.options.path ?? '.'}/${this.getBaseName()}`);
|
|
139
222
|
const basePath = relative(dir, this.options.path ?? '.') || '.';
|
|
140
223
|
(this.options.extraImports?.(basePath, base) ?? []).forEach(v => this.entityImports.add(v));
|
|
141
224
|
const entityImports = [...this.entityImports].filter(e => e !== this.meta.className);
|
|
142
|
-
|
|
225
|
+
const importMap = new Map();
|
|
226
|
+
for (const entity of entityImports) {
|
|
143
227
|
const file = this.options.onImport?.(entity, basePath, extension, base) ?? {
|
|
144
228
|
path: `${basePath}/${this.options.fileName(entity)}${extension}`,
|
|
145
229
|
name: entity,
|
|
146
230
|
};
|
|
147
231
|
if (file.path === '') {
|
|
148
232
|
if (file.name === '') {
|
|
149
|
-
|
|
233
|
+
continue;
|
|
150
234
|
}
|
|
151
|
-
|
|
152
|
-
|
|
235
|
+
importMap.set(file.path, `import ${this.quote(file.name)};`);
|
|
236
|
+
continue;
|
|
153
237
|
}
|
|
154
238
|
if (file.name === '') {
|
|
155
|
-
|
|
156
|
-
|
|
239
|
+
importMap.set(file.path, `import * as ${entity} from ${this.quote(file.path)};`);
|
|
240
|
+
continue;
|
|
157
241
|
}
|
|
158
242
|
if (file.name === 'default') {
|
|
159
|
-
|
|
160
|
-
|
|
243
|
+
importMap.set(file.path, `import ${entity} from ${this.quote(file.path)};`);
|
|
244
|
+
continue;
|
|
161
245
|
}
|
|
162
246
|
if (file.name === entity) {
|
|
163
|
-
|
|
164
|
-
|
|
247
|
+
importMap.set(file.path, `import { ${entity} } from ${this.quote(file.path)};`);
|
|
248
|
+
continue;
|
|
165
249
|
}
|
|
166
|
-
|
|
167
|
-
}
|
|
250
|
+
importMap.set(file.path, `import { ${identifierRegex.test(file.name) ? file.name : this.quote(file.name)} as ${entity} } from ${this.quote(file.path)};`);
|
|
251
|
+
}
|
|
252
|
+
if (this.enumImports.size) {
|
|
253
|
+
for (const [name, exports] of this.enumImports.entries()) {
|
|
254
|
+
const file = this.options.onImport?.(name, basePath, extension, base) ?? {
|
|
255
|
+
path: `${basePath}/${this.options.fileName(name)}${extension}`,
|
|
256
|
+
name,
|
|
257
|
+
};
|
|
258
|
+
importMap.set(file.path, `import { ${exports.join(', ')} } from ${this.quote(file.path)};`);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
for (const key of [...importMap.keys()].sort()) {
|
|
262
|
+
imports.add(importMap.get(key));
|
|
263
|
+
}
|
|
168
264
|
return Array.from(imports.values()).join('\n');
|
|
169
265
|
}
|
|
170
266
|
getEntityClass(classBody) {
|
|
@@ -174,8 +270,8 @@ export class SourceFile {
|
|
|
174
270
|
}
|
|
175
271
|
ret += `class ${this.meta.className}`;
|
|
176
272
|
if (this.meta.extends) {
|
|
177
|
-
this.entityImports.add(this.meta.extends);
|
|
178
|
-
ret += ` extends ${this.meta.extends}`;
|
|
273
|
+
this.entityImports.add(Utils.className(this.meta.extends));
|
|
274
|
+
ret += ` extends ${Utils.className(this.meta.extends)}`;
|
|
179
275
|
}
|
|
180
276
|
else if (this.options.useCoreBaseEntity) {
|
|
181
277
|
ret += ` extends ${this.referenceCoreImport('BaseEntity')}`;
|
|
@@ -194,6 +290,7 @@ export class SourceFile {
|
|
|
194
290
|
getPropertyDefinition(prop, padLeft) {
|
|
195
291
|
const padding = ' '.repeat(padLeft);
|
|
196
292
|
const propName = identifierRegex.test(prop.name) ? prop.name : this.quote(prop.name);
|
|
293
|
+
const enumMode = this.options.enumMode;
|
|
197
294
|
let hiddenType = '';
|
|
198
295
|
if (prop.hidden) {
|
|
199
296
|
hiddenType += ` & ${this.referenceCoreImport('Hidden')}`;
|
|
@@ -211,7 +308,11 @@ export class SourceFile {
|
|
|
211
308
|
: (() => {
|
|
212
309
|
if (isScalar) {
|
|
213
310
|
if (prop.enum) {
|
|
214
|
-
|
|
311
|
+
const method = enumMode === 'ts-enum' ? 'getEnumClassName' : 'getEnumTypeName';
|
|
312
|
+
if (prop.nativeEnumName) {
|
|
313
|
+
return this.namingStrategy[method](prop.nativeEnumName, undefined, this.meta.schema);
|
|
314
|
+
}
|
|
315
|
+
return this.namingStrategy[method](prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
215
316
|
}
|
|
216
317
|
breakdownOfIType = this.breakdownOfIType(prop);
|
|
217
318
|
if (typeof breakdownOfIType !== 'undefined') {
|
|
@@ -246,8 +347,14 @@ export class SourceFile {
|
|
|
246
347
|
return `${padding}${ret};\n`;
|
|
247
348
|
}
|
|
248
349
|
if (prop.enum && typeof prop.default === 'string') {
|
|
350
|
+
if (enumMode === 'union-type') {
|
|
351
|
+
return `${padding}${ret} = ${this.quote(prop.default)};\n`;
|
|
352
|
+
}
|
|
353
|
+
const enumClassName = prop.nativeEnumName
|
|
354
|
+
? this.namingStrategy.getEnumClassName(prop.nativeEnumName, undefined, this.meta.schema)
|
|
355
|
+
: this.namingStrategy.getEnumClassName(prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
249
356
|
const enumVal = this.namingStrategy.enumValueToEnumProperty(prop.default, prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
250
|
-
return `${padding}${ret} = ${
|
|
357
|
+
return `${padding}${ret} = ${enumClassName}${identifierRegex.test(enumVal) ? `.${enumVal}` : `[${this.quote(enumVal)}]`};\n`;
|
|
251
358
|
}
|
|
252
359
|
if (prop.fieldNames?.length > 1) {
|
|
253
360
|
// TODO: Composite FKs with default values require additions to default/defaultRaw that are not yet supported.
|
|
@@ -263,15 +370,51 @@ export class SourceFile {
|
|
|
263
370
|
return `${padding}${ret} = ${prop.ref ? this.referenceCoreImport('ref') : this.referenceCoreImport('rel')}(${propType}, ${defaultVal});\n`;
|
|
264
371
|
}
|
|
265
372
|
getEnumClassDefinition(prop, padLeft) {
|
|
373
|
+
const enumMode = this.options.enumMode;
|
|
374
|
+
if (prop.nativeEnumName) {
|
|
375
|
+
const imports = [];
|
|
376
|
+
if (enumMode !== 'union-type') {
|
|
377
|
+
imports.push(prop.runtimeType);
|
|
378
|
+
}
|
|
379
|
+
if (!this.options.inferEntityType && enumMode !== 'ts-enum') {
|
|
380
|
+
const enumTypeName = this.namingStrategy.getEnumTypeName(prop.nativeEnumName, undefined, this.meta.schema);
|
|
381
|
+
imports.push(enumTypeName);
|
|
382
|
+
}
|
|
383
|
+
this.enumImports.set(prop.runtimeType, imports);
|
|
384
|
+
return '';
|
|
385
|
+
}
|
|
266
386
|
const enumClassName = this.namingStrategy.getEnumClassName(prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
387
|
+
const enumTypeName = this.namingStrategy.getEnumTypeName(prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
267
388
|
const padding = ' '.repeat(padLeft);
|
|
268
|
-
let ret = `export enum ${enumClassName} {\n`;
|
|
269
389
|
const enumValues = prop.items;
|
|
390
|
+
if (enumMode === 'union-type') {
|
|
391
|
+
return `export type ${enumTypeName} = ${enumValues.map(item => this.quote(item)).join(' | ')};\n`;
|
|
392
|
+
}
|
|
393
|
+
let ret = '';
|
|
394
|
+
if (enumMode === 'dictionary') {
|
|
395
|
+
ret += `export const ${enumClassName} = {\n`;
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
ret += `export enum ${enumClassName} {\n`;
|
|
399
|
+
}
|
|
270
400
|
for (const enumValue of enumValues) {
|
|
271
401
|
const enumName = this.namingStrategy.enumValueToEnumProperty(enumValue, prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
272
|
-
|
|
402
|
+
if (enumMode === 'dictionary') {
|
|
403
|
+
ret += `${padding}${identifierRegex.test(enumName) ? enumName : this.quote(enumName)}: ${this.quote(enumValue)},\n`;
|
|
404
|
+
}
|
|
405
|
+
else {
|
|
406
|
+
ret += `${padding}${identifierRegex.test(enumName) ? enumName : this.quote(enumName)} = ${this.quote(enumValue)},\n`;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
if (enumMode === 'dictionary') {
|
|
410
|
+
ret += '} as const;\n';
|
|
411
|
+
}
|
|
412
|
+
else {
|
|
413
|
+
ret += '}\n';
|
|
414
|
+
}
|
|
415
|
+
if (enumMode === 'dictionary') {
|
|
416
|
+
ret += `\nexport type ${enumTypeName} = (typeof ${enumClassName})[keyof typeof ${enumClassName}];\n`;
|
|
273
417
|
}
|
|
274
|
-
ret += '}\n';
|
|
275
418
|
return ret;
|
|
276
419
|
}
|
|
277
420
|
serializeObject(options, wordwrap, spaces, level = 0) {
|
|
@@ -301,8 +444,8 @@ export class SourceFile {
|
|
|
301
444
|
}
|
|
302
445
|
getEntityDeclOptions() {
|
|
303
446
|
const options = {};
|
|
304
|
-
if (this.meta.
|
|
305
|
-
options.tableName = this.quote(this.meta.
|
|
447
|
+
if (this.meta.tableName !== this.namingStrategy.classToTableName(this.meta.className)) {
|
|
448
|
+
options.tableName = this.quote(this.meta.tableName);
|
|
306
449
|
}
|
|
307
450
|
if (this.meta.schema && this.meta.schema !== this.platform.getDefaultSchemaName()) {
|
|
308
451
|
options.schema = this.quote(this.meta.schema);
|
|
@@ -311,7 +454,7 @@ export class SourceFile {
|
|
|
311
454
|
options.expression = this.quote(this.meta.expression);
|
|
312
455
|
}
|
|
313
456
|
else if (typeof this.meta.expression === 'function') {
|
|
314
|
-
options.expression =
|
|
457
|
+
options.expression = this.meta.expression.toString();
|
|
315
458
|
}
|
|
316
459
|
if (this.meta.repositoryClass) {
|
|
317
460
|
this.entityImports.add(this.meta.repositoryClass);
|
|
@@ -344,14 +487,14 @@ export class SourceFile {
|
|
|
344
487
|
}
|
|
345
488
|
if (this.meta.discriminatorMap) {
|
|
346
489
|
options.discriminatorMap = Object.fromEntries(Object.entries(this.meta.discriminatorMap)
|
|
347
|
-
.map(([discriminatorValue,
|
|
490
|
+
.map(([discriminatorValue, cls]) => [discriminatorValue, this.quote(Utils.className(cls))]));
|
|
348
491
|
}
|
|
349
492
|
return options;
|
|
350
493
|
}
|
|
351
494
|
getPropertyDecorator(prop, padLeft) {
|
|
352
495
|
const padding = ' '.repeat(padLeft);
|
|
353
496
|
const options = {};
|
|
354
|
-
let decorator = `@${this.
|
|
497
|
+
let decorator = `@${this.referenceDecoratorImport(this.getDecoratorType(prop))}`;
|
|
355
498
|
if (prop.kind === ReferenceKind.MANY_TO_MANY) {
|
|
356
499
|
this.getManyToManyDecoratorOptions(options, prop);
|
|
357
500
|
}
|
|
@@ -372,7 +515,7 @@ export class SourceFile {
|
|
|
372
515
|
decorator = [...indexes.sort(), decorator].map(d => padding + d).join('\n');
|
|
373
516
|
const decoratorArgs = [];
|
|
374
517
|
if (prop.formula) {
|
|
375
|
-
decoratorArgs.push(
|
|
518
|
+
decoratorArgs.push(prop.formula.toString());
|
|
376
519
|
}
|
|
377
520
|
if (Utils.hasObjectKeys(options)) {
|
|
378
521
|
decoratorArgs.push(`${this.serializeObject(options)}`);
|
|
@@ -401,26 +544,26 @@ export class SourceFile {
|
|
|
401
544
|
let propIndexIsNonTrivialIndex = false;
|
|
402
545
|
const nonTrivialIndexes = this.meta.indexes.filter(i => i.properties?.length === 1 && i.properties[0] === prop.name);
|
|
403
546
|
for (const i of nonTrivialIndexes) {
|
|
404
|
-
ret.push(`@${this.
|
|
547
|
+
ret.push(`@${this.referenceDecoratorImport('Index')}(${this.serializeObject(this.getIndexOptions(i, false))})`);
|
|
405
548
|
if (prop.index === i.name) {
|
|
406
549
|
propIndexIsNonTrivialIndex = true;
|
|
407
550
|
delete options.index;
|
|
408
551
|
}
|
|
409
552
|
}
|
|
410
553
|
if (prop.index && !options.index && !propIndexIsNonTrivialIndex) {
|
|
411
|
-
ret.push(`@${this.
|
|
554
|
+
ret.push(`@${this.referenceDecoratorImport('Index')}(${typeof prop.index === 'string' ? `{ name: ${this.quote(prop.index)} }` : ''})`);
|
|
412
555
|
}
|
|
413
556
|
let propIndexIsNonTrivialUnique = false;
|
|
414
557
|
const nonTrivialUnique = this.meta.uniques.filter(i => i.properties?.length === 1 && i.properties[0] === prop.name);
|
|
415
558
|
for (const i of nonTrivialUnique) {
|
|
416
|
-
ret.push(`@${this.
|
|
559
|
+
ret.push(`@${this.referenceDecoratorImport('Unique')}(${this.serializeObject(this.getUniqueOptions(i, false))})`);
|
|
417
560
|
if (prop.unique === i.name) {
|
|
418
561
|
propIndexIsNonTrivialUnique = true;
|
|
419
562
|
delete options.unique;
|
|
420
563
|
}
|
|
421
564
|
}
|
|
422
565
|
if (prop.unique && !options.unique && !propIndexIsNonTrivialUnique) {
|
|
423
|
-
ret.push(`@${this.
|
|
566
|
+
ret.push(`@${this.referenceDecoratorImport('Unique')}(${typeof prop.unique === 'string' ? `{ name: ${this.quote(prop.unique)} }` : ''})`);
|
|
424
567
|
}
|
|
425
568
|
return ret;
|
|
426
569
|
}
|
|
@@ -431,7 +574,7 @@ export class SourceFile {
|
|
|
431
574
|
if (prop.primary && (prop.enum || !(typeof prop.kind === 'undefined' || prop.kind === ReferenceKind.SCALAR))) {
|
|
432
575
|
options.primary = true;
|
|
433
576
|
}
|
|
434
|
-
['persist', 'hydrate'
|
|
577
|
+
['persist', 'hydrate']
|
|
435
578
|
.filter(key => prop[key] === false)
|
|
436
579
|
.forEach(key => options[key] = false);
|
|
437
580
|
['onCreate', 'onUpdate', 'serializer']
|
|
@@ -530,12 +673,23 @@ export class SourceFile {
|
|
|
530
673
|
this.propTypeBreakdowns.set(prop, r);
|
|
531
674
|
return r;
|
|
532
675
|
}
|
|
533
|
-
getScalarPropertyDecoratorOptions(options, prop) {
|
|
676
|
+
getScalarPropertyDecoratorOptions(options, prop, quote = true) {
|
|
534
677
|
if (prop.fieldNames[0] !== this.namingStrategy.propertyToColumnName(prop.name)) {
|
|
535
678
|
options.fieldName = this.quote(prop.fieldNames[0]);
|
|
536
679
|
}
|
|
537
680
|
if (prop.enum) {
|
|
538
|
-
options.
|
|
681
|
+
if (this.options.enumMode === 'union-type') {
|
|
682
|
+
options.items = `[${prop.items.map(item => this.quote(item)).join(', ')}]`;
|
|
683
|
+
}
|
|
684
|
+
else if (prop.nativeEnumName) {
|
|
685
|
+
const enumClassName = this.namingStrategy.getEnumClassName(prop.nativeEnumName, undefined, this.meta.schema);
|
|
686
|
+
options.items = `() => ${enumClassName}`;
|
|
687
|
+
options.nativeEnumName = this.quote(prop.nativeEnumName);
|
|
688
|
+
}
|
|
689
|
+
else {
|
|
690
|
+
const enumClassName = this.namingStrategy.getEnumClassName(prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
691
|
+
options.items = `() => ${enumClassName}`;
|
|
692
|
+
}
|
|
539
693
|
}
|
|
540
694
|
// For enum properties, we don't need a column type
|
|
541
695
|
// or the property length or other information in the decorator.
|
|
@@ -564,7 +718,7 @@ export class SourceFile {
|
|
|
564
718
|
return ((useDefault && !hasUsableNullDefault) || (prop.optional && !prop.nullable));
|
|
565
719
|
})() // also when there is the "| Opt" type modifier (because reflect-metadata can't extract the base)
|
|
566
720
|
) {
|
|
567
|
-
options.type = this.quote(prop.type);
|
|
721
|
+
options.type = quote ? this.quote(prop.type) : prop.type;
|
|
568
722
|
}
|
|
569
723
|
}
|
|
570
724
|
const columnTypeFromMappedRuntimeType = mappedRuntimeType.getColumnType({ ...prop, autoincrement: false }, this.platform);
|
|
@@ -591,7 +745,7 @@ export class SourceFile {
|
|
|
591
745
|
assign('length');
|
|
592
746
|
}
|
|
593
747
|
// those are already included in the `columnType` in most cases, and when that option is present, they would be ignored anyway
|
|
594
|
-
/* v8 ignore next
|
|
748
|
+
/* v8 ignore next */
|
|
595
749
|
if (mappedColumnType instanceof DecimalType && !options.columnType) {
|
|
596
750
|
assign('precision');
|
|
597
751
|
assign('scale');
|
|
@@ -606,10 +760,8 @@ export class SourceFile {
|
|
|
606
760
|
options.autoincrement = true;
|
|
607
761
|
}
|
|
608
762
|
}
|
|
609
|
-
else {
|
|
610
|
-
|
|
611
|
-
options.autoincrement = false;
|
|
612
|
-
}
|
|
763
|
+
else if (prop.primary && this.platform.isNumericColumn(mappedColumnType) && this.meta.getPrimaryProps().length === 1) {
|
|
764
|
+
options.autoincrement = false;
|
|
613
765
|
}
|
|
614
766
|
if (prop.generated) {
|
|
615
767
|
options.generated = typeof prop.generated === 'string' ? this.quote(prop.generated) : `${prop.generated}`;
|
|
@@ -618,16 +770,19 @@ export class SourceFile {
|
|
|
618
770
|
getManyToManyDecoratorOptions(options, prop) {
|
|
619
771
|
this.entityImports.add(prop.type);
|
|
620
772
|
options.entity = `() => ${prop.type}`;
|
|
773
|
+
if (prop.orderBy) {
|
|
774
|
+
options.orderBy = inspect(prop.orderBy);
|
|
775
|
+
}
|
|
621
776
|
if (prop.mappedBy) {
|
|
622
777
|
options.mappedBy = this.quote(prop.mappedBy);
|
|
623
778
|
return;
|
|
624
779
|
}
|
|
625
|
-
if (prop.pivotTable !== this.namingStrategy.joinTableName(this.meta.collection, prop.type, prop.name)) {
|
|
780
|
+
if (prop.pivotTable !== this.namingStrategy.joinTableName(this.meta.collection, prop.type, prop.name, this.meta.tableName)) {
|
|
626
781
|
options.pivotTable = this.quote(prop.pivotTable);
|
|
627
782
|
}
|
|
628
|
-
if (prop.pivotEntity && prop.pivotEntity !== prop.pivotTable) {
|
|
629
|
-
this.entityImports.add(prop.pivotEntity);
|
|
630
|
-
options.pivotEntity = `() => ${prop.pivotEntity}`;
|
|
783
|
+
if (prop.pivotEntity && Utils.className(prop.pivotEntity) !== prop.pivotTable) {
|
|
784
|
+
this.entityImports.add(Utils.className(prop.pivotEntity));
|
|
785
|
+
options.pivotEntity = `() => ${Utils.className(prop.pivotEntity)}`;
|
|
631
786
|
}
|
|
632
787
|
if (prop.joinColumns.length === 1) {
|
|
633
788
|
options.joinColumn = this.quote(prop.joinColumns[0]);
|
|
@@ -652,6 +807,9 @@ export class SourceFile {
|
|
|
652
807
|
this.entityImports.add(prop.type);
|
|
653
808
|
options.entity = `() => ${prop.type}`;
|
|
654
809
|
options.mappedBy = this.quote(prop.mappedBy);
|
|
810
|
+
if (prop.orderBy) {
|
|
811
|
+
options.orderBy = inspect(prop.orderBy);
|
|
812
|
+
}
|
|
655
813
|
}
|
|
656
814
|
getEmbeddedPropertyDeclarationOptions(options, prop) {
|
|
657
815
|
this.entityImports.add(prop.type);
|
|
@@ -659,7 +817,7 @@ export class SourceFile {
|
|
|
659
817
|
if (prop.array) {
|
|
660
818
|
options.array = true;
|
|
661
819
|
}
|
|
662
|
-
if (prop.object) {
|
|
820
|
+
if (prop.object && !prop.array) {
|
|
663
821
|
options.object = true;
|
|
664
822
|
}
|
|
665
823
|
if (prop.prefix === false || typeof prop.prefix === 'string') {
|
|
@@ -692,10 +850,10 @@ export class SourceFile {
|
|
|
692
850
|
if (prop.ownColumns && prop.ownColumns.length !== prop.fieldNames.length) {
|
|
693
851
|
options.referencedColumnNames = prop.referencedColumnNames.map(fieldName => this.quote(fieldName));
|
|
694
852
|
}
|
|
695
|
-
if (
|
|
853
|
+
if (prop.updateRule) {
|
|
696
854
|
options.updateRule = this.quote(prop.updateRule);
|
|
697
855
|
}
|
|
698
|
-
if (
|
|
856
|
+
if (prop.deleteRule) {
|
|
699
857
|
options.deleteRule = this.quote(prop.deleteRule);
|
|
700
858
|
}
|
|
701
859
|
if (prop.primary) {
|
|
@@ -742,4 +900,10 @@ export class SourceFile {
|
|
|
742
900
|
? `${this.options.coreImportsPrefix}${identifier}`
|
|
743
901
|
: identifier;
|
|
744
902
|
}
|
|
903
|
+
referenceDecoratorImport(identifier) {
|
|
904
|
+
this.decoratorImports.add(identifier);
|
|
905
|
+
return this.options.coreImportsPrefix
|
|
906
|
+
? `${this.options.coreImportsPrefix}${identifier}`
|
|
907
|
+
: identifier;
|
|
908
|
+
}
|
|
745
909
|
}
|
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.300",
|
|
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.300"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@mikro-orm/core": "^6.
|
|
56
|
+
"@mikro-orm/core": "^6.6.8"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
|
-
"@mikro-orm/core": "7.0.0-dev.
|
|
59
|
+
"@mikro-orm/core": "7.0.0-dev.300"
|
|
60
60
|
}
|
|
61
61
|
}
|