-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add BigNumber implementation #6253
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
3 Ignored Deployments
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing 🔥
const val = new BigNumber(this.raw_unit_price ?? this.unit_price) | ||
|
||
this.unit_price = val.numeric | ||
this.raw_unit_price = val.raw! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we can create a util to do that for 1 or N fields.
modelToBigNumber(this, "unit_price")
or
modelToBigNumber(this, ["unit_price", "quantity"])
subliminal suggestion to set quantity as BigNumber 😆
aa036cc
to
921da4f
Compare
42dd181
to
a9f3308
Compare
What
Introduces a BigNumber class implementation, enabling us to work with high-precision numeric values.
Scope
Not in scope
How
There are significant changes to three areas in this PR:
Schemas
Domains that need high-precision values will have two DB columns for each value in the database: a standard numeric column and a raw value column.
The standard column is for basic operations like sorting and filtering in the database and is what should be publicly exposed in our API.
The raw value is initially used solely for precise calculations and is stored as a JSONB column. Keeping it as JSONB is flexible and will allow us to extend the concept in future iterations. As of now, the raw value will only require a single property
value
.(De)-Serialization
We cast the raw JSONB value to a
BigNumberRawValue
when reading from the database.We serialize the standard value to a
BigNumber
when reading from the database.We use the standard numeric value to construct the raw value upon writing to the database.
For example, the unit price and raw unit price on line items will be inserted as follows:
Totals calculations
For totals calculations, we will use the
bignumber.js
library. The library ships with aBigNumber
class with arithmetic methods for precise calculations.When we need to perform a calculation, we construct the BigNumber class from the library using the raw value from the database.
Let's have a look at an oversimplified example:
A note on backward compatibility
Our BigNumber implementation is built to support the existing behavior of numeric values in the database. So even though we serialize the value to a BigNumber, you will still be able to treat it as a standard number, as we've always done.
For example, the following works perfectly fine:
However, the type of
unit_price
will benumber | BigNumber
.