Skip to content
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

Unable To Process Numeric Type #54

Open
R-K-H opened this issue Mar 31, 2022 · 9 comments
Open

Unable To Process Numeric Type #54

R-K-H opened this issue Mar 31, 2022 · 9 comments
Labels
enhancement New feature or request

Comments

@R-K-H
Copy link

R-K-H commented Mar 31, 2022

Hello,

Screen Shot 2022-03-31 at 6 26 30 PM

Forgive my ignorance for the types configuration parameter, but is there a way to process numeric's when setting up a query? My work around so far has been type casting through the use of ::NUMERIC, but I have to hard code the value into the query for the library to play nice with everything else.

Is there an override I might apply or something I can do (I've even cast it as DataType.Numeric, but that doesn't help as it's not supported).

Thanks,

@malthe
Copy link
Owner

malthe commented Apr 1, 2022

There currently is no support for the numeric type – mostly because there is no obvious type on the JavaScript-side.

But you can cast it to text on the database-side and then parse the representation into your desired value type. You could also extract the integer part and then decimal part into two different columns.

@R-K-H
Copy link
Author

R-K-H commented Apr 1, 2022

Understood. But I am unable to use it in an INSERT query the database side is fine to handle as it's cast as NUMERIC, but the javascript side will not accept this as the text literal as passed.

Just for clarity:

const uuid = "c5e72e18-f3d7-446c-9c0e-243a26ac0176";
const balance_total = 0.02477774221871698;
const details = {};

const query = new Query(
"INSERT INTO balances (account_uuid, balance_total, details)
VALUES ($1::UUID, $2::NUMERIC, $3::JSONB)",
[uuid, balance_total, details]
);

No matter what I do to balance_total (set it to string, convert to BN) I am unable to get it to work with the above query.

Thanks for your help.

@malthe
Copy link
Owner

malthe commented Apr 11, 2022

Lots of decimals there! Is this crypto? ;-)

I would advise you to avoid using floating point to represent money. It is better to use an integer – for USD this would be cents, for Bitcoin perhaps it would be a millionth of a Bitcoin.

@R-K-H
Copy link
Author

R-K-H commented Apr 11, 2022

Been using this data model for years and have over 33B records. Just trying to see if there is a work around for processing it using the library.

Numeric type has been more than accommodating for my use case.

@malthe
Copy link
Owner

malthe commented Apr 11, 2022

Yes – on the PostgreSQL side it is (because you can specify the number of digits). But on the JavaScript side, you're stuck with a hybrid number type that has limited precision.

That said, you should be able to do $2::TEXT::NUMERIC – or you could try $2::FLOAT::NUMERIC.

@R-K-H
Copy link
Author

R-K-H commented Apr 12, 2022

Roger. Thanks for the tip. Is there any documentation for parsing it say by using BN.js or some sort. I've looked through the code and didn't see any way off the first glance.

Appreciate your help.

@malthe
Copy link
Owner

malthe commented Apr 13, 2022

The problem is that we'd have to go through https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/numeric.c to figure out how to parse the numeric encoding.

That's not so difficult but is there a "canonical library" for doing numeric values in JavaScript? It could also be returned as a Number, or as [Number, Number] to improve accuracy.

Or as a string?

@R-K-H
Copy link
Author

R-K-H commented Apr 14, 2022

Nothing that I know of off the top of my head (and some searching), just some libraries that web3 frameworks etc are using for their support of large decimals for crypto.

https://github.com/indutny/bn.js/
https://mikemcl.github.io/decimal.js/

Those are the only two I've seen in 3rd party SDKs so far, a lot of structure around these types encoded on the platform / service side...

@malthe
Copy link
Owner

malthe commented Apr 14, 2022

I suppose one option could be to always return unsupported types as text – this way it is rather straight-forward for the user to convert to some other value.

@malthe malthe added the enhancement New feature or request label Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants