8000 refactor: update debug doc to mention pretty printing debug queries · adonisjs/lucid.adonisjs.com@88595b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 88595b4

Browse files
committed
refactor: update debug doc to mention pretty printing debug queries
1 parent b5e8545 commit 88595b4

File tree

1 file changed

+28
-35
lines changed

1 file changed

+28
-35
lines changed

content/docs/guides/debugging.md

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ summary: A guide on debugging Lucid database queries.
44

55
# Debugging
66

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.
88

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:
1010

1111
```ts
1212
// title: config/database.ts
@@ -33,7 +33,7 @@ const dbConfig = defineConfig({
3333
})
3434
```
3535

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.
3737

3838
:::codegroup
3939

@@ -68,50 +68,43 @@ db
6868

6969
:::
7070

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.
7273

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.
7475

7576
```ts
76-
// title: start/events.ts
77-
import emitter from '@adonisjs/core/services/emitter'
77+
// title: config/database.ts
78+
import env from '#start/env'
79+
import { defineConfig } from '@adonisjs/lucid'
7880

79-
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+
},
8195
})
8296
```
8397

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.
85100

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.
87102

88103
```ts
89104
// title: start/events.ts
90105
import emitter from '@adonisjs/core/services/emitter'
91-
import db from '@adonisjs/lucid/services/db'
92-
93-
emitter.on('db:query', db.prettyPrint)
94-
```
95-
96-
## Debugging in production
97-
98-
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.
101-
102-
```ts
103-
// title: start/events.ts
104-
import db from '@adonisjs/lucid/services/db'
105-
import emitter from '@adonisjs/core/services/emitter'
106-
107-
import logger from '@adonisjs/core/services/logger'
108-
import app from '@adonisjs/core/services/app'
109106

110-
emitter.on('db:query', (query) => {
111-
if (app.inProduction) {
112-
logger.debug(query)
113-
} else {
114-
db.prettyPrint(query)
115-
}
107+
emitter.on('db:query', function (query) {
108+
logger.debug(query)
116109
})
117110
```

0 commit comments

Comments
 (0)
0