Ready to get started?
Join Adocasts Plus for $8/mo, or sign into an existing Adocasts Plus account, to get access to all of our lessons.
Building with AdonisJS & Inertia #9.0
Querying & Listing Sortable Course Modules
We'll query and add a sortable list of a course's modules on the courses show page.
- Created by
- @tomgobich
- Published
Join the Discussion 7 comments
-
How can i log the query to check if i am writing query correct or not?
console.log(query.toSQL())
is not working for me. Any way that i can log query and check in storage/logs/adonis.log?
1-
Responding to rajeeb-shakya
Hi Rajeeb!
Console logging a call to
toSQL()as you have should work, you should be seeing something like the below in your console.
However, there is a nicer way to do this, using pretty printed query logging. Within your
config/database.ts, setprettyPrintDebugQueries: true.const dbConfig = defineConfig({ prettyPrintDebugQueries: true, // 👈 connection: 'postgres', connections: { postgres: { client: 'pg', connection: { host: env.get('DB_HOST'), port: env.get('DB_PORT'), user: env.get('DB_USER'), password: env.get('DB_PASSWORD'), database: env.get('DB_DATABASE'), }, migrations: { naturalSort: true, paths: ['database/migrations'], }, }, }, })Copied!- config
- database.ts
Then, you can either globally enable query logging, by adding a
debug: trueto your driver object.const dbConfig = defineConfig({ prettyPrintDebugQueries: true, connection: 'postgres', connections: { postgres: { debug: true, // 👈 client: 'pg', connection: { host: env.get('DB_HOST'), port: env.get('DB_PORT'), user: env.get('DB_USER'), password: env.get('DB_PASSWORD'), database: env.get('DB_DATABASE'), }, migrations: { naturalSort: true, paths: ['database/migrations'], }, }, }, })Copied!- config
- database.ts
Or, you can enable this this on a per-query basis by adding
debug(true)to the query.await Collection.query().where('slug', params.slug).debug(true).firstOrFail()Copied!With that set, you should see something like below in your console

Now, unfortunately, this pretty print can't be logged to a file. You can still log the query to a file though. Within your
config/logger, set the non-production logger target to a file at the destination you'd like. If that's a file in storage, it'd look something like:const loggerConfig = defineConfig({ default: 'app', loggers: { app: { enabled: true, name: env.get('APP_NAME'), level: env.get('LOG_LEVEL'), transport: { targets: targets() // 👇 .pushIf(!app.inProduction, targets.file({ destination: './storage/local.log' })) .pushIf(app.inProduction, targets.file({ destination: 1 })) .toArray(), }, }, }, })Copied!- config
- logger.ts
Then, add a global listener for the query within a preload.
node ace make:preload eventsCopied!import emitter from '@adonisjs/core/services/emitter' import logger from '@adonisjs/core/services/logger' emitter.on('db:query', function (query) { logger.debug(query) })Copied!- start
- events.ts
With this all your logs, including queries, will be written to the file at the location you've specified! Hope this helps!! 😊
0-
Responding to tomgobich
Thanks Tom. Thats what i am seeking for. I will find way to print those queries in log file.
thanks
1-
Responding to rajeeb-shakya
Anytime! Okay cool, yeah updating the logger destination and logging the queries, as shown in the last two code blocks in my comment above, should get that working for ya!
0
-
-
-
Hey @tomgobich,
When I importimport Sortable from 'vuedraggable'I keep getting this error :
[Error] TypeError: undefined is not a constructor (evaluating 'new RefImpl(rawValue, shallow)')I am using the same version "vuedraggable": "^4.1.0"
I need to delete cache even after removing it cause it sticks to the project. Any idea why?
1-
Responding to memsbdm
-
Responding to memsbdm
Glad you were able to find a workaround, and thanks for sharing!! That's the first I've seen of that one, I'll have to keep my eye on it.
0
-
-