Skip to content

Commit 6299e73

Browse files
authored
Merge pull request #7 from simone-sanfratello/native
feature: native option
2 parents bea276d + d65f26a commit 6299e73

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ typings/
6262

6363
# vim swap files
6464
*.swp
65+
66+
# npm package lock
67+
package-lock.json

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This plugin will add the `pg` namespace in your Fastify instance, with the follo
1515
```
1616
connect: the function to get a connection from the pool
1717
pool: the pool instance
18-
Client: a clinet constructor for a single query
18+
Client: a client constructor for a single query
1919
query: an utility to perform a query without a transaction
2020
```
2121

@@ -95,6 +95,34 @@ fastify.listen(3000, err => {
9595
```
9696
As you can see there is no need to close the client, since is done internally. Promises and async await are supported as well.
9797

98+
### Native option
99+
If you want to gain the maximum performances you can install [pg-native](https://github.com/brianc/node-pg-native), and pass `native: true` to the plugin options.
100+
*Note: it requires PostgreSQL client libraries & tools installed, see [instructions](https://github.com/brianc/node-pg-native#install).*
101+
Note: trying to use native options without successfully installation of `pg-native` will get a warning and fallback to regular `pg` module.
102+
103+
```js
104+
const fastify = require('fastify')
105+
106+
fastify.register(require('fastify-postgres'), {
107+
connectionString: 'postgres://postgres@localhost/postgres',
108+
native: true
109+
})
110+
111+
fastify.get('/user/:id', (req, reply) => {
112+
fastify.pg.query(
113+
'SELECT id, username, hash, salt FROM users WHERE id=$1', [req.params.id],
114+
function onResult (err, result) {
115+
reply.send(err || result)
116+
}
117+
)
118+
})
119+
120+
fastify.listen(3000, err => {
121+
if (err) throw err
122+
console.log(`server listening on ${fastify.server.address().port}`)
123+
})
124+
```
125+
98126
## Acknowledgements
99127

100128
This project is kindly sponsored by:

index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
'use strict'
22

33
const fp = require('fastify-plugin')
4-
const pg = require('pg')
4+
let pg = require('pg')
55

66
function fastifyPostgres (fastify, options, next) {
7+
if (options.native) {
8+
delete options.native
9+
if (!pg.native) {
10+
console.warn('pg-native not installed, can\'t use native option - fallback to pg module')
11+
} else {
12+
pg = pg.native
13+
}
14+
}
15+
716
const pool = new pg.Pool(options)
817

918
fastify.decorate('pg', {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
},
3232
"devDependencies": {
3333
"fastify": "^0.30.1",
34+
"pg-native": "^2.2.0",
3435
"standard": "^10.0.3",
3536
"tap": "^10.7.2"
3637
}

test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,28 @@ test('use query util with promises', t => {
9191
})
9292
})
9393
})
94+
95+
test('use native module', t => {
96+
t.plan(2)
97+
98+
const fastify = Fastify()
99+
100+
fastify.register(fastifyPostgres, {
101+
connectionString: 'postgres://postgres@localhost/postgres',
102+
native: true
103+
})
104+
105+
fastify.ready(err => {
106+
t.error(err)
107+
fastify.pg
108+
.query('SELECT 1 AS one')
109+
.then(result => {
110+
t.ok(result.rows[0].one === 1)
111+
fastify.close()
112+
})
113+
.catch(err => {
114+
t.fail(err)
115+
fastify.close()
116+
})
117+
})
118+
})

0 commit comments

Comments
 (0)