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 hydration of relations with custom types via joined st…
…rategy This was resulting in an additional query used by the fallback select-in mechanism, so it wasn't surfacing that obviously. Closes #5518
- Loading branch information
Showing
3 changed files
with
77 additions
and
1 deletion.
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
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,72 @@ | ||
import { Collection, Entity, ManyToOne, MikroORM, OneToMany, PrimaryKey, Property, Ref, types } from '@mikro-orm/sqlite'; | ||
import { mockLogger } from '../helpers'; | ||
|
||
abstract class BaseEntity { | ||
|
||
@PrimaryKey({ | ||
type: types.uuid, | ||
onCreate: () => crypto.randomUUID(), | ||
}) | ||
id!: string; | ||
|
||
} | ||
|
||
@Entity() | ||
class Product extends BaseEntity { | ||
|
||
@Property() | ||
name!: string; | ||
|
||
@Property() | ||
price!: number; | ||
|
||
} | ||
|
||
@Entity() | ||
class Order extends BaseEntity { | ||
|
||
@OneToMany(() => OrderItem, e => e.order) | ||
items!: Collection<OrderItem>; | ||
|
||
} | ||
|
||
|
||
@Entity() | ||
class OrderItem { | ||
|
||
@ManyToOne(() => Order, { primary: true, ref: true }) | ||
order!: Ref<Order>; | ||
|
||
@ManyToOne(() => Product, { primary: true, ref: true }) | ||
product!: Ref<Product>; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: ':memory:', | ||
entities: [Product, Order, OrderItem], | ||
}); | ||
await orm.schema.refreshDatabase(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
test('basic CRUD example', async () => { | ||
const product = orm.em.create(Product, { name: 'Product 1', price: 100 }); | ||
const order = orm.em.create(Order, {}); | ||
order.items.add(orm.em.create(OrderItem, { order, product })); | ||
await orm.em.flush(); | ||
orm.em.clear(); | ||
|
||
const mock = mockLogger(orm); | ||
const [item] = await orm.em.find(OrderItem, { order: order.id }, { | ||
populate: ['product'], | ||
}); | ||
expect(item.product.isInitialized()).toBe(true); | ||
expect(mock).toHaveBeenCalledTimes(1); | ||
}); |