@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
@@ -1,6 +1,6 @@
1
1
  import type { EntityManager } from '../EntityManager.js';
2
2
  import type { ColumnType, PropertyOptions, ReferenceOptions, EnumOptions, EmbeddedOptions, ManyToOneOptions, OneToManyOptions, OneToOneOptions, ManyToManyOptions, IndexColumnOptions } from '../metadata/types.js';
3
- import type { AnyString, GeneratedColumnCallback, Constructor, CheckCallback, FilterQuery, EntityName, Dictionary, EntityMetadata, PrimaryKeyProp, EntityRepositoryType, Hidden, Opt, Primary, EntityClass, EntitySchemaWithMeta, InferEntity, MaybeReturnType, Ref, IndexCallback, FormulaCallback, EntityCtor, IsNever, IWrappedEntity, DefineConfig, Config, MaybePromise } from '../typings.js';
3
+ import type { AnyString, GeneratedColumnCallback, Constructor, CheckCallback, FilterQuery, EntityName, Dictionary, EntityMetadata, PrimaryKeyProp, EntityRepositoryType, Hidden, Opt, Primary, EntityClass, EntitySchemaWithMeta, InferEntity, MaybeReturnType, Ref, LazyRef, IndexCallback, TriggerCallback, FormulaCallback, EntityCtor, IsNever, IWrappedEntity, DefineConfig, Config, MaybePromise, IndexHints } from '../typings.js';
4
4
  import type { Raw } from '../utils/RawQueryFragment.js';
5
5
  import type { ScalarReference } from './Reference.js';
6
6
  import type { SerializeOptions } from '../serialization/EntitySerializer.js';
@@ -12,7 +12,7 @@ import type { Collection } from './Collection.js';
12
12
  import type { FilterOptions, FindOptions, FindOneOptions } from '../drivers/IDatabaseDriver.js';
13
13
  /** Union of all option keys supported across all property definition types (scalar, enum, embedded, relations). */
14
14
  export type UniversalPropertyKeys = keyof PropertyOptions<any> | keyof EnumOptions<any> | keyof EmbeddedOptions<any, any> | keyof ReferenceOptions<any, any> | keyof ManyToOneOptions<any, any> | keyof OneToManyOptions<any, any> | keyof OneToOneOptions<any, any> | keyof ManyToManyOptions<any, any>;
15
- type BuilderExtraKeys = '~options' | '~type' | '$type' | 'strictNullable';
15
+ type BuilderExtraKeys = '~options' | '~type' | '$type' | 'strictNullable' | 'lazyRef';
16
16
  type ExcludeKeys = 'entity' | 'items';
17
17
  type BuilderKeys = Exclude<UniversalPropertyKeys, ExcludeKeys> | BuilderExtraKeys;
18
18
  type IncludeKeysForProperty = Exclude<keyof PropertyOptions<any>, ExcludeKeys> | BuilderExtraKeys;
@@ -36,9 +36,23 @@ export interface PropertyChain<Value, Options> {
36
36
  nullable: true;
37
37
  strictNullable: true;
38
38
  }>;
39
- ref(): PropertyChain<Value, Omit<Options, 'ref'> & {
39
+ ref(): Options extends {
40
+ lazyRef: true;
41
+ } ? never : PropertyChain<Value, Omit<Options, 'ref'> & {
40
42
  ref: true;
41
43
  }>;
44
+ /**
45
+ * Mark a to-one relation (`m:1`/`1:1`) as a {@apilink LazyRef} — direct entity at runtime, type-restricted
46
+ * until `Loaded<>` narrows it. Type-level only, no `ref: true` wrapping. Not compatible with `.ref()` or
47
+ * `.mapToPk()` — those chains are rejected in both directions at compile time.
48
+ */
49
+ lazyRef(): HasKind<Options, 'm:1' | '1:1'> extends true ? Options extends {
50
+ ref: true;
51
+ } | {
52
+ mapToPk: true;
53
+ } ? never : PropertyChain<Value, Omit<Options, 'lazyRef'> & {
54
+ lazyRef: true;
55
+ }> : never;
42
56
  primary(): PropertyChain<Value, Omit<Options, 'primary'> & {
43
57
  primary: true;
44
58
  }>;
@@ -103,10 +117,19 @@ export interface PropertyChain<Value, Options> {
103
117
  groups(...groups: string[]): PropertyChain<Value, Options>;
104
118
  customOrder(...customOrder: string[] | number[] | boolean[]): PropertyChain<Value, Options>;
105
119
  extra(extra: string): PropertyChain<Value, Options>;
106
- ignoreSchemaChanges(...ignoreSchemaChanges: ('type' | 'extra' | 'default')[]): PropertyChain<Value, Options>;
107
- index(index?: boolean | string): PropertyChain<Value, Options>;
108
- unique(unique?: boolean | string): PropertyChain<Value, Options>;
120
+ ignoreSchemaChanges(...ignoreSchemaChanges: ('type' | 'extra' | 'default' | 'collation')[]): PropertyChain<Value, Options>;
121
+ /** Explicitly specify index on a property. When a string name is passed, it enables type-safe `using` in `FindOptions`. */
122
+ index<N extends string>(name: N): PropertyChain<Value, Omit<Options, 'index'> & {
123
+ index: N;
124
+ }>;
125
+ index(index?: boolean): PropertyChain<Value, Options>;
126
+ /** Set column as unique. When a string name is passed, it enables type-safe `using` in `FindOptions`. (SQL only) */
127
+ unique<N extends string>(name: N): PropertyChain<Value, Omit<Options, 'unique'> & {
128
+ unique: N;
129
+ }>;
130
+ unique(unique?: boolean): PropertyChain<Value, Options>;
109
131
  comment(comment: string): PropertyChain<Value, Options>;
132
+ collation(collation: string): PropertyChain<Value, Options>;
110
133
  accessor(accessor?: string | boolean): PropertyChain<Value, Options>;
111
134
  eager(eager?: boolean): HasKind<Options, 'm:1' | '1:m' | '1:1' | 'm:n'> extends true ? PropertyChain<Value, Options> : never;
112
135
  cascade(...cascade: Cascade[]): HasKind<Options, 'm:1' | '1:m' | '1:1' | 'm:n'> extends true ? PropertyChain<Value, Options> : never;
@@ -117,7 +140,9 @@ export interface PropertyChain<Value, Options> {
117
140
  owner(): HasKind<Options, '1:1' | 'm:n'> extends true ? PropertyChain<Value, Omit<Options, 'owner'> & {
118
141
  owner: true;
119
142
  }> : never;
120
- mapToPk(): HasKind<Options, 'm:1' | '1:1'> extends true ? PropertyChain<Value, Omit<Options, 'mapToPk'> & {
143
+ mapToPk(): HasKind<Options, 'm:1' | '1:1'> extends true ? Options extends {
144
+ lazyRef: true;
145
+ } ? never : PropertyChain<Value, Omit<Options, 'mapToPk'> & {
121
146
  mapToPk: true;
122
147
  }> : never;
123
148
  orphanRemoval(orphanRemoval?: boolean): HasKind<Options, '1:m' | '1:1'> extends true ? PropertyChain<Value, Options> : never;
@@ -317,9 +342,24 @@ export declare class UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys
317
342
  /**
318
343
  * Enable `ScalarReference` wrapper for lazy values. Use this in combination with `lazy: true` to have a type-safe accessor object in place of the value.
319
344
  */
320
- ref(): UniversalPropertyOptionsBuilder<Value, Omit<Options, 'ref'> & {
345
+ ref(): Options extends {
346
+ lazyRef: true;
347
+ } ? never : UniversalPropertyOptionsBuilder<Value, Omit<Options, 'ref'> & {
321
348
  ref: true;
322
349
  }, IncludeKeys>;
350
+ /**
351
+ * Mark a to-one relation (`m:1`/`1:1`) as a {@apilink LazyRef} — the runtime value stays a plain entity
352
+ * (no `Reference` wrapper) but the TypeScript type restricts access to the primary key until `Loaded<>`
353
+ * narrows it. Type-level only; does not set `ref: true`. Not compatible with `.ref()` or `.mapToPk()` —
354
+ * those chains are rejected in both directions at compile time.
355
+ */
356
+ lazyRef(): HasKind<Options, 'm:1' | '1:1'> extends true ? Options extends {
357
+ ref: true;
358
+ } | {
359
+ mapToPk: true;
360
+ } ? never : UniversalPropertyOptionsBuilder<Value, Omit<Options, 'lazyRef'> & {
361
+ lazyRef: true;
362
+ }, IncludeKeys> : never;
323
363
  /**
324
364
  * Set to true to omit the property when {@link https://mikro-orm.io/docs/serializing Serializing}.
325
365
  */
@@ -338,12 +378,20 @@ export declare class UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys
338
378
  concurrencyCheck(concurrencyCheck?: boolean): Pick<UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys>, IncludeKeys>;
339
379
  /**
340
380
  * Explicitly specify index on a property.
381
+ * When a string name is passed, it is captured as a literal type for use with the `using` option in `FindOptions`.
341
382
  */
342
- index(index?: boolean | string): Pick<UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys>, IncludeKeys>;
383
+ index<N extends string>(name: N): Pick<UniversalPropertyOptionsBuilder<Value, Omit<Options, 'index'> & {
384
+ index: N;
385
+ }, IncludeKeys>, IncludeKeys>;
386
+ index(index?: boolean): Pick<UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys>, IncludeKeys>;
343
387
  /**
344
388
  * Set column as unique for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
389
+ * When a string name is passed, it is captured as a literal type for use with the `using` option in `FindOptions`.
345
390
  */
346
- unique(unique?: boolean | string): Pick<UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys>, IncludeKeys>;
391
+ unique<N extends string>(name: N): Pick<UniversalPropertyOptionsBuilder<Value, Omit<Options, 'unique'> & {
392
+ unique: N;
393
+ }, IncludeKeys>, IncludeKeys>;
394
+ unique(unique?: boolean): Pick<UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys>, IncludeKeys>;
347
395
  /**
348
396
  * Specify column with check constraints. (Postgres driver only)
349
397
  *
@@ -431,6 +479,11 @@ export declare class UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys
431
479
  * Specify comment of column for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
432
480
  */
433
481
  comment(comment: string): Pick<UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys>, IncludeKeys>;
482
+ /**
483
+ * Specify column-level collation for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
484
+ * Emits a `COLLATE` clause on the column definition; the accepted collation names are dialect-specific.
485
+ */
486
+ collation(collation: string): Pick<UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys>, IncludeKeys>;
434
487
  /** mysql only */
435
488
  extra(extra: string): Pick<UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys>, IncludeKeys>;
436
489
  /**
@@ -438,7 +491,7 @@ export declare class UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys
438
491
  *
439
492
  * @see https://mikro-orm.io/docs/defining-entities#sql-generated-columns
440
493
  */
441
- ignoreSchemaChanges(...ignoreSchemaChanges: ('type' | 'extra' | 'default')[]): Pick<UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys>, IncludeKeys>;
494
+ ignoreSchemaChanges(...ignoreSchemaChanges: ('type' | 'extra' | 'default' | 'collation')[]): Pick<UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys>, IncludeKeys>;
442
495
  array(): Pick<UniversalPropertyOptionsBuilder<Value, Omit<Options, 'array'> & {
443
496
  array: true;
444
497
  }, IncludeKeys>, IncludeKeys>;
@@ -496,7 +549,9 @@ export declare class UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys
496
549
  /** What to do when the reference to the target entity gets updated. */
497
550
  updateRule(updateRule: 'cascade' | 'no action' | 'set null' | 'set default' | AnyString): Pick<UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys>, IncludeKeys>;
498
551
  /** Map this relation to the primary key value instead of an entity. */
499
- mapToPk(): Pick<UniversalPropertyOptionsBuilder<Value, Omit<Options, 'mapToPk'> & {
552
+ mapToPk(): Options extends {
553
+ lazyRef: true;
554
+ } ? never : Pick<UniversalPropertyOptionsBuilder<Value, Omit<Options, 'mapToPk'> & {
500
555
  mapToPk: true;
501
556
  }, IncludeKeys>, IncludeKeys>;
502
557
  /** Set the constraint type. Immediate constraints are checked for each statement, while deferred ones are only checked at the end of the transaction. Only for postgres unique constraints. */
@@ -540,7 +595,7 @@ export type PropertyBuilders = {
540
595
  datetime: (length?: number) => UniversalPropertyOptionsBuilder<InferPropertyValueType<typeof types.datetime>, EmptyOptions, IncludeKeysForProperty>;
541
596
  time: (length?: number) => UniversalPropertyOptionsBuilder<InferPropertyValueType<typeof types.time>, EmptyOptions, IncludeKeysForProperty>;
542
597
  type: <T extends PropertyValueType>(type: T) => UniversalPropertyOptionsBuilder<InferPropertyValueType<T>, EmptyOptions, IncludeKeysForProperty>;
543
- enum: <const T extends readonly (number | string)[] | (() => Dictionary)>(items?: T) => UniversalPropertyOptionsBuilder<T extends () => Dictionary ? ValueOf<ReturnType<T>> : T extends readonly (infer Value)[] ? Value : T, EmptyOptions, IncludeKeysForEnumOptions>;
598
+ enum: <const T extends readonly (number | string)[] | Dictionary | (() => Dictionary)>(items?: T) => UniversalPropertyOptionsBuilder<T extends () => Dictionary ? ValueOf<ReturnType<T>> : T extends readonly (infer Value)[] ? Value : T extends Dictionary ? ValueOf<T> : T, EmptyOptions, IncludeKeysForEnumOptions>;
544
599
  embedded: <Target extends EntityTarget | EntityTarget[]>(target: Target) => PropertyChain<InferEntity<Target extends (infer T)[] ? T : Target>, EmptyOptions & {
545
600
  kind: 'embedded';
546
601
  }>;
@@ -560,12 +615,15 @@ export type PropertyBuilders = {
560
615
  /** Own keys + base entity keys (when TBase is not `never`). Guards against `keyof never = string | number | symbol`. */
561
616
  type AllKeys<TProperties, TBase> = keyof TProperties | (IsNever<TBase> extends true ? never : keyof TBase);
562
617
  /** Metadata descriptor for `defineEntity()`, combining entity options with property definitions. */
563
- export interface EntityMetadataWithProperties<TName extends string, TTableName extends string, TProperties extends Record<string, any>, TPK extends (keyof TProperties)[] | undefined = undefined, TBase = never, TRepository = never, TForceObject extends boolean = false> extends Omit<Partial<EntityMetadata<InferEntityFromProperties<TProperties, TPK, TBase, TRepository>>>, 'properties' | 'extends' | 'primaryKeys' | 'hooks' | 'discriminatorColumn' | 'versionProperty' | 'concurrencyCheckKeys' | 'serializedPrimaryKey' | 'indexes' | 'uniques' | 'repository' | 'filters' | 'orderBy'> {
618
+ export interface EntityMetadataWithProperties<TName extends string, TTableName extends string, TProperties extends Record<string, any>, TPK extends (keyof TProperties)[] | undefined = undefined, TBase = never, TRepository = never, TForceObject extends boolean = false, TDiscriminatorColumn extends string | undefined = undefined, TDiscriminatorValue extends string | number | undefined = undefined, TBaseDiscriminatorColumn extends string | undefined = undefined> extends Omit<Partial<EntityMetadata<InferEntityFromProperties<TProperties, TPK, TBase, TRepository>>>, 'properties' | 'extends' | 'primaryKeys' | 'hooks' | 'discriminator' | 'discriminatorColumn' | 'discriminatorValue' | 'versionProperty' | 'concurrencyCheckKeys' | 'serializedPrimaryKey' | 'indexes' | 'uniques' | 'triggers' | 'repository' | 'filters' | 'orderBy'> {
564
619
  name: TName;
565
620
  tableName?: TTableName;
566
621
  extends?: {
567
622
  '~entity': TBase;
568
- } | EntityCtor<TBase>;
623
+ '~discriminatorColumn'?: TBaseDiscriminatorColumn;
624
+ } | (EntityCtor<TBase> & {
625
+ '~discriminatorColumn'?: TBaseDiscriminatorColumn;
626
+ });
569
627
  properties: TProperties | ((properties: PropertyBuilders) => TProperties);
570
628
  primaryKeys?: TPK & InferPrimaryKeyConstraint<TProperties>[];
571
629
  hooks?: DefineEntityHooks;
@@ -585,7 +643,10 @@ export interface EntityMetadataWithProperties<TName extends string, TTableName e
585
643
  } | {
586
644
  [K in Extract<AllKeys<TProperties, TBase>, string>]?: QueryOrderKeysFlat;
587
645
  }[];
588
- discriminatorColumn?: string;
646
+ discriminator?: TDiscriminatorColumn;
647
+ /** Alias for `discriminator`. */
648
+ discriminatorColumn?: TDiscriminatorColumn;
649
+ discriminatorValue?: TDiscriminatorValue;
589
650
  versionProperty?: AllKeys<TProperties, TBase>;
590
651
  concurrencyCheckKeys?: Set<AllKeys<TProperties, TBase>>;
591
652
  serializedPrimaryKey?: AllKeys<TProperties, TBase>;
@@ -595,6 +656,7 @@ export interface EntityMetadataWithProperties<TName extends string, TTableName e
595
656
  type?: string;
596
657
  options?: Dictionary;
597
658
  expression?: string | IndexCallback<InferEntityFromProperties<TProperties, TPK, TBase>>;
659
+ where?: string | FilterQuery<InferEntityFromProperties<TProperties, TPK, TBase>>;
598
660
  columns?: IndexColumnOptions[];
599
661
  include?: NoInfer<AllKeys<TProperties, TBase>> | NoInfer<AllKeys<TProperties, TBase>>[];
600
662
  fillFactor?: number;
@@ -607,15 +669,25 @@ export interface EntityMetadataWithProperties<TName extends string, TTableName e
607
669
  name?: string;
608
670
  options?: Dictionary;
609
671
  expression?: string | IndexCallback<InferEntityFromProperties<TProperties, TPK, TBase>>;
672
+ where?: string | FilterQuery<InferEntityFromProperties<TProperties, TPK, TBase>>;
610
673
  deferMode?: DeferMode | `${DeferMode}`;
611
674
  columns?: IndexColumnOptions[];
612
675
  include?: NoInfer<AllKeys<TProperties, TBase>> | NoInfer<AllKeys<TProperties, TBase>>[];
613
676
  fillFactor?: number;
614
677
  disabled?: boolean;
615
678
  }[];
679
+ triggers?: {
680
+ name?: string;
681
+ timing: 'before' | 'after' | 'instead of';
682
+ events: ('insert' | 'update' | 'delete' | 'truncate')[];
683
+ forEach?: 'row' | 'statement';
684
+ body?: string | Raw | TriggerCallback<InferEntityFromProperties<TProperties, TPK, TBase>>;
685
+ when?: string;
686
+ expression?: string;
687
+ }[];
616
688
  }
617
689
  /** Defines an entity schema using property builders, with full type inference from the property definitions. */
618
- export declare function defineEntity<const TName extends string, const TTableName extends string, const TProperties extends Record<string, any>, const TPK extends (keyof TProperties)[] | undefined = undefined, const TBase = never, const TRepository = never, const TForceObject extends boolean = false>(meta: EntityMetadataWithProperties<TName, TTableName, TProperties, TPK, TBase, TRepository, TForceObject>): EntitySchemaWithMeta<TName, TTableName, InferEntityFromProperties<TProperties, TPK, TBase, TRepository, TForceObject>, TBase, TProperties>;
690
+ export declare function defineEntity<const TName extends string, const TTableName extends string, const TProperties extends Record<string, any>, const TPK extends (keyof TProperties)[] | undefined = undefined, const TBase = never, const TRepository = never, const TForceObject extends boolean = false, const TDiscriminatorColumn extends string | undefined = undefined, const TDiscriminatorValue extends string | number | undefined = undefined, const TBaseDiscriminatorColumn extends string | undefined = undefined>(meta: EntityMetadataWithProperties<TName, TTableName, TProperties, TPK, TBase, TRepository, TForceObject, TDiscriminatorColumn, TDiscriminatorValue, TBaseDiscriminatorColumn>): EntitySchemaWithMeta<TName, TTableName, InferEntityFromProperties<TProperties, TPK, TBase, TRepository, TForceObject, TBaseDiscriminatorColumn, TDiscriminatorValue>, TBase, TProperties, EntityCtor<InferEntityFromProperties<TProperties, TPK, TBase, TRepository, TForceObject, TBaseDiscriminatorColumn, TDiscriminatorValue>>, TDiscriminatorColumn>;
619
691
  /** Defines an entity schema for an existing class, combining the class with property builders. */
620
692
  export declare function defineEntity<const TEntity = any, const TProperties extends Record<string, any> = Record<string, any>, const TClassName extends string = string, const TTableName extends string = string, const TBase = never, const TClass extends EntityCtor = EntityCtor<TEntity>>(meta: Omit<Partial<EntityMetadata<TEntity>>, 'properties' | 'extends' | 'className' | 'tableName' | 'hooks'> & {
621
693
  class: TClass;
@@ -651,7 +723,7 @@ type InferJSType<T> = T extends typeof Type<infer TValue, any> ? NonNullable<TVa
651
723
  type InferColumnType<T extends string> = T extends 'int' | 'int4' | 'integer' | 'bigint' | 'int8' | 'int2' | 'tinyint' | 'smallint' | 'mediumint' ? number : T extends 'double' | 'double precision' | 'real' | 'float8' | 'decimal' | 'numeric' | 'float' | 'float4' ? number : T extends 'datetime' | 'time' | 'time with time zone' | 'timestamp' | 'timestamp with time zone' | 'timetz' | 'timestamptz' | 'date' | 'interval' ? Date : T extends 'ObjectId' | 'objectId' | 'character varying' | 'varchar' | 'char' | 'character' | 'uuid' | 'text' | 'tinytext' | 'mediumtext' | 'longtext' | 'enum' ? string : T extends 'boolean' | 'bool' | 'bit' ? boolean : T extends 'blob' | 'tinyblob' | 'mediumblob' | 'longblob' | 'bytea' ? Buffer : T extends 'point' | 'line' | 'lseg' | 'box' | 'circle' | 'path' | 'polygon' | 'geometry' ? number[] : T extends 'tsvector' | 'tsquery' ? string[] : T extends 'json' | 'jsonb' ? any : any;
652
724
  type BaseEntityMethodKeys = 'toObject' | 'toPOJO' | 'serialize' | 'assign' | 'populate' | 'init' | 'toReference';
653
725
  /** Infers the entity type from a `defineEntity()` properties map, resolving builders, base classes, and primary keys. */
654
- export type InferEntityFromProperties<Properties extends Record<string, any>, PK extends (keyof Properties)[] | undefined = undefined, Base = never, Repository = never, ForceObject extends boolean = false> = (IsNever<Base> extends true ? {} : Base extends {
726
+ export type InferEntityFromProperties<Properties extends Record<string, any>, PK extends (keyof Properties)[] | undefined = undefined, Base = never, Repository = never, ForceObject extends boolean = false, BaseDiscriminatorColumn extends string | undefined = undefined, DiscriminatorValue extends string | number | undefined = undefined> = (IsNever<Base> extends true ? {} : Base extends {
655
727
  toObject(...args: any[]): any;
656
728
  } ? Pick<IWrappedEntity<{
657
729
  -readonly [K in keyof Properties]: InferBuilderValue<MaybeReturnType<Properties[K]>>;
@@ -659,17 +731,22 @@ export type InferEntityFromProperties<Properties extends Record<string, any>, PK
659
731
  [PrimaryKeyProp]?: InferCombinedPrimaryKey<Properties, PK, Base>;
660
732
  } & (IsNever<Repository> extends true ? {} : {
661
733
  [EntityRepositoryType]?: Repository extends Constructor<infer R> ? R : Repository;
662
- }) & Omit<Base, typeof PrimaryKeyProp>>, BaseEntityMethodKeys> : {}) & {
734
+ }) & NarrowDiscriminator<Omit<Base, typeof PrimaryKeyProp>, BaseDiscriminatorColumn, DiscriminatorValue>>, BaseEntityMethodKeys> : {}) & {
663
735
  -readonly [K in keyof Properties]: InferBuilderValue<MaybeReturnType<Properties[K]>>;
664
736
  } & {
665
737
  [PrimaryKeyProp]?: InferCombinedPrimaryKey<Properties, PK, Base>;
666
738
  } & (IsNever<Repository> extends true ? {} : {
667
739
  [EntityRepositoryType]?: Repository extends Constructor<infer R> ? R : Repository;
668
- }) & (IsNever<Base> extends true ? {} : Omit<Base, typeof PrimaryKeyProp>) & (ForceObject extends true ? {
740
+ }) & (IsNever<Base> extends true ? {} : NarrowDiscriminator<Omit<Base, typeof PrimaryKeyProp>, BaseDiscriminatorColumn, DiscriminatorValue>) & (ForceObject extends true ? {
669
741
  [Config]?: DefineConfig<{
670
742
  forceObject: true;
671
743
  }>;
672
- } : {});
744
+ } : {}) & {
745
+ [IndexHints]?: [Properties];
746
+ };
747
+ type NarrowDiscriminator<Base, DiscColumn extends string | undefined, DiscValue> = DiscColumn extends string ? DiscColumn extends keyof Base ? DiscValue extends string | number ? Omit<Base, DiscColumn> & {
748
+ [K in DiscColumn]: DiscValue;
749
+ } : Base : Base : Base;
673
750
  type InferCombinedPrimaryKey<Properties extends Record<string, any>, PK, Base> = PK extends undefined ? CombinePrimaryKeys<InferPrimaryKey<Properties>, ExtractBasePrimaryKey<Base>> : PK;
674
751
  type ExtractBasePrimaryKey<Base> = Base extends {
675
752
  [PrimaryKeyProp]?: infer BasePK;
@@ -711,6 +788,9 @@ type MaybeNullable<Value, Options> = Options extends {
711
788
  type MaybeRelationRef<Value, Options> = Options extends {
712
789
  mapToPk: true;
713
790
  } ? Value : Options extends {
791
+ lazyRef: true;
792
+ kind: '1:1' | 'm:1';
793
+ } ? Value extends object ? LazyRef<Value> : never : Options extends {
714
794
  ref: true;
715
795
  kind: '1:1';
716
796
  } ? Value extends object ? Ref<Value> : never : Options extends {
@@ -173,6 +173,16 @@ export class UniversalPropertyOptionsBuilder {
173
173
  ref() {
174
174
  return this.assignOptions({ ref: true });
175
175
  }
176
+ /**
177
+ * Mark a to-one relation (`m:1`/`1:1`) as a {@apilink LazyRef} — the runtime value stays a plain entity
178
+ * (no `Reference` wrapper) but the TypeScript type restricts access to the primary key until `Loaded<>`
179
+ * narrows it. Type-level only; does not set `ref: true`. Not compatible with `.ref()` or `.mapToPk()` —
180
+ * those chains are rejected in both directions at compile time.
181
+ */
182
+ lazyRef() {
183
+ // purely type-level — no runtime metadata change
184
+ return this;
185
+ }
176
186
  /**
177
187
  * Set to true to omit the property when {@link https://mikro-orm.io/docs/serializing Serializing}.
178
188
  */
@@ -191,15 +201,9 @@ export class UniversalPropertyOptionsBuilder {
191
201
  concurrencyCheck(concurrencyCheck = true) {
192
202
  return this.assignOptions({ concurrencyCheck });
193
203
  }
194
- /**
195
- * Explicitly specify index on a property.
196
- */
197
204
  index(index = true) {
198
205
  return this.assignOptions({ index });
199
206
  }
200
- /**
201
- * Set column as unique for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
202
- */
203
207
  unique(unique = true) {
204
208
  return this.assignOptions({ unique });
205
209
  }
@@ -312,6 +316,13 @@ export class UniversalPropertyOptionsBuilder {
312
316
  comment(comment) {
313
317
  return this.assignOptions({ comment });
314
318
  }
319
+ /**
320
+ * Specify column-level collation for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
321
+ * Emits a `COLLATE` clause on the column definition; the accepted collation names are dialect-specific.
322
+ */
323
+ collation(collation) {
324
+ return this.assignOptions({ collation });
325
+ }
315
326
  /** mysql only */
316
327
  extra(extra) {
317
328
  return this.assignOptions({ extra });
package/entity/utils.js CHANGED
@@ -94,11 +94,10 @@ export function applyPopulateHints(populate, hints) {
94
94
  if (!entry) {
95
95
  continue;
96
96
  }
97
- if (hint.strategy != null) {
98
- entry.strategy = hint.strategy;
99
- }
100
- if (hint.joinType != null) {
101
- entry.joinType = hint.joinType;
97
+ for (const key of Object.keys(hint)) {
98
+ if (hint[key] != null) {
99
+ entry[key] = hint[key];
100
+ }
102
101
  }
103
102
  }
104
103
  }
package/enums.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { EntityKey, ExpandProperty } from './typings.js';
2
- import type { Transaction } from './connections/Connection.js';
2
+ import type { InflightQueryAbortStrategy, Transaction } from './connections/Connection.js';
3
3
  import type { LogContext } from './logging/Logger.js';
4
4
  /** Controls when the `EntityManager` flushes pending changes to the database. */
5
5
  export declare enum FlushMode {
@@ -307,6 +307,13 @@ export interface TransactionOptions {
307
307
  flushMode?: FlushMode | `${FlushMode}`;
308
308
  ignoreNestedTransactions?: boolean;
309
309
  loggerContext?: LogContext;
310
+ /**
311
+ * `AbortSignal` cancelling every query within the transaction (including the implicit flush).
312
+ * Cancelling mid-transaction triggers a rollback once the in-flight query settles.
313
+ */
314
+ signal?: AbortSignal;
315
+ /** Cancellation strategy paired with {@link signal}. */
316
+ inflightQueryAbortStrategy?: InflightQueryAbortStrategy;
310
317
  }
311
318
  export declare abstract class PlainObject {
312
319
  }
package/errors.d.ts CHANGED
@@ -74,6 +74,8 @@ export declare class MetadataError<T extends AnyEntity = AnyEntity> extends Vali
74
74
  static viewEntityWithoutExpression(meta: EntityMetadata): MetadataError;
75
75
  static mixedInheritanceStrategies(root: EntityMetadata, child: EntityMetadata): MetadataError;
76
76
  static tptNotSupportedByDriver(meta: EntityMetadata): MetadataError;
77
+ /** Thrown when database triggers are defined on an entity using a driver that does not support them. */
78
+ static triggersNotSupportedByDriver(meta: EntityMetadata): MetadataError;
77
79
  private static fromMessage;
78
80
  }
79
81
  /** Error thrown when an entity lookup fails to find the expected result. */
package/errors.js CHANGED
@@ -243,6 +243,10 @@ export class MetadataError extends ValidationError {
243
243
  static tptNotSupportedByDriver(meta) {
244
244
  return new MetadataError(`Entity ${meta.className} uses TPT (Table-Per-Type) inheritance which is not supported by the current driver. TPT requires SQL JOINs and is only available with SQL drivers.`);
245
245
  }
246
+ /** Thrown when database triggers are defined on an entity using a driver that does not support them. */
247
+ static triggersNotSupportedByDriver(meta) {
248
+ return new MetadataError(`Entity ${meta.className} defines database triggers which are not supported by the current driver. Triggers are only available with SQL drivers.`);
249
+ }
246
250
  static fromMessage(meta, prop, message) {
247
251
  return new MetadataError(`${meta.className}.${prop.name} ${message}`);
248
252
  }
package/index.d.ts CHANGED
@@ -2,8 +2,8 @@
2
2
  * @packageDocumentation
3
3
  * @module core
4
4
  */
5
- export { EntityMetadata, PrimaryKeyProp, EntityRepositoryType, OptionalProps, EagerProps, HiddenProps, Config, EntityName, } from './typings.js';
6
- export type { CompiledFunctions, Constructor, ConnectionType, Dictionary, Primary, IPrimaryKey, ObjectQuery, FilterQuery, IWrappedEntity, InferEntityName, EntityData, Highlighter, MaybePromise, AnyEntity, EntityClass, EntityProperty, PopulateOptions, Populate, Loaded, New, LoadedReference, LoadedCollection, IMigrator, IMigrationGenerator, MigratorEvent, GetRepository, MigrationObject, DeepPartial, PrimaryProperty, Cast, IsUnknown, EntityDictionary, EntityDTO, EntityDTOFlat, EntityDTOProp, SerializeDTO, MigrationDiff, GenerateOptions, FilterObject, IMigrationRunner, IEntityGenerator, ISeedManager, SeederObject, IMigratorStorage, RequiredEntityData, CheckCallback, IndexCallback, FormulaCallback, FormulaTable, SchemaTable, SchemaColumns, SimpleColumnMeta, Rel, Ref, ScalarRef, EntityRef, ISchemaGenerator, MigrationInfo, MigrateOptions, MigrationResult, MigrationRow, EntityKey, EntityValue, EntityDataValue, FilterKey, EntityType, FromEntityType, Selected, IsSubset, EntityProps, ExpandProperty, ExpandScalar, FilterItemValue, ExpandQuery, Scalar, ExpandHint, FilterValue, MergeLoaded, MergeSelected, TypeConfig, AnyString, ClearDatabaseOptions, CreateSchemaOptions, EnsureDatabaseOptions, UpdateSchemaOptions, DropSchemaOptions, RefreshDatabaseOptions, AutoPath, UnboxArray, MetadataProcessor, ImportsResolver, RequiredNullable, DefineConfig, Opt, Hidden, EntitySchemaWithMeta, InferEntity, CheckConstraint, GeneratedColumnCallback, FilterDef, EntityCtor, Subquery, PopulateHintOptions, Prefixes, } from './typings.js';
5
+ export { EntityMetadata, PrimaryKeyProp, EntityRepositoryType, OptionalProps, EagerProps, HiddenProps, Config, EntityName, IndexHints, } from './typings.js';
6
+ export type { CompiledFunctions, Constructor, ConnectionType, Dictionary, Primary, IPrimaryKey, ObjectQuery, FilterQuery, IWrappedEntity, InferEntityName, EntityData, Highlighter, MaybePromise, AnyEntity, EntityClass, EntityProperty, PopulateOptions, Populate, Loaded, New, LoadedReference, LoadedCollection, IMigrator, IMigrationGenerator, MigratorEvent, GetRepository, MigrationObject, DeepPartial, PrimaryProperty, Cast, IsUnknown, EntityDictionary, EntityDTO, EntityDTOFlat, EntityDTOProp, SerializeDTO, MigrationDiff, GenerateOptions, FilterObject, IndexFilterQuery, ExtractIndexHints, IndexName, IndexColumns, WithUsingOptions, IMigrationRunner, IEntityGenerator, ISeedManager, SeederObject, IMigratorStorage, RequiredEntityData, CheckCallback, TriggerCallback, IndexCallback, FormulaCallback, FormulaColumns, FormulaTable, SchemaTable, SchemaColumns, SchemaColumnRef, EntityDataPropValue, SimpleColumnMeta, Rel, Ref, LazyRef, ScalarRef, EntityRef, ISchemaGenerator, MigrationInfo, MigrateOptions, MigrationResult, MigrationRow, EntityKey, EntityValue, EntityDataValue, FilterKey, EntityType, FromEntityType, Selected, IsSubset, EntityProps, ExpandProperty, ExpandScalar, FilterItemValue, ExpandQuery, Scalar, ExpandHint, FilterValue, MergeLoaded, MergeSelected, TypeConfig, AnyString, ClearDatabaseOptions, CreateSchemaOptions, EnsureDatabaseOptions, UpdateSchemaOptions, DropSchemaOptions, RefreshDatabaseOptions, AutoPath, UnboxArray, MetadataProcessor, ImportsResolver, RequiredNullable, DefineConfig, Opt, Hidden, EntitySchemaWithMeta, InferEntity, CheckConstraint, TriggerDef, RoutineReturns, RoutineBodyCallback, RoutineJsBody, RoutineIgnoreField, RoutineParamConfig, RoutineConfig, RoutineRuntimeType, RoutineArgs, RoutineReturn, GeneratedColumnCallback, FilterDef, EntityCtor, Subquery, PopulateHintOptions, Prefixes, } from './typings.js';
7
7
  export * from './enums.js';
8
8
  export * from './errors.js';
9
9
  export * from './exceptions.js';
package/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * @packageDocumentation
3
3
  * @module core
4
4
  */
5
- export { EntityMetadata, PrimaryKeyProp, EntityRepositoryType, OptionalProps, EagerProps, HiddenProps, Config, EntityName, } from './typings.js';
5
+ export { EntityMetadata, PrimaryKeyProp, EntityRepositoryType, OptionalProps, EagerProps, HiddenProps, Config, EntityName, IndexHints, } from './typings.js';
6
6
  export * from './enums.js';
7
7
  export * from './errors.js';
8
8
  export * from './exceptions.js';
@@ -85,6 +85,9 @@ export class EntitySchema {
85
85
  if (options.items instanceof Function) {
86
86
  options.items = Utils.extractEnumValues(options.items());
87
87
  }
88
+ else if (options.items && !Array.isArray(options.items) && typeof options.items === 'object') {
89
+ options.items = Utils.extractEnumValues(options.items);
90
+ }
88
91
  // enum arrays are simple numeric/string arrays, the constraint is enforced in the custom type only
89
92
  if (options.array && !options.type) {
90
93
  options.type = new EnumArrayType(`${this._meta.className}.${name}`, options.items);
@@ -251,6 +254,21 @@ export class EntitySchema {
251
254
  return this;
252
255
  }
253
256
  this.setClass(this._meta.class);
257
+ // `discriminator` is the property name on the entity / embeddable; `discriminatorColumn` is an
258
+ // optional column-name override on that property. For backward compat, when only
259
+ // `discriminatorColumn` is given (legacy), it is treated as the property name. Once normalized,
260
+ // `meta.discriminatorColumn` always holds the property name (so existing readers keep working),
261
+ // and `meta.discriminatorFieldName` carries the explicit column override (if any).
262
+ if (this._meta.discriminator &&
263
+ this._meta.discriminatorColumn &&
264
+ this._meta.discriminator !== this._meta.discriminatorColumn) {
265
+ this._meta.discriminatorFieldName = this._meta.discriminatorColumn;
266
+ this._meta.discriminatorColumn = this._meta.discriminator;
267
+ }
268
+ else {
269
+ this._meta.discriminatorColumn ??= this._meta.discriminator;
270
+ this._meta.discriminator ??= this._meta.discriminatorColumn;
271
+ }
254
272
  // Abstract TPT entities keep their name because they have their own table
255
273
  const isTPT = this._meta.inheritance === 'tpt' || this.isPartOfTPTHierarchy();
256
274
  if (this._meta.abstract && !this._meta.discriminatorColumn && !isTPT) {
@@ -54,6 +54,16 @@ export declare class MetadataDiscovery {
54
54
  * Define properties for a polymorphic pivot table.
55
55
  */
56
56
  private definePolymorphicPivotProperties;
57
+ /**
58
+ * Mirror of definePolymorphicPivotProperties for union-target M:N
59
+ * (e.g. Post.attachments -> Image | Video via shared pivot with a target-side discriminator).
60
+ *
61
+ * Pivot shape:
62
+ * (owner_fk..., discriminator_column, target_fk...)
63
+ * - owner side is a normal M:1 to the single owner entity
64
+ * - target side is a discriminator column + per-target-type virtual M:1 relations
65
+ */
66
+ private defineUnionTargetPolymorphicPivotProperties;
57
67
  /**
58
68
  * Create a virtual M:1 relation from pivot to a polymorphic owner entity.
59
69
  * This enables single-query join loading for inverse-side polymorphic M:N.
@@ -66,6 +76,7 @@ export declare class MetadataDiscovery {
66
76
  private initPolyEmbeddables;
67
77
  private initPolymorphicRelation;
68
78
  private initEmbeddables;
79
+ private sameRelationTargetRoot;
69
80
  private initSingleTableInheritance;
70
81
  /**
71
82
  * First pass of TPT initialization: sets up hierarchy relationships
@@ -105,6 +116,7 @@ export declare class MetadataDiscovery {
105
116
  private initAutoincrement;
106
117
  private createSchemaTable;
107
118
  private initCheckConstraints;
119
+ private initTriggers;
108
120
  private initGeneratedColumn;
109
121
  private getDefaultVersionValue;
110
122
  private inferDefaultValue;