Skip to content
VibORM
Esc
↑↓navigate↵open⌘Jpreview
On this page

BigInt

BigInt scalar type for large integers beyond JavaScript's Number.MAX_SAFE_INTEGER

Basic Usage

import { s } from "viborm";

s.bigInt()               // Required bigint
s.bigInt().nullable()    // bigint | null
s.bigInt().default(0n)   // With default

Use s.bigInt() when values can exceed Number.MAX_SAFE_INTEGER (2^53 - 1) — snowflake IDs, cryptocurrency amounts, nanosecond timestamps; otherwise s.int() is enough.

BigInt supports the full modifier reference. Column types live on Native Types.

Auto-Increment

s.bigInt().id().increment(); // Auto-incrementing bigint primary key

As with s.int(), an explicit 0n is refused on create only; zero filters are ordinary comparisons.

Examples

// Auto-incrementing primary key
const id = s.bigInt().id().increment();

// Twitter-style snowflake ID
const snowflakeId = s.bigInt().id();

// Large counter
const viewCount = s.bigInt().default(0n);

// Cryptocurrency amount (in smallest unit)
const satoshis = s.bigInt().default(0n);

Working with BigInt

// Create with bigint literal
await client.post.create({
  data: {
    id: 1234567890123456789n,
    viewCount: 0n,
  }
});

// Filter and update with bigint
await client.post.update({
  where: { id: 1n },
  data: {
    viewCount: { increment: 1n },
  }
});

Exact reads and raw SQL

Model reads are exact on every driver: VibORM selects a bigint column as text and parses it into a bigint, so a value beyond ±2^53 survives a driver that returns JavaScript numbers (Cloudflare D1 and Durable Objects, among others).

Raw SQL returns what the driver returns. On those drivers an integer beyond ±2^53 read by $queryRaw loses precision before VibORM sees it. Select it as text and convert it yourself:

const rows = await client.$queryRaw<{ id: string }>`
  SELECT CAST("id" AS TEXT) AS "id" FROM "post"
`;
const ids = rows.map((row) => BigInt(row.id));

Was this page helpful?