RobinBlomberg / kysely-codegen

Generate Kysely type definitions from your database.
MIT License
771 stars 69 forks source link

Postgres Numeric: select numeric field by number #161

Open dcposch opened 3 months ago

dcposch commented 3 months ago

Summary

Currently, a Postgres numeric can be inserted as number, but only queried as a string.

I know that Postgres numeric types can hold values larger than the ~2^53 largest JS integer, so I understand the rationale for allowing strings, but not why it's mandatory.

Proposed fix

Current:

export type Numeric = ColumnType<string, number | string, number | string>;

We could simplify to:

export type Numeric = string | number;

Upvote & Fund

Fund with Polar

dcposch commented 3 months ago

Happy to submit a PR, let me know.

RobinBlomberg commented 1 month ago

Hello! I just added the CLI option --numeric-parser which will be released in kysely-codegen@0.16.0 soon. 👌

// --numeric-parser=string (default)
safeNumeric: '9007199254740991'
unsafeNumeric: '9007199254740992'

// --numeric-parser=number-or-string
safeNumeric: 9007199254740991
unsafeNumeric: '9007199254740992'

// --numeric-parser=number
safeNumeric: 9007199254740991
unsafeNumeric: 9007199254740992

It will also set the selection type to string, number | string or number accordingly.

jsbrn commented 1 month ago

Hello! I just added the CLI option --numeric-parser which will be released in kysely-codegen@0.15.0 soon. 👌

I am on 0.15.0 but I don't see the option.

RobinBlomberg commented 1 month ago

Hello! I just added the CLI option --numeric-parser which will be released in kysely-codegen@0.15.0 soon. 👌

I am on 0.15.0 but I don't see the option.

Hello! Sorry, I meant 0.16.0. It should come out soon.

jsbrn commented 1 month ago

Cool! My application uses bigint for some critical tables and I don't want to parseInt dozens of times if there's a better solution coming down the pipe. Any ETA on the update?

RobinBlomberg commented 1 month ago

Should be released in kysely-codegen@0.16.0! 🚀

LetrixZ commented 3 weeks ago

Hello! I just added the CLI option --numeric-parser which will be released in kysely-codegen@0.16.0 soon. 👌

// --numeric-parser=string (default)
safeNumeric: '9007199254740991'
unsafeNumeric: '9007199254740992'

// --numeric-parser=number-or-string
safeNumeric: 9007199254740991
unsafeNumeric: '9007199254740992'

// --numeric-parser=number
safeNumeric: 9007199254740991
unsafeNumeric: 9007199254740992

It will also set the selection type to string, number | string or number accordingly.

Am I using it wrong?

kysely-codegen@0.16.5

bun kysely-codegen --out-file=src/lib/db/types.ts --numeric-parser=number

Generates: export type Int8 = ColumnType<string, bigint | number | string, bigint | number | string>;

RobinBlomberg commented 3 weeks ago

It's possible that this doesn't work for pre-defined types (like Int8), but I will have to investigate.

lukeoguro commented 1 week ago

Hey, just making a note to say I'm getting a similar issue using kysely-codegen@0.16.8.

export type Int8 = ColumnType<string, bigint | number | string>;

The command I'm running is: kysely-codegen --out-file ./src/types/db-schema.ts --dialect=postgres --numeric-parser=number --camel-case.