A Hapi Postgresql Wrapper for Hapijs that support new hapi structure and using decorator method
npm install hapi-pgsqlThis Hapi Plugin creates a Connection to PostgreSQL when your server starts up and makes it available anywhere in your app's route handlers via request.pgsql.
When you shut down your server (e.g. the server.stop in your tests) the connection is closed for you.
This plugin supports Hapi v17 and above.
For older Hapi version, might consider a plugin that I love using before building this https://www.npmjs.com/package/hapi-postgres-connection
1.0.1 - Updated to support Hapi 19, bumped NodeJS to version 12+
1.0.2 - Optimized Package Size
``sh`
npm install --save hapi-pgsql
yarn add hapi-pgsql
in your server:
`js`
await server.register({
plugin: require('hapi-pgsql'),
options: {
database_url: 'postgresql://username:password@hostname/database',
}
})request.pgsql
Now all your route handlers have access to Postgres
via:
js
server.route({
method: 'GET',
path: '/',
handler: async (request, h) => {
const time = await request.pgsql.query('SELECT NOW()')
return Hello World! Time: ${time.rows[0].now};
}
});
`$3
`js
server.route({
method: 'POST',
path: '/login',
handler: async (request, h) => {
const todos = await request.pgsql.query(
SELECT * FROM user WHERE email = $1 AND password = $2 LIMIT 1,
[request.payload.email, request.payload.password]
)
return todos.rows[0]
}
});
``