From 157b09bd844fa013bf2600b95cb1bf996bba404c Mon Sep 17 00:00:00 2001 From: Simone Sanfratello Date: Sat, 14 Oct 2017 05:16:52 +0200 Subject: [PATCH 1/4] feature: native option --- README.md | 30 ++++++++++++- index.js | 8 +++- package-lock.json | 107 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++ test.js | 25 +++++++++++ 5 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 package-lock.json diff --git a/README.md b/README.md index 8dbe5a6..6db9c1a 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 +Use native libpq to gain high performance; it will use [pg-native](https://github.com/brianc/node-pg-native) instead of [pg](https://github.com/brianc/node-pg). +Note: it requires PostgreSQL client libraries & tools installed, see +[instructions](https://github.com/brianc/node-pg-native#install). + +```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..773a2eb 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,15 @@ 'use strict' const fp = require('fastify-plugin') -const pg = require('pg') function fastifyPostgres (fastify, options, next) { + let pg + if (options.native) { + pg = require('pg-native') + } else { + pg = require('pg') + } + const pool = new pg.Pool(options) fastify.decorate('pg', { diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..02a1cfa --- /dev/null +++ b/package-lock.json @@ -0,0 +1,107 @@ +{ + "name": "fastify-postgres", + "version": "0.1.1", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "bindings": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.2.1.tgz", + "integrity": "sha1-FK1hE4EtLTfXLme0ystLtyZQXxE=" + }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + }, + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" + }, + "libpq": { + "version": "1.8.7", + "resolved": "https://registry.npmjs.org/libpq/-/libpq-1.8.7.tgz", + "integrity": "sha1-wt6xIeKPf4S9OyRRr/9otmY+dPk=", + "requires": { + "bindings": "1.2.1", + "nan": "2.7.0" + } + }, + "nan": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.7.0.tgz", + "integrity": "sha1-2Vv3IeyHfgjbJ27T/G63j5CDrUY=" + }, + "pg-native": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-native/-/pg-native-2.2.0.tgz", + "integrity": "sha1-vIR1b07L9gD/fLSOpYVBQYMM8eQ=", + "requires": { + "libpq": "1.8.7", + "pg-types": "1.12.1", + "readable-stream": "1.0.31" + } + }, + "pg-types": { + "version": "1.12.1", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-1.12.1.tgz", + "integrity": "sha1-1kCH45A7WP+q0nnnWVxSIIoUw9I=", + "requires": { + "postgres-array": "1.0.2", + "postgres-bytea": "1.0.0", + "postgres-date": "1.0.3", + "postgres-interval": "1.1.1" + } + }, + "postgres-array": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-1.0.2.tgz", + "integrity": "sha1-jgsy6wO/d6XAp4UeBEHBaaJWojg=" + }, + "postgres-bytea": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", + "integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=" + }, + "postgres-date": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.3.tgz", + "integrity": "sha1-4tiXAu/bJY/52c7g/pG9BpdSV6g=" + }, + "postgres-interval": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.1.1.tgz", + "integrity": "sha512-OkuCi9t/3CZmeQreutGgx/OVNv9MKHGIT5jH8KldQ4NLYXkvmT9nDVxEuCENlNwhlGPE374oA/xMqn05G49pHA==", + "requires": { + "xtend": "4.0.1" + } + }, + "readable-stream": { + "version": "1.0.31", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.0.31.tgz", + "integrity": "sha1-jyUC4LyeOw2huUUgqrtOJgPsr64=", + "requires": { + "core-util-is": "1.0.2", + "inherits": "2.0.3", + "isarray": "0.0.1", + "string_decoder": "0.10.31" + } + }, + "string_decoder": { + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=" + }, + "xtend": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", + "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=" + } + } +} diff --git a/package.json b/package.json index 788c433..702ae6a 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,9 @@ "fastify-plugin": "^0.1.1", "pg": "^7.3.0" }, + "optionalDependencies": { + "pg-native": "^2.2.0" + }, "devDependencies": { "fastify": "^0.29.2", "standard": "^10.0.3", 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() + }) + }) +}) From d25969b97aa5d5a29273f0f8f29377ef177f8aac Mon Sep 17 00:00:00 2001 From: Simone Sanfratello Date: Sat, 14 Oct 2017 18:59:11 +0200 Subject: [PATCH 2/4] feature: native option --- .gitignore | 3 ++ index.js | 10 +++-- package-lock.json | 107 ---------------------------------------------- package.json | 5 +-- 4 files changed, 11 insertions(+), 114 deletions(-) delete mode 100644 package-lock.json 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/index.js b/index.js index 773a2eb..e77ee6e 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,15 @@ 'use strict' const fp = require('fastify-plugin') +let pg = require('pg') function fastifyPostgres (fastify, options, next) { - let pg if (options.native) { - pg = require('pg-native') - } else { - pg = require('pg') + if (!pg.native) { + console.warn('pg-native not installed, can\'t use native option') + } else { + pg = pg.native + } } const pool = new pg.Pool(options) diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index 02a1cfa..0000000 --- a/package-lock.json +++ /dev/null @@ -1,107 +0,0 @@ -{ - "name": "fastify-postgres", - "version": "0.1.1", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "bindings": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.2.1.tgz", - "integrity": "sha1-FK1hE4EtLTfXLme0ystLtyZQXxE=" - }, - "core-util-is": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", - "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" - }, - "inherits": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", - "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" - }, - "isarray": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", - "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" - }, - "libpq": { - "version": "1.8.7", - "resolved": "https://registry.npmjs.org/libpq/-/libpq-1.8.7.tgz", - "integrity": "sha1-wt6xIeKPf4S9OyRRr/9otmY+dPk=", - "requires": { - "bindings": "1.2.1", - "nan": "2.7.0" - } - }, - "nan": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/nan/-/nan-2.7.0.tgz", - "integrity": "sha1-2Vv3IeyHfgjbJ27T/G63j5CDrUY=" - }, - "pg-native": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/pg-native/-/pg-native-2.2.0.tgz", - "integrity": "sha1-vIR1b07L9gD/fLSOpYVBQYMM8eQ=", - "requires": { - "libpq": "1.8.7", - "pg-types": "1.12.1", - "readable-stream": "1.0.31" - } - }, - "pg-types": { - "version": "1.12.1", - "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-1.12.1.tgz", - "integrity": "sha1-1kCH45A7WP+q0nnnWVxSIIoUw9I=", - "requires": { - "postgres-array": "1.0.2", - "postgres-bytea": "1.0.0", - "postgres-date": "1.0.3", - "postgres-interval": "1.1.1" - } - }, - "postgres-array": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-1.0.2.tgz", - "integrity": "sha1-jgsy6wO/d6XAp4UeBEHBaaJWojg=" - }, - "postgres-bytea": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", - "integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=" - }, - "postgres-date": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.3.tgz", - "integrity": "sha1-4tiXAu/bJY/52c7g/pG9BpdSV6g=" - }, - "postgres-interval": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.1.1.tgz", - "integrity": "sha512-OkuCi9t/3CZmeQreutGgx/OVNv9MKHGIT5jH8KldQ4NLYXkvmT9nDVxEuCENlNwhlGPE374oA/xMqn05G49pHA==", - "requires": { - "xtend": "4.0.1" - } - }, - "readable-stream": { - "version": "1.0.31", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.0.31.tgz", - "integrity": "sha1-jyUC4LyeOw2huUUgqrtOJgPsr64=", - "requires": { - "core-util-is": "1.0.2", - "inherits": "2.0.3", - "isarray": "0.0.1", - "string_decoder": "0.10.31" - } - }, - "string_decoder": { - "version": "0.10.31", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", - "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=" - }, - "xtend": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", - "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=" - } - } -} diff --git a/package.json b/package.json index 702ae6a..f020833 100644 --- a/package.json +++ b/package.json @@ -27,11 +27,10 @@ "homepage": "https://github.com/fastify/fastify-postgres#readme", "dependencies": { "fastify-plugin": "^0.1.1", - "pg": "^7.3.0" - }, - "optionalDependencies": { + "pg": "^7.3.0", "pg-native": "^2.2.0" }, + "optionalDependencies": {}, "devDependencies": { "fastify": "^0.29.2", "standard": "^10.0.3", From 300e15b0344f1bbdcaff43620db98f7700fd22af Mon Sep 17 00:00:00 2001 From: Simone Sanfratello Date: Sun, 15 Oct 2017 16:04:35 +0200 Subject: [PATCH 3/4] feature: native option --- README.md | 5 +++-- index.js | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6db9c1a..1fb158e 100644 --- a/README.md +++ b/README.md @@ -96,9 +96,10 @@ 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 -Use native libpq to gain high performance; it will use [pg-native](https://github.com/brianc/node-pg-native) instead of [pg](https://github.com/brianc/node-pg). +Use native `libpq` to gain high performance; it will use [pg-native](https://github.com/brianc/node-pg-native) instead of [pg](https://github.com/brianc/node-pg). Note: it requires PostgreSQL client libraries & tools installed, see -[instructions](https://github.com/brianc/node-pg-native#install). +[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') diff --git a/index.js b/index.js index e77ee6e..8b36372 100644 --- a/index.js +++ b/index.js @@ -5,8 +5,9 @@ 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') + console.warn('pg-native not installed, can\'t use native option - fallback to pg module') } else { pg = pg.native } From a953100ddb427e1d91eb50e0f592becede75af6d Mon Sep 17 00:00:00 2001 From: Simone Sanfratello Date: Mon, 16 Oct 2017 05:18:45 +0200 Subject: [PATCH 4/4] feature: native option --- README.md | 5 ++--- package.json | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1fb158e..cd92d4b 100644 --- a/README.md +++ b/README.md @@ -96,9 +96,8 @@ 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 -Use native `libpq` to gain high performance; it will use [pg-native](https://github.com/brianc/node-pg-native) instead of [pg](https://github.com/brianc/node-pg). -Note: it requires PostgreSQL client libraries & tools installed, see -[instructions](https://github.com/brianc/node-pg-native#install). +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 diff --git a/package.json b/package.json index f020833..2d34ed5 100644 --- a/package.json +++ b/package.json @@ -27,11 +27,10 @@ "homepage": "https://github.com/fastify/fastify-postgres#readme", "dependencies": { "fastify-plugin": "^0.1.1", - "pg": "^7.3.0", - "pg-native": "^2.2.0" + "pg": "^7.3.0" }, - "optionalDependencies": {}, "devDependencies": { + "pg-native": "^2.2.0", "fastify": "^0.29.2", "standard": "^10.0.3", "tap": "^10.7.2"