-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Labels
Description
Environment
Knex version: Latest
Database + version: better-sqlite3
OS: All
Feature discussion / request
- Explain what kind of behaviour you are getting and how you think it should do
When using the better-sqlite3 dialect, the defaultSafeIntegers option passed in the connection configuration is ignored. This prevents users from enabling BigInt support for safe integer handling in better-sqlite3.
The expected behavior is that if defaultSafeIntegers: true is passed in the connection options, Knex should configure the better-sqlite3 database instance to use safe integers (returning BigInts for integers).
- Error message
No error message is thrown, but data precision loss occurs for large integers because they are returned as Javascript Numbers instead of BigInts.
Example:
- Input (BigInt):
9223372036854776603 - Output (Number):
9223372036854776600(Precision lost)
- Reduced test code
const Knex = require('knex');
const Database = require('better-sqlite3');
const knex = Knex({
client: 'better-sqlite3',
connection: {
filename: ':memory:',
defaultSafeIntegers: true, // This option is currently ignored
},
});
async function main() {
await knex.schema.createTable('test', (t) => {
t.bigInteger('id');
});
const bigIntId = BigInt('9223372036854776603'); // Max BigInt
console.log('Input:', bigIntId); // 9223372036854776603n
await knex('test').insert({ id: bigIntId });
const result = await knex('test').first();
console.log('Type of id:', typeof result.id);
console.log('Value of id:', result.id);
// Actual Output: 9223372036854776600 (Number)
// Expected Output: 9223372036854776603n (BigInt)
if (typeof result.id === 'bigint') {
console.log('PASS: Returned as BigInt');
} else {
console.log('FAIL: Returned as ' + typeof result.id);
}
await knex.destroy();
}
main();