Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate enum casting helper functions (extensions) from prisma enums #95

Closed
luap2703 opened this issue Mar 26, 2024 · 4 comments
Closed

Comments

@luap2703
Copy link

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.

@valtyr
Copy link
Owner

valtyr commented Apr 9, 2024

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 😄

@michaelwallabi
Copy link

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.

@chris-verclytte
Copy link

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`

@valtyr
Copy link
Owner

valtyr commented May 8, 2024

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

@valtyr valtyr closed this as completed May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants