Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): ignore current context when creating repository instance
Closes #5395
- Loading branch information
Showing
2 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |