loopbackio / loopbackio/loopback-next

Add a built-in Decimal/Numeric type

Open
#1,902 5 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature Juggler major OpenAPI Schema
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.
  • 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 Decimal type 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.