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.
136 lines (109 loc) • 2.94 kB
text/typescript
import {
MikroORM,
Entity,
PrimaryKey,
Property,
SimpleLogger,
ManyToOne,
OneToMany,
Collection,
Ref,
ref,
} from '@mikro-orm/sqlite';
()
class Author {
()
id!: number;
()
name: string;
(() => Book, book => book.author)
books = new Collection<Book>(this);
constructor({ id, name }: { id?: number; name: string }) {
if (id) {
this.id = id;
}
this.name = name;
}
}
()
class Book {
()
id!: number;
()
title: string;
(() => Author, { ref: true })
author: Ref<Author>;
(() => Publisher, { ref: true })
publisher!: Ref<Publisher>;
constructor({ id, title, author }: { id?: number; title: string; author: Author | Ref<Author> }) {
if (id) {
this.id = id;
}
this.title = title;
this.author = ref(author);
}
}
()
class Publisher {
()
id!: number;
()
name: string;
(() => Book, book => book.publisher)
books = new Collection<Book, Publisher>(this);
constructor({ id, name = 'asd' }: { id?: number; name?: string }) {
if (id) {
this.id = id;
}
this.name = name;
}
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Author, Book],
dbName: ':memory:',
loggerFactory: options => new SimpleLogger(options),
});
await orm.schema.refreshDatabase();
const authors = [
new Author({ id: 1, name: 'a' }),
new Author({ id: 2, name: 'b' }),
new Author({ id: 3, name: 'c' }),
new Author({ id: 4, name: 'd' }),
new Author({ id: 5, name: 'e' }),
];
orm.em.persist(authors);
const publishers = [
new Publisher({ id: 1, name: 'AAA' }),
new Publisher({ id: 2, name: 'BBB' }),
];
orm.em.persist(publishers);
const books = [
new Book({ id: 1, title: 'One', author: authors[0] }),
new Book({ id: 2, title: 'Two', author: authors[0] }),
new Book({ id: 3, title: 'Three', author: authors[1] }),
new Book({ id: 4, title: 'Four', author: authors[2] }),
new Book({ id: 5, title: 'Five', author: authors[2] }),
new Book({ id: 6, title: 'Six', author: authors[2] }),
];
books[0].publisher = ref(publishers[0]);
books[1].publisher = ref(publishers[1]);
books[2].publisher = ref(publishers[1]);
books[3].publisher = ref(publishers[1]);
books[4].publisher = ref(publishers[1]);
books[5].publisher = ref(publishers[1]);
orm.em.persist(books);
await orm.em.flush();
orm.em.clear();
});
afterAll(() => orm.close(true));
test('GH #4977', async () => {
const author = await orm.em.findOneOrFail(Author, { id: 1 });
const books = await author.books.loadItems({ populate: ['*'] });
books.forEach(({ author, publisher }) => {
expect(author.isInitialized()).toBeTruthy();
expect(publisher.isInitialized()).toBeTruthy();
});
expect(books).toMatchSnapshot();
});