diff --git a/.changeset/honest-poets-sniff.md b/.changeset/honest-poets-sniff.md new file mode 100644 index 0000000000000..47f4e6f24bf05 --- /dev/null +++ b/.changeset/honest-poets-sniff.md @@ -0,0 +1,5 @@ +--- +"@medusajs/utils": patch +--- + +chore(utils): Improve big number decorator diff --git a/packages/utils/src/dal/mikro-orm/__tests__/big-number-field.spec.ts b/packages/utils/src/dal/mikro-orm/__tests__/big-number-field.spec.ts index 43a9e8e0a3777..77b439d84e298 100644 --- a/packages/utils/src/dal/mikro-orm/__tests__/big-number-field.spec.ts +++ b/packages/utils/src/dal/mikro-orm/__tests__/big-number-field.spec.ts @@ -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() @@ -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 }) }) }) diff --git a/packages/utils/src/dal/mikro-orm/big-number-field.ts b/packages/utils/src/dal/mikro-orm/big-number-field.ts index a931e392d0abe..5641b3467939f 100644 --- a/packages/utils/src/dal/mikro-orm/big-number-field.ts +++ b/packages/utils/src/dal/mikro-orm/big-number-field.ts @@ -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) } }