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
29 changes: 13 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
'use strict'

const defaultPg = require('pg')
const fp = require('fastify-plugin')
var defaultPg = require('pg')

function transactionUtil (pool, fn, cb) {
pool.connect((err, client, done) => {
if (err) return cb(err)

const shouldAbort = (err) => {
if (err) {
client.query('ROLLBACK', () => {
done()
})
client.query('ROLLBACK', done)
}

return !!err
}

Expand All @@ -21,9 +20,8 @@ function transactionUtil (pool, fn, cb) {

client.query('COMMIT', (err) => {
done()
if (err) {
return cb(err)
}
if (err) return cb(err)

return cb(null, res)
})
}
Expand All @@ -34,9 +32,7 @@ function transactionUtil (pool, fn, cb) {
const promise = fn(client, commit)

if (promise && typeof promise.then === 'function') {
promise.then(
(res) => commit(null, res),
(e) => commit(e))
promise.then((res) => commit(null, res), (e) => commit(e))
}
})
})
Expand All @@ -49,7 +45,8 @@ function transact (fn, cb) {

return new Promise((resolve, reject) => {
transactionUtil(this, fn, function (err, res) {
if (err) { return reject(err) }
if (err) return reject(err)

return resolve(res)
})
})
Expand Down Expand Up @@ -81,26 +78,26 @@ function fastifyPostgres (fastify, options, next) {
transact: transact.bind(pool)
}

fastify.addHook('onClose', (fastify, done) => pool.end(done))

if (name) {
if (!fastify.pg) {
fastify.decorate('pg', {})
}

if (fastify.pg[name]) {
return next(new Error('Connection name has already been registered: ' + name))
return next(new Error(`fastify-postgres '${name}' instance name has already been registered`))
}

fastify.pg[name] = db
} else {
if (fastify.pg) {
next(new Error('fastify-postgres has already registered'))
return next(new Error('fastify-postgres has already been registered'))
} else {
fastify.pg = db
fastify.decorate('pg', db)
}
}

fastify.addHook('onClose', (fastify, done) => pool.end(done))

next()
}

Expand Down
21 changes: 14 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"description": "Fastify PostgreSQL connection plugin",
"main": "index.js",
"scripts": {
"test": "standard && tap test.js",
"load-data": "docker run --rm --link fastify-postgres:postgres postgres:11-alpine psql -h postgres -d postgres -c 'CREATE TABLE users(id serial PRIMARY KEY, username VARCHAR (50) NOT NULL);' -U postgres",
"postgres": "docker run --rm -p 5432:5432 --name fastify-postgres postgres:11-alpine"
"test": "standard && tap -J test/*.test.js",
"test:report": "standard && tap -J --coverage-report=html test/*.test.js",
"test:verbose": "standard && tap -J test/*.test.js -Rspec",
"load-data": "docker run --rm -d --link fastify-postgres:postgres postgres:11-alpine psql -h postgres -d postgres -c 'CREATE TABLE users(id serial PRIMARY KEY, username VARCHAR (50) NOT NULL);' -U postgres",
"postgres": "docker run --rm -d -p 5432:5432 --name fastify-postgres postgres:11-alpine"
},
"repository": {
"type": "git",
Expand All @@ -27,16 +29,21 @@
},
"homepage": "https://github.com/fastify/fastify-postgres#readme",
"dependencies": {
"fastify-plugin": "^1.4.0"
"fastify-plugin": "^1.5.0"
},
"devDependencies": {
"fastify": "^2.0.0",
"fastify": "^2.3.0",
"pg": "*",
"pg-native": "^3.0.0",
"standard": "^12.0.0",
"tap": "^12.5.2"
"standard": "^12.0.1",
"tap": "^12.7.0"
},
"peerDependencies": {
"pg": ">=6.0.0"
},
"greenkeeper": {
"ignore": [
"tap"
]
}
}
Loading