Storing decimal numbers using TypeORM and the BigNumber.js library is a reliable and

My entity's decimal field is defined as follows:

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

I am mapping this entity field from my DTO, which manages the request body of my NestJS controller. In the DTO, the field is defined like this:

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

Removing

@Transform(value => new BigNumber(value))
allows the code to work, but the amount field in the DTO is not a BigNumber object - it just holds a string value. However, in my service component, I need to use functions specific to BigNumber, which becomes impossible since the amount behaves like a string.

For example, when I try to call *.compareTo() on the DTO field, I receive an error stating that the function compareTo does not exist. On the other hand, if I add the @Transform decorator to the DTO, functions like compareTo work fine, but then I encounter errors with the database (I am using PostgreSQL). Thank you for any advice.

Answer №1

If you include @Type(() => BigNumber) after the @Transform decorator, it should improve the functionality. For more details, refer to the "Working with nested objects" section on https://www.npmjs.com/package/class-transformer

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I prevent buttons from being created using ngFor in Angular?

I need help with creating an HTML table that includes a cell with a button and a dropdown generated using ngFor. How can I disable the buttons (generated via ngFor) if no value is selected from the dropdown? Here's what I have tried so far: In my App ...

Issue with Angular UI Bootstrap accordion heading behavior when used in conjunction with a checkbox is causing

I have implemented a checkbox in the header of an accordion control using Bootstrap. However, I am facing an issue where the model only updates the first time the checkbox is clicked. Below is the HTML code for the accordion: <accordion ng-repeat="tim ...

Modifying the appearance of a Component within a NavLink

I'm currently working on a navbar using NavLink from React-Router-Dom. It's fine to use the 'isActive' prop to style the active Link, but I'm stuck on how to style the subelements inside it. For more specific details, please take a ...

Deployment of Django/Postgres app on AWS ElasticBeanstalk encountered an unsuccessful outcome

Struggling to deploy my Django, Postgres, DjangoQ, Redis, and ES app on AWS Elastic Beanstalk using docker-compose.yml. I used EB CLI (eb init, eb create) and it shows the environment is launched successfully but I'm facing some issues. The EC2 ins ...

What sets apart module imports in JavaScript and Typescript?

Exploring the realm of Shadow DOM and Custom elements, I've encountered an interesting discrepancy between how JavaScript (JS) and TypeScript (TS) handle modular imports. Am I missing something here? My primary JS file has this structure... // impor ...

Show a few values as a string in Angular using Typescript

I am working with JSON data and I want to know how to display hobbies without including the word "all" in an Angular/TypeScript application. { "Name": "Mustermann1", "Vorname": "Max1", "maennlich& ...

The JestImportMeta interface is mistakenly extending the ImportMeta interface, causing an error

While transitioning from jest version 27 to v29, I encountered this issue: node_modules/@jest/environment/build/index.d.ts:329:26 - error TS2430: Interface 'JestImportMeta' improperly extends interface 'ImportMeta'. The types returned ...

What is the best way to access all the attributes (excluding methods) of an object in a class instance?

My goal is to generate a new object type that inherits all the properties from an existing class instance. In other words, I am looking for a way to transform a class instance into a plain object. For example, consider the following scenario: ...

Issue with package: Unable to locate the module specified as './artifacts/index.win32-ia32-msvc.node'

I am encountering an issue while using Parcel for the first time. When I execute npx parcel .\app\index.html, I receive the following error: Error: Module not found './artifacts/index.win32-ia32-msvc.node' Require stack: - C:\Users ...

Enhance Button functionality in Mantine TypeScript with props

I am looking to implement a feature where a button changes its background color based on the current page or path within my application. My approach involves adding a new prop (e.g., selected) to the Button component in Mantine using TypeScript. To achie ...

How can you use console.log() effectively in an Angular application?

While debugging an Angular project, I have encountered a situation where console.log() does not seem to be working properly despite ensuring that all logging levels are enabled in the Chrome console settings. I have tried placing the console.log() statemen ...

Does an async function get automatically awaited if called in a constructor?

I am currently working on updating some code due to a library upgrade that requires it to be made async. The code in question has a base class that is inherited by other classes, and I need to call some functions in the constructor that are now asynchronou ...

Tips for adding type definitions to an ObjectConstructor in your Typescript code within .tsx files

When I use my custom object constructor for object.keys and .values in .ts files, everything works fine. However, when I try to use them in .tsx files, the compiler interprets it as JSX and I get an error. const myObject = (<TypedObject>Object).keys( ...

Guide to Implementing StoreApi in Zustand LibraryLearn how to utilize Store

While reading the documentation for zustand, I came across a useful piece of information. In addition to the standard `set` and `get` parameters, there is an extra parameter called `api` in the StateCreator function. Check out the example below: import cr ...

Error in Typescript: Function not being triggered on button click

As someone who is just starting out with typescript, I've been tackling a simple code that should display an alert message in the browser when a button is clicked. I've experimented with the button and input tags, as well as using both onclick ev ...

Determining the data type of a property within an interface using TypeScript

Is there a way to extract the type from an interface based on its property name in order to use it in a Record? I am struggling with the syntax needed to retrieve the type by property name. My goal is to make this process more future-proof so that if the i ...

Listening to Azure Service Bus messages using NestJS

I have a unique service that manages the business logic for my product. Whenever a message is received through the Azure service bus, I need to call this service. To retrieve messages from the Azure service bus queue, we are utilizing the Azure npm packa ...

Correctly inputting MongoDB-specific collection methods which are currently defaulting to "any"

I'm facing an issue with typing Mongo collection method responses in my app. Specifically, I am having trouble finding a way to type the response of the deleteMany method. The documented response should look like this: { "acknowledged" : true, "delete ...

There is no function called regexp_like(character varying, unknown) available

I am currently working on a Postgresql query using SQL. select * from cdr_data where REGEXP_LIKE(identifiant,'^73') and REGEXP_REPLACE(callednumber,'^256','') ~ '^73' An error is occurring when I run the quer ...

Angular Compilation Blocked Due to Circular Dependency Error

Currently, I am utilizing WebStorm as my IDE to work on a personal project that I envision turning into a game in the future. The primary goal of this project is to create an Alpha version that I can showcase to potential employers, as I am actively seekin ...