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
Best effort support for JS BigInt in queries #14485
Conversation
4162dc2
to
e7696e1
Compare
7154930
to
89e43b8
Compare
89e43b8
to
49a5d2d
Compare
Thank you for this PR, I'll set some time aside this friday/weekend to review this |
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.
Very nice, only a few notes. Thank you very much for your work on this!
Co-authored-by: Zoé <zoe@ephys.dev>
Co-authored-by: Zoé <zoe@ephys.dev>
Co-authored-by: Zoé <zoe@ephys.dev>
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 really appreciate you taking this off my backlog, thank you! :)
🎉 This PR is included in version 7.0.0-alpha.13 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This adds supports for sending JS BigInt values to databases. For retrieving SQL BigInts as JS BigInts, see sequelize#14296
Pull Request Checklist
Please make sure to review and check all of these items:
yarn test
oryarn test-DIALECT
pass with this change (including linting)?Description Of Change
bigint
primitives inModel.findByPk
bigint
s in SQL queries unless necessary because of limitations in the underlying driverBackground
I'm working on an app that uses Postgres and needs to use the full
BIGINT
/INT8
range for PK columns. The obvious thing is to use JavaScriptBigInt
s for that to prevent loss of precision when going overNumber.MAX_SAFE_INTEGER
.I saw #14296 that aims to address how values from the database get mapped to values in JavaScript.
Even without something like that, I found out that sequelize already has some support for
BigInt
, and that configuring thepg
driver to deserializeINT8
asBigInt
actually gets me very far:That just left
Model.findByPk
, as passing aBigInt
to that throws:That was easy to fix (at least for Postgres), it was just a matter of allowing
BigInt
s through in the type check that threw the error.Then I noticed that when I passed a
BigInt
toModel.findByPk
or into awhere
object, the number would become a string literal in the resulting SQL. That seemed a bit wasteful, so I tried to fix that also. Getting it to work with all the dialects was a bit tricky, as not all of the underlying drivers supportBigInt
yet. Thus the "best effort" label. I still think it's worth it to try to DTRT when aBigInt
is passed into a query.