Replies: 3 comments 1 reply
-
I wrote a simple SQL query just to simplify things. In reality, of course, the queries that I'm writing are more complex |
Beta Was this translation helpful? Give feedback.
-
Hey @Newbie012, thanks a lot for sharing this. Do you know if it is possible to use the prisma types directly in the template literal of tools like pgtyped/slonik/zapatos/postgres.js instead of having to generate them beforehand with the aforementioned tools? |
Beta Was this translation helpful? Give feedback.
-
I found a community extension which maybe a perfect solution for this problem. |
Beta Was this translation helpful? Give feedback.
-
I didn't invent anything, I just combined two epic libraries - Prisma and PgTyped. This is what I came up with:
Write type-safe raw queries with Prisma and PgTyped
File structure:
db.ts
context.ts
db/queries/myQuery.ts + myQuery.types.ts
This is where we write are raw query. At first, we will write something like:
Currently, there's nothing special. The magic starts only after you run pgtyped:
(Make sure you have a configuration file. More on that on - https://pgtyped.now.sh/docs/getting-started)
Once that's done, pgtyped has generated a types file (in my case my emitTemplate is
{{dir}}/{{name}}.types.ts
) calleddb/queries/myQuery.types.ts
:Once it's generated, I manually attach the interface
IGetCampaignGeoQuery
togetCampaignGeo
this way:Now, for the fun part - mixing all together
Since I'm using nexus, I'll show my implementation that way:
And that's it.
Beta Was this translation helpful? Give feedback.
All reactions