Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ typings/

# vim swap files
*.swp

# npm package lock
package-lock.json
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This plugin will add the `pg` namespace in your Fastify instance, with the follo
```
connect: the function to get a connection from the pool
pool: the pool instance
Client: a clinet constructor for a single query
Client: a client constructor for a single query
query: an utility to perform a query without a transaction
```

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

### Native option
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.
*Note: it requires PostgreSQL client libraries & tools installed, see [instructions](https://github.com/brianc/node-pg-native#install).*
Note: trying to use native options without successfully installation of `pg-native` will get a warning and fallback to regular `pg` module.

```js
const fastify = require('fastify')

fastify.register(require('fastify-postgres'), {
connectionString: 'postgres://postgres@localhost/postgres',
native: true
})

fastify.get('/user/:id', (req, reply) => {
fastify.pg.query(
'SELECT id, username, hash, salt FROM users WHERE id=$1', [req.params.id],
function onResult (err, result) {
reply.send(err || result)
}
)
})

fastify.listen(3000, err => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})
```

## Acknowledgements

This project is kindly sponsored by:
Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
'use strict'

const fp = require('fastify-plugin')
const pg = require('pg')
let pg = require('pg')

function fastifyPostgres (fastify, options, next) {
if (options.native) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are passing the options object to Pool, it's better that we remove the native key from the object.

if (options.native) {
   delete options.native
   ...

delete options.native
if (!pg.native) {
console.warn('pg-native not installed, can\'t use native option - fallback to pg module')
} else {
pg = pg.native
}
}

const pool = new pg.Pool(options)

fastify.decorate('pg', {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"devDependencies": {
"fastify": "^0.30.1",
"pg-native": "^2.2.0",
"standard": "^10.0.3",
"tap": "^10.7.2"
}
Expand Down
25 changes: 25 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,28 @@ test('use query util with promises', t => {
})
})
})

test('use native module', t => {
t.plan(2)

const fastify = Fastify()

fastify.register(fastifyPostgres, {
connectionString: 'postgres://postgres@localhost/postgres',
native: true
})

fastify.ready(err => {
t.error(err)
fastify.pg
.query('SELECT 1 AS one')
.then(result => {
t.ok(result.rows[0].one === 1)
fastify.close()
})
.catch(err => {
t.fail(err)
fastify.close()
})
})
})