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.

66 lines (48 loc) 1.12 kB
import { Entity, PrimaryKey, Property, MikroORM, wrap, Ref, OneToOne } from '@mikro-orm/postgresql'; @Entity() class A { @PrimaryKey() id!: number; @OneToOne({ entity: 'B', mappedBy: 'a', ref: true, nullable: true, }) b!: Ref<B>; @Property({ persist: false }) get calcProp() { return this.b.getEntity().prop; } } @Entity() class B { @PrimaryKey() id!: number; @OneToOne({ entity: () => A, ref: true }) a!: Ref<A>; @Property() prop: string = 'foo'; } describe('GH issue 535', () => { let orm: MikroORM; beforeAll(async () => { orm = await MikroORM.init({ entities: [A, B], dbName: `mikro_orm_test_gh_535`, }); await orm.schema.refreshDatabase(); }); afterAll(async () => { await orm.close(true); }); test(`GH issue 535`, async () => { const a = new A(); const b = new B(); a.b = wrap(b).toReference(); await orm.em.persistAndFlush([a, b]); orm.em.clear(); const fetchedA = await orm.em.findOneOrFail(A, { id: a.id }, { populate: ['b'] }); expect(fetchedA.calcProp).toBe('foo'); }); });