-
-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): fix custom pivot table entities for unidirectional relations
- Loading branch information
Showing
6 changed files
with
226 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
tests/features/composite-keys/__snapshots__/custom-pivot-entity-uni.sqlite.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`custom pivot entity for m:n with additional properties (unidirectional) schema 1`] = ` | ||
"pragma foreign_keys = off; | ||
create table \`order\` (\`id\` integer not null primary key autoincrement, \`paid\` integer not null, \`shipped\` integer not null, \`created\` datetime not null); | ||
create table \`product\` (\`id\` integer not null primary key autoincrement, \`name\` text not null, \`current_price\` integer not null); | ||
create table \`order_item\` (\`order_id\` integer not null, \`product_id\` integer not null, \`amount\` integer not null default 1, \`offered_price\` integer not null default 0, constraint \`order_item_order_id_foreign\` foreign key(\`order_id\`) references \`order\`(\`id\`) on update cascade, constraint \`order_item_product_id_foreign\` foreign key(\`product_id\`) references \`product\`(\`id\`) on update cascade, primary key (\`order_id\`, \`product_id\`)); | ||
create index \`order_item_order_id_index\` on \`order_item\` (\`order_id\`); | ||
create index \`order_item_product_id_index\` on \`order_item\` (\`product_id\`); | ||
pragma foreign_keys = on; | ||
" | ||
`; |
2 changes: 1 addition & 1 deletion
2
tests/features/composite-keys/__snapshots__/custom-pivot-entity.sqlite.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
tests/features/composite-keys/custom-pivot-entity-uni.sqlite.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
import { Entity, PrimaryKey, MikroORM, ManyToOne, PrimaryKeyType, Property, wrap, OneToMany, Collection, ManyToMany } from '@mikro-orm/core'; | ||
|
||
@Entity() | ||
export class Order { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
@OneToMany(() => OrderItem, item => item.order) | ||
items = new Collection<OrderItem>(this); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
@ManyToMany({ entity: () => Product, pivotEntity: () => OrderItem }) | ||
products = new Collection<Product>(this); | ||
|
||
@Property() | ||
paid: boolean = false; | ||
|
||
@Property() | ||
shipped: boolean = false; | ||
|
||
@Property() | ||
created: Date = new Date(); | ||
|
||
} | ||
|
||
@Entity() | ||
export class Product { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property() | ||
name: string; | ||
|
||
@Property() | ||
currentPrice: number; | ||
|
||
constructor(name: string, currentPrice: number) { | ||
this.name = name; | ||
this.currentPrice = currentPrice; | ||
} | ||
|
||
} | ||
|
||
@Entity() | ||
export class OrderItem { | ||
|
||
@ManyToOne({ primary: true }) | ||
order: Order; | ||
|
||
@ManyToOne({ primary: true }) | ||
product: Product; | ||
|
||
@Property({ default: 1 }) | ||
amount!: number; | ||
|
||
@Property({ default: 0 }) | ||
offeredPrice: number; | ||
|
||
[PrimaryKeyType]: [number, number]; | ||
|
||
constructor(order: Order, product: Product) { | ||
this.order = order; | ||
this.product = product; | ||
this.offeredPrice = product.currentPrice; | ||
} | ||
|
||
} | ||
|
||
describe('custom pivot entity for m:n with additional properties (unidirectional)', () => { | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [Product, OrderItem, Order], | ||
dbName: ':memory:', | ||
type: 'sqlite', | ||
}); | ||
await orm.getSchemaGenerator().createSchema(); | ||
}); | ||
|
||
afterAll(() => orm.close(true)); | ||
|
||
beforeEach(() => orm.getSchemaGenerator().clearDatabase()); | ||
|
||
test(`schema`, async () => { | ||
const sql = await orm.getSchemaGenerator().getCreateSchemaSQL(); | ||
expect(sql).toMatchSnapshot(); | ||
}); | ||
|
||
async function createEntities() { | ||
const order1 = new Order(); | ||
const order2 = new Order(); | ||
const order3 = new Order(); | ||
const product1 = new Product('p1', 111); | ||
const product2 = new Product('p2', 222); | ||
const product3 = new Product('p3', 333); | ||
const product4 = new Product('p4', 444); | ||
const product5 = new Product('p5', 555); | ||
const item11 = new OrderItem(order1, product1); | ||
item11.offeredPrice = 123; | ||
const item12 = new OrderItem(order1, product2); | ||
item12.offeredPrice = 3123; | ||
const item21 = new OrderItem(order2, product1); | ||
item21.offeredPrice = 4123; | ||
const item22 = new OrderItem(order2, product2); | ||
item22.offeredPrice = 1123; | ||
const item23 = new OrderItem(order2, product5); | ||
item23.offeredPrice = 1263; | ||
const item31 = new OrderItem(order3, product3); | ||
item31.offeredPrice = 7123; | ||
const item32 = new OrderItem(order3, product4); | ||
item32.offeredPrice = 9123; | ||
const item33 = new OrderItem(order3, product5); | ||
item33.offeredPrice = 5123; | ||
|
||
await orm.em.fork().persistAndFlush([order1, order2, order3]); | ||
return { order1, order2, product1, product2, product3, product4, product5 }; | ||
} | ||
|
||
test(`should work`, async () => { | ||
const { product1, product2, product3, product4, product5 } = await createEntities(); | ||
const productRepository = orm.em.getRepository(Product); | ||
|
||
const orders = await orm.em.find(Order, {}, { populate: true }); | ||
expect(orders).toHaveLength(3); | ||
|
||
// test M:N lazy load | ||
orm.em.clear(); | ||
let order = (await orm.em.findOne(Order, { products: product1.id }))!; | ||
expect(order.products.isInitialized()).toBe(false); | ||
await order.products.init(); | ||
expect(order.products.isInitialized()).toBe(true); | ||
expect(order.products.count()).toBe(2); | ||
expect(order.products.getItems()[0]).toBeInstanceOf(Product); | ||
expect(order.products.getItems()[0].id).toBeDefined(); | ||
expect(wrap(order.products.getItems()[0]).isInitialized()).toBe(true); | ||
|
||
// test collection CRUD | ||
// remove | ||
expect(order.products.count()).toBe(2); | ||
order.products.remove(t => t.id === product1.id); // we need to get reference as product1 is detached from current EM | ||
await orm.em.persistAndFlush(order); | ||
orm.em.clear(); | ||
order = (await orm.em.findOne(Order, order.id, { populate: ['products'] as const }))!; | ||
expect(order.products.count()).toBe(1); | ||
|
||
// add | ||
order.products.add(productRepository.getReference(product1.id)); // we need to get reference as product1 is detached from current EM | ||
const product6 = new Product('fresh', 555); | ||
order.products.add(product6); | ||
await orm.em.persistAndFlush(order); | ||
orm.em.clear(); | ||
order = (await orm.em.findOne(Order, order.id, { populate: ['products'] as const }))!; | ||
expect(order.products.count()).toBe(3); | ||
|
||
// contains | ||
expect(order.products.contains(productRepository.getReference(product1.id))).toBe(true); | ||
expect(order.products.contains(productRepository.getReference(product2.id))).toBe(true); | ||
expect(order.products.contains(productRepository.getReference(product3.id))).toBe(false); | ||
expect(order.products.contains(productRepository.getReference(product4.id))).toBe(false); | ||
expect(order.products.contains(productRepository.getReference(product5.id))).toBe(false); | ||
expect(order.products.contains(productRepository.getReference(product6.id))).toBe(true); | ||
|
||
// removeAll | ||
order.products.removeAll(); | ||
await orm.em.persistAndFlush(order); | ||
orm.em.clear(); | ||
order = (await orm.em.findOne(Order, order.id, { populate: ['products'] as const }))!; | ||
expect(order.products.count()).toBe(0); | ||
}); | ||
|
||
test(`search by m:n property and loadCount() works`, async () => { | ||
await createEntities(); | ||
const res = await orm.em.find(Order, { products: { name: 'p1' } }); | ||
expect(res).toHaveLength(2); | ||
const count = await res[0].products.loadCount(); | ||
expect(count).toBe(2); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters