|
1 | 1 | # fastify-postgres |
2 | | -Fastify PostgreSQL connection plugin |
| 2 | + |
| 3 | +[](http://standardjs.com/) [](https://travis-ci.org/fastify/fastify-postgres) |
| 4 | + |
| 5 | +Fastify PostgreSQL connection plugin, with this you can share the same PostgreSQL connection pool in every part of your server. |
| 6 | +Under the hood the [node-postgres](https://github.com/brianc/node-postgres) is used, the options that you pass to `register` will be passed to the PostgreSQL pool builder. |
| 7 | + |
| 8 | +## Install |
| 9 | +``` |
| 10 | +npm i fastify-postgres --save |
| 11 | +``` |
| 12 | +## Usage |
| 13 | +Add it to you project with `register` and you are done! |
| 14 | +This plugin will add the `pg` namespace in your Fastify instance, with the following properties: |
| 15 | +``` |
| 16 | +connect: the function to get a connection from the pool |
| 17 | +pool: the pool instance |
| 18 | +Client: a clinet constructor for a single query |
| 19 | +query: an utility to perform a query without a transaction |
| 20 | +``` |
| 21 | + |
| 22 | +Example: |
| 23 | +```js |
| 24 | +const fastify = require('fastify') |
| 25 | + |
| 26 | +fastify.register(require('fastify-postgres'), { |
| 27 | + connectionString: 'postgres://postgres@localhost/postgres' |
| 28 | +}) |
| 29 | + |
| 30 | +fastify.get('/user/:id', (req, reply) => { |
| 31 | + fastify.pg.connect(onConnect) |
| 32 | + |
| 33 | + function onConnect (err, client, release) { |
| 34 | + if (err) return reply.send(err) |
| 35 | + |
| 36 | + client.query( |
| 37 | + 'SELECT id, username, hash, salt FROM users WHERE id=$1', [req.params.id], |
| 38 | + function onResult (err, result) { |
| 39 | + release() |
| 40 | + reply.send(err || result) |
| 41 | + } |
| 42 | + ) |
| 43 | + } |
| 44 | +}) |
| 45 | + |
| 46 | +fastify.listen(3000, err => { |
| 47 | + if (err) throw err |
| 48 | + console.log(`server listening on ${fastify.server.address().port}`) |
| 49 | +}) |
| 50 | +``` |
| 51 | + |
| 52 | +Async await is supported as well! |
| 53 | +```js |
| 54 | +const fastify = require('fastify') |
| 55 | + |
| 56 | +fastify.register(require('fastify-postgres'), { |
| 57 | + connectionString: 'postgres://postgres@localhost/postgres' |
| 58 | +}) |
| 59 | + |
| 60 | +fastify.get('/user/:id', async (req, reply) => { |
| 61 | + const client = await fastify.pg.connect() |
| 62 | + const { result } = await client.query( |
| 63 | + 'SELECT id, username, hash, salt FROM users WHERE id=$1', [req.params.id], |
| 64 | + ) |
| 65 | + client.release() |
| 66 | + return result |
| 67 | +}) |
| 68 | + |
| 69 | +fastify.listen(3000, err => { |
| 70 | + if (err) throw err |
| 71 | + console.log(`server listening on ${fastify.server.address().port}`) |
| 72 | +}) |
| 73 | +``` |
| 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. |
| 97 | + |
| 98 | +## Acknowledgements |
| 99 | + |
| 100 | +This project is kindly sponsored by: |
| 101 | +- [nearForm](http://nearform.com) |
| 102 | +- [LetzDoIt](http://www.letzdoitapp.com/) |
| 103 | + |
| 104 | +## License |
| 105 | + |
| 106 | +Licensed under [MIT](./LICENSE). |
0 commit comments