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.

79 lines (61 loc) 1.57 kB
import { MikroORM, ObjectId, Entity, PrimaryKey, Property, OneToOne } from '@mikro-orm/mongodb'; @Entity() class Author { @PrimaryKey() _id!: ObjectId; @Property() termsAccepted: boolean = false; @OneToOne('AuthorDetail', 'author', { owner: true }) authorDetail!: any; } @Entity() class AuthorDetail { @PrimaryKey() _id!: ObjectId; @Property() name!: string; @OneToOne(() => Author, b => b.authorDetail) author!: Author; constructor(name: string) { this.name = name; } } let orm: MikroORM; beforeAll(async () => { orm = await MikroORM.init({ entities: [AuthorDetail], clientUrl: 'mongodb://localhost:27017/mikro-orm-3548', }); await orm.schema.clearDatabase(); }); afterAll(() => orm.close(true)); test('GH issue 3548', async () => { const authorDetail = new AuthorDetail('name'); const author = new Author(); authorDetail.author = author; author.authorDetail = authorDetail; await orm.em.fork().persist(author).flush(); const r1 = await orm.em.fork().find(AuthorDetail, {}, { populate: ['*'] }); expect(r1[0]).toMatchObject({ _id: expect.any(ObjectId), name: 'name', author: { _id: expect.any(ObjectId), termsAccepted: false, authorDetail: { name: 'name', }, }, }); const r2 = await orm.em.fork().find(Author, {}, { populate: ['*'] }); expect(r2[0]).toMatchObject({ _id: expect.any(ObjectId), authorDetail: { _id: expect.any(ObjectId), name: 'name', author: { termsAccepted: false, }, }, }); });