Skip to content

Commit 87ca1bd

Browse files
authored
Add async/await example to 1-project-structure.md (#104)
1 parent c82aebc commit 87ca1bd

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Diff for: content/guides/1-project-structure.md

+38
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,42 @@ module.exports = {
156156
}
157157
```
158158

159+
Using async/await:
160+
161+
```
162+
module.exports = {
163+
async query(text, params) {
164+
const start = Date.now()
165+
const res = await pool.query(text, params)
166+
const duration = Date.now() - start
167+
console.log('executed query', { text, duration, rows: res.rowCount })
168+
return res
169+
},
170+
171+
async getClient() {
172+
const client = await pool.connect()
173+
const query = client.query
174+
const release = client.release
175+
// set a timeout of 5 seconds, after which we will log this client's last query
176+
const timeout = setTimeout(() => {
177+
console.error('A client has been checked out for more than 5 seconds!')
178+
console.error(`The last executed query on this client was: ${client.lastQuery}`)
179+
}, 5000)
180+
// monkey patch the query method to keep track of the last query executed
181+
client.query = (...args) => {
182+
client.lastQuery = args
183+
return query.apply(client, args)
184+
}
185+
client.release = () => {
186+
// clear our timeout
187+
clearTimeout(timeout)
188+
// set the methods back to their old un-monkey-patched version
189+
client.query = query
190+
client.release = release
191+
return release.apply(client)
192+
}
193+
return client
194+
}
195+
}
196+
```
159197
That should hopefully give us enough diagnostic information to track down any leaks.

0 commit comments

Comments
 (0)