You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/guides/debugging.md
+28-35Lines changed: 28 additions & 35 deletions
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,9 @@ summary: A guide on debugging Lucid database queries.
4
4
5
5
# Debugging
6
6
7
-
Lucid emits the `db:query` event when debugging is enabled globally or for an individual query.
7
+
You can debug SQL queries by first enabling the `debug` mode and then listen for the `db:query` event to get notified as SQL queries are executed.
8
8
9
-
You can enable debugging globally by setting the `debug` flag to `true` inside the `config/database.ts` file.
9
+
The debug mode can be enabled globally for a database connection by setting the `debug` flag to `true` inside the `config/database.ts` file. For example:
10
10
11
11
```ts
12
12
// title: config/database.ts
@@ -33,7 +33,7 @@ const dbConfig = defineConfig({
33
33
})
34
34
```
35
35
36
-
You can enable debugging for an individual query using the `debug` method on the query builder.
36
+
Or, you can enable it for an individual query using the `debug` method on the query builder.
37
37
38
38
:::codegroup
39
39
@@ -68,50 +68,43 @@ db
68
68
69
69
:::
70
70
71
-
## Listening to the Event
71
+
## Pretty printing debug queries
72
+
Once the debugging has been enabled you can set the `prettyPrintDebugQueries` flag to `true` within the `config/database.ts` file.
72
73
73
-
Once you have enabled debugging, you can listen for the `db:query` event using the [emitter](https://docs.adonisjs.com/guides/emitter) service.
74
+
This flag will register an event listener for the `db:query` event and will print the SQL queries to the console.
emitter.on('db:query', function ({ sql, bindings }) {
80
-
console.log(sql, bindings)
81
+
const dbConfig =defineConfig({
82
+
// highlight-start
83
+
prettyPrintDebugQueries: true,
84
+
// highlight-end
85
+
connection: 'postgres',
86
+
connections: {
87
+
postgres: {
88
+
client: 'pg',
89
+
connection: {
90
+
// ...
91
+
},
92
+
debug: true
93
+
},
94
+
},
81
95
})
82
96
```
83
97
84
-
### Pretty print queries
98
+
## Manually listening for the event
99
+
If you do not want to pretty print SQL queries and write them to the console, then you can self listen for the `db:query` event and self handle the treatment of debug logs.
85
100
86
-
You can use the `db.prettyPrint` method as the event listener to pretty-print the queries on the console.
101
+
In the following example, we use the application logger to log the queries.
Pretty printing queries add additional overhead to the process and can impact the performance of your application. Hence, we recommend using the [Logger](https://docs.adonisjs.com/guides/logger) to log the database queries during production.
99
-
100
-
Following is a complete example of switching the event listener based upon the application environment.
0 commit comments