RobinBlomberg / kysely-codegen

Generate Kysely type definitions from your database.
MIT License
690 stars 62 forks source link

The generated `type Json = ColumnType<JsonValue, string, string>` should just be `JsonValue` for Postgres. #124

Closed afg419 closed 6 months ago

afg419 commented 6 months ago

If I'm not mistaken, the generated json type type Json = ColumnType<JsonValue, string, string> seems to be overly restrictive for Postgres, as I can use kysely to insert and update JsonValue values, in addition to strings. Since JsonValue | string is just JsonValue, the whole thing can be collapsed type Json = JsonValue.

Upvote & Fund

Fund with Polar

afg419 commented 6 months ago

That is, both of these work:

await kysely.insertInto('myTable').values({ address: { street: '111 First Ave', zip: '00000', state: 'MA' }}).executeTakeFirst()

await kysely.insertInto('myTable').values({ address: JSON.stringify({ street: '111 First Ave', zip: '00000', state: 'MA' })}).executeTakeFirst()
BoscoDomingo commented 6 months ago

I came here to post this exact issue. I changed the generated types and it works flawlessly (at least for insertion).

In fact, it works better than inserting a string because (at least in JS/TS) stringifying an object escapes the double quotes, therefore taking up significantly more space than needed. I wouldn't be surprised if that also causes us to lose the SQL optimizations and indexing abilities.

In essence, I echo the OP

BoscoDomingo commented 6 months ago

Pushed a PR, I'm expecting that to work. @RobinBlomberg would love a 👍🏼!