valtyr/prisma-kysely

GeneratedAlways Kysely type output

Closed this issue · 5 comments

mkcode commented

Hi, Really appreciate using this library. Thanks for creating it!

I am wondering if there is anyway I can get prisma-kysely to output the "GeneratedAlways" Kysely type. See: https://kysely-org.github.io/kysely/types/GeneratedAlways.html

For context, I want this for use with autoincrement primary key columns, which I am currently defining like so in my prisma schema:

    id            BigInt   @id @db.UnsignedBigInt @default(autoincrement())

This outputs a Generated<number> type definition which is working well enough for me, but I believe the GeneratedAlways<number> Kysely type is more correct for this case.

Thanks!

mkcode commented

Also, realizing that the output schema for this prisma schema is number when it should be bigint.

Using bigint rather than number in my app is more trouble than it is worth though, so I am rolling back to using Int rather than BigInt in the prisma db schema.

Nvm - I see that we can override the type maps to support BigInt.

valtyr commented

Hey there @mkcode, thanks for the interest! I'm all for supporting this but I'm wondering what property of the Prisma schema we could use to trigger it. IMO it would be pretty heavy handed to force all ID fields with default values to be GeneratedAlways. Maybe we could add a config field that enables this behavior. What do you think?

mkcode commented

A little research into prisma leads to this issue: prisma/prisma#3401

Which indicates that there is no way in the prisma schema to specify this currently.
Per field granularity would certainly be better than per project, which doesn't seem possible currently.

I would think therefore that a per project setting, that only applies to fields where @id is specified in the schema would be most appropriate.

A setting like readOnlyIds=true (default false) would be good. Actually, maybe it would be preferable to add an idTypeOverride to the override type options? It would allow more generic type overrides, but there is a slight mismatch there between the field type and prisma metadata.

Both config options seem good to me - the common thing between them is only applying these changes to the @id fields in the prisma schema.

valtyr commented

Hey @mkcode, sorry for the delay. I've made an attempt to add support for this in PR #43. The PR adds a config parameter called readOnlyIds that, if set, makes all @id fields with default values GeneratedAlways. I've made a snapshot with the changes: 0.0.0-snapshot-20230515215152. It would be awesome if you could try it out and tell me if it suits your use case.

mkcode commented

@valtyr - I just checked this out, and it works beautifully, in general and for my use case. Thank you for getting this in!