@mikro-orm/entity-generator 6.5.10-dev.18 → 6.5.10-dev.19

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.
@@ -36,31 +36,40 @@ class EntityGenerator {
36
36
  const metadata = await this.getEntityMetadata(schema, options);
37
37
  const defaultPath = `${this.config.get('baseDir')}/generated-entities`;
38
38
  const baseDir = core_1.Utils.normalizePath(options.path ?? defaultPath);
39
+ this.sources.length = 0;
40
+ if (options.entitySchema) {
41
+ options.entityDefinition = 'entitySchema';
42
+ }
43
+ const map = {
44
+ defineEntity: DefineEntitySourceFile_1.DefineEntitySourceFile,
45
+ entitySchema: EntitySchemaSourceFile_1.EntitySchemaSourceFile,
46
+ decorators: SourceFile_1.SourceFile,
47
+ };
48
+ if (options.entityDefinition !== 'decorators') {
49
+ options.scalarTypeInDecorator = true;
50
+ }
39
51
  for (const meta of metadata) {
40
- if (!meta.pivotTable || options.outputPurePivotTables || this.referencedEntities.has(meta)) {
41
- if (options.defineEntity) {
42
- this.sources.push(new DefineEntitySourceFile_1.DefineEntitySourceFile(meta, this.namingStrategy, this.platform, { ...options, scalarTypeInDecorator: true }));
43
- }
44
- else if (options.entitySchema) {
45
- this.sources.push(new EntitySchemaSourceFile_1.EntitySchemaSourceFile(meta, this.namingStrategy, this.platform, { ...options, scalarTypeInDecorator: true }));
46
- }
47
- else {
48
- this.sources.push(new SourceFile_1.SourceFile(meta, this.namingStrategy, this.platform, options));
49
- }
52
+ if (meta.pivotTable && !options.outputPurePivotTables && !this.referencedEntities.has(meta)) {
53
+ continue;
50
54
  }
55
+ this.sources.push(new map[options.entityDefinition](meta, this.namingStrategy, this.platform, options));
51
56
  }
57
+ const files = this.sources.map(file => [file.getBaseName(), file.generate()]);
52
58
  if (options.save) {
53
59
  await (0, fs_extra_1.ensureDir)(baseDir);
54
- await Promise.all(this.sources.map(async (file) => {
55
- const fileName = file.getBaseName();
56
- const fileDir = (0, node_path_1.dirname)(fileName);
57
- if (fileDir !== '.') {
58
- await (0, fs_extra_1.ensureDir)((0, node_path_1.join)(baseDir, fileDir));
59
- }
60
- return (0, fs_extra_1.writeFile)((0, node_path_1.join)(baseDir, fileName), file.generate(), { flush: true });
61
- }));
60
+ const promises = [];
61
+ for (const [fileName, data] of files) {
62
+ promises.push((async () => {
63
+ const fileDir = (0, node_path_1.dirname)(fileName);
64
+ if (fileDir !== '.') {
65
+ await (0, fs_extra_1.ensureDir)((0, node_path_1.join)(baseDir, fileDir));
66
+ }
67
+ await (0, fs_extra_1.writeFile)((0, node_path_1.join)(baseDir, fileName), data, { flush: true });
68
+ })());
69
+ }
70
+ await Promise.all(promises);
62
71
  }
63
- return this.sources.map(file => file.generate());
72
+ return files.map(([, data]) => data);
64
73
  }
65
74
  async getEntityMetadata(schema, options) {
66
75
  const metadata = schema.getTables()
package/SourceFile.js CHANGED
@@ -204,6 +204,7 @@ class SourceFile {
204
204
  getPropertyDefinition(prop, padLeft) {
205
205
  const padding = ' '.repeat(padLeft);
206
206
  const propName = exports.identifierRegex.test(prop.name) ? prop.name : this.quote(prop.name);
207
+ const enumMode = this.options.enumMode;
207
208
  let hiddenType = '';
208
209
  if (prop.hidden) {
209
210
  hiddenType += ` & ${this.referenceCoreImport('Hidden')}`;
@@ -221,7 +222,8 @@ class SourceFile {
221
222
  : (() => {
222
223
  if (isScalar) {
223
224
  if (prop.enum) {
224
- return prop.runtimeType;
225
+ const method = enumMode === 'ts-enum' ? 'getEnumClassName' : 'getEnumTypeName';
226
+ return this.namingStrategy[method](prop.fieldNames[0], this.meta.collection, this.meta.schema);
225
227
  }
226
228
  breakdownOfIType = this.breakdownOfIType(prop);
227
229
  if (typeof breakdownOfIType !== 'undefined') {
@@ -256,8 +258,13 @@ class SourceFile {
256
258
  return `${padding}${ret};\n`;
257
259
  }
258
260
  if (prop.enum && typeof prop.default === 'string') {
261
+ const method = enumMode === 'union-type' ? 'getEnumTypeName' : 'getEnumClassName';
262
+ const enumClassName = this.namingStrategy[method](prop.fieldNames[0], this.meta.collection, this.meta.schema);
263
+ if (enumMode === 'union-type') {
264
+ return `${padding}${ret} = ${this.quote(prop.default)};\n`;
265
+ }
259
266
  const enumVal = this.namingStrategy.enumValueToEnumProperty(prop.default, prop.fieldNames[0], this.meta.collection, this.meta.schema);
260
- return `${padding}${ret} = ${propType}${exports.identifierRegex.test(enumVal) ? `.${enumVal}` : `[${this.quote(enumVal)}]`};\n`;
267
+ return `${padding}${ret} = ${enumClassName}${exports.identifierRegex.test(enumVal) ? `.${enumVal}` : `[${this.quote(enumVal)}]`};\n`;
261
268
  }
262
269
  if (prop.fieldNames?.length > 1) {
263
270
  // TODO: Composite FKs with default values require additions to default/defaultRaw that are not yet supported.
@@ -274,14 +281,38 @@ class SourceFile {
274
281
  }
275
282
  getEnumClassDefinition(prop, padLeft) {
276
283
  const enumClassName = this.namingStrategy.getEnumClassName(prop.fieldNames[0], this.meta.collection, this.meta.schema);
284
+ const enumTypeName = this.namingStrategy.getEnumTypeName(prop.fieldNames[0], this.meta.collection, this.meta.schema);
277
285
  const padding = ' '.repeat(padLeft);
278
- let ret = `export enum ${enumClassName} {\n`;
286
+ const enumMode = this.options.enumMode;
279
287
  const enumValues = prop.items;
288
+ if (enumMode === 'union-type') {
289
+ return `export type ${enumTypeName} = ${enumValues.map(item => this.quote(item)).join(' | ')};\n`;
290
+ }
291
+ let ret = '';
292
+ if (enumMode === 'dictionary') {
293
+ ret += `export const ${enumClassName} = {\n`;
294
+ }
295
+ else {
296
+ ret += `export enum ${enumClassName} {\n`;
297
+ }
280
298
  for (const enumValue of enumValues) {
281
299
  const enumName = this.namingStrategy.enumValueToEnumProperty(enumValue, prop.fieldNames[0], this.meta.collection, this.meta.schema);
282
- ret += `${padding}${exports.identifierRegex.test(enumName) ? enumName : this.quote(enumName)} = ${this.quote(enumValue)},\n`;
300
+ if (enumMode === 'dictionary') {
301
+ ret += `${padding}${exports.identifierRegex.test(enumName) ? enumName : this.quote(enumName)}: ${this.quote(enumValue)},\n`;
302
+ }
303
+ else {
304
+ ret += `${padding}${exports.identifierRegex.test(enumName) ? enumName : this.quote(enumName)} = ${this.quote(enumValue)},\n`;
305
+ }
306
+ }
307
+ if (enumMode === 'dictionary') {
308
+ ret += '} as const;\n';
309
+ }
310
+ else {
311
+ ret += '}\n';
312
+ }
313
+ if (enumMode === 'dictionary') {
314
+ ret += `\nexport type ${enumTypeName} = (typeof ${enumClassName})[keyof typeof ${enumClassName}];\n`;
283
315
  }
284
- ret += '}\n';
285
316
  return ret;
286
317
  }
287
318
  serializeObject(options, wordwrap, spaces, level = 0) {
@@ -545,7 +576,13 @@ class SourceFile {
545
576
  options.fieldName = this.quote(prop.fieldNames[0]);
546
577
  }
547
578
  if (prop.enum) {
548
- options.items = `() => ${prop.runtimeType}`;
579
+ if (this.options.enumMode === 'union-type') {
580
+ options.items = `[${prop.items.map(item => this.quote(item)).join(', ')}]`;
581
+ }
582
+ else {
583
+ const enumClassName = this.namingStrategy.getEnumClassName(prop.fieldNames[0], this.meta.collection, this.meta.schema);
584
+ options.items = `() => ${enumClassName}`;
585
+ }
549
586
  }
550
587
  // For enum properties, we don't need a column type
551
588
  // or the property length or other information in the decorator.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/entity-generator",
3
- "version": "6.5.10-dev.18",
3
+ "version": "6.5.10-dev.19",
4
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
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -58,13 +58,13 @@
58
58
  "access": "public"
59
59
  },
60
60
  "dependencies": {
61
- "@mikro-orm/knex": "6.5.10-dev.18",
61
+ "@mikro-orm/knex": "6.5.10-dev.19",
62
62
  "fs-extra": "11.3.2"
63
63
  },
64
64
  "devDependencies": {
65
65
  "@mikro-orm/core": "^6.5.9"
66
66
  },
67
67
  "peerDependencies": {
68
- "@mikro-orm/core": "6.5.10-dev.18"
68
+ "@mikro-orm/core": "6.5.10-dev.19"
69
69
  }
70
70
  }