Skip to content

Commit

Permalink
Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
delvedor committed Oct 11, 2017
1 parent 30d07b7 commit e5f06bd
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This plugin will add the `pg` namespace in your Fastify instance, with the follo
connect: the function to get a connection from the pool
pool: the pool instance
Client: a clinet constructor for a single query
query: an utility to perform a query without a transaction
```

Example:
Expand Down Expand Up @@ -70,6 +71,29 @@ fastify.listen(3000, err => {
console.log(`server listening on ${fastify.server.address().port}`)
})
```
Use of `pg.query`
```js
const fastify = require('fastify')

fastify.register(require('fastify-postgres'), {
connectionString: 'postgres://postgres@localhost/postgres'
})

fastify.get('/user/:id', (req, reply) => {
fastify.pg.query(
'SELECT id, username, hash, salt FROM users WHERE id=$1', [req.params.id],
function onResult (err, result) {
reply.send(err || result)
}
)
})

fastify.listen(3000, err => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})
```
As you can see there is no need to close the client, since is done internally. Promises and async await are supported as well.

## Acknowledgements

Expand Down

0 comments on commit e5f06bd

Please sign in to comment.