Search code examples
numbersprecisionloopback

loopback number precision


I'm using postgresql with loopback. I have a field in a model called amount being used as a Number. Now midway through the project, a new requirement came to hold amount value with precision. But I won't be able to store amount for example. as 1.50, as it is converted and stored as 1.5, which happens with javascript number type values.

Other approach for holding precision is to converting the property from number to string. But this change will be a major code change throughout all the modules.

Also there is a property called precision and scale for PostgreSQL connector. Is there any way in the model declaration (.json file) itself to hold precision with type number? Just wanted to be sure if there is any other approach before going through this change. Any help will be highly appreciated.


Solution

  • You can these fields to the model.json. I have not tested the code but i think this will work

    "amount": {
      "type": "Number",
      "required": true,
      "postgresql": {
          "dataType": "numeric",
          "dataPrecision": 20,
          "dataScale": 0
        }
    }