Skip to content

Commit

Permalink
chore(utils): Improve big number decorator (#6525)
Browse files Browse the repository at this point in the history
**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
  • Loading branch information
adrien2p authored Feb 27, 2024
1 parent 753bd93 commit b3d8264
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-poets-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/utils": patch
---

chore(utils): Improve big number decorator
42 changes: 29 additions & 13 deletions packages/utils/src/dal/mikro-orm/__tests__/big-number-field.spec.ts
Original file line number Diff line number Diff line change
@@ -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"

describe("@MikroOrmBigNumberProperty", () => {
it("should correctly assign and update BigNumber values", () => {
class TestAmount {
@MikroOrmBigNumberProperty()
amount: BigNumber | number
@Entity()
class TestAmount {
@PrimaryKey()
id: string

raw_amount: BigNumberRawValue
@MikroOrmBigNumberProperty()
amount: BigNumber | number

@MikroOrmBigNumberProperty({ nullable: true })
nullable_amount: BigNumber | number | null = null
raw_amount: BigNumberRawValue

raw_nullable_amount: BigNumberRawValue | null = null
}
@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", () => {
const testAmount = new TestAmount()

expect(testAmount.amount).toBeUndefined()
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 })
})
})
21 changes: 6 additions & 15 deletions packages/utils/src/dal/mikro-orm/big-number-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}

0 comments on commit b3d8264

Please sign in to comment.