question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access to raw sql functions using Prisma APIs.

See original GitHub issue

Problem

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:open
  • Created 3 years ago
  • Reactions:45
  • Comments:5

github_iconTop GitHub Comments

13reactions
jpalumickascommented, Feb 22, 2021

Hello,

I have similar problem. I want to use findMany with custom raw SQL inside, because I want to have built in Cursor-based pagination and not write it in custom $queryRaw.

4reactions
darrylyoungcommented, Mar 17, 2021

I’d also like to mix raw queries with the Prisma API. Here’s an example model to help explain my use case.

model Product {
  id          String   @id @default(cuid())
  name        String   @unique
  description Json
  message     Message
}

Previously, the description field was String but I’m looking into adding translations at a database level so I decided to use Json for this field. There, instead of a single English string, I could now store something like this:

{
  "en": {
    "greeting": "Hello"
  },
  "de": {
    "greeting": "Hallo"
  },
  "es": {
    "greeting": "Hola"
  }
}

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.

const result = await ctx.prisma.$queryRaw<
  Product[]
>`SELECT id, name, description -> ${locale} as "description" from "Product"`

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 the Json fields, 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 with findMany, I’d have to write everything manually with $queryRaw.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found