MikroORM7.2 中文文档
实战配方/使用原生 BigInt 主键(MySQL 与 PostgreSQL)
已译 96/96

使用原生 BigInt 主键(MySQL 与 PostgreSQL)

从 v6 开始,bigint 由原生的 BigInt 类型表示,因此不需要在装饰器选项中显式指定类型:

@PrimaryKey()
id: bigint;

你也可以指定希望把 bigint 映射到的目标类型:

@PrimaryKey({ type: new BigIntType('bigint') })
id1: bigint;

@PrimaryKey({ type: new BigIntType('string') })
id2: string;

@PrimaryKey({ type: new BigIntType('number') })
id3: number;

映射到 number 类型时,JavaScript 无法表示 bigint 的所有可能取值——只有不超过 Number.MAX_SAFE_INTEGER(2^53 - 1)的值才是安全的。