chore(): upgrade mikro orm (#13450)

This commit is contained in:
Adrien de Peretti authored and GitHub committed 2025-09-19 21:39:18 +02:00
1 parent 4c1c1dd4c0
commit 8ece06d8ed
64 files changed
+836 -700

No files matched your search

@@ -1,6 +1,6 @@
import { BigNumberInput } from "@medusajs/types"
import { Property } from "@mikro-orm/core"
import { isDefined, isPresent, trimZeros } from "../../common"
import { EntityProperty, Platform, Property, Type } from "@mikro-orm/core"
import { isDefined, isObject, isPresent, trimZeros } from "../../common"
import { BigNumber } from "../../totals/big-number"
export function MikroOrmBigNumberProperty(
@@ -24,6 +24,11 @@ export function MikroOrmBigNumberProperty(
return value
},
set(value: BigNumberInput) {
// convert 'null' to null
if (value === "null") {
value = null as unknown as BigNumberInput
}
if (options?.nullable && !isPresent(value)) {
this.__helper.__data[columnName] = null
this.__helper.__data[rawColumnName] = null
@@ -89,7 +94,7 @@ export function MikroOrmBigNumberProperty(
})
Property({
type: "any",
type: BigNumberNumeric,
columnType: "numeric",
trackChanges: false,
runtimeType: "any",
@@ -97,3 +102,43 @@ export function MikroOrmBigNumberProperty(
})(target, columnName)
}
}
class BigNumberNumeric extends Type<string | number, string> {
constructor(public mode?: "number" | "string") {
super()
}
override convertToJSValue(value: string): number | string {
if ((this.mode ?? this.prop?.runtimeType) === "number") {
return +value
}
if (isObject(value)) {
return value // Special case for BigNumberRawValue because the setter will manage the dispatch automatically at a later stage
}
return String(value)
}
override compareValues(a: string, b: string): boolean {
return this.format(a) === this.format(b)
}
private format(val: string | number) {
/* istanbul ignore next */
if (this.prop?.scale == null) {
return +val
}
const base = Math.pow(10, this.prop.scale)
return Math.round((+val + Number.EPSILON) * base) / base
}
override getColumnType(prop: EntityProperty, platform: Platform) {
return platform.getDecimalTypeDeclarationSQL(prop)
}
override compareAsType(): string {
return this.mode ?? this.prop?.runtimeType ?? "string"
}
}
@@ -59,15 +59,15 @@ export class MikroOrmBase {
async transaction<TManager = unknown>(
task: (transactionManager: TManager) => Promise<any>,
options: {
manager?: TManager
isolationLevel?: string
enableNestedTransactions?: boolean
transaction?: TManager
} = {}
): Promise<any> {
this.manager_.global = true
return await transactionWrapper(this.manager_, task, options).catch(
dbErrorMapper
)
const manager = this.getFreshManager()
return await transactionWrapper(manager, task, options).catch(dbErrorMapper)
}
async serialize<TOutput extends object | object[]>(
@@ -463,12 +463,9 @@ export function mikroOrmBaseRepositoryFactory<const T extends object>(
if (!("strategy" in findOptions_.options)) {
if (findOptions_.options.limit != null || findOptions_.options.offset) {
// TODO: from 7+ it will be the default strategy
Object.assign(findOptions_.options, {
strategy: LoadStrategy.SELECT_IN,
})
} else {
Object.assign(findOptions_.options, {
strategy: LoadStrategy.JOINED,
strategy: LoadStrategy.BALANCED,
})
}
}
@@ -162,7 +162,7 @@ describe("Entity builder", () => {
kind: "scalar",
setter: true,
trackChanges: false,
type: "any",
type: expect.any(Function),
runtimeType: "any",
},
raw_spend_limit: {
@@ -306,7 +306,7 @@ describe("Entity builder", () => {
kind: "scalar",
setter: true,
trackChanges: false,
type: "any",
type: expect.any(Function),
runtimeType: "any",
},
raw_spend_limit: {
@@ -441,7 +441,7 @@ describe("Entity builder", () => {
kind: "scalar",
setter: true,
trackChanges: false,
type: "any",
type: expect.any(Function),
runtimeType: "any",
},
raw_spend_limit: {
@@ -555,7 +555,7 @@ describe("Entity builder", () => {
kind: "scalar",
setter: true,
trackChanges: false,
type: "any",
type: expect.any(Function),
runtimeType: "any",
},
raw_spend_limit: {
@@ -701,7 +701,7 @@ describe("Entity builder", () => {
kind: "scalar",
setter: true,
trackChanges: false,
type: "any",
type: expect.any(Function),
runtimeType: "any",
},
raw_spend_limit: {
@@ -858,7 +858,7 @@ describe("Entity builder", () => {
kind: "scalar",
setter: true,
trackChanges: false,
type: "any",
type: expect.any(Function),
runtimeType: "any",
},
created_at: {
@@ -6,7 +6,7 @@ import type {
Infer,
PropertyType,
} from "@medusajs/types"
import { Entity, Filter } from "@mikro-orm/core"
import { Entity, Filter, MetadataStorage } from "@mikro-orm/core"
import {
mikroOrmFreeTextSearchFilterOptionsFactory,
@@ -116,12 +116,16 @@ function createMikrORMEntity() {
Filter(mikroOrmFreeTextSearchFilterOptionsFactory(modelName))(
MikroORMEntity
)
const RegisteredEntity = Entity({ tableName })(
Filter(mikroOrmSoftDeletableFilterOptions)(MikroORMEntity)
) as Infer<T>
ENTITIES[modelName] = RegisteredEntity
return RegisteredEntity
Entity({ tableName })(
Filter(mikroOrmSoftDeletableFilterOptions)(MikroORMEntity)
) as any
const entityMetadata =
MetadataStorage.getMetadataFromDecorator(MikroORMEntity)
ENTITIES[modelName] = entityMetadata.class as Constructor<any>
return entityMetadata.class as Infer<T>
}
/**
@@ -16,6 +16,7 @@ describe("Internal Module Service Factory", () => {
[modelRepositoryName]: {
transaction: (task) => task(),
getFreshManager: jest.fn().mockReturnThis(),
getActiveManager: jest.fn().mockReturnThis(),
find: jest.fn(),
findAndCount: jest.fn(),
create: jest.fn(),
@@ -28,6 +29,7 @@ describe("Internal Module Service Factory", () => {
[`composite${Model.name}Repository`]: {
transaction: (task) => task(),
getFreshManager: jest.fn().mockReturnThis(),
getActiveManager: jest.fn().mockReturnThis(),
find: jest.fn(),
findAndCount: jest.fn(),
create: jest.fn(),
@@ -7,6 +7,7 @@ const baseRepoMock = {
serialize: jest.fn().mockImplementation((item) => item),
transaction: (task) => task("transactionManager"),
getFreshManager: jest.fn().mockReturnThis(),
getActiveManager: jest.fn().mockReturnThis(),
}
const defaultContext = {
@@ -23,7 +23,7 @@ export function InjectManager(managerProperty?: string): MethodDecorator {
const originalContext = args[argIndex] ?? {}
const copiedContext = {} as Context
for (const key in originalContext) {
if (key === "manager" || key === "transactionManager") {
if (key === "transactionManager") {
continue
}
@@ -51,8 +51,8 @@ export function InjectManager(managerProperty?: string): MethodDecorator {
)
}
copiedContext.manager =
originalContext.manager ?? resourceWithManager.getFreshManager()
copiedContext.manager ??=
resourceWithManager.getFreshManager(originalContext)
if (originalContext?.transactionManager) {
copiedContext.transactionManager = originalContext?.transactionManager
@@ -35,7 +35,7 @@ export function InjectTransactionManager(
async (transactionManager) => {
const copiedContext = {} as Context
for (const key in originalContext) {
if (key === "manager" || key === "transactionManager") {
if (key === "transactionManager") {
continue
}
@@ -52,10 +52,6 @@ export function InjectTransactionManager(
copiedContext.transactionManager = transactionManager
if (originalContext?.manager) {
copiedContext.manager = originalContext?.manager
}
copiedContext.__type = MedusaContextType
args[argIndex] = copiedContext
@@ -63,6 +59,7 @@ export function InjectTransactionManager(
return await originalMethod.apply(this, args)
},
{
manager: originalContext?.manager,
transaction: originalContext?.transactionManager,
isolationLevel: (originalContext as Context)?.isolationLevel,
enableNestedTransactions: