extension-read-replicas icon indicating copy to clipboard operation
extension-read-replicas copied to clipboard

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

Open nrikiji opened this issue 2 years ago • 0 comments

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.

nrikiji avatar Mar 25 '24 02:03 nrikiji