Skip to content

Commit 4db4943

Browse files
committed
Updated readme with example
1 parent d7d188a commit 4db4943

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ This plugin will add the `pg` namespace in your Fastify instance, with the follo
1818
connect: the function to get a connection from the pool
1919
pool: the pool instance
2020
Client: a client constructor for a single query
21-
query: an utility to perform a query without a transaction
21+
query: a utility to perform a query _without_ a transaction
22+
transact: a utility to perform a query _with_ a transaction
2223
```
2324

2425
Example:
@@ -95,6 +96,30 @@ fastify.listen(3000, err => {
9596
console.log(`server listening on ${fastify.server.address().port}`)
9697
})
9798
```
99+
100+
Use of `pg.transact`
101+
```js
102+
const fastify = require('fastify')
103+
104+
fastify.register(require('fastify-postgres'), {
105+
connectionString: 'postgres://postgres@localhost/postgres'
106+
})
107+
108+
fastify.post('/user/:username', (req, reply) => {
109+
fastify.pg.transact(
110+
'INSERT INTO users(username) VALUES($1) RETURNING id', [req.params.username],
111+
function onResult (err, result) {
112+
reply.send(err || result)
113+
}
114+
)
115+
})
116+
117+
fastify.listen(3000, err => {
118+
if (err) throw err
119+
console.log(`server listening on ${fastify.server.address().port}`)
120+
})
121+
```
122+
98123
As you can see there is no need to close the client, since is done internally. Promises and async await are supported as well.
99124

100125
### Native option

0 commit comments

Comments
 (0)