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.

59 lines (42 loc) 1.14 kB
import { Entity, MikroORM, PrimaryKey, OneToMany, ManyToOne, Collection, BeforeCreate } from '@mikro-orm/sqlite'; import { v4 } from 'uuid'; abstract class Base { @PrimaryKey() id!: string; @BeforeCreate() definePrimaryKey() { this.id = v4(); } } @Entity() class Publisher extends Base { @OneToMany('Book', (b: Book) => b.publisher) books = new Collection<Book>(this); } @Entity() class Book extends Base { @ManyToOne(() => Publisher, { nullable: true }) publisher?: Publisher; } describe('GH issue 893', () => { let orm: MikroORM; beforeAll(async () => { orm = await MikroORM.init({ entities: [Base, Book, Publisher], dbName: ':memory:', }); await orm.schema.createSchema(); }); afterAll(async () => { await orm.close(true); }); test(`GH issue 893`, async () => { const publisher = new Publisher(); const book = new Book(); publisher.books.add(book); await orm.em.persistAndFlush(publisher); orm.em.clear(); const reloadedBook = await orm.em.findOne(Book, { id: book.id }); expect(reloadedBook?.publisher).not.toBeNull(); }); });