File tree Expand file tree Collapse file tree 1 file changed +26
-1
lines changed Expand file tree Collapse file tree 1 file changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,8 @@ This plugin will add the `pg` namespace in your Fastify instance, with the follo
1818connect: the function to get a connection from the pool
1919pool: the pool instance
2020Client: 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
2425Example:
@@ -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+
98123As 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
You can’t perform that action at this time.
0 commit comments