Hello! I’ve seen rumblings of this in outdated git...
# orm-help
g
Hello! I’ve seen rumblings of this in outdated github issues and there seems to be a thread here on slack but i wanted to confirm. I am trying to get the generated SQL that prisma creates, is there a
toSQL()
function? I’ve seen other ORMs that have this functionality. I don’t need to log this query (I saw that functionality) I need the raw statement at the moment the code is run
👀 1
k
I don't know what you are using, but for me I can get those via https://github.com/notiz-dev/nestjs-prisma There is a logger functionality included in this package and you can optionally turn on query logs which shows you
everything
sent as sql to the database
g
yep i saw that but it logs every query centrally, i imagine this is for debugging/monitoring. what i want is to get the raw SQL instead of executing the query, something like
Copy code
const query = await prisma.user.findMany().toSql();
t
Prisma don’t have synchronized way because it uses rust as glue language and query built in it. But, you can get it from emitting event by $on api listens ‘query’. https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#log You can set breakpoint or global flag for logging.
Copy code
prisma.$on('query', (e) => {
  if(global.loggerOn) {
    console.log(e.query)
    // global.currentQuery = e.query
    // debugger
    // so on
  }
})

async function yourEntryPoint() {
  global.loggerOn = true
  await prisma.foo.findMany(...)
  global.loggerOn = false
}
I can’t find way unsubscribes $on in document but you can also do so if it can instead of global flag.