---
title: BigInt
description: BigInt scalar type for large integers beyond JavaScript's Number.MAX_SAFE_INTEGER
icon: binary
---

## Basic Usage

```ts
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()`](/docs/schema/scalars/number) is enough.

BigInt supports the full [modifier reference](/docs/schema/scalars). Column types live on [Native Types](/docs/schema/native-types).

## Auto-Increment

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

As with [`s.int()`](/docs/schema/scalars/number#auto-increment), an explicit
`0n` is refused on create only; zero filters are ordinary comparisons.

## Examples

```ts
// 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

```ts
// 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:

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