@mikro-orm/core 7.1.0-dev.5 → 7.1.0-dev.50

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.
Files changed (66) hide show
  1. package/EntityManager.d.ts +87 -12
  2. package/EntityManager.js +252 -40
  3. package/README.md +2 -1
  4. package/connections/Connection.d.ts +67 -1
  5. package/connections/Connection.js +64 -0
  6. package/drivers/IDatabaseDriver.d.ts +45 -7
  7. package/entity/BaseEntity.d.ts +68 -1
  8. package/entity/BaseEntity.js +18 -0
  9. package/entity/Collection.d.ts +6 -3
  10. package/entity/Collection.js +15 -4
  11. package/entity/EntityAssigner.js +8 -0
  12. package/entity/EntityFactory.js +5 -3
  13. package/entity/EntityLoader.d.ts +8 -1
  14. package/entity/EntityLoader.js +103 -30
  15. package/entity/EntityRepository.d.ts +27 -9
  16. package/entity/EntityRepository.js +12 -0
  17. package/entity/Reference.d.ts +42 -1
  18. package/entity/Reference.js +9 -0
  19. package/entity/defineEntity.d.ts +101 -21
  20. package/entity/defineEntity.js +17 -6
  21. package/entity/utils.js +4 -5
  22. package/enums.d.ts +8 -1
  23. package/errors.d.ts +2 -0
  24. package/errors.js +4 -0
  25. package/index.d.ts +2 -2
  26. package/index.js +1 -1
  27. package/metadata/EntitySchema.js +18 -0
  28. package/metadata/MetadataDiscovery.d.ts +12 -0
  29. package/metadata/MetadataDiscovery.js +173 -22
  30. package/metadata/MetadataValidator.d.ts +26 -0
  31. package/metadata/MetadataValidator.js +237 -1
  32. package/metadata/Routine.d.ts +74 -0
  33. package/metadata/Routine.js +140 -0
  34. package/metadata/index.d.ts +1 -0
  35. package/metadata/index.js +1 -0
  36. package/metadata/types.d.ts +78 -9
  37. package/naming-strategy/AbstractNamingStrategy.d.ts +1 -1
  38. package/naming-strategy/NamingStrategy.d.ts +1 -1
  39. package/package.json +1 -1
  40. package/platforms/Platform.d.ts +18 -3
  41. package/platforms/Platform.js +58 -6
  42. package/serialization/EntitySerializer.js +4 -3
  43. package/typings.d.ts +399 -22
  44. package/typings.js +43 -12
  45. package/unit-of-work/UnitOfWork.js +43 -5
  46. package/utils/AbstractMigrator.d.ts +20 -5
  47. package/utils/AbstractMigrator.js +263 -28
  48. package/utils/AbstractSchemaGenerator.d.ts +1 -1
  49. package/utils/AbstractSchemaGenerator.js +4 -1
  50. package/utils/Configuration.d.ts +30 -0
  51. package/utils/Configuration.js +45 -0
  52. package/utils/DataloaderUtils.d.ts +10 -1
  53. package/utils/DataloaderUtils.js +78 -0
  54. package/utils/EntityComparator.js +1 -1
  55. package/utils/QueryHelper.d.ts +16 -0
  56. package/utils/QueryHelper.js +15 -0
  57. package/utils/TransactionManager.js +2 -0
  58. package/utils/Utils.js +1 -1
  59. package/utils/fs-utils.d.ts +2 -0
  60. package/utils/fs-utils.js +7 -1
  61. package/utils/index.d.ts +1 -0
  62. package/utils/index.js +1 -0
  63. package/utils/partition-utils.d.ts +17 -0
  64. package/utils/partition-utils.js +79 -0
  65. package/utils/upsert-utils.d.ts +2 -0
  66. package/utils/upsert-utils.js +26 -1
@@ -4,7 +4,7 @@ import { type ReferenceKind } from '../enums.js';
4
4
  export declare abstract class AbstractNamingStrategy implements NamingStrategy {
5
5
  getClassName(file: string, separator?: string): string;
6
6
  classToMigrationName(timestamp: string, customMigrationName?: string): string;
7
- indexName(tableName: string, columns: string[], type: 'primary' | 'foreign' | 'unique' | 'index' | 'sequence' | 'check' | 'default'): string;
7
+ indexName(tableName: string, columns: string[], type: 'primary' | 'foreign' | 'unique' | 'index' | 'sequence' | 'check' | 'default' | 'trigger'): string;
8
8
  /**
9
9
  * @inheritDoc
10
10
  */
@@ -75,7 +75,7 @@ export interface NamingStrategy {
75
75
  /**
76
76
  * Returns key/constraint name for the given type. Some drivers might not support all the types (e.g. mysql and sqlite enforce the PK name).
77
77
  */
78
- indexName(tableName: string, columns: string[], type: 'primary' | 'foreign' | 'unique' | 'index' | 'sequence' | 'check' | 'default'): string;
78
+ indexName(tableName: string, columns: string[], type: 'primary' | 'foreign' | 'unique' | 'index' | 'sequence' | 'check' | 'default' | 'trigger'): string;
79
79
  /**
80
80
  * Returns alias name for given entity. The alias needs to be unique across the query, which is by default
81
81
  * ensured via appended index parameter. It is optional to use it as long as you ensure it will be unique.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.1.0-dev.5",
3
+ "version": "7.1.0-dev.50",
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",
@@ -43,6 +43,8 @@ export declare abstract class Platform {
43
43
  getEnumArrayCheckConstraintExpression(column: string, items: string[]): string | null;
44
44
  /** Whether this platform supports materialized views. */
45
45
  supportsMaterializedViews(): boolean;
46
+ /** Whether this platform supports declarative table partitioning in schema generation. */
47
+ supportsPartitionedTables(): boolean;
46
48
  /** Returns the schema helper instance for this platform, or undefined if not supported. */
47
49
  getSchemaHelper(): unknown;
48
50
  /** Whether the platform automatically creates indexes on foreign key columns. */
@@ -168,7 +170,7 @@ export declare abstract class Platform {
168
170
  getUuidTypeDeclarationSQL(column: {
169
171
  length?: number;
170
172
  }): string;
171
- /** Extracts the base type name from a full SQL type declaration (e.g. "varchar(255)" -> "varchar"). */
173
+ /** Extracts the base type name from a full SQL type declaration (e.g. `varchar(255)` -> `varchar`). */
172
174
  extractSimpleType(type: string): string;
173
175
  /**
174
176
  * This should be used only to compare types, it can strip some information like the length.
@@ -211,6 +213,11 @@ export declare abstract class Platform {
211
213
  getFullTextWhereClause(prop: EntityProperty): string;
212
214
  supportsCreatingFullTextIndex(): boolean;
213
215
  getFullTextIndexExpression(indexName: string, schemaName: string | undefined, tableName: string, columns: SimpleColumnMeta[]): string;
216
+ /**
217
+ * Generates the SQL index hint clause for the given index names.
218
+ * Returns `undefined` when the platform does not support index hints (e.g. PostgreSQL, SQLite).
219
+ */
220
+ formatIndexHint(indexNames: string[]): string | undefined;
214
221
  /** Whether the driver automatically parses JSON columns into JS objects. */
215
222
  convertsJsonAutomatically(): boolean;
216
223
  /** Converts a JS value to its JSON database representation (typically JSON.stringify). */
@@ -271,9 +278,17 @@ export declare abstract class Platform {
271
278
  /** Whether the platform supports unsigned integer columns. */
272
279
  supportsUnsigned(): boolean;
273
280
  /**
274
- * Returns the default name of index for the given columns
281
+ * Maximum length of identifiers (table, column, index, constraint, …) the platform supports.
282
+ * Names produced by {@link getIndexName} above this limit are hash-truncated. Defaults to
283
+ * `Infinity` (no truncation); SQL platforms override with their engine's limit
284
+ * (PG 63, MySQL/MariaDB 64, MSSQL/Oracle 128).
285
+ */
286
+ getMaxIdentifierLength(): number;
287
+ /**
288
+ * Returns the default name of index for the given columns, hash-truncated if it would
289
+ * exceed {@link getMaxIdentifierLength}.
275
290
  */
276
- getIndexName(tableName: string, columns: string[], type: 'index' | 'unique' | 'foreign' | 'primary' | 'sequence'): string;
291
+ getIndexName(tableName: string, columns: string[], type: 'index' | 'unique' | 'foreign' | 'primary' | 'sequence' | 'check'): string;
277
292
  /** Returns the default primary key constraint name. */
278
293
  getDefaultPrimaryName(tableName: string, columns: string[]): string;
279
294
  /** Whether the platform supports custom names for primary key constraints. */
@@ -2,12 +2,22 @@ import { clone } from '../utils/clone.js';
2
2
  import { EntityRepository } from '../entity/EntityRepository.js';
3
3
  import { UnderscoreNamingStrategy } from '../naming-strategy/UnderscoreNamingStrategy.js';
4
4
  import { ExceptionConverter } from './ExceptionConverter.js';
5
+ import { MetadataError } from '../errors.js';
5
6
  import { ArrayType, BigIntType, BlobType, BooleanType, CharacterType, DateTimeType, DateType, DecimalType, DoubleType, EnumType, FloatType, IntegerType, IntervalType, JsonType, MediumIntType, SmallIntType, StringType, TextType, TimeType, TinyIntType, Type, Uint8ArrayType, UnknownType, UuidType, } from '../types/index.js';
6
7
  import { parseJsonSafe, Utils } from '../utils/Utils.js';
7
8
  import { ReferenceKind } from '../enums.js';
8
9
  import { Raw } from '../utils/RawQueryFragment.js';
9
10
  /** Symbol used to tag cloned embeddable data for JSON serialization handling. */
10
11
  export const JsonProperty = Symbol('JsonProperty');
12
+ const MULTI_WORD_SQL_TYPES = [
13
+ 'timestamp with time zone',
14
+ 'timestamp without time zone',
15
+ 'time with time zone',
16
+ 'time without time zone',
17
+ 'character varying',
18
+ 'double precision',
19
+ 'bit varying',
20
+ ];
11
21
  /** Abstract base class providing database-specific behavior and SQL dialect differences. */
12
22
  export class Platform {
13
23
  exceptionConverter = new ExceptionConverter();
@@ -62,6 +72,10 @@ export class Platform {
62
72
  supportsMaterializedViews() {
63
73
  return false;
64
74
  }
75
+ /** Whether this platform supports declarative table partitioning in schema generation. */
76
+ supportsPartitionedTables() {
77
+ return false;
78
+ }
65
79
  /** Returns the schema helper instance for this platform, or undefined if not supported. */
66
80
  getSchemaHelper() {
67
81
  return undefined;
@@ -222,9 +236,15 @@ export class Platform {
222
236
  column.length ??= 36;
223
237
  return this.getVarcharTypeDeclarationSQL(column);
224
238
  }
225
- /** Extracts the base type name from a full SQL type declaration (e.g. "varchar(255)" -> "varchar"). */
239
+ /** Extracts the base type name from a full SQL type declaration (e.g. `varchar(255)` -> `varchar`). */
226
240
  extractSimpleType(type) {
227
- return /[^(), ]+/.exec(type.toLowerCase())[0];
241
+ const lower = type.toLowerCase();
242
+ for (const multiWord of MULTI_WORD_SQL_TYPES) {
243
+ if (lower.startsWith(multiWord)) {
244
+ return multiWord;
245
+ }
246
+ }
247
+ return /[^(), ]+/.exec(lower)[0];
228
248
  }
229
249
  /**
230
250
  * This should be used only to compare types, it can strip some information like the length.
@@ -248,6 +268,7 @@ export class Platform {
248
268
  return Type.getType(CharacterType);
249
269
  case 'string':
250
270
  case 'varchar':
271
+ case 'character varying':
251
272
  return Type.getType(StringType);
252
273
  case 'interval':
253
274
  return Type.getType(IntervalType);
@@ -267,6 +288,7 @@ export class Platform {
267
288
  case 'float':
268
289
  return Type.getType(FloatType);
269
290
  case 'double':
291
+ case 'double precision':
270
292
  return Type.getType(DoubleType);
271
293
  case 'integer':
272
294
  return Type.getType(IntegerType);
@@ -286,8 +308,12 @@ export class Platform {
286
308
  return Type.getType(DateType);
287
309
  case 'datetime':
288
310
  case 'timestamp':
311
+ case 'timestamp with time zone':
312
+ case 'timestamp without time zone':
289
313
  return Type.getType(DateTimeType);
290
314
  case 'time':
315
+ case 'time with time zone':
316
+ case 'time without time zone':
291
317
  return Type.getType(TimeType);
292
318
  case 'object':
293
319
  case 'json':
@@ -415,6 +441,13 @@ export class Platform {
415
441
  getFullTextIndexExpression(indexName, schemaName, tableName, columns) {
416
442
  throw new Error('Full text searching is not supported by this driver.');
417
443
  }
444
+ /**
445
+ * Generates the SQL index hint clause for the given index names.
446
+ * Returns `undefined` when the platform does not support index hints (e.g. PostgreSQL, SQLite).
447
+ */
448
+ formatIndexHint(indexNames) {
449
+ return undefined;
450
+ }
418
451
  /** Whether the driver automatically parses JSON columns into JS objects. */
419
452
  convertsJsonAutomatically() {
420
453
  return true;
@@ -506,7 +539,8 @@ export class Platform {
506
539
  if (raw) {
507
540
  return this.formatQuery(raw.sql, raw.params);
508
541
  }
509
- return `${quote}${id.toString().replace('.', `${quote}.${quote}`)}${quote}`;
542
+ const escaped = id.toString().replaceAll(quote, quote + quote);
543
+ return `${quote}${escaped.replace('.', `${quote}.${quote}`)}${quote}`;
510
544
  }
511
545
  /** Quotes a literal value for safe embedding in SQL. */
512
546
  quoteValue(value) {
@@ -596,10 +630,26 @@ export class Platform {
596
630
  return false;
597
631
  }
598
632
  /**
599
- * Returns the default name of index for the given columns
633
+ * Maximum length of identifiers (table, column, index, constraint, …) the platform supports.
634
+ * Names produced by {@link getIndexName} above this limit are hash-truncated. Defaults to
635
+ * `Infinity` (no truncation); SQL platforms override with their engine's limit
636
+ * (PG 63, MySQL/MariaDB 64, MSSQL/Oracle 128).
637
+ */
638
+ getMaxIdentifierLength() {
639
+ return Infinity;
640
+ }
641
+ /**
642
+ * Returns the default name of index for the given columns, hash-truncated if it would
643
+ * exceed {@link getMaxIdentifierLength}.
600
644
  */
601
645
  getIndexName(tableName, columns, type) {
602
- return this.namingStrategy.indexName(tableName, columns, type);
646
+ const indexName = this.namingStrategy.indexName(tableName, columns, type);
647
+ const max = this.getMaxIdentifierLength();
648
+ if (indexName.length > max) {
649
+ const suffix = type === 'primary' ? 'pkey' : type;
650
+ return `${indexName.substring(0, max - 8 - type.length)}_${Utils.hash(indexName, 5)}_${suffix}`;
651
+ }
652
+ return indexName;
603
653
  }
604
654
  /** Returns the default primary key constraint name. */
605
655
  getDefaultPrimaryName(tableName, columns) {
@@ -651,7 +701,9 @@ export class Platform {
651
701
  }
652
702
  /** Platform-specific validation of entity metadata. */
653
703
  validateMetadata(meta) {
654
- return;
704
+ if (meta.partitionBy && !this.supportsPartitionedTables()) {
705
+ throw new MetadataError(`Entity ${meta.className} uses partitionBy, but ${this.constructor.name} does not support partitioned tables`);
706
+ }
655
707
  }
656
708
  /**
657
709
  * Generates a custom order by statement given a set of in order values, eg.
@@ -16,7 +16,8 @@ function isVisible(meta, propName, options) {
16
16
  if (Array.isArray(options.fields) && options.fields.length > 0 && !matchesPath(options.fields, propName)) {
17
17
  return false;
18
18
  }
19
- if (Array.isArray(options.populate) && matchesPath(options.populate, propName)) {
19
+ if (Array.isArray(options.populate) &&
20
+ options.populate.find(item => item === propName || item.startsWith(propName + '.'))) {
20
21
  return true;
21
22
  }
22
23
  if (options.exclude?.find(item => item === propName)) {
@@ -224,7 +225,7 @@ export class EntitySerializer {
224
225
  return this.serialize(child, childOptions);
225
226
  }
226
227
  const pk = this.processCustomType(wrapped.getPrimaryKey(), prop, wrapped.__platform, options.convertCustomTypes);
227
- if (options.forceObject || wrapped.__config.get('serialization').forceObject) {
228
+ if (options.forceObject ?? wrapped.__config.get('serialization').forceObject) {
228
229
  return Utils.primaryKeyToObject(meta, pk, visible);
229
230
  }
230
231
  if (Utils.isPlainObject(pk)) {
@@ -248,7 +249,7 @@ export class EntitySerializer {
248
249
  return this.serialize(item, this.extractChildOptions(options, prop.name));
249
250
  }
250
251
  const pk = this.processCustomType(wrapped.getPrimaryKey(), prop, wrapped.__platform, options.convertCustomTypes);
251
- if (options.forceObject || wrapped.__config.get('serialization').forceObject) {
252
+ if (options.forceObject ?? wrapped.__config.get('serialization').forceObject) {
252
253
  return Utils.primaryKeyToObject(wrapped.__meta, pk);
253
254
  }
254
255
  return pk;