Core features

Integer fidelity

Preserve SQLite integers outside JavaScript's safe number range and account for Expo's numeric limits.

SQLite stores signed 64-bit integers, while a JavaScript number represents an integer exactly only between -9,007,199,254,740,991 and 9,007,199,254,740,991. The Node.js, Bun, and browser drivers of Sirannon return an integer inside that range as a number and a larger one as a bigint.

Store a value past the safe range

When a value needs the precision of a bigint, write the integer literal with an n suffix. JavaScript rounds a plain number literal for 9,007,199,254,740,993 before the database receives it.

The complete integers.ts example stores that value together with an ordinary count in a temporary database file.

integers.ts
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { Sirannon } from '@delali/sirannon-db'
import { betterSqlite3 } from '@delali/sirannon-db/driver/better-sqlite3'
 
const directory = await mkdtemp(join(tmpdir(), 'sirannon-integers-'))
const sirannon = new Sirannon({ driver: betterSqlite3() })
 
try {
  const db = await sirannon.open('logistics', join(directory, 'logistics.db'))
  await db.execute('CREATE TABLE shipments (id INTEGER PRIMARY KEY, tracking_code INTEGER NOT NULL, parcels INTEGER NOT NULL)')
  await db.execute('INSERT INTO shipments (id, tracking_code, parcels) VALUES (?, ?, ?)', [1, 9007199254740993n, 12])
  const shipment = await db.queryOne<{ tracking_code: bigint; parcels: number }>(
    'SELECT tracking_code, parcels FROM shipments WHERE id = ?', [1]
  )
  if (shipment === undefined) throw new Error('The shipment is missing')
  console.log(shipment)
  console.log(typeof shipment.tracking_code, typeof shipment.parcels)
} finally {
  try {
    await sirannon.shutdown()
  } finally {
    await rm(directory, { recursive: true, force: true })
  }
}

Read the exact value back

The query returns the tracking code as a bigint and the parcel count as a number. The safe range includes both of its ends, so the driver returns 9,007,199,254,740,991 as a number.

The driver can therefore return either type for one column, depending on the value in each row. Type the column as number | bigint when the values in your application can cross that boundary. By the same rule, lastInsertRowId can be either type.

JSON.stringify throws on a bigint, so convert the value to a decimal string for the JSON of your application, or use the tagged value encoding of Sirannon when you implement its protocol.

How each driver behaves

DriverBehaviour
betterSqlite3The driver enables safe integers in better-sqlite3 and narrows safe values to numbers.
nodeSqliteThe driver enables setReadBigInts and applies the same narrowing.
bunSqliteThe driver opens Bun SQLite with safeIntegers: true and applies the same narrowing.
waSqliteThe driver reads SQLite integers through column_int64 and narrows safe values to numbers.
expoSqliteThe driver converts a safe bigint parameter to a number and refuses a larger bigint parameter. Expo returns every numeric value as a JavaScript number.

The Node.js, Bun, and browser drivers preserve the full range of signed SQLite integers. The server tags every integer outside the safe range in its replies, so a client receives the results of these drivers with every digit intact.

Plan for Expo

The Expo driver accepts a bigint parameter only when the value is inside the safe integer range of JavaScript. For a larger bigint, the driver throws a RangeError while it binds the parameter, and the query rejects with QUERY_ERROR. Because of that refusal, the driver binds every bigint either exactly or not at all.

JavaScript rounds an unsafe number before the driver receives it. Expo returns numeric results as JavaScript numbers as well, so the driver is unable to recover the digits that Expo rounds.

When an Expo app works with large identifiers, store them as TEXT. Bind and compare them as decimal strings, and limit numeric columns to values inside the safe range. Text preserves every digit of an identifier, although SQLite sorts text lexically by default. SQLite converts the text to a number for arithmetic, so Expo can round the result of that arithmetic when it returns it.