Hapi PG Pool
Create a PostgreSQL connection pool available to Hapi request handlers.
Implements pooling with the pg-pool
package.
Using
-
Install from NPM
npm i hapi-pg-pool -S
-
Ensure you have either your favourite PostgreSQL client dependency, or install the pg
client
npm i pg -S
-
Register the extension
server.register({
register: require('hapi-pg-pool'),
options: {
database: 'postgres',
user: 'postgres',
password: 'postgres'
}
}, function (err) {
if (err) {
throw err
}
})
-
Use the pool in your handlers
server.route({
method: 'GET',
path: '/',
handler: function(request, reply) {
console.log('sleepy...');
request.pg.query('SELECT pg_sleep(1)', function (err, result) {
if (err) {
server.log('error', err)
return reply(err)
}
reply('awake!')
})
}
});
Refer to pg-pool
for more ways to use the request.pg
(Pool) object.
License
MIT.