-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Support bigint mode when using better-sqlite3
#6328
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
Support bigint mode when using better-sqlite3
#6328
Conversation
Hi there. In brief, I was working on a larger set of changes that would address this need systematically (with respect to the types returned by queries), and add safety to better-sqlite3 specifically by rejecting queries that return numbers that are not safe (in other words: changing the default to always use safeIntegers, casting the values back to number, and throwing an error if it would be unsafe to do so). I prepared individual pieces that could be reviewed and/or merged separately, but I was not sure whether the work in this PR would interact with the other PRs I was preparing, so I had it staged separately until everything was working together. (This PR is not ready for review, it was just holding one of those pieces) I'm leaning towards coming back to that deeper work later and focusing on getting PRs merged so that I don't wind up generating a bunch of merge conflicts, which means that I can likely close this in favor of your PR (it wouldn't wind up changing to integrate with the safety change). If you'd like, I'm happy to work with you to get your original PR merged instead of this one? The main requests I'd have are to support per-query configuration of this option and to utilize the existing integration testing framework instead of making ad-hoc knex instances in the tests. You can see what I mean in the code here. |
|
@myndzi
If you’re able to help get this merged as PR #6032, I’d be happy to try incorporating the feedback you shared and iterating on the implementation. |
For now, I'm shelving the full "casting behavior" thing. It's mergeable with a few more tests, but I'm not happy with the compromises I had to make; there is a clear point to refactor that would clean up those compromises, but it would touch more code than I want to change until we've knocked down the PR backlog. I definitely agree with you that the extra safety is an improvement, but I want to be clear that it's not expected to be solved by this PR.
Let me come back to point 3 in a moment. First, I should apologize for not having caught your PR in the first place. I'm still kinda in a mode where I expect all our PRs to be old and mostly abandoned. So I wound up creating a PR that does exactly what I would want from yours already 😅. This leaves me in an awkward position, because I'd rather merge yours and give you the contribution credit. I'm very happy to spend some time helping you understand the decisions I made (which are by no means the final word or anything!) if you're interested. Especially if you're planning on contributing further (we're seeking all the help we can get!), there's a lot of value in getting folks up to speed on the codebase and how things work / what patterns are used and why. With that said,
When I skimmed your PR, I didn't actually notice that the tests you added were in the knexInstance = knex({
client: 'better-sqlite3',
useNullAsDefault: true,
connection: {
filename: ':memory:',
options: {
defaultSafeIntegers: true,
},
},
});This is actively "connecting" to a real sqlite3 instance using the Since you chose to do it this way, I believe you already understand why an integration test is correct here: the goal is to ensure that the code that was written actually has the desired effect, and the only way to do that is to actually exercise the interface between knex and the While the way that integration tests are run (and databases are selected) could use some improvement, it will be easiest to implement that improvement later, after the tests in all the pending PRs have been merged. Keeping them "working the same way as the current tests work" makes that job easier in the future. Next steps
If you update your PR to satisfy these things, it'll be good to go. Feel free to ask me questions here (or join the discord) if you need something, I'm happy to help get it there. |
|
@myndzi
I frequently use Knex in the Node.js ecosystem, and if there are areas that need improvement, I’m more than willing to contribute as long as time allows.
As you mentioned, the tests in my PR needed to verify interactions with the actual driver, so they should have been written as integration tests, and writing them as unit tests was not appropriate. Although it’s a bit embarrassing, I realize that my understanding of the tests I wrote was insufficient, and thanks to your detailed explanation, I now understand what I missed. Thank you. 🙏
I strongly agree with the gradual improvement approach you suggested. I was planning to work on my PR (#6320) by incorporating the work from the current PR (#6328) and reflecting the next steps feedback, but it looks like it has already been merged… 😅 Should I proceed by working on this as a separate PR? |
|
@dong-jun-shin yup, iterating in a separate PR would be great! |
Replaces #5482, fixes #6319
This change adds support for selecting how to treat integers (number or bigint) when using
better-sqlite3. Adds code, documentation, and tests for using the knex factory's client options and query builder.optionsmethod to enable or disable numbers-as-bigints.Further work incoming, but this is useful out of the box and isolated; some of the other work involves trickier refactoring.
Open question: should we use the property
defaultSafeIntegersin the factory options? This is not a pass-through option to the driver no matter what it is named, so here I've kept it with the namesafeIntegersin both places it's used.Related: #6327
DRI:@myndzi