expeditavoluptas
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.
56 lines (39 loc) • 1.29 kB
text/typescript
import { Entity, PrimaryKey, MikroORM, ManyToOne, PrimaryKeyType, Property, Collection, ManyToMany } from '@mikro-orm/core';
import { SqliteDriver } from '@mikro-orm/sqlite';
()
export class Order {
()
id!: number;
({ entity: () => Product, pivotEntity: () => OrderItem })
products = new Collection<Product>(this);
}
()
export class Product {
()
id!: number;
({ entity: () => Order, pivotEntity: () => OrderItem })
orders = new Collection<Order>(this);
}
()
export class OrderItem {
({ primary: true })
order: Order;
({ primary: true })
product: Product;
({ default: 1 })
amount!: number;
[PrimaryKeyType]?: [number, number];
constructor(order: Order, product: Product) {
this.order = order;
this.product = product;
}
}
test(`validation of bidirectional M:N with pivotEntity`, async () => {
const err = `Product.orders and Order.products use the same 'pivotEntity', but don't form a bidirectional relation. Specify 'inversedBy' or 'mappedBy' to link them.`;
await expect(MikroORM.init({
entities: [Product, OrderItem, Order],
dbName: ':memory:',
driver: SqliteDriver,
connect: false,
})).rejects.toThrowError(err);
});