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.
105 lines (78 loc) • 1.76 kB
text/typescript
import { Collection, Entity, ManyToOne, MikroORM, OneToMany, OneToOne, PrimaryKey, Property } from '@mikro-orm/sqlite';
()
class Author {
()
id!: number;
()
name: string;
(() => Book, book => book.author)
books = new Collection<Book>(this);
constructor(name: string) {
this.name = name;
}
}
()
class Book {
()
id!: number;
()
title!: string;
()
author!: Author;
constructor(title: string, author: Author) {
this.title = title;
this.author = author;
}
}
()
class Publisher {
()
id!: number;
()
name: string;
()
owner!: Author;
constructor(name: string, owner: Author) {
this.name = name;
this.owner = owner;
}
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Author, Book, Publisher],
dbName: ':memory:',
ensureDatabase: { create: true },
});
});
afterAll(async () => {
await orm.close(true);
});
test('$some on nested relation', async () => {
const author1 = new Author('Author 1');
const author2 = new Author('Author 2');
const book1 = new Book('Book 1', author1);
const book2 = new Book('Book 2', author1);
const book3 = new Book('Book 3', author2);
const publisher1 = new Publisher('Publisher 1', author1);
const publisher2 = new Publisher('Publisher 2', author2);
await orm.em.fork().persistAndFlush([
publisher1,
publisher2,
author1,
author2,
book1,
book2,
book3,
]);
const res = await orm.em.find(Publisher, {
owner: {
books: {
$some: {
title: 'Book 2',
},
},
},
});
expect(res).toHaveLength(1);
});