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.
158 lines (125 loc) • 2.8 kB
text/typescript
import { MikroORM } from '@mikro-orm/sqlite';
import { v4 } from 'uuid';
import {
Collection,
Entity,
ManyToOne,
OneToMany,
PrimaryKey,
PrimaryKeyProp,
Property,
Ref,
Unique,
} from '@mikro-orm/core';
class Company {
id: string = v4();
name!: string;
}
class User {
id: string = v4();
}
class Reader {
[PrimaryKeyProp]?: ['user_id', 'company_id', 'book_id'];
user!: Ref<User>;
company!: Ref<Company>;
book!: Ref<Book>;
}
class Book {
[PrimaryKeyProp]?: ['id', 'company'];
id: string = v4();
company!: Ref<Company>;
readers = new Collection<Reader>(this);
reviewers = new Collection<BookReviewer>(this);
}
class BookReviewer {
id: string = v4();
company!: Ref<Company>;
book!: Ref<Book>;
user!: Ref<User>;
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Company, Book, User],
dbName: `:memory:`,
});
await orm.schema.createSchema();
});
afterAll(async () => {
await orm.close(true);
});
test('sharing column in composite pk + seeding', async () => {
const company = orm.em.create(Company, { name: 'c' });
const user = orm.em.create(User, {});
const book = orm.em.create(Book, { company });
const reader = orm.em.create(Reader, {
book: [book.id, company.id],
company: company.id,
user: user.id,
});
await orm.em.flush();
const reviewer = orm.em.create(BookReviewer, {
book: [book.id, company.id],
company: company.id,
user: user.id,
});
await orm.em.flush();
expect(reviewer.book.unwrap()).toBe(book);
});