aldis-ameriks / pg-typegen

Generate TypeScript type definitions from Postgres database
MIT License
7 stars 1 forks source link

bigint postgres type is generated as string #18

Closed gevera closed 2 years ago

gevera commented 2 years ago

Hi there. I've noticed an issue while generating types

CREATE TABLE IF NOT EXISTS person(
    id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    first_name varchar(50) NOT NULL,
    last_name varchar(50),
    email varchar(80) UNIQUE,
    telephone varchar(80),
    active boolean DEFAULT true
);

Result

export interface PersonEntity {
  active: boolean | null;
  email: string | null;
  first_name: string;
  id: string;
  last_name: string | null;
  telephone: string | null;
};

Expected the id to be a number and not a string

And thank you for this great library

aldis-ameriks commented 2 years ago

Hi @gevera. Sorry for late reply.

BigInt is actually returned as string in javascript as default, by most postgres database clients. This is because bigint is 64 bit integer, however, javascript supports integer values up to 53 bits (numbers in javascript are 64 bit floats), higher numbers will start having rounding errors, which can be disastrous when dealing with database fields like ids.

Alternatively, javascript has a dedicated type BigInt, which can be enabled in pg-typegen, by setting the "bigint" flag. However, if you do use BigInt, keep in mind that there will be JSON serialization issues to consider. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

gevera commented 2 years ago

That makes sense. I guess we can close the issue