Package Exports
- hapi-node-postgres-7
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hapi-node-postgres-7) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
hapijs-node-postgres
hapi-node-postgres-7
Install the plugin
npm install --save hapi-node-postgres-7
Register the plugin
server.register({
register: Hapi_PG,
options: {
connectionString: 'postgres://user:password@localhost:5432/my_app' //optional
}
}, (err) => {
if (err) {
throw err;
}
console.log('Server is running');
});
Use Postgres via the request object
handler: function (request, reply) {
request.pg.client.query(`select * from users where user_id = ${request.params.id}`)
.then((res) => {
if (res.rows && res.rows.length > 0) {
return reply({ result: res.rows }).code(200);
} else {
return reply('Not Found').code(404);
}
})
.catch((err) => {
return reply({ error: err }).code(500);
});
}Use Postgres via the server object
var { pool } = server.plugins['hapijs-node-postgres'];
pool.query(`select * from users limit 10`)
.then((result) => {
// print the results from the table
console.log(result.rows);
})
.catch(e => { throw e });Example
- Checkout this
Bugs & PR's
- PR's are welcome just follow the coding style