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

Prisma logging outputting multiple repeating lines when used with enhancePrisma() #4230

Closed
tomADC443 opened this issue Oct 12, 2023 · 2 comments
Labels
kind/bug Something isn't working status/done

Comments

@tomADC443
Copy link

tomADC443 commented Oct 12, 2023

What is the problem?

When I implement prisma logging, following their guide: Prisma Logging the logging works fine but when I use the Blitz wrapper enhancePrisma() Logs entries appear multiple times.

when initializing the client in the standard non-blitz way:

import { Prisma, PrismaClient } from "@prisma/client"
const customPrismaClient = new PrismaClient({
log: ['query']
})

Logs look this:

...
prisma:query SELECT "public"."user_account"."id" FROM "public"."user_account" WHERE ....
prisma:query SELECT "public"."user_permission"."user_id", "public"."user_permission"....
...

but when using the blitz wrapper...

import { Prisma, PrismaClient } from "@prisma/client"
const EnhancedPrisma = enhancePrisma(PrismaClient)
const customPrismaClient = new EnhancedPrisma({
log: ['query']
})

logs appear multiple times like so:

...
prisma:query SELECT "public"."user_account"."id" FROM "public"."user_account" WHERE ....
prisma:query SELECT "public"."user_account"."id" FROM "public"."user_account" WHERE ....
prisma:query SELECT "public"."user_account"."id" FROM "public"."user_account" WHERE ....
prisma:query SELECT "public"."user_permission"."user_id", "public"."user_permission"....
prisma:query SELECT "public"."user_permission"."user_id", "public"."user_permission"....
prisma:query SELECT "public"."user_permission"."user_id", "public"."user_permission"....
...

Sometimes the logs get repeated 2 sometimes even more
I can't say for sure but I have the feeling that the longer the server runs the more repetitions I see.

running Prisma 5.4.1

Paste all your error logs here:

example of repeated logs

prisma:query SELECT "public"."user_account"."id" FROM "public"."user_account" WHERE ("public"."user_account"."id" = $1 AND 1=1) LIMIT $2 OFFSET $3
prisma:query SELECT "public"."user_account"."id" FROM "public"."user_account" WHERE ("public"."user_account"."id" = $1 AND 1=1) LIMIT $2 OFFSET $3
prisma:query SELECT "public"."user_account"."id" FROM "public"."user_account" WHERE ("public"."user_account"."id" = $1 AND 1=1) LIMIT $2 OFFSET $3
prisma:query SELECT "public"."user_account"."id" FROM "public"."user_account" WHERE ("public"."user_account"."id" = $1 AND 1=1) LIMIT $2 OFFSET $3
prisma:query SELECT "public"."user_permission"."user_id", "public"."user_permission"."permission_identifier" FROM "public"."user_permission" WHERE "public"."user_permission"."user_id" IN ($1) OFFSET $2
prisma:query SELECT "public"."user_permission"."user_id", "public"."user_permission"."permission_identifier" FROM "public"."user_permission" WHERE "public"."user_permission"."user_id" IN ($1) OFFSET $2
prisma:query SELECT "public"."user_permission"."user_id", "public"."user_permission"."permission_identifier" FROM "public"."user_permission" WHERE "public"."user_permission"."user_id" IN ($1) OFFSET $2

Paste all relevant code snippets here:

To reproduce:

import { Prisma, PrismaClient } from "@prisma/client"
const EnhancedPrisma = enhancePrisma(PrismaClient)
const customPrismaClient = new EnhancedPrisma({
log: ['query']
})

What are detailed steps to reproduce this?

  1. Enable logging following this guide: https://www.prisma.io/docs/concepts/components/prisma-client/working-with-prismaclient/logging#log-to-stdout
  2. Do some db related stuff and monitor the logs

Run blitz -v and paste the output here:

Blitz version: 2.0.0-beta.31 (local)
macOS | darwin-arm64 | Node: v18.17.1


 Package manager: npm

  System:
    OS: macOS 14.0
    CPU: (8) arm64 Apple M1 Pro
    Memory: 58.05 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.17.1 - /var/folders/kb/gkpdtd6557b_j0dv2z3tkkvr0000gn/T/yarn--1697116396786-0.11871254532655695/node
    Yarn: 1.22.19 - /var/folders/kb/gkpdtd6557b_j0dv2z3tkkvr0000gn/T/yarn--1697116396786-0.11871254532655695/yarn
    npm: 9.5.1 - /opt/homebrew/bin/npm
  npmPackages:
    @blitzjs/auth: 2.0.0-beta.31 => 2.0.0-beta.31 
    @blitzjs/next: 2.0.0-beta.31 => 2.0.0-beta.31 
    @blitzjs/rpc: 2.0.0-beta.31 => 2.0.0-beta.31 
    @prisma/client: 5.4.x => 4.16.1 
    blitz: 2.0.0-beta.31 => 2.0.0-beta.31 
    next: 13.2.x => 13.2.4 
    prisma: 4.16.x => 4.16.1 
    react: ~18.2.0 => 18.2.0 
    react-dom: ~18.2.0 => 18.2.0 
    typescript: 5.2.2 => 5.1.6

Please include below any other applicable logs and screenshots that show your problem:

No response

@tomADC443 tomADC443 added kind/bug Something isn't working status/triage labels Oct 12, 2023
@flybayer
Copy link
Member

This one is really odd to me, logging used to work fine. It's possible Prisma made internal changes that render this enhancePrisma wrapper incompatible.

Here's the code for it, you can easily copy and paste it into your code to help debug at least.

Basically it does 3 things:

  1. Ensure there is only a single client created
  2. Wrap with a Proxy so that running new PrismaClient() in the browser doesn't throw an error. It's possible Prisma has changed to support that now
  3. Add the $reset() method for testing convenience

A simpler approach for (1) without enhancePrisma is

if (!globalThis._prismaClient) {
  globalThis._prismaClient = new PrismaClient()
}
export const prisma = globalThis._prismaClient

And you can solve (2) by doing your own check to not instantiate the client in the browser

@tomADC443
Copy link
Author

Thanks! I will look into this and get back to you 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working status/done
Projects
Status: Done
Development

No branches or pull requests

3 participants