Skip to content

Commit 62e0760

Browse files
authored
chore: update mikro-orm to 6.6.12 (#15018)
* chore: update mikro-orm to 6.6.10 * fix refund test * fix * fix alias bug * fix error related to autoJoinRefsForFilters * update to latest
1 parent 2b0cd5f commit 62e0760

File tree

7 files changed

+143
-89
lines changed

7 files changed

+143
-89
lines changed

.changeset/rare-snakes-itch.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@medusajs/utils": patch
3+
"@medusajs/deps": patch
4+
---
5+
6+
chore: update mikro-orm to 6.6.10

packages/core/utils/src/dal/mikro-orm/mikro-orm-create-connection.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ export async function mikroOrmCreateConnection(
113113
useBatchUpdates: true,
114114
implicitTransactions: false,
115115
ignoreUndefinedInQuery: true,
116+
// Introduced in MikroORM 6.5.0: when enabled, MikroORM auto-joins referenced entities
117+
// (e.g. INNER JOINs PaymentSession when querying Payment) to apply their global filters
118+
// (e.g. softDeletable). For non-nullable FKs this uses INNER JOIN, silently excluding
119+
// owning entities (e.g. Payment) when the referenced entity (e.g. PaymentSession) is
120+
// soft-deleted. Medusa was designed around MikroORM 6.4.x where this didn't exist, so
121+
// we disable it to preserve the expected behavior.
122+
autoJoinRefsForFilters: false,
116123
batchSize: 100,
117124
metadataCache: {
118125
enabled: true,

packages/core/utils/src/modules-sdk/medusa-internal-service.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,9 @@ export function registerInternalServiceEventSubscriber(
5555
context.manager) as EntityManager
5656
if (manager && subscriber) {
5757
const subscriberInstance = new subscriber(context)
58-
// There is no public API to unregister subscribers or check if a subscriber is already
59-
// registered. This means that we need to manually check if the subscriber is already
60-
// registered, otherwise we will register the same subscriber twice.
61-
const hasListeners = (manager.getEventManager() as any).subscribers.some(
62-
(s) => s.constructor.name === subscriberInstance.constructor.name
63-
)
58+
const hasListeners = Array.from(
59+
manager.getEventManager().getSubscribers()
60+
).some((s) => s.constructor.name === subscriberInstance.constructor.name)
6461
if (!hasListeners) {
6562
manager.getEventManager().registerSubscriber(subscriberInstance)
6663
}

packages/deps/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
"build": "yarn run -T rimraf dist && yarn run -T tsc --build"
4040
},
4141
"dependencies": {
42-
"@mikro-orm/cli": "6.4.16",
43-
"@mikro-orm/core": "6.4.16",
44-
"@mikro-orm/knex": "6.4.16",
45-
"@mikro-orm/migrations": "6.4.16",
46-
"@mikro-orm/postgresql": "6.4.16",
42+
"@mikro-orm/cli": "6.6.12",
43+
"@mikro-orm/core": "6.6.12",
44+
"@mikro-orm/knex": "6.6.12",
45+
"@mikro-orm/migrations": "6.6.12",
46+
"@mikro-orm/postgresql": "6.6.12",
4747
"@opentelemetry/api": "^1.9.0",
4848
"@opentelemetry/instrumentation-pg": "^0.52.0",
4949
"@opentelemetry/resources": "^2.0.0",

packages/modules/order/src/utils/base-repository-find.ts

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Constructor, Context, DAL } from "@medusajs/framework/types"
22
import { MikroOrmBaseRepository, toMikroORMEntity } from "@medusajs/framework/utils"
3-
import { LoadStrategy } from "@medusajs/framework/mikro-orm/core"
3+
import { LoadStrategy, raw } from "@medusajs/framework/mikro-orm/core"
44
import { Order, OrderClaim, OrderLineItemAdjustment } from "@models"
55

66
import { mapRepositoryToOrderModel } from "."
@@ -116,7 +116,13 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
116116
const version = config.where?.version ?? defaultVersion
117117
delete config.where?.version
118118

119-
configurePopulateWhere(config, isRelatedEntity, version)
119+
configurePopulateWhere(
120+
config,
121+
isRelatedEntity,
122+
version,
123+
strategy === LoadStrategy.SELECT_IN,
124+
manager
125+
)
120126

121127
let loadAdjustments = false
122128
if (config.options.populate.includes("items.item.adjustments")) {
@@ -253,11 +259,16 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
253259
})
254260
}
255261

256-
const [result, count] = await manager.findAndCount(
257-
this.entity,
258-
config.where,
259-
config.options
260-
)
262+
// The count query uses JOINED strategy internally (MikroORM 6.6+), but
263+
// populateWhere version subqueries reference SELECT_IN aliases (e.g. "o0")
264+
// that don't exist in the JOINED count context. Since version filters only
265+
// control which items to load (not which root entities to count), we run
266+
// find and count separately with different populateWhere options.
267+
const countOptions = { ...config.options, populateWhere: undefined }
268+
const [result, count] = await Promise.all([
269+
manager.find(this.entity, config.where, config.options),
270+
manager.count(this.entity, config.where, countOptions),
271+
])
261272

262273
if (loadAdjustments) {
263274
const orders = !isRelatedEntity
@@ -339,36 +350,65 @@ function configurePopulateWhere(
339350
config.options.populateWhere ??= {}
340351
const popWhere = config.options.populateWhere
341352

342-
// isSelectIn && isRelatedEntity - Order is always the FROM clause (field o0.id)
343353
if (isRelatedEntity) {
344354
popWhere.order ??= {}
345355

346356
const popWhereOrder = popWhere.order
347357

348-
popWhereOrder.version = isSelectIn
349-
? getVersionSubQuery(manager, "o0", "id")
350-
: version
358+
if (!isSelectIn) {
359+
// For JOINED strategy, version is a reference to the order alias (e.g. "o1"."version")
360+
// This is trivially true since Order has one row per id, but kept for consistency
361+
popWhereOrder.version = version
362+
}
363+
// For SELECT_IN strategy, the order.version condition is always trivially true
364+
// (Order has one row per id) so we skip it entirely
351365

352366
// related entity shipping method
353367
if (hasRelation("shipping_methods")) {
354368
popWhere.shipping_methods ??= {}
355-
popWhere.shipping_methods.version = isSelectIn
356-
? getVersionSubQuery(manager, "s0")
357-
: version
369+
if (isSelectIn) {
370+
// For MikroORM 6.6.x+, populateWhere conditions for force-joined relations are
371+
// embedded as inline JOIN conditions. Use alias callback so [::alias::] gets
372+
// replaced with the actual shipping_method alias at query-build time.
373+
const fragment = raw(
374+
(alias) =>
375+
`"${alias}"."version" = (select "_sub0"."version" from "order" as "_sub0" where "_sub0"."id" = "${alias}"."order_id")`
376+
)
377+
;(popWhere.shipping_methods as any)[fragment.toString()] = []
378+
} else {
379+
popWhere.shipping_methods.version = version
380+
}
358381
}
359382

360383
if (hasRelation("items") || hasRelation("order.items")) {
361384
popWhereOrder.items ??= {}
362-
popWhereOrder.items.version = isSelectIn
363-
? getVersionSubQuery(manager, "o0", "id")
364-
: version
385+
if (isSelectIn) {
386+
// In MikroORM 6.6.x+, the global alias counter changed (no longer per-entity-type),
387+
// so "o0" no longer exists in the query context for related entity queries.
388+
// Instead, use a self-referential raw fragment: the [::alias::] placeholder is
389+
// replaced with the order_item's own JOIN alias, and order_item.order_id is used
390+
// to look up the order's current version.
391+
const fragment = raw(
392+
(alias) =>
393+
`"${alias}"."version" = (select "_sub0"."version" from "order" as "_sub0" where "_sub0"."id" = "${alias}"."order_id")`
394+
)
395+
;(popWhereOrder.items as any)[fragment.toString()] = []
396+
} else {
397+
popWhereOrder.items.version = version
398+
}
365399
}
366400

367401
if (hasRelation("shipping_methods")) {
368402
popWhereOrder.shipping_methods ??= {}
369-
popWhereOrder.shipping_methods.version = isSelectIn
370-
? getVersionSubQuery(manager, "o0", "id")
371-
: version
403+
if (isSelectIn) {
404+
const fragment = raw(
405+
(alias) =>
406+
`"${alias}"."version" = (select "_sub0"."version" from "order" as "_sub0" where "_sub0"."id" = "${alias}"."order_id")`
407+
)
408+
;(popWhereOrder.shipping_methods as any)[fragment.toString()] = []
409+
} else {
410+
popWhereOrder.shipping_methods.version = version
411+
}
372412
}
373413

374414
return

packages/modules/pricing/src/repositories/pricing.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class PricingRepository
5151
`
5252
)
5353
this.#availableAttributes.clear()
54-
rows.forEach(({ attribute }) => {
54+
rows.forEach(({ attribute }: { attribute: string }) => {
5555
this.#availableAttributes.add(attribute)
5656
})
5757
}
@@ -72,11 +72,11 @@ export class PricingRepository
7272
const context = { ...(pricingContext.context || {}) }
7373

7474
// Extract quantity and currency from context
75-
const quantity = context.quantity
75+
const quantity = context.quantity as number | undefined
7676
delete context.quantity
7777

7878
// Currency code is required
79-
const currencyCode = context.currency_code
79+
const currencyCode = context.currency_code as string | undefined
8080
delete context.currency_code
8181

8282
if (!currencyCode) {

0 commit comments

Comments
 (0)