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:
committed by
GitHub
parent
753bd93ba1
commit
b3d826497b
@@ -1,21 +1,40 @@
|
||||
import { BigNumberRawValue } from "@medusajs/types"
|
||||
import { BigNumber } from "../../../totals/big-number"
|
||||
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", () => {
|
||||
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", () => {
|
||||
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()
|
||||
|
||||
expect(testAmount.amount).toBeUndefined()
|
||||
@@ -24,7 +43,6 @@ describe("@MikroOrmBigNumberProperty", () => {
|
||||
testAmount.amount = 100
|
||||
|
||||
expect(testAmount.amount).toEqual(100)
|
||||
expect((testAmount as any).amount_).toEqual(100)
|
||||
expect(testAmount.raw_amount).toEqual({
|
||||
value: "100",
|
||||
precision: 20,
|
||||
@@ -45,7 +63,6 @@ describe("@MikroOrmBigNumberProperty", () => {
|
||||
testAmount.amount = 200
|
||||
|
||||
expect(testAmount.amount).toEqual(200)
|
||||
expect((testAmount as any).amount_).toEqual(200)
|
||||
expect(testAmount.raw_amount).toEqual({
|
||||
value: "200",
|
||||
precision: 20,
|
||||
@@ -56,7 +73,6 @@ describe("@MikroOrmBigNumberProperty", () => {
|
||||
testAmount.amount = new BigNumber(300, { precision: 5 })
|
||||
|
||||
expect(testAmount.amount).toEqual(300)
|
||||
expect((testAmount as any).amount_).toEqual(300)
|
||||
expect(testAmount.raw_amount).toEqual({ value: "300", precision: 5 })
|
||||
})
|
||||
})
|
||||
|
||||
@@ -9,16 +9,15 @@ export function MikroOrmBigNumberProperty(
|
||||
} = {}
|
||||
) {
|
||||
return function (target: any, columnName: string) {
|
||||
const targetColumn = columnName + "_"
|
||||
const rawColumnName = options.rawColumnName ?? `raw_${columnName}`
|
||||
|
||||
Object.defineProperty(target, columnName, {
|
||||
get() {
|
||||
return this[targetColumn]
|
||||
return this.__helper.__data[columnName]
|
||||
},
|
||||
set(value: BigNumberInput) {
|
||||
if (options?.nullable && !isPresent(value)) {
|
||||
this[targetColumn] = null
|
||||
this.__helper.__data[columnName] = null
|
||||
this[rawColumnName] = null
|
||||
|
||||
return
|
||||
@@ -42,30 +41,22 @@ export function MikroOrmBigNumberProperty(
|
||||
bigNumber = new BigNumber(value)
|
||||
}
|
||||
|
||||
this[targetColumn] = bigNumber.numeric
|
||||
this.__helper.__data[columnName] = bigNumber.numeric
|
||||
|
||||
const raw = bigNumber.raw!
|
||||
raw.value = trimZeros(raw.value as string)
|
||||
|
||||
this[rawColumnName] = raw
|
||||
|
||||
this.__helper.__touched = !this.__helper.hydrator.isRunning()
|
||||
},
|
||||
})
|
||||
|
||||
Property({
|
||||
type: "number",
|
||||
columnType: "numeric",
|
||||
fieldName: columnName,
|
||||
serializer: () => {
|
||||
return undefined
|
||||
},
|
||||
trackChanges: false,
|
||||
...options,
|
||||
})(target, targetColumn)
|
||||
|
||||
Property({
|
||||
type: "number",
|
||||
persist: false,
|
||||
getter: true,
|
||||
setter: true,
|
||||
})(target, columnName)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user