Back to Mikro Orm

Using native BigInt PKs (MySQL and PostgreSQL)

docs/versioned_docs/version-5.9/using-bigint-pks.md

7.0.14790 B
Original Source

We can use BigIntType to support bigints. By default, it will represent the value as a string.

ts
import { Entity, PrimaryKey, t } from '@mikro-orm/core';

@Entity()
export class Book {

  @PrimaryKey({ type: t.bigint })
  id: string;

}

bigint can fit larger numbers than JavaScript number, for this reason it is mapped to a string. If we want to map it to a number anyway, we can implement custom type that will do so. Similarly, we can define one to use the native bigint type:

ts
export class NativeBigIntType extends BigIntType {

  convertToJSValue(value: any): any {
    if (!value) {
      return value;
    }

    return BigInt(value);
  }

}

@Entity()
export class Book {

  @PrimaryKey({ type: NativeBigIntType })
  id: bigint;

}