valtyr/prisma-kysely

Generate enum casting helper functions (extensions) from prisma enums

Closed this issue · 4 comments

Thanks for the great library!

The biggest drawback we currently face using kysely, this extension, postgresql and prisma is, that it is required to wrap every Postgresql enum before inserting/filtering. If we forget to do so, the query throws.

As this article shows, it is quite easy to add extensions to kysely by for example wrapping a sql tag around a variable to cast something.

Like this: sql${ProductStatus.ARCHIVED}::product_status,

Wouldn't it be super helpful if a set of helper functions would directly be generated based on the Prisma schema's enums?
We are currently building these wrappers manually which is prone to erros, especially when renaming an enum.

Hey! I don't think I've ever come across this issue if I'm totally honest. But I'd totally be willing to consider a pull request that provides a clear example and an implementation 😄

Using kysely for the first time and ran into this same issue. Very possible I'm doing something incorrectly but seems that raw Prisma Enums cannot be used in SQL without an explicit cast. My example...

This query:

await prisma.$kysely.deleteFrom("SomeTable")
        .where("relatedEntityType", "=", EntityType.User)
        .execute();

Causes this error at runtime:

Raw query failed. Code: `42883`. Message: `ERROR: operator does not exist: "EntityType" = text
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.`

Changing this query to:

await prisma.$kysely.deleteFrom("SomeTable")
        .where("relatedEntityType", "=", sql<EntityType>`${EntityType.User}::"EntityType"`)
        .execute();

Works just fine but is verbose and means we have to be super vigilant about not using enums directly. Is there a better way? I'd be happy to contribute a PR if what @luap2703 suggests is the best path forward.

Indeed, same here.

Another common use case is relying on the DB to generate UUID for id columns.

It means, each time you want to filter a query by id, you'll need to cast the id to UUID:

await prisma.$kysely.selectFrom("MyTable").where("id", "=", sql`${entityId}::uuid`

Hey guys, I feel like this issue isn't directly related to prisma-kysely. It probably belongs in the https://github.com/eoin-obrien/prisma-extension-kysely repo (which uses prisma-kysely under the hood). The prisma-kysely package only really relates to generating Kysely schema definitions from Prisma schemas, and as of right now doesn't at all touch runtime code.

Helpers and such should probably be suggested on the prisma-extension-kysely repo.

Thanks :D