@mikro-orm/entity-generator 6.3.5-dev.1 → 6.3.5-dev.11

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/SourceFile.d.ts CHANGED
@@ -12,8 +12,8 @@ export declare class SourceFile {
12
12
  protected readonly entityImports: Set<string>;
13
13
  constructor(meta: EntityMetadata, namingStrategy: NamingStrategy, platform: Platform, options: GenerateOptions);
14
14
  generate(): string;
15
- protected getIndexOptions(index: EntityMetadata['indexes'][number]): IndexOptions<Dictionary>;
16
- protected getUniqueOptions(index: EntityMetadata['uniques'][number]): UniqueOptions<Dictionary>;
15
+ protected getIndexOptions(index: EntityMetadata['indexes'][number], isAtEntityLevel?: boolean): IndexOptions<Dictionary>;
16
+ protected getUniqueOptions(index: EntityMetadata['uniques'][number], isAtEntityLevel?: boolean): UniqueOptions<Dictionary>;
17
17
  protected generateImports(): string;
18
18
  protected getEntityClass(classBody: string): string;
19
19
  getBaseName(extension?: string): string;
package/SourceFile.js CHANGED
@@ -34,12 +34,18 @@ class SourceFile {
34
34
  ret += `@${this.referenceCoreImport('Entity')}(${core_1.Utils.hasObjectKeys(options) ? this.serializeObject(options) : ''})\n`;
35
35
  }
36
36
  }
37
- this.meta.indexes.forEach(index => {
37
+ for (const index of this.meta.indexes) {
38
+ if (index.properties?.length === 1 && typeof this.meta.properties[index.properties[0]] !== 'undefined') {
39
+ continue;
40
+ }
38
41
  ret += `@${this.referenceCoreImport('Index')}(${this.serializeObject(this.getIndexOptions(index))})\n`;
39
- });
40
- this.meta.uniques.forEach(index => {
42
+ }
43
+ for (const index of this.meta.uniques) {
44
+ if (index.properties?.length === 1 && typeof this.meta.properties[index.properties[0]] !== 'undefined') {
45
+ continue;
46
+ }
41
47
  ret += `@${this.referenceCoreImport('Unique')}(${this.serializeObject(this.getUniqueOptions(index))})\n`;
42
- });
48
+ }
43
49
  let classHead = '';
44
50
  if (this.meta.className === this.options.customBaseEntityName) {
45
51
  const defineConfigTypeSettings = {};
@@ -90,7 +96,7 @@ class SourceFile {
90
96
  }
91
97
  return ret;
92
98
  }
93
- getIndexOptions(index) {
99
+ getIndexOptions(index, isAtEntityLevel = true) {
94
100
  const indexOpt = {};
95
101
  if (typeof index.name === 'string') {
96
102
  indexOpt.name = this.quote(index.name);
@@ -98,12 +104,12 @@ class SourceFile {
98
104
  if (index.expression) {
99
105
  indexOpt.expression = this.quote(index.expression);
100
106
  }
101
- if (index.properties) {
107
+ if (isAtEntityLevel && index.properties) {
102
108
  indexOpt.properties = core_1.Utils.asArray(index.properties).map(prop => this.quote('' + prop));
103
109
  }
104
110
  return indexOpt;
105
111
  }
106
- getUniqueOptions(index) {
112
+ getUniqueOptions(index, isAtEntityLevel = true) {
107
113
  const uniqueOpt = {};
108
114
  if (typeof index.name === 'string') {
109
115
  uniqueOpt.name = this.quote(index.name);
@@ -111,9 +117,12 @@ class SourceFile {
111
117
  if (index.expression) {
112
118
  uniqueOpt.expression = this.quote(index.expression);
113
119
  }
114
- if (index.properties) {
120
+ if (isAtEntityLevel && index.properties) {
115
121
  uniqueOpt.properties = core_1.Utils.asArray(index.properties).map(prop => this.quote('' + prop));
116
122
  }
123
+ if (index.deferMode) {
124
+ uniqueOpt.deferMode = `${this.referenceCoreImport('DeferMode')}.INITIALLY_${index.deferMode.toUpperCase()}`;
125
+ }
117
126
  return uniqueOpt;
118
127
  }
119
128
  generateImports() {
@@ -375,16 +384,6 @@ class SourceFile {
375
384
  return `${decorator}(${decoratorArgs.join(', ')})\n`;
376
385
  }
377
386
  getPropertyIndexes(prop, options) {
378
- if (prop.kind === core_1.ReferenceKind.SCALAR) {
379
- const ret = [];
380
- if (prop.index) {
381
- ret.push(`@${this.referenceCoreImport('Index')}(${typeof prop.index === 'string' ? `{ name: ${this.quote(prop.index)} }` : ''})`);
382
- }
383
- if (prop.unique) {
384
- ret.push(`@${this.referenceCoreImport('Unique')}(${typeof prop.unique === 'string' ? `{ name: ${this.quote(prop.unique)} }` : ''})`);
385
- }
386
- return ret;
387
- }
388
387
  const processIndex = (type) => {
389
388
  const propType = prop[type];
390
389
  if (!propType) {
@@ -402,7 +401,32 @@ class SourceFile {
402
401
  };
403
402
  processIndex('index');
404
403
  processIndex('unique');
405
- return [];
404
+ const ret = [];
405
+ let propIndexIsNonTrivialIndex = false;
406
+ const nonTrivialIndexes = this.meta.indexes.filter(i => i.properties?.length === 1 && i.properties[0] === prop.name);
407
+ for (const i of nonTrivialIndexes) {
408
+ ret.push(`@${this.referenceCoreImport('Index')}(${this.serializeObject(this.getIndexOptions(i, false))})`);
409
+ if (prop.index === i.name) {
410
+ propIndexIsNonTrivialIndex = true;
411
+ delete options.index;
412
+ }
413
+ }
414
+ if (prop.index && !options.index && !propIndexIsNonTrivialIndex) {
415
+ ret.push(`@${this.referenceCoreImport('Index')}(${typeof prop.index === 'string' ? `{ name: ${this.quote(prop.index)} }` : ''})`);
416
+ }
417
+ let propIndexIsNonTrivialUnique = false;
418
+ const nonTrivialUnique = this.meta.uniques.filter(i => i.properties?.length === 1 && i.properties[0] === prop.name);
419
+ for (const i of nonTrivialUnique) {
420
+ ret.push(`@${this.referenceCoreImport('Unique')}(${this.serializeObject(this.getUniqueOptions(i, false))})`);
421
+ if (prop.unique === i.name) {
422
+ propIndexIsNonTrivialUnique = true;
423
+ delete options.unique;
424
+ }
425
+ }
426
+ if (prop.unique && !options.unique && !propIndexIsNonTrivialUnique) {
427
+ ret.push(`@${this.referenceCoreImport('Unique')}(${typeof prop.unique === 'string' ? `{ name: ${this.quote(prop.unique)} }` : ''})`);
428
+ }
429
+ return ret;
406
430
  }
407
431
  getCommonDecoratorOptions(options, prop) {
408
432
  if (prop.nullable && !prop.mappedBy) {
@@ -432,24 +456,29 @@ class SourceFile {
432
456
  if (typeof prop.comment === 'string') {
433
457
  options.comment = this.quote(prop.comment);
434
458
  }
435
- if (typeof prop.fieldNames !== 'undefined' && prop.fieldNames.length > 1) {
436
- // TODO: Composite FKs with default values require additions to default/defaultRaw that are not yet supported.
437
- return;
459
+ // TODO: Composite FKs with default values require additions to default/defaultRaw that are not yet supported.
460
+ if (prop.fieldNames?.length <= 1) {
461
+ if (typeof prop.defaultRaw !== 'undefined' && prop.defaultRaw !== 'null' &&
462
+ prop.defaultRaw !== (typeof prop.default === 'string' ? this.quote(prop.default) : `${prop.default}`)) {
463
+ options.defaultRaw = `\`${prop.defaultRaw}\``;
464
+ }
465
+ else if (prop.default != null && (prop.ref || (!prop.enum && (typeof prop.kind === 'undefined' || prop.kind === core_1.ReferenceKind.SCALAR) && (() => {
466
+ const mappedDeclaredType = this.platform.getMappedType(prop.type);
467
+ const mappedRawType = (prop.customTypes?.[0] ?? ((prop.type !== 'unknown' && mappedDeclaredType instanceof core_1.UnknownType)
468
+ ? this.platform.getMappedType(prop.columnTypes[0])
469
+ : mappedDeclaredType));
470
+ const rawType = mappedRawType.runtimeType;
471
+ const serializedType = (prop.customType ?? mappedRawType).runtimeType;
472
+ return prop.runtimeType !== rawType || rawType !== serializedType;
473
+ })()))) {
474
+ options.default = typeof prop.default === 'string' ? this.quote(prop.default) : prop.default;
475
+ }
476
+ }
477
+ if (typeof prop.extra === 'string') {
478
+ options.extra = this.quote(prop.extra);
438
479
  }
439
- if (typeof prop.defaultRaw !== 'undefined' && prop.defaultRaw !== 'null' &&
440
- prop.defaultRaw !== (typeof prop.default === 'string' ? this.quote(prop.default) : `${prop.default}`)) {
441
- options.defaultRaw = `\`${prop.defaultRaw}\``;
442
- }
443
- else if (prop.default != null && (prop.ref || (!prop.enum && (typeof prop.kind === 'undefined' || prop.kind === core_1.ReferenceKind.SCALAR) && (() => {
444
- const mappedDeclaredType = this.platform.getMappedType(prop.type);
445
- const mappedRawType = (prop.customTypes?.[0] ?? ((prop.type !== 'unknown' && mappedDeclaredType instanceof core_1.UnknownType)
446
- ? this.platform.getMappedType(prop.columnTypes[0])
447
- : mappedDeclaredType));
448
- const rawType = mappedRawType.runtimeType;
449
- const serializedType = (prop.customType ?? mappedRawType).runtimeType;
450
- return prop.runtimeType !== rawType || rawType !== serializedType;
451
- })()))) {
452
- options.default = typeof prop.default === 'string' ? this.quote(prop.default) : prop.default;
480
+ if (prop.deferMode) {
481
+ options.deferMode = `${this.referenceCoreImport('DeferMode')}.INITIALLY_${prop.deferMode.toUpperCase()}`;
453
482
  }
454
483
  }
455
484
  getScalarPropertyDecoratorOptions(options, prop) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/entity-generator",
3
- "version": "6.3.5-dev.1",
3
+ "version": "6.3.5-dev.11",
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.3.5-dev.1",
61
+ "@mikro-orm/knex": "6.3.5-dev.11",
62
62
  "fs-extra": "11.2.0"
63
63
  },
64
64
  "devDependencies": {
65
65
  "@mikro-orm/core": "^6.3.4"
66
66
  },
67
67
  "peerDependencies": {
68
- "@mikro-orm/core": "6.3.5-dev.1"
68
+ "@mikro-orm/core": "6.3.5-dev.11"
69
69
  }
70
70
  }