Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

sqlite-value.ts

30 lines872 Bytes

1import { SQLiteBigIntOutOfRangeError } from './errors'
2​
3export const SQLITE_BIGINT_MIN = -9_223_372_036_854_775_808n
4export const SQLITE_BIGINT_MAX = 9_223_372_036_854_775_807n
5export const PERSISTED_TYPE_TAG = `__tanstack_db_persisted_type__`
6export const PERSISTED_VALUE_TAG = `value`
7​
8export type SerializedSQLiteBigInt = {
9 [PERSISTED_TYPE_TAG]: `bigint`
10 [PERSISTED_VALUE_TAG]: string
12​
13export function assertSQLiteBigIntInRange(value: bigint): bigint {
14 if (value < SQLITE_BIGINT_MIN || value > SQLITE_BIGINT_MAX) {
15 throw new SQLiteBigIntOutOfRangeError(
16 value,
17 SQLITE_BIGINT_MIN,
18 SQLITE_BIGINT_MAX,
19 )
20 }
21 return value
23​
24export function serializeSQLiteBigInt(value: bigint): SerializedSQLiteBigInt {
25 assertSQLiteBigIntInRange(value)
26 return {
27 [PERSISTED_TYPE_TAG]: `bigint`,
28 [PERSISTED_VALUE_TAG]: value.toString(),
29 }