@mikro-orm/core 7.0.16-dev.6 → 7.0.16-dev.8

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.
@@ -665,8 +665,11 @@ export class EntityLoader {
665
665
  }
666
666
  return ret;
667
667
  }, []);
668
- // we need to automatically select the FKs too, e.g. for 1:m relations to be able to wire them with the items
669
- if (prop.kind === ReferenceKind.ONE_TO_MANY || prop.kind === ReferenceKind.MANY_TO_MANY) {
668
+ // we need to automatically select the FKs too, e.g. for 1:m and inverse 1:1 relations
669
+ // to be able to wire them with the items
670
+ if (prop.kind === ReferenceKind.ONE_TO_MANY ||
671
+ prop.kind === ReferenceKind.MANY_TO_MANY ||
672
+ (prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner)) {
670
673
  const owner = prop.targetMeta.properties[prop.mappedBy];
671
674
  // when the owning FK is lazy, we need to explicitly select it even without user-provided fields,
672
675
  // otherwise the driver will exclude it and we won't be able to map children to their parent collections
@@ -1531,7 +1531,7 @@ export class MetadataDiscovery {
1531
1531
  const table = this.createSchemaTable(meta);
1532
1532
  for (const check of meta.checks) {
1533
1533
  const fieldNames = check.property ? meta.properties[check.property].fieldNames : [];
1534
- check.name ??= this.#namingStrategy.indexName(meta.tableName, fieldNames, 'check');
1534
+ check.name ??= this.#platform.getIndexName(meta.tableName, fieldNames, 'check');
1535
1535
  if (check.expression instanceof Function) {
1536
1536
  check.expression = check.expression(columns, table);
1537
1537
  }
@@ -1556,7 +1556,7 @@ export class MetadataDiscovery {
1556
1556
  }
1557
1557
  if (expression) {
1558
1558
  meta.checks.push({
1559
- name: this.#namingStrategy.indexName(meta.tableName, prop.fieldNames, 'check'),
1559
+ name: this.#platform.getIndexName(meta.tableName, prop.fieldNames, 'check'),
1560
1560
  property: prop.name,
1561
1561
  expression,
1562
1562
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.0.16-dev.6",
3
+ "version": "7.0.16-dev.8",
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
  "keywords": [
6
6
  "data-mapper",
@@ -271,9 +271,17 @@ export declare abstract class Platform {
271
271
  /** Whether the platform supports unsigned integer columns. */
272
272
  supportsUnsigned(): boolean;
273
273
  /**
274
- * Returns the default name of index for the given columns
274
+ * Maximum length of identifiers (table, column, index, constraint, …) the platform supports.
275
+ * Names produced by {@link getIndexName} above this limit are hash-truncated. Defaults to
276
+ * `Infinity` (no truncation); SQL platforms override with their engine's limit
277
+ * (PG 63, MySQL/MariaDB 64, MSSQL/Oracle 128).
275
278
  */
276
- getIndexName(tableName: string, columns: string[], type: 'index' | 'unique' | 'foreign' | 'primary' | 'sequence'): string;
279
+ getMaxIdentifierLength(): number;
280
+ /**
281
+ * Returns the default name of index for the given columns, hash-truncated if it would
282
+ * exceed {@link getMaxIdentifierLength}.
283
+ */
284
+ getIndexName(tableName: string, columns: string[], type: 'index' | 'unique' | 'foreign' | 'primary' | 'sequence' | 'check'): string;
277
285
  /** Returns the default primary key constraint name. */
278
286
  getDefaultPrimaryName(tableName: string, columns: string[]): string;
279
287
  /** Whether the platform supports custom names for primary key constraints. */
@@ -618,10 +618,26 @@ export class Platform {
618
618
  return false;
619
619
  }
620
620
  /**
621
- * Returns the default name of index for the given columns
621
+ * Maximum length of identifiers (table, column, index, constraint, …) the platform supports.
622
+ * Names produced by {@link getIndexName} above this limit are hash-truncated. Defaults to
623
+ * `Infinity` (no truncation); SQL platforms override with their engine's limit
624
+ * (PG 63, MySQL/MariaDB 64, MSSQL/Oracle 128).
625
+ */
626
+ getMaxIdentifierLength() {
627
+ return Infinity;
628
+ }
629
+ /**
630
+ * Returns the default name of index for the given columns, hash-truncated if it would
631
+ * exceed {@link getMaxIdentifierLength}.
622
632
  */
623
633
  getIndexName(tableName, columns, type) {
624
- return this.namingStrategy.indexName(tableName, columns, type);
634
+ const indexName = this.namingStrategy.indexName(tableName, columns, type);
635
+ const max = this.getMaxIdentifierLength();
636
+ if (indexName.length > max) {
637
+ const suffix = type === 'primary' ? 'pkey' : type;
638
+ return `${indexName.substring(0, max - 8 - type.length)}_${Utils.hash(indexName, 5)}_${suffix}`;
639
+ }
640
+ return indexName;
625
641
  }
626
642
  /** Returns the default primary key constraint name. */
627
643
  getDefaultPrimaryName(tableName, columns) {
package/typings.js CHANGED
@@ -176,9 +176,10 @@ export class EntityMetadata {
176
176
  // Virtual entities evaluate expressions at query time, view entities create actual database views.
177
177
  this.virtual = !!this.expression && !this.view;
178
178
  if (config) {
179
+ const platform = config.getPlatform();
179
180
  for (const prop of this.props) {
180
181
  if (prop.enum && !prop.nativeEnumName && prop.items?.every(item => typeof item === 'string')) {
181
- const name = config.getNamingStrategy().indexName(this.tableName, prop.fieldNames, 'check');
182
+ const name = platform.getIndexName(this.tableName, prop.fieldNames, 'check');
182
183
  const exists = this.checks.findIndex(check => check.name === name);
183
184
  if (exists !== -1) {
184
185
  this.checks.splice(exists, 1);
@@ -186,9 +187,7 @@ export class EntityMetadata {
186
187
  this.checks.push({
187
188
  name,
188
189
  property: prop.name,
189
- expression: config
190
- .getPlatform()
191
- .getEnumCheckConstraintExpression(prop.fieldNames[0], prop.items),
190
+ expression: platform.getEnumCheckConstraintExpression(prop.fieldNames[0], prop.items),
192
191
  });
193
192
  }
194
193
  }
package/utils/Utils.js CHANGED
@@ -141,7 +141,7 @@ export function parseJsonSafe(value) {
141
141
  /** Collection of general-purpose utility methods used throughout the ORM. */
142
142
  export class Utils {
143
143
  static PK_SEPARATOR = '~~~';
144
- static #ORM_VERSION = '7.0.16-dev.6';
144
+ static #ORM_VERSION = '7.0.16-dev.8';
145
145
  /**
146
146
  * Checks if the argument is instance of `Object`. Returns false for arrays.
147
147
  */