undeexcepturi
Version:
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.
66 lines (46 loc) • 1.43 kB
text/typescript
import { Collection, Entity, ManyToOne, MikroORM, OneToMany, PrimaryKey, wrap } from '@mikro-orm/sqlite';
()
class Parent {
()
id!: number;
(() => Child, child => child.parent)
children = new Collection<Child>(this);
}
()
export default class Child {
()
id!: number;
(() => Parent)
parent!: Parent;
}
describe('GH issue 2882', () => {
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
dbName: ':memory:',
entities: [Parent, Child],
});
await orm.schema.refreshDatabase();
});
beforeEach(async () => {
await orm.schema.clearDatabase();
});
afterAll(async () => {
await orm.close(true);
});
test(`should not leave duplicate entity in collection`, async () => {
const p = new Parent();
await orm.em.fork().persistAndFlush(p);
const parent = await orm.em.findOneOrFail(Parent, p.id, { populate: ['children'] });
expect(wrap(parent, true).__em?.id).toBe(1);
await orm.em.transactional(async em => {
const parent = await em.findOneOrFail(Parent, p.id);
em.create(Child, { parent });
});
expect(parent.children).toHaveLength(1);
expect(wrap(parent.children[0], true).__em?.id).toBe(1);
await orm.em.find(Child, {});
expect(parent.children).toHaveLength(1);
expect(wrap(parent.children[0], true).__em?.id).toBe(1);
});
});