Skip to content

Commit e5f06bd

Browse files
committed
Updated README.md
1 parent 30d07b7 commit e5f06bd

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This plugin will add the `pg` namespace in your Fastify instance, with the follo
1616
connect: the function to get a connection from the pool
1717
pool: the pool instance
1818
Client: a clinet constructor for a single query
19+
query: an utility to perform a query without a transaction
1920
```
2021

2122
Example:
@@ -70,6 +71,29 @@ fastify.listen(3000, err => {
7071
console.log(`server listening on ${fastify.server.address().port}`)
7172
})
7273
```
74+
Use of `pg.query`
75+
```js
76+
const fastify = require('fastify')
77+
78+
fastify.register(require('fastify-postgres'), {
79+
connectionString: 'postgres://postgres@localhost/postgres'
80+
})
81+
82+
fastify.get('/user/:id', (req, reply) => {
83+
fastify.pg.query(
84+
'SELECT id, username, hash, salt FROM users WHERE id=$1', [req.params.id],
85+
function onResult (err, result) {
86+
reply.send(err || result)
87+
}
88+
)
89+
})
90+
91+
fastify.listen(3000, err => {
92+
if (err) throw err
93+
console.log(`server listening on ${fastify.server.address().port}`)
94+
})
95+
```
96+
As you can see there is no need to close the client, since is done internally. Promises and async await are supported as well.
7397

7498
## Acknowledgements
7599

0 commit comments

Comments
 (0)