@mikro-orm/core 7.0.0-dev.178 → 7.0.0-dev.179

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.
@@ -414,7 +414,7 @@ declare const propertyBuilders: {
414
414
  interval: () => UniversalPropertyOptionsBuilder<string, EmptyOptions, IncludeKeysForProperty>;
415
415
  unknown: () => UniversalPropertyOptionsBuilder<{}, EmptyOptions, IncludeKeysForProperty>;
416
416
  };
417
- export interface EntityMetadataWithProperties<TName extends string, TTableName extends string, TProperties extends Record<string, any>, TPK extends (keyof TProperties)[] | undefined = undefined, TBase = never> extends Omit<Partial<EntityMetadata<InferEntityFromProperties<TProperties, TPK, TBase>>>, 'properties' | 'extends' | 'primaryKeys' | 'hooks' | 'discriminatorColumn' | 'versionProperty' | 'concurrencyCheckKeys' | 'serializedPrimaryKey' | 'indexes' | 'uniques'> {
417
+ export interface EntityMetadataWithProperties<TName extends string, TTableName extends string, TProperties extends Record<string, any>, TPK extends (keyof TProperties)[] | undefined = undefined, TBase = never> extends Omit<Partial<EntityMetadata<InferEntityFromProperties<TProperties, TPK, TBase>>>, 'properties' | 'extends' | 'primaryKeys' | 'hooks' | 'discriminatorColumn' | 'versionProperty' | 'concurrencyCheckKeys' | 'serializedPrimaryKey' | 'indexes' | 'uniques' | 'repository'> {
418
418
  name: TName;
419
419
  tableName?: TTableName;
420
420
  extends?: {
@@ -423,6 +423,7 @@ export interface EntityMetadataWithProperties<TName extends string, TTableName e
423
423
  properties: TProperties | ((properties: typeof propertyBuilders) => TProperties);
424
424
  primaryKeys?: TPK & InferPrimaryKey<TProperties>[];
425
425
  hooks?: DefineEntityHooks<InferEntityFromProperties<TProperties, TPK, TBase>>;
426
+ repository?: (() => Constructor) | (() => unknown);
426
427
  discriminatorColumn?: keyof TProperties;
427
428
  versionProperty?: keyof TProperties;
428
429
  concurrencyCheckKeys?: Set<keyof TProperties>;
@@ -34,6 +34,7 @@ export declare class MetadataDiscovery {
34
34
  private initManyToOneFieldName;
35
35
  private initManyToManyFieldName;
36
36
  private initManyToManyFields;
37
+ private isExplicitTableName;
37
38
  private initManyToOneFields;
38
39
  private initOneToManyFields;
39
40
  private processEntity;
@@ -383,13 +383,13 @@ export class MetadataDiscovery {
383
383
  prop.fieldNames = this.initManyToManyFieldName(prop, prop.name);
384
384
  }
385
385
  }
386
- initManyToOneFieldName(prop, name) {
386
+ initManyToOneFieldName(prop, name, tableName) {
387
387
  const meta2 = prop.targetMeta;
388
388
  const ret = [];
389
389
  for (const primaryKey of meta2.primaryKeys) {
390
390
  this.initFieldName(meta2.properties[primaryKey]);
391
391
  for (const fieldName of meta2.properties[primaryKey].fieldNames) {
392
- ret.push(this.namingStrategy.joinKeyColumnName(name, fieldName, meta2.compositePK));
392
+ ret.push(this.namingStrategy.joinKeyColumnName(name, fieldName, meta2.compositePK, tableName));
393
393
  }
394
394
  }
395
395
  return ret;
@@ -433,8 +433,13 @@ export class MetadataDiscovery {
433
433
  prop.inverseJoinColumns = prop2.joinColumns;
434
434
  }
435
435
  prop.referencedColumnNames ??= Utils.flatten(meta.primaryKeys.map(primaryKey => meta.properties[primaryKey].fieldNames));
436
- prop.joinColumns ??= prop.referencedColumnNames.map(referencedColumnName => this.namingStrategy.joinKeyColumnName(meta.root.className, referencedColumnName, meta.compositePK, meta.root.tableName));
437
- prop.inverseJoinColumns ??= this.initManyToOneFieldName(prop, meta2.root.className);
436
+ const ownerTableName = this.isExplicitTableName(meta.root) ? meta.root.tableName : undefined;
437
+ const inverseTableName = this.isExplicitTableName(meta2.root) ? meta2.root.tableName : undefined;
438
+ prop.joinColumns ??= prop.referencedColumnNames.map(referencedColumnName => this.namingStrategy.joinKeyColumnName(meta.root.className, referencedColumnName, meta.compositePK, ownerTableName));
439
+ prop.inverseJoinColumns ??= this.initManyToOneFieldName(prop, meta2.root.className, inverseTableName);
440
+ }
441
+ isExplicitTableName(meta) {
442
+ return meta.tableName !== this.namingStrategy.classToTableName(meta.className);
438
443
  }
439
444
  initManyToOneFields(prop) {
440
445
  const meta2 = prop.targetMeta;
@@ -622,13 +627,23 @@ export class MetadataDiscovery {
622
627
  }
623
628
  // handle self-referenced m:n with same default field names
624
629
  if (meta.className === targetType && prop.joinColumns.every((joinColumn, idx) => joinColumn === prop.inverseJoinColumns[idx])) {
625
- prop.joinColumns = prop.referencedColumnNames.map(name => this.namingStrategy.joinKeyColumnName(meta.tableName + '_1', name, meta.compositePK));
626
- prop.inverseJoinColumns = prop.referencedColumnNames.map(name => this.namingStrategy.joinKeyColumnName(meta.tableName + '_2', name, meta.compositePK));
630
+ // use tableName only when explicitly provided by user, otherwise use className for backwards compatibility
631
+ const baseName = this.isExplicitTableName(meta) ? meta.tableName : meta.className;
632
+ prop.joinColumns = prop.referencedColumnNames.map(name => this.namingStrategy.joinKeyColumnName(baseName + '_1', name, meta.compositePK));
633
+ prop.inverseJoinColumns = prop.referencedColumnNames.map(name => this.namingStrategy.joinKeyColumnName(baseName + '_2', name, meta.compositePK));
627
634
  if (prop.inversedBy) {
628
635
  const prop2 = targetMeta.properties[prop.inversedBy];
629
636
  prop2.inverseJoinColumns = prop.joinColumns;
630
637
  prop2.joinColumns = prop.inverseJoinColumns;
631
638
  }
639
+ // propagate updated joinColumns to all child entities that inherit this property (STI)
640
+ for (const childMeta of this.discovered.filter(m => m.root === meta && m !== meta)) {
641
+ const childProp = childMeta.properties[prop.name];
642
+ if (childProp) {
643
+ childProp.joinColumns = prop.joinColumns;
644
+ childProp.inverseJoinColumns = prop.inverseJoinColumns;
645
+ }
646
+ }
632
647
  }
633
648
  pivotMeta2.properties[meta.name + '_owner'] = this.definePivotProperty(prop, meta.name + '_owner', meta.class, targetType + '_inverse', true, meta.className === targetType);
634
649
  pivotMeta2.properties[targetType + '_inverse'] = this.definePivotProperty(prop, targetType + '_inverse', targetMeta.class, meta.name + '_owner', false, meta.className === targetType);
@@ -16,7 +16,7 @@ export class MetadataProvider {
16
16
  prop.type = Array.isArray(tmp) ? tmp.map(t => Utils.className(t)).sort().join(' | ') : Utils.className(tmp);
17
17
  prop.target = tmp instanceof EntitySchema ? tmp.meta.class : tmp;
18
18
  }
19
- else if (!prop.type && !(prop.enum && (prop.items?.length ?? 0) > 0)) {
19
+ else if (!prop.type && !((prop.enum || prop.array) && (prop.items?.length ?? 0) > 0)) {
20
20
  throw new Error(`Please provide either 'type' or 'entity' attribute in ${meta.className}.${prop.name}.`);
21
21
  }
22
22
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.178",
4
+ "version": "7.0.0-dev.179",
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",
package/utils/Utils.js CHANGED
@@ -123,7 +123,7 @@ export function parseJsonSafe(value) {
123
123
  }
124
124
  export class Utils {
125
125
  static PK_SEPARATOR = '~~~';
126
- static #ORM_VERSION = '7.0.0-dev.178';
126
+ static #ORM_VERSION = '7.0.0-dev.179';
127
127
  /**
128
128
  * Checks if the argument is instance of `Object`. Returns false for arrays.
129
129
  */