diff --git a/.gitignore b/.gitignore index ad7dae2..9701c5c 100644 --- a/.gitignore +++ b/.gitignore @@ -62,3 +62,6 @@ typings/ # vim swap files *.swp + +# npm package lock +package-lock.json diff --git a/README.md b/README.md index 8dbe5a6..cd92d4b 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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: diff --git a/index.js b/index.js index 4dce722..8b36372 100644 --- a/index.js +++ b/index.js @@ -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) { + 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', { diff --git a/package.json b/package.json index 0316c5d..5be007d 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ }, "devDependencies": { "fastify": "^0.30.1", + "pg-native": "^2.2.0", "standard": "^10.0.3", "tap": "^10.7.2" } diff --git a/test.js b/test.js index f866a3e..6439933 100644 --- a/test.js +++ b/test.js @@ -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() + }) + }) +})