0

I read the docs from here : https://docs.adonisjs.com/guides/database/debugging#pretty-print-queries

And I try like this :

import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Database from '@ioc:Adonis/Lucid/Database'
import Event from '@ioc:Adonis/Core/Event'

export default class UsersController {
  public async index({ params, response }: HttpContextContract) {
    const results = await Database
                            .query()  
                            .select('*')
                            .from('users as a')
                            .innerJoin('posts as b', 'a.id', 'b.user_id')
                            .groupBy('a.id')
    Event.on('db:query', Database.prettyPrint)
    return response.status(200).json({ code: 200, status: 'success', data: results })
  }
}

I try using the pretty print queries like that and I call the query. Then I check on the terminal and postman too. But I don't find the result

Where do I check the debugging results of adonis api query?

Please help. Thanks

1 Answers1

1

Inside file .adonisrc.json add preloads events value

{
"preloads": [
    "./start/routes",
    "./start/kernel",
    "./start/events", // here
  ],
}

and create a new file start/events.js add example code as documented

import Event from '@ioc:Adonis/Core/Event'
import Database from '@ioc:Adonis/Lucid/Database'

import Logger from '@ioc:Adonis/Core/Logger'
import Application from '@ioc:Adonis/Core/Application'

Event.on('db:query', (query) => {
  if (Application.inProduction) {
    Logger.debug(query)    
  } else {
    Database.prettyPrint(query)
  }
})