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 cast helper for Postgres #111

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

jlnstrk
Copy link

@jlnstrk jlnstrk commented Oct 22, 2024

Closes #104. Related: eoin-obrien/prisma-extension-kysely#72, kysely-org/kysely#198.

Hey! This change adds to the generated code a helper function that can be used to type-safely cast enum literals in PostgreSQL queries.

To give an example, a query that before might have looked like this:

const unit: TemperatureUnit = "CELSIUS";
kysely.selectFrom("temperatures")
    .where("unit", "=", sql<TemperatureUnit>`${unit}::temperature_unit` /* zero type safety here! */)
    .selectAll()

Can be written as follows with this extension:

const unit: TemperatureUnit = "CELSIUS";
kysely.selectFrom("temperatures")
    .where("unit", "=", castEnumValue(unit, "temperature_unit" /* type-checked! */))
    .selectAll()

The change is compatible with the support for Prisma's multiSchema feature introduced in #56.

Curious to hear if this is something you'd consider merging.

@jlnstrk jlnstrk requested a review from valtyr as a code owner October 22, 2024 19:29
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

Successfully merging this pull request may close these issues.

Enum wrappers for postgres
1 participant