prisma / extension-read-replicas

Prisma Client Extension to add read replica support to your Prisma Client
Apache License 2.0
101 stars 6 forks source link

Is there a sample that logs queries to a replica database? #34

Closed nrikiji closed 2 months ago

nrikiji commented 6 months ago

I want to do the same thing as the code below. What I want to do is log the query to be executed. Is it possible to log the query for a read-only database?

const prisma = new PrismaClient({
  log: ['query', 'info', 'warn', 'error'],
})

prisma.$on('query', async (e) => {
  console.log(`${e.query} ${e.params}`)
})

Below is the code we tried.

const prismaClient = new PrismaClient({
  log: ['query', 'info', 'warn', 'error'],
})

prismaClient.$on('query', async (e) => {
  console.log(`${e.query} ${e.params}`)
})

const prisma = prismaClient.$extends(
  readReplicas({
    url: Config.DATABASE_URL_REPLICA,
  })
)

await prisma.$primary().$queryRaw・・・ // This will be written to the log.
await prisma.$replica().$queryRaw・・・  // This is not written to the log.
hugw commented 3 months ago

Facing the same issue.

Also, using export DEBUG="prisma*" doesn't work either.

SevInf commented 2 months ago

Version 0.4.0 allow to pass pre-configured clients to the extension and there is logging example in the README now https://github.com/prisma/extension-read-replicas#pre-configured-clients