hapi-node-firebird
Version:
Wrap requests with a pooled firebird database connection (forked from jedireza's work with postgres https://github.com/jedireza/hapi-node-postgres)
66 lines (44 loc) • 1.18 kB
JavaScript
;
const Hoek = require('hoek');
const Fb = require('node-firebird');
const DEFAULTS = {
attach: 'onPreHandler',
detach: 'tail'
};
exports.register = function (server, options, next) {
const config = Hoek.applyToDefaults(DEFAULTS, options);
const pool = Fb.pool(5, config);
server.ext(config.attach, (request, reply) => {
pool.get( (err, db, done) => {
if (err) {
reply(err);
console.log(err);
return;
}
request.Fb = {
db,
done
};
reply.continue();
return;
});
});
server.on(config.detach, (request, reply, err) => {
if (err) {
reply(err);
console.log(err);
return;
}
if (request.Fb) {
// Destroy pool
pool.destroy();
console.log('Pool Destroyed');
}
});
next();
};
exports.plugin = {
name: 'hapi-node-firebird',
pkg: require('./package.json'),
once: true,
};