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:
- TypeORM: How to Limit Query Execution Time
- TypeORM: 2 Ways to Exclude a Column from being Selected
- TypeORM: Adding Fields with Nullable/Default Data
- Using ENUM Type in TypeORM
- PostgreSQL: How to Rename a Column of a Table
- Unsigned (non-negative) Integer in PostgreSQL
You can also check out our Node.js topic page and database topic page for the latest tutorials and examples.