Skip to content

Commit

Permalink
fix(mapping): allow defining hooks on base entities
Browse files Browse the repository at this point in the history
Closes #104
  • Loading branch information
B4nan committed Sep 1, 2019
1 parent c001000 commit 6938398
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
4 changes: 0 additions & 4 deletions lib/decorators/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ function hook(type: string) {
return function (target: any, method: string) {
const meta = MetadataStorage.getMetadata(target.constructor.name);

if (!meta.hooks) {
meta.hooks = {};
}

if (!meta.hooks[type]) {
meta.hooks[type] = [];
}
Expand Down
5 changes: 5 additions & 0 deletions lib/metadata/MetadataDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ export class MetadataDiscovery {
if (primary && !meta.primaryKey) {
meta.primaryKey = primary.name;
}

Object.keys(base.hooks).forEach(type => {
meta.hooks[type] = meta.hooks[type] || [];
meta.hooks[type].unshift(...base.hooks[type]);
});
}

private getDefaultVersionValue(prop: EntityProperty): string {
Expand Down
2 changes: 1 addition & 1 deletion lib/metadata/MetadataStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class MetadataStorage {
static getMetadata<T extends IEntityType<T> = any>(entity: string): EntityMetadata<T>; // tslint:disable-next-line:lines-between-class-members
static getMetadata<T extends IEntityType<T> = any>(entity?: string): Record<string, EntityMetadata> | EntityMetadata<T> {
if (entity && !MetadataStorage.metadata[entity]) {
MetadataStorage.metadata[entity] = { properties: {} } as EntityMetadata;
MetadataStorage.metadata[entity] = { properties: {}, hooks: {} } as EntityMetadata;
}

if (entity) {
Expand Down
11 changes: 5 additions & 6 deletions tests/EntityManager.mongo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import { Logger } from '../lib/utils';
import { FooBar } from './entities/FooBar';
import { FooBaz } from './entities/FooBaz';

/**
* @class EntityManagerMongoTest
*/
describe('EntityManagerMongo', () => {

let orm: MikroORM;
Expand Down Expand Up @@ -253,7 +250,7 @@ describe('EntityManagerMongo', () => {
const fork = orm.em.fork();

expect(fork).not.toBe(orm.em);
expect(fork['metadata']).toBe(orm.em['metadata']);
expect(fork.metadata).toBe(orm.em.metadata);
expect(fork.getUnitOfWork().getIdentityMap()).toEqual({});

// request context is not started so we can use UoW and EF getters
Expand Down Expand Up @@ -938,11 +935,13 @@ describe('EntityManagerMongo', () => {
expect(author.id).toBeNull();
expect(author.version).toBeUndefined();
expect(author.versionAsString).toBeUndefined();
expect(author.hookTest).toBe(false);

await repo.persist(author);
expect(author.id).not.toBeNull();
expect(author.version).toBe(1);
expect(author.versionAsString).toBe('v1');
expect(author.hookTest).toBe(true);

author.name = 'John Snow';
await repo.flush();
Expand Down Expand Up @@ -1223,12 +1222,12 @@ describe('EntityManagerMongo', () => {
const baz2 = FooBaz.create('fz2');
bar.baz = baz1;
await orm.em.persist(bar);
expect(orm.em.getUnitOfWork()['originalEntityData'][bar.__uuid].baz).toEqual(baz1._id);
expect(orm.em.getUnitOfWork().originalEntityData[bar.__uuid].baz).toEqual(baz1._id);

// replacing reference with value will trigger orphan removal
bar.baz = baz2;
await orm.em.persist(bar);
expect(orm.em.getUnitOfWork()['originalEntityData'][bar.__uuid].baz).toEqual(baz2._id);
expect(orm.em.getUnitOfWork().originalEntityData[bar.__uuid].baz).toEqual(baz2._id);
await expect(orm.em.findOne(FooBaz, baz1)).resolves.toBeNull();
await expect(orm.em.findOne(FooBaz, baz2)).resolves.not.toBeNull();

Expand Down
10 changes: 9 additions & 1 deletion tests/entities-sql/BaseEntity2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Collection, IEntity, PrimaryKey } from '../../lib';
import { BeforeCreate, Collection, IEntity, PrimaryKey, Property } from '../../lib';
import { MetadataStorage } from '../../lib/metadata';
import { ReferenceType } from '../../lib/entity';

Expand All @@ -7,6 +7,9 @@ export abstract class BaseEntity2 {
@PrimaryKey()
id: number;

@Property({ persist: false })
hookTest = false;

protected constructor() {
const meta = MetadataStorage.getMetadata(this.constructor.name);
const props = meta.properties;
Expand All @@ -18,6 +21,11 @@ export abstract class BaseEntity2 {
});
}

@BeforeCreate()
baseBeforeCreate() {
this.hookTest = true;
}

}

export interface BaseEntity2 extends IEntity<number> { }
10 changes: 9 additions & 1 deletion tests/entities/BaseEntity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IEntity, PrimaryKey, Property } from '../../lib';
import { BeforeCreate, IEntity, PrimaryKey, Property } from '../../lib';
import { ObjectID } from 'bson';

export abstract class BaseEntity {
Expand All @@ -15,6 +15,14 @@ export abstract class BaseEntity {
@Property()
foo: string;

@Property({ persist: false })
hookTest = false;

@BeforeCreate()
baseBeforeCreate() {
this.hookTest = true;
}

}

export interface BaseEntity extends IEntity<string> { }

0 comments on commit 6938398

Please sign in to comment.