Kinda Code
Home/Node/TypeORM: How to store BIGINT correctly

TypeORM: How to store BIGINT correctly

Last updated: April 22, 2022

If you’re using TypeORM to work with PostgreSQL or MySQL and want to save BIGINT (from -2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807)) to your database, you have to define your column as follows:

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    email: string;

    // BigInt column here
    @Column({type: 'bigint'})
    bigThing: string;
}

The important thing here is that bigint values are too large to be represented by the number primitive. In TypeORM, they don’t fit into the regular number type and you should map a bigint property to a string instead. If you set number type, you are likely to run into the error: integer out of range.

Further reading:

You can also check out our Node.js topic page and database topic page for the latest tutorials and examples.