UNPKG

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.

56 lines (40 loc) 1.28 kB
import { Collection, Entity, ManyToOne, MikroORM, OneToMany, PrimaryKey } from '@mikro-orm/sqlite'; @Entity() export class B { @PrimaryKey() id!: number; @ManyToOne({ entity: 'A', mapToPk: true }) entity!: number; } @Entity() export class A { @PrimaryKey() id!: number; @OneToMany({ entity: 'B', mappedBy: 'entity' }) entities = new Collection<B>(this); } describe('GH issue 1128', () => { let orm: MikroORM; beforeAll(async () => { orm = await MikroORM.init({ entities: [A, B], dbName: ':memory:', }); await orm.schema.createSchema(); }); afterAll(() => orm.close(true)); test('mapToPk option returns nothing when its set on M:1 side and its populated from 1:M side', async () => { await orm.em.transactional(async () => { const a = orm.em.create(A, {}); await orm.em.persistAndFlush(a); const b1 = orm.em.create(B, { entity: a.id }); const b2 = orm.em.create(B, { entity: a.id }); a.entities.add(b1, b2); }); orm.em.clear(); const entity = await orm.em.findOneOrFail(A, { id: 1 }, { populate: ['*'] }); expect(entity.entities).toHaveLength(2); expect(entity.entities[0].entity).toBe(entity.id); expect(entity.entities[1].entity).toBe(entity.id); }); });