Access to raw sql functions using Prisma APIs.
See original GitHub issueProblem
Having to use raw sql query when I would like to make use of a native database function.
Suggested solution
In my case I’m using Postgresql and would love to be able to use the extract function to retrieve information from a timestamp.
SELECT * FROM "Appointment" WHERE extract(month from timestamp) = 2;
I’ve done this with Mikro-orm in a prior project and a similar implementation with Prisma could look something like this:
prisma.appointment.findMany({ where: { "extract(month from timestamp)": 2 } as any });
Alternatives
My primary frustration with having to use a raw query is that the results of the query aren’t mapped as they would be with a query using the API.
So an alternative could be an option to have raw queries also map the results of the query.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:45
- Comments:5
Top Results From Across the Web
Raw database access (Reference) - Prisma
Learn how you can send raw SQL and MongoDB queries to your database using the raw() methods from the Prisma Client API.
Read more >How To Build a REST API with Prisma and PostgreSQL
Prisma is an open source database toolkit. In this tutorial, you will build a REST API for a small blogging application in TypeScript...
Read more >Is there a way to use DATE functions in prisma without raw ...
Currently, this is only possible via a raw query but I would suggest creating a feature request here that allows raw function access...
Read more >Modern application development with Prisma, GraphQL (or ...
For example, imagine adding a new required column to one of your database tables that your application code writes to. If you're using...
Read more >API with NestJS #72. Working with PostgreSQL using raw SQL ...
The repository pattern and working with models. It might be a good idea to keep the logic of accessing data in a single...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hello,
I have similar problem. I want to use
findManywith custom raw SQL inside, because I want to have built in Cursor-based pagination and not write it in custom $queryRaw.I’d also like to mix raw queries with the Prisma API. Here’s an example model to help explain my use case.
Previously, the
descriptionfield wasStringbut I’m looking into adding translations at a database level so I decided to useJsonfor this field. There, instead of a single English string, I could now store something like this:Then, when querying for Products, a user could pass their locale as an argument and it could grab the correct translation for them. I managed to get that working with the following.
That works as expected but I obviously want to get everything for an individual Product and that, in the case of the example model above, would include relations – in this case, to
Message(in reality, my model is much more complicated and has a bunch of relations and other fields). I was hoping I could get Prisma to do its thing and allow me to specify how I want theJsonfields, while doing everything else automatically, but I found out that it’s not the case. To filter JSON like that, and also get everything else like I would so easily withfindMany, I’d have to write everything manually with$queryRaw.