loopbackio / loopbackio/loopback-next
Add a built-in Decimal/Numeric type
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 5.1k
- Forks
- 1.1k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 27
Description
When working with money (currency) values, it's important to use a data type that preserves exact precision. Consider the following example illustrating the problem of using JavaScript's built-in number type (floating-point numbers):
$ node
> 0.1 + 0.2
0.30000000000000004
We should add a new type allowing users to safely work with Decimal values.
- When storing the value in a database, connectors should use database-specific Decimal type.
- MongoDB has
Decimal128, see https://github.com/strongloop/loopback-connector-mongodb/pull/475 - PostgreSQL
- Microsoft SQL
- Oracle
- MySQL
- db2
- MongoDB has
- At runtime, it would be great if model data could use a Decimal type too. Unfortunately JavaScript does not provide such a built-in type, but there are npm modules like big.js that can be leveraged as a workaround. Alternatively, we can represent Decimal values as strings for the initial iteration.
- In JSON, decimal values must be represented as strings to avoid loss of precision.
- Code emitting JSON Schema (Swagger, OpenAPI etc.) should indicate that the value type is decimal. Unfortunately, OpenAPI does not seem to support
Decimaltype and while there were some discussions, typically proposing{type: number format: decimal}, most of them ended with no outcome. Since LB4 will be storing decimals in string format, I think we should use{type: string, format: decimal}as the schema for Decimal values.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No files or tests are named. Trace existing model type handling, database connector mappings, JSON serialization, and OpenAPI or JSON Schema generation; done means Decimal values preserve precision, use connector-specific database types, serialize as strings, and emit a string/decimal schema.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, openapi, typescript
- Domain
- api, backend-api-design, databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100