chore(utils): Improve big number decorator (#6525)
**What** - Re add the ability to use the original column name as part of a select in both select in and joined strategy - The undefined error message will now display the original column name instead of the underscored one
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/utils": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
chore(utils): Improve big number decorator
|
||||||
@@ -1,21 +1,40 @@
|
|||||||
import { BigNumberRawValue } from "@medusajs/types"
|
import { BigNumberRawValue } from "@medusajs/types"
|
||||||
import { BigNumber } from "../../../totals/big-number"
|
import { BigNumber } from "../../../totals/big-number"
|
||||||
import { MikroOrmBigNumberProperty } from "../big-number-field"
|
import { MikroOrmBigNumberProperty } from "../big-number-field"
|
||||||
|
import { Entity, MikroORM, PrimaryKey } from "@mikro-orm/core"
|
||||||
|
|
||||||
|
@Entity()
|
||||||
|
class TestAmount {
|
||||||
|
@PrimaryKey()
|
||||||
|
id: string
|
||||||
|
|
||||||
|
@MikroOrmBigNumberProperty()
|
||||||
|
amount: BigNumber | number
|
||||||
|
|
||||||
|
raw_amount: BigNumberRawValue
|
||||||
|
|
||||||
|
@MikroOrmBigNumberProperty({ nullable: true })
|
||||||
|
nullable_amount: BigNumber | number | null = null
|
||||||
|
|
||||||
|
raw_nullable_amount: BigNumberRawValue | null = null
|
||||||
|
}
|
||||||
|
|
||||||
describe("@MikroOrmBigNumberProperty", () => {
|
describe("@MikroOrmBigNumberProperty", () => {
|
||||||
|
let orm!: MikroORM
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
orm = await MikroORM.init({
|
||||||
|
entities: [TestAmount],
|
||||||
|
dbName: "test",
|
||||||
|
type: "postgresql",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await orm.close()
|
||||||
|
})
|
||||||
|
|
||||||
it("should correctly assign and update BigNumber values", () => {
|
it("should correctly assign and update BigNumber values", () => {
|
||||||
class TestAmount {
|
|
||||||
@MikroOrmBigNumberProperty()
|
|
||||||
amount: BigNumber | number
|
|
||||||
|
|
||||||
raw_amount: BigNumberRawValue
|
|
||||||
|
|
||||||
@MikroOrmBigNumberProperty({ nullable: true })
|
|
||||||
nullable_amount: BigNumber | number | null = null
|
|
||||||
|
|
||||||
raw_nullable_amount: BigNumberRawValue | null = null
|
|
||||||
}
|
|
||||||
|
|
||||||
const testAmount = new TestAmount()
|
const testAmount = new TestAmount()
|
||||||
|
|
||||||
expect(testAmount.amount).toBeUndefined()
|
expect(testAmount.amount).toBeUndefined()
|
||||||
@@ -24,7 +43,6 @@ describe("@MikroOrmBigNumberProperty", () => {
|
|||||||
testAmount.amount = 100
|
testAmount.amount = 100
|
||||||
|
|
||||||
expect(testAmount.amount).toEqual(100)
|
expect(testAmount.amount).toEqual(100)
|
||||||
expect((testAmount as any).amount_).toEqual(100)
|
|
||||||
expect(testAmount.raw_amount).toEqual({
|
expect(testAmount.raw_amount).toEqual({
|
||||||
value: "100",
|
value: "100",
|
||||||
precision: 20,
|
precision: 20,
|
||||||
@@ -45,7 +63,6 @@ describe("@MikroOrmBigNumberProperty", () => {
|
|||||||
testAmount.amount = 200
|
testAmount.amount = 200
|
||||||
|
|
||||||
expect(testAmount.amount).toEqual(200)
|
expect(testAmount.amount).toEqual(200)
|
||||||
expect((testAmount as any).amount_).toEqual(200)
|
|
||||||
expect(testAmount.raw_amount).toEqual({
|
expect(testAmount.raw_amount).toEqual({
|
||||||
value: "200",
|
value: "200",
|
||||||
precision: 20,
|
precision: 20,
|
||||||
@@ -56,7 +73,6 @@ describe("@MikroOrmBigNumberProperty", () => {
|
|||||||
testAmount.amount = new BigNumber(300, { precision: 5 })
|
testAmount.amount = new BigNumber(300, { precision: 5 })
|
||||||
|
|
||||||
expect(testAmount.amount).toEqual(300)
|
expect(testAmount.amount).toEqual(300)
|
||||||
expect((testAmount as any).amount_).toEqual(300)
|
|
||||||
expect(testAmount.raw_amount).toEqual({ value: "300", precision: 5 })
|
expect(testAmount.raw_amount).toEqual({ value: "300", precision: 5 })
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -9,16 +9,15 @@ export function MikroOrmBigNumberProperty(
|
|||||||
} = {}
|
} = {}
|
||||||
) {
|
) {
|
||||||
return function (target: any, columnName: string) {
|
return function (target: any, columnName: string) {
|
||||||
const targetColumn = columnName + "_"
|
|
||||||
const rawColumnName = options.rawColumnName ?? `raw_${columnName}`
|
const rawColumnName = options.rawColumnName ?? `raw_${columnName}`
|
||||||
|
|
||||||
Object.defineProperty(target, columnName, {
|
Object.defineProperty(target, columnName, {
|
||||||
get() {
|
get() {
|
||||||
return this[targetColumn]
|
return this.__helper.__data[columnName]
|
||||||
},
|
},
|
||||||
set(value: BigNumberInput) {
|
set(value: BigNumberInput) {
|
||||||
if (options?.nullable && !isPresent(value)) {
|
if (options?.nullable && !isPresent(value)) {
|
||||||
this[targetColumn] = null
|
this.__helper.__data[columnName] = null
|
||||||
this[rawColumnName] = null
|
this[rawColumnName] = null
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -42,30 +41,22 @@ export function MikroOrmBigNumberProperty(
|
|||||||
bigNumber = new BigNumber(value)
|
bigNumber = new BigNumber(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
this[targetColumn] = bigNumber.numeric
|
this.__helper.__data[columnName] = bigNumber.numeric
|
||||||
|
|
||||||
const raw = bigNumber.raw!
|
const raw = bigNumber.raw!
|
||||||
raw.value = trimZeros(raw.value as string)
|
raw.value = trimZeros(raw.value as string)
|
||||||
|
|
||||||
this[rawColumnName] = raw
|
this[rawColumnName] = raw
|
||||||
|
|
||||||
|
this.__helper.__touched = !this.__helper.hydrator.isRunning()
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
Property({
|
Property({
|
||||||
type: "number",
|
type: "number",
|
||||||
columnType: "numeric",
|
columnType: "numeric",
|
||||||
fieldName: columnName,
|
trackChanges: false,
|
||||||
serializer: () => {
|
|
||||||
return undefined
|
|
||||||
},
|
|
||||||
...options,
|
...options,
|
||||||
})(target, targetColumn)
|
|
||||||
|
|
||||||
Property({
|
|
||||||
type: "number",
|
|
||||||
persist: false,
|
|
||||||
getter: true,
|
|
||||||
setter: true,
|
|
||||||
})(target, columnName)
|
})(target, columnName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user