Denis Stephanov
Denis Stephanov

Reputation: 5261

Store decimal number with TypeORM and BigNumber.js

on my entity decimal field looks like this:

  @Column('decimal', { precision: 10, scale: 2 })
  amount!: BigNumber

I map this entity field from my DTO, which handle request body of nest js controller. In DTO I have defines field like this:

  @Transform(value => new BigNumber(value))
  amount!: BigNumber

when I remove @Transform(value => new BigNumber(value)) code start working but amount field of DTO isn't BigNumber object, just holds string with amount. In my service component need use BigNumber's functions but I can't because this amount behaves like a string.

For instance, when I call *.compareTo() on DTO field, I got error that there is no function compareTo, if I added @Transform decorator to DTO, compareTo and other functions works fine, but I have error in DB. I am using postgres. Thank you for any advice.

Upvotes: 1

Views: 6022

Answers (1)

makroso
makroso

Reputation: 11

Adding @Type(() => BigNumber) below @Transform decorator will probably help. See "Working with nested objects" section in https://www.npmjs.com/package/class-transformer

Upvotes: 1

Related Questions