SQLite stores signed 64-bit integers. JavaScript numbers represent every integer exactly only between -9,007,199,254,740,991 and 9,007,199,254,740,991. Sirannon's Node, Bun, and browser drivers return safe integers as number and larger integers as bigint.
Store a value past the safe range
Write an integer literal with an n suffix when it needs bigint precision. A plain number literal for 9,007,199,254,740,993 rounds before the database receives it.
The complete integers.ts example stores that value alongside an ordinary count in a temporary database file.
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 tracking code returns as a bigint, while the parcel count returns as a number. The safe-range boundary is inclusive at both ends.
A column can therefore return either type depending on its value. Use number | bigint when application data can cross that boundary. lastInsertRowId follows the same rule.
JSON.stringify cannot serialise a bigint directly. Convert it to a decimal string for application JSON, or use Sirannon's tagged value encoding when implementing its protocol.
How each driver behaves
| Driver | Behaviour |
|---|---|
betterSqlite3 | The driver enables safe integers in better-sqlite3 and narrows safe values to numbers. |
nodeSqlite | The driver enables setReadBigInts and applies the same narrowing. |
bunSqlite | The driver opens Bun SQLite with safeIntegers: true and applies the same narrowing. |
waSqlite | The driver reads SQLite integers through column_int64 and narrows safe values to numbers. |
expoSqlite | The driver converts safe bigint parameters to numbers and refuses larger bigint parameters. Expo reads numeric values through JavaScript numbers. |
The Node, Bun, and browser drivers preserve the full signed SQLite integer range. Their result values can also pass through the client protocol without precision loss because the server tags integers outside the safe range.
Plan for Expo
The experimental Expo driver accepts a bigint parameter only when it fits within JavaScript's safe integer range. A larger bigint fails binding with a RangeError, which the query API reports as a query failure. This refusal prevents an unsafe bigint from silently rounding during binding.
An unsafe number has already lost precision before the driver receives it. Numeric results from Expo also use JavaScript numbers, so the driver cannot recover digits that Expo has rounded.
Store large identifiers as TEXT when Expo needs them. Bind and compare decimal strings as text, and keep numeric columns within the safe range. Text preserves the identifier's digits, but its default ordering is lexical and SQL arithmetic does not retain the same text-only guarantee.