Skip to content

Commit

Permalink
fix(core): fix support for custom repositories in `@CreateRequestCont…
Browse files Browse the repository at this point in the history
…ext` on type level
  • Loading branch information
B4nan committed Apr 2, 2024
1 parent 43c7f14 commit aacac83
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/decorators/CreateRequestContext.ts
Expand Up @@ -3,7 +3,7 @@ import { RequestContext } from '../utils/RequestContext';
import { EntityManager } from '../EntityManager';
import { EntityRepository } from '../entity/EntityRepository';

export function CreateRequestContext<T>(getContext?: MikroORM | Promise<MikroORM> | ((type: T) => MikroORM | Promise<MikroORM> | EntityManager | EntityRepository<any>), respectExistingContext = false): MethodDecorator {
export function CreateRequestContext<T>(getContext?: MikroORM | Promise<MikroORM> | ((type: T) => MikroORM | Promise<MikroORM> | EntityManager | EntityRepository<any> | { getEntityManager(): EntityManager }), respectExistingContext = false): MethodDecorator {
return function (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = async function (this: T, ...args: any[]) {
Expand Down Expand Up @@ -51,6 +51,6 @@ export function CreateRequestContext<T>(getContext?: MikroORM | Promise<MikroORM
};
}

export function EnsureRequestContext<T>(getContext?: MikroORM | Promise<MikroORM> | ((type: T) => MikroORM | Promise<MikroORM> | EntityManager | EntityRepository<any>)): MethodDecorator {
export function EnsureRequestContext<T>(getContext?: MikroORM | Promise<MikroORM> | ((type: T) => MikroORM | Promise<MikroORM> | EntityManager | EntityRepository<any> | { getEntityManager(): EntityManager })): MethodDecorator {
return CreateRequestContext(getContext, true);
}
33 changes: 31 additions & 2 deletions tests/decorators.test.ts
Expand Up @@ -13,6 +13,7 @@ import {
RequestContext,
EntityManager,
EntityRepository,
EntityRepositoryType,
} from '@mikro-orm/core';
import type { Dictionary } from '@mikro-orm/core';
import { Test } from './entities';
Expand Down Expand Up @@ -122,9 +123,37 @@ class TestClass4 {

}

class BookRepository extends EntityRepository<Book> {

save(book: Book): void {
this.em.persist(book);
}

flush(): Promise<void> {
return this.em.flush();
}

}

export class Book {

id!: string;

@Property({
unique: true,
})
isbn!: string;

@Property()
name!: string;

[EntityRepositoryType]?: BookRepository;

}

class TestClass5 {

constructor(private readonly repo: EntityRepository<any>) {}
constructor(private readonly repo: BookRepository) {}

@CreateRequestContext<TestClass5>(t => t.repo)
foo() {
Expand All @@ -147,7 +176,7 @@ describe('decorators', () => {
ManyToMany({ entity: () => Test })(new Test2(), 'test0'); // calling multiple times won't throw
expect(storage[key].properties.test0).toMatchObject({ kind: ReferenceKind.MANY_TO_MANY, name: 'test0' });
expect(storage[key].properties.test0.entity()).toBe(Test);
expect(Object.keys(MetadataStorage.getMetadata())).toHaveLength(7);
expect(Object.keys(MetadataStorage.getMetadata())).toHaveLength(8);
MetadataStorage.clear();
expect(Object.keys(MetadataStorage.getMetadata())).toHaveLength(0);
});
Expand Down

0 comments on commit aacac83

Please sign in to comment.