Skip to content

Commit 51191e7

Browse files
authored
Merge pull request #1 from fastify/init
Init
2 parents eecdf42 + e5f06bd commit 51191e7

File tree

6 files changed

+305
-1
lines changed

6 files changed

+305
-1
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,8 @@ typings/
5757
# dotenv environment variables file
5858
.env
5959

60+
# mac files
61+
.DS_Store
62+
63+
# vim swap files
64+
*.swp

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: node_js
2+
3+
node_js:
4+
- "8"
5+
- "6"
6+
- "4"
7+
8+
services:
9+
- postgresql
10+
11+
notifications:
12+
email:
13+
on_success: never
14+
on_failure: always

README.md

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,106 @@
11
# fastify-postgres
2-
Fastify PostgreSQL connection plugin
2+
3+
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) [![Build Status](https://travis-ci.org/fastify/fastify-postgres.svg?branch=master)](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).

index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict'
2+
3+
const fp = require('fastify-plugin')
4+
const promisify = require('util.promisify')
5+
const pg = require('pg')
6+
7+
function fastifyPostgres (fastify, options, next) {
8+
const pool = new pg.Pool(options)
9+
10+
fastify.decorate('pg', {
11+
connect: pool.connect.bind(pool),
12+
pool: pool,
13+
Client: pg.Client,
14+
query: promisify(query)
15+
})
16+
17+
function query (text, value, callback) {
18+
if (typeof value === 'function') {
19+
callback = value
20+
value = null
21+
}
22+
23+
pool.connect(onConnect)
24+
25+
function onConnect (err, client, release) {
26+
if (err) return callback(err)
27+
28+
if (value) {
29+
client.query(text, value, onResult)
30+
} else {
31+
client.query(text, onResult)
32+
}
33+
34+
function onResult (err, result) {
35+
release()
36+
callback(err, result)
37+
}
38+
}
39+
}
40+
41+
fastify.addHook('onClose', onClose)
42+
43+
next()
44+
}
45+
46+
function onClose (fastify, done) {
47+
fastify.pg.pool.end(done)
48+
}
49+
50+
module.exports = fp(fastifyPostgres, '>=0.13.1')

package.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "fastify-postgres",
3+
"version": "0.1.0",
4+
"description": "Fastify PostgreSQL connection plugin",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "standard && tap test.js",
8+
"postgres": "docker run --rm -p 5432:5432 postgres:9.6-alpine"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/fastify/fastify-postgres.git"
13+
},
14+
"keywords": [
15+
"fastify",
16+
"postgres",
17+
"postgresql",
18+
"database",
19+
"connection",
20+
"sql"
21+
],
22+
"author": "Tomas Della Vedova - @delvedor (http://delved.org)",
23+
"license": "MIT",
24+
"bugs": {
25+
"url": "https://github.com/fastify/fastify-postgres/issues"
26+
},
27+
"homepage": "https://github.com/fastify/fastify-postgres#readme",
28+
"dependencies": {
29+
"fastify-plugin": "^0.1.1",
30+
"pg": "^7.3.0",
31+
"util.promisify": "^1.0.0"
32+
},
33+
"devDependencies": {
34+
"fastify": "^0.29.2",
35+
"standard": "^10.0.3",
36+
"tap": "^10.7.2"
37+
}
38+
}

test.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
'use strict'
2+
3+
const t = require('tap')
4+
const test = t.test
5+
const Fastify = require('fastify')
6+
const fastifyPostgres = require('./index')
7+
8+
test('fastify.pg namespace should exist', t => {
9+
t.plan(5)
10+
11+
const fastify = Fastify()
12+
13+
fastify.register(fastifyPostgres, {
14+
connectionString: 'postgres://postgres@localhost/postgres'
15+
})
16+
17+
fastify.ready(err => {
18+
t.error(err)
19+
t.ok(fastify.pg)
20+
t.ok(fastify.pg.connect)
21+
t.ok(fastify.pg.pool)
22+
t.ok(fastify.pg.Client)
23+
fastify.close()
24+
})
25+
})
26+
27+
test('should be able to connect and perform a query', t => {
28+
t.plan(4)
29+
30+
const fastify = Fastify()
31+
32+
fastify.register(fastifyPostgres, {
33+
connectionString: 'postgres://postgres@localhost/postgres'
34+
})
35+
36+
fastify.ready(err => {
37+
t.error(err)
38+
fastify.pg.connect(onConnect)
39+
})
40+
41+
function onConnect (err, client, done) {
42+
t.error(err)
43+
client.query('SELECT NOW()', (err, result) => {
44+
done()
45+
t.error(err)
46+
t.ok(result.rows)
47+
fastify.close()
48+
})
49+
}
50+
})
51+
52+
test('use query util', t => {
53+
t.plan(3)
54+
55+
const fastify = Fastify()
56+
57+
fastify.register(fastifyPostgres, {
58+
connectionString: 'postgres://postgres@localhost/postgres'
59+
})
60+
61+
fastify.ready(err => {
62+
t.error(err)
63+
fastify.pg.query('SELECT NOW()', (err, result) => {
64+
t.error(err)
65+
t.ok(result.rows)
66+
fastify.close()
67+
})
68+
})
69+
})
70+
71+
test('use query util with promises', t => {
72+
t.plan(2)
73+
74+
const fastify = Fastify()
75+
76+
fastify.register(fastifyPostgres, {
77+
connectionString: 'postgres://postgres@localhost/postgres'
78+
})
79+
80+
fastify.ready(err => {
81+
t.error(err)
82+
fastify.pg
83+
.query('SELECT NOW()')
84+
.then(result => {
85+
t.ok(result.rows)
86+
fastify.close()
87+
})
88+
.catch(err => {
89+
t.fail(err)
90+
fastify.close()
91+
})
92+
})
93+
})

0 commit comments

Comments
 (0)