Skip to content

Commit

Permalink
fix(core): ignore current context when creating repository instance
Browse files Browse the repository at this point in the history
Closes #5395
  • Loading branch information
B4nan committed Apr 1, 2024
1 parent 7e2830e commit 4c12fc5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/EntityManager.ts
Expand Up @@ -168,7 +168,7 @@ export class EntityManager<Driver extends IDatabaseDriver = IDatabaseDriver> {
if (!this.repositoryMap[entityName]) {
const meta = this.metadata.get(entityName);
const RepositoryClass = this.config.getRepositoryClass(meta.repository) as Constructor<EntityRepository<any>>;
this.repositoryMap[entityName] = new RepositoryClass(this.getContext(false), entityName);
this.repositoryMap[entityName] = new RepositoryClass(this, entityName);
}

return this.repositoryMap[entityName] as unknown as GetRepository<Entity, Repository>;
Expand Down
36 changes: 36 additions & 0 deletions tests/issues/GH5395.test.ts
@@ -0,0 +1,36 @@
import { Entity, MikroORM, PrimaryKey, Property } from '@mikro-orm/sqlite';

@Entity()
class Post {

@PrimaryKey()
id!: number;

@Property()
name!: string;

}

let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [Post],
dbName: ':memory:',
});
await orm.schema.createSchema();
});

afterAll(() => orm.close(true));

test('em.getRepository() and context resolution (GH #5395)', async () => {
const em = orm.em.fork({ useContext: true });
await em.transactional(async _ => {
// this could happen in a nested service without access to tx-specific EM
await em.getRepository(Post).find({ id: 1 });
});
await em.getRepository(Post).find({ id: 1 });

await em.getRepository(Post).getEntityManager().getContext().find(Post, { id: 1 });
expect(em.id).toBe(em.getRepository(Post).getEntityManager().id);
});

0 comments on commit 4c12fc5

Please sign in to comment.