Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

better-sqlite3: defaultSafeIntegers option is ignored #6319

@dong-jun-shin

Description

@dong-jun-shin

Environment

Knex version: Latest
Database + version: better-sqlite3
OS: All

Feature discussion / request

  1. 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).

  1. 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)
  1. 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();

Relation Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions