@mikro-orm/core 7.0.0-dev.200 → 7.0.0-dev.201

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.
@@ -3,11 +3,11 @@ import type { ColumnType, PropertyOptions, ReferenceOptions, EnumOptions, Embedd
3
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 } from '../typings.js';
4
4
  import type { ScalarReference } from './Reference.js';
5
5
  import type { SerializeOptions } from '../serialization/EntitySerializer.js';
6
- import type { Cascade, DeferMode, EmbeddedPrefixMode, EventType, LoadStrategy, QueryOrderMap } from '../enums.js';
6
+ import type { Cascade, DeferMode, EmbeddedPrefixMode, LoadStrategy, QueryOrderMap } from '../enums.js';
7
+ import type { EventSubscriber } from '../events/EventSubscriber.js';
7
8
  import type { IType, Type } from '../types/Type.js';
8
9
  import { types } from '../types/index.js';
9
10
  import type { Collection } from './Collection.js';
10
- import type { EventSubscriber } from '../events/EventSubscriber.js';
11
11
  import type { FilterOptions } from '../drivers/IDatabaseDriver.js';
12
12
  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>;
13
13
  type BuilderExtraKeys = '~options' | '~type' | '$type';
@@ -422,7 +422,7 @@ export interface EntityMetadataWithProperties<TName extends string, TTableName e
422
422
  } | EntityCtor<TBase>;
423
423
  properties: TProperties | ((properties: typeof propertyBuilders) => TProperties);
424
424
  primaryKeys?: TPK & InferPrimaryKey<TProperties>[];
425
- hooks?: DefineEntityHooks<InferEntityFromProperties<TProperties, TPK, TBase, TRepository>>;
425
+ hooks?: DefineEntityHooks;
426
426
  repository?: () => TRepository;
427
427
  discriminatorColumn?: keyof TProperties;
428
428
  versionProperty?: keyof TProperties;
@@ -444,12 +444,13 @@ export interface EntityMetadataWithProperties<TName extends string, TTableName e
444
444
  }[];
445
445
  }
446
446
  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>(meta: EntityMetadataWithProperties<TName, TTableName, TProperties, TPK, TBase, TRepository>): EntitySchemaWithMeta<TName, TTableName, InferEntityFromProperties<TProperties, TPK, TBase, TRepository>, TBase, TProperties>;
447
- 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'> & {
447
+ 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'> & {
448
448
  class: TClass;
449
449
  className?: TClassName;
450
450
  tableName?: TTableName;
451
451
  extends?: TBase;
452
452
  properties: TProperties | ((properties: typeof propertyBuilders) => TProperties);
453
+ hooks?: DefineEntityHooks<TEntity>;
453
454
  }): EntitySchemaWithMeta<TClassName, TTableName, TEntity, TBase, TProperties, TClass>;
454
455
  export declare namespace defineEntity {
455
456
  var properties: {
@@ -493,11 +494,19 @@ export declare namespace defineEntity {
493
494
  };
494
495
  }
495
496
  export { propertyBuilders as p };
496
- export interface DefineEntityHooks<T> extends Partial<MapToArray<Pick<EventSubscriber<T>, keyof typeof EventType>>> {
497
+ type EntityHookValue<T, K extends keyof EventSubscriber<T>> = (keyof T | NonNullable<EventSubscriber<T>[K]>)[];
498
+ export interface DefineEntityHooks<T = any> {
499
+ onInit?: EntityHookValue<T, 'onInit'>;
500
+ onLoad?: EntityHookValue<T, 'onLoad'>;
501
+ beforeCreate?: EntityHookValue<T, 'beforeCreate'>;
502
+ afterCreate?: EntityHookValue<T, 'afterCreate'>;
503
+ beforeUpdate?: EntityHookValue<T, 'beforeUpdate'>;
504
+ afterUpdate?: EntityHookValue<T, 'afterUpdate'>;
505
+ beforeUpsert?: EntityHookValue<T, 'beforeUpsert'>;
506
+ afterUpsert?: EntityHookValue<T, 'afterUpsert'>;
507
+ beforeDelete?: EntityHookValue<T, 'beforeDelete'>;
508
+ afterDelete?: EntityHookValue<T, 'afterDelete'>;
497
509
  }
498
- type MapToArray<T extends Record<string, any>> = {
499
- [K in keyof T]: NonNullable<T[K]>[];
500
- };
501
510
  type PropertyValueType = PropertyOptions<any>['type'];
502
511
  type InferPropertyValueType<T extends PropertyValueType> = T extends string ? InferTypeByString<T> : T extends NumberConstructor ? number : T extends StringConstructor ? string : T extends BooleanConstructor ? boolean : T extends DateConstructor ? Date : T extends ArrayConstructor ? string[] : T extends Constructor<infer TType> ? TType extends Type<infer TValue, any> ? NonNullable<TValue> : TType : T extends Type<infer TValue, any> ? NonNullable<TValue> : any;
503
512
  type InferTypeByString<T extends string> = T extends keyof typeof types ? InferJSType<typeof types[T]> : InferColumnType<T>;
@@ -1,5 +1,6 @@
1
1
  import { EntityMetadata, type AnyEntity, type EntityKey, type Constructor, type DeepPartial, type EntityName, type EntityProperty, type CleanKeys, type ExpandProperty, type IsNever, type EntityCtor } from '../typings.js';
2
- import { ReferenceKind } from '../enums.js';
2
+ import { type EventType, ReferenceKind } from '../enums.js';
3
+ import type { EventSubscriber } from '../events/EventSubscriber.js';
3
4
  import { Type } from '../types/Type.js';
4
5
  import type { PropertyOptions, ManyToOneOptions, OneToOneOptions, OneToManyOptions, ManyToManyOptions, EmbeddedOptions, EnumOptions, PrimaryKeyOptions, SerializedPrimaryKeyOptions, IndexOptions, UniqueOptions } from './types.js';
5
6
  type TypeType = string | NumberConstructor | StringConstructor | BooleanConstructor | DateConstructor | ArrayConstructor | Constructor<Type<any>> | Type<any>;
@@ -83,5 +84,23 @@ export declare class EntitySchema<Entity = any, Base = never, Class extends Enti
83
84
  private createProperty;
84
85
  private rename;
85
86
  private renameCompositeOptions;
87
+ /**
88
+ * Adds a lifecycle hook handler to the entity schema.
89
+ * This method allows registering hooks after the entity is defined,
90
+ * which can be useful for avoiding circular type references.
91
+ *
92
+ * @example
93
+ * ```ts
94
+ * export const Article = defineEntity({
95
+ * name: 'Article',
96
+ * properties: { ... },
97
+ * });
98
+ *
99
+ * Article.addHook('beforeCreate', async args => {
100
+ * args.entity.slug = args.entity.title.toLowerCase();
101
+ * });
102
+ * ```
103
+ */
104
+ addHook<Event extends EventType | `${EventType}`>(event: Event, handler: NonNullable<EventSubscriber<Entity>[Event]>): this;
86
105
  }
87
106
  export {};
@@ -320,4 +320,26 @@ export class EntitySchema {
320
320
  this.rename(options, 'referenceColumnName', 'referencedColumnNames');
321
321
  this.rename(options, 'columnType', 'columnTypes');
322
322
  }
323
+ /**
324
+ * Adds a lifecycle hook handler to the entity schema.
325
+ * This method allows registering hooks after the entity is defined,
326
+ * which can be useful for avoiding circular type references.
327
+ *
328
+ * @example
329
+ * ```ts
330
+ * export const Article = defineEntity({
331
+ * name: 'Article',
332
+ * properties: { ... },
333
+ * });
334
+ *
335
+ * Article.addHook('beforeCreate', async args => {
336
+ * args.entity.slug = args.entity.title.toLowerCase();
337
+ * });
338
+ * ```
339
+ */
340
+ addHook(event, handler) {
341
+ this._meta.hooks[event] ??= [];
342
+ this._meta.hooks[event].push(handler);
343
+ return this;
344
+ }
323
345
  }
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.200",
4
+ "version": "7.0.0-dev.201",
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.200';
126
+ static #ORM_VERSION = '7.0.0-dev.201';
127
127
  /**
128
128
  * Checks if the argument is instance of `Object`. Returns false for arrays.
129
129
  */