-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
attempt to run tests with hapi-pg ... hangs. dwyl/hapi-login-example-…
- Loading branch information
Showing
4 changed files
with
66 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,54 @@ | ||
var pg = require('pg'); | ||
var assert = require('assert'); | ||
var internals = {}; | ||
var pkg = require('./package.json'); | ||
|
||
var CLIENT, DONE, CON = 0; // Don't worry these "Globals" are scoped by module | ||
|
||
function connect (callback) { | ||
console.log('>>>> CON: ', ++CON); | ||
if (CLIENT || DONE) { | ||
console.log(CLIENT, DONE); | ||
return callback(); | ||
} | ||
pg.connect(process.env.POSTGRES_URL, function(err, client, done) { | ||
if(err) { | ||
return callback(err); | ||
} | ||
CLIENT = client; // make available for all subs | ||
DONE = done; | ||
return callback(err); | ||
}); | ||
} | ||
exports.connect = connect; | ||
var pg = require('pg.js'); | ||
var Hoek = require('hoek'); | ||
|
||
var defaults = { | ||
connectionString: undefined, | ||
attach: 'onPreHandler', | ||
detach: 'tail' | ||
}; | ||
|
||
exports.register = function(server, options, next) { | ||
// if POSTGRES_URL Environment Variable is unset halt the server.start | ||
assert(process.env.POSTGRES_URL, 'Please set POSTGRES_URL Env Variable'); | ||
|
||
server.ext('onPreHandler', function (request, reply) { | ||
connect(function (err) { | ||
server.log(['info', 'hapi-postgres-connection'], 'DB Connection Active'); | ||
request.pg = { | ||
client: CLIENT, | ||
done: DONE | ||
var config = Hoek.applyToDefaults(defaults, options); | ||
|
||
server.ext(config.attach, function(request, reply) { | ||
var connectionString = generateConnection(options.connectionString, request); | ||
|
||
// if a connection string is not resolved, we stop the process | ||
if(!connectionString) { | ||
return reply.continue(); | ||
} | ||
|
||
pg.connect(connectionString, function(err, client, done) { | ||
if ( err ) throw err; | ||
|
||
request.postgres = { | ||
client: client, | ||
done: done | ||
} | ||
reply.continue(); | ||
}); | ||
|
||
}); | ||
|
||
server.on('tail', function(request, err) { | ||
CLIENT.end(); | ||
DONE(); | ||
server.log(['info', 'hapi-postgres-connection'], 'DB Connection Closed'); | ||
server.on('stop', function(request, err) { | ||
console.log('STOP!!') | ||
if ( request.postgres ) { | ||
request.postgres.done(); | ||
request.postgres.client.end(); | ||
} | ||
}); | ||
|
||
next(); | ||
}; | ||
} | ||
|
||
exports.register.attributes = { | ||
pkg: pkg | ||
}; | ||
pkg: require('./package.json') | ||
} | ||
|
||
function generateConnection(connectionString, request) { | ||
if(typeof connectionString === 'function') { | ||
return connectionString(request); | ||
} else { | ||
return connectionString; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,20 @@ var assert = require('assert'); | |
|
||
var server = new Hapi.Server({ debug: { request: ['error'] } }); | ||
|
||
var hapiPgOpts = { | ||
connectionString: process.env.POSTGRES_URL | ||
}; | ||
|
||
server.connection(); | ||
server.register({ register: require('../index.js') }, function(err) { | ||
assert(!err, 'No error connecting to postgres'); | ||
|
||
server.register({ | ||
register: require('hapi-pg'), | ||
options: hapiPgOpts | ||
}, function (err) { | ||
if (err) { | ||
console.error(err); | ||
throw err; | ||
} | ||
}); | ||
|
||
server.route({ | ||
|
@@ -15,9 +26,9 @@ server.route({ | |
handler: function(request, reply) { | ||
var email = '[email protected]'; | ||
var select = escape('SELECT * FROM people WHERE (email = %L)', email); | ||
request.pg.client.query(select, function(err, result) { | ||
request.postgres.client.query(select, function(err, result) { | ||
// console.log(err, result); | ||
request.pg.done(); | ||
request.postgres.done(); | ||
return reply(result.rows[0]); | ||
}) | ||
} | ||
|
@@ -30,12 +41,12 @@ server.route({ | |
var insert = escape('INSERT INTO logs (message) VALUES (%L)', | ||
request.payload.message); | ||
var select = 'SELECT * FROM logs WHERE (log_id = 2)'; | ||
request.pg.client.query(insert, function(err, result) { | ||
console.log(err, result); | ||
// request.pg.done(); | ||
request.pg.client.query(select, function(err, result) { | ||
console.log(err, result); | ||
// request.pg.done(); | ||
request.postgres.client.query(insert, function(err, result) { | ||
// console.log(err, result); | ||
// request.postgres.done(); | ||
request.postgres.client.query(select, function(err, result) { | ||
// console.log(err, result); | ||
// request.postgres.done(); | ||
return reply(result.rows[0]); | ||
}) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters