Skip to content

Commit

Permalink
feat(core): add second EM parameter to onCreate and onUpdate call…
Browse files Browse the repository at this point in the history
…back

Closes #5201
  • Loading branch information
B4nan committed Feb 2, 2024
1 parent 7fc779f commit a964aeb
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/decorators/Property.ts
Expand Up @@ -12,6 +12,7 @@ import type {
EntityKey,
} from '../typings';
import type { Type, types } from '../types';
import type { EntityManager } from '../EntityManager';

export function Property<T extends object>(options: PropertyOptions<T> = {}) {
return function (target: any, propertyName: string) {
Expand Down Expand Up @@ -101,12 +102,12 @@ export type PropertyOptions<Owner> = {
* Automatically set the property value when entity gets created, executed during flush operation.
* @param entity
*/
onCreate?: (entity: Owner) => any;
onCreate?: (entity: Owner, em: EntityManager) => any;
/**
* Automatically update the property value every time entity gets updated, executed during flush operation.
* @param entity
*/
onUpdate?: (entity: Owner) => any;
onUpdate?: (entity: Owner, em: EntityManager) => any;
/**
* Specify default column value for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}.
* This is a runtime value, assignable to the entity property. (SQL only)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/typings.ts
Expand Up @@ -446,8 +446,8 @@ export interface EntityProperty<Owner = any, Target = any> {
getterName?: keyof Owner;
cascade: Cascade[];
orphanRemoval?: boolean;
onCreate?: (entity: Owner) => any;
onUpdate?: (entity: Owner) => any;
onCreate?: (entity: Owner, em: EntityManager) => any;
onUpdate?: (entity: Owner, em: EntityManager) => any;
deleteRule?: 'cascade' | 'no action' | 'set null' | 'set default' | AnyString;
updateRule?: 'cascade' | 'no action' | 'set null' | 'set default' | AnyString;
strategy?: LoadStrategy;
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/unit-of-work/ChangeSetComputer.ts
Expand Up @@ -5,6 +5,7 @@ import { ChangeSet, ChangeSetType } from './ChangeSet';
import { helper, type Collection, type EntityValidator } from '../entity';
import type { Platform } from '../platforms';
import { ReferenceKind } from '../enums';
import type { EntityManager } from '../EntityManager';

export class ChangeSetComputer {

Expand All @@ -14,7 +15,8 @@ export class ChangeSetComputer {
private readonly collectionUpdates: Set<Collection<AnyEntity>>,
private readonly metadata: MetadataStorage,
private readonly platform: Platform,
private readonly config: Configuration) {
private readonly config: Configuration,
private readonly em: EntityManager) {
this.comparator = this.config.getComparator(this.metadata);
}

Expand Down Expand Up @@ -84,12 +86,12 @@ export class ChangeSetComputer {
*/
private processPropertyInitializers<T>(entity: T, prop: EntityProperty<T>, type: ChangeSetType, map: Map<T, [string, unknown][]>, nested?: boolean): void {
if (prop.onCreate && type === ChangeSetType.CREATE && entity[prop.name] == null) {
entity[prop.name] = prop.onCreate(entity);
entity[prop.name] = prop.onCreate(entity, this.em);
}

if (prop.onUpdate && type === ChangeSetType.UPDATE) {
const pairs = map.get(entity) ?? [];
pairs.push([prop.name, prop.onUpdate(entity)]);
pairs.push([prop.name, prop.onUpdate(entity, this.em)]);
map.set(entity, pairs);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/unit-of-work/UnitOfWork.ts
Expand Up @@ -56,7 +56,7 @@ export class UnitOfWork {
this.platform = this.em.getPlatform();
this.eventManager = this.em.getEventManager();
this.comparator = this.em.getComparator();
this.changeSetComputer = new ChangeSetComputer(this.em.getValidator(), this.collectionUpdates, this.metadata, this.platform, this.em.config);
this.changeSetComputer = new ChangeSetComputer(this.em.getValidator(), this.collectionUpdates, this.metadata, this.platform, this.em.config, this.em);
this.changeSetPersister = new ChangeSetPersister(this.em.getDriver(), this.metadata, this.em.config.getHydrator(this.metadata), this.em.getEntityFactory(), this.em.getValidator(), this.em.config);
}

Expand Down

0 comments on commit a964aeb

Please sign in to comment.