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
text/typescript
import { Entity, MikroORM, PrimaryKey, OneToMany, ManyToOne, Collection, BeforeCreate } from '@mikro-orm/sqlite';
import { v4 } from 'uuid';
abstract class Base {
()
id!: string;
()
definePrimaryKey() {
this.id = v4();
}
}
()
class Publisher extends Base {
('Book', (b: Book) => b.publisher)
books = new Collection<Book>(this);
}
()
class Book extends Base {
(() => 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();
});
});