2

I've just set my local application to log Prisma's queries (log: ['query']), but I need to see what values it is using while I debug my application, which is currently not happening, my queries are being printed like this:

FROM "public"."potato" WHERE "public"."potato"."id" IN ($1) OFFSET $2

I have two questions:

  1. How do I print the values that are replaced by those placeholders?
  2. What is those placeholders being used for and what are they?

Note: Not that it matters or makes a difference, but I'm using postgres.

RinnieR
  • 209
  • 5
  • 15

1 Answers1

2

I think what you're looking for is https://www.prisma.io/docs/concepts/components/prisma-client/working-with-prismaclient/logging#event-based-logging.

Like this you can get the passed in parameters using the code mentioned in the docs

prisma.$on('query', (e) => {
  console.log('Query: ' + e.query)
  console.log('Params: ' + e.params)
  console.log('Duration: ' + e.duration + 'ms')
})

The placeholders are for parameters. See here https://stackoverflow.com/a/7505842/9530790 for why parameterized queries is beneficial.

shmuels
  • 1,039
  • 1
  • 9
  • 22
  • But how do I implement that on a class? It's an event, right? – RinnieR Sep 08 '22 at 21:16
  • 3
    I'm getting a Argument of type '"query"' is not assignable to parameter of type '"beforeExit"' – RinnieR Sep 08 '22 at 21:19
  • 1
    The Typescript typings on Prisma are pretty messed up - I had to hit it with a `@ts-ignore` and then set the callback to `(e: Prisma.QueryEvent)`, but then it worked fine as-is. – bsplosion Feb 17 '23 at 14:08
  • 1
    Alternatively, you supply these type hints for everything on the first line for it to work correctly without errors: `prisma.$on('query' as any, (e: Prisma.QueryEvent) => {` – bsplosion Feb 17 '23 at 14:15