All Projects → jedireza → Hapi Node Postgres

jedireza / Hapi Node Postgres

Licence: mit
📦 Wrap hapi requests with a pg connection

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Hapi Node Postgres

api.pokedextracker.com
API for pokedextracker.com
Stars: ✭ 38 (+18.75%)
Mutual labels:  postgres, hapi
Typescript Seed
Typescript Seed Project (Angular, Hapi, Cookie Auth, TypeORM, Postgres)
Stars: ✭ 12 (-62.5%)
Mutual labels:  postgres, hapi
Go Kallax
Kallax is a PostgreSQL typesafe ORM for the Go language.
Stars: ✭ 853 (+2565.63%)
Mutual labels:  postgres
Hapi Auth Keycloak
JSON Web Token based Authentication powered by Keycloak
Stars: ✭ 29 (-9.37%)
Mutual labels:  hapi
Kotgres
SQL generator and result set mapper for Postgres and Kotlin
Stars: ✭ 21 (-34.37%)
Mutual labels:  postgres
Pg Extra
🐘 some helpful extensions for node-postgres
Stars: ✭ 12 (-62.5%)
Mutual labels:  postgres
Hapi Boom Decorators
Decorates a Hapi server's response toolkit with functions to make it easy to reply with Boom errors
Stars: ✭ 28 (-12.5%)
Mutual labels:  hapi
Hotseat Api
Rest API of a barber shop application - Built with Express, TypeORM and Postgres
Stars: ✭ 27 (-15.62%)
Mutual labels:  postgres
Docker Pgredshift
Redshift docker image based on postgres
Stars: ✭ 32 (+0%)
Mutual labels:  postgres
Openlok
🚈 React.js app to access the Deutsche Bahn API (German Railway Corporation)
Stars: ✭ 15 (-53.12%)
Mutual labels:  hapi
Entityframeworkcore.bootkit
EntityFrameworkCore Start Kit
Stars: ✭ 29 (-9.37%)
Mutual labels:  postgres
Guardian auth
The Guardian Authentication Implementation Using Ecto/Postgresql Elixir Phoenix [ User Authentication ]
Stars: ✭ 15 (-53.12%)
Mutual labels:  postgres
Ecoleta
Ecoleta - Developed during the event NLW 1.0 by @Rocketseat
Stars: ✭ 29 (-9.37%)
Mutual labels:  postgres
Joi2gql
Conversion of Joi schemas into GraphQL data types
Stars: ✭ 11 (-65.62%)
Mutual labels:  hapi
Zeeql3
The ZeeQL (EOF/CoreData/AR like) Database Toolkit for Swift
Stars: ✭ 29 (-9.37%)
Mutual labels:  postgres
Node Pg Migrate
Node.js database migration management for Postgresql
Stars: ✭ 838 (+2518.75%)
Mutual labels:  postgres
Awesome Postgres
A curated list of awesome PostgreSQL software, libraries, tools and resources, inspired by awesome-mysql
Stars: ✭ 7,468 (+23237.5%)
Mutual labels:  postgres
Example Api
A base API project to bootstrap and prototype quickly.
Stars: ✭ 27 (-15.62%)
Mutual labels:  postgres
Pgwatch2
PostgreSQL metrics monitor/dashboard
Stars: ✭ 960 (+2900%)
Mutual labels:  postgres
Punchcard
The Punchcard CMS
Stars: ✭ 29 (-9.37%)
Mutual labels:  postgres

hapi-node-postgres

Wrap requests with a Postgres connection.

Build Status Dependency Status devDependency Status peerDependency Status

We use the pg (node-postgres) module and take advantage of its connection pooling feature.

Note: Your project should have its own pg dependency installed. We depend on pg via peerDependencies. If you elect to use native bindings you'll also need to install the pg-native package.

Install

$ npm install hapi-node-postgres

Usage

In request handlers

In your request handlers you'll have access to request.pg.client which you can use to make DB requests. We even clean up the connection for you after the request is complete.

During your request handler you can set request.pg.kill to true, and we'll remove the connection from the pool instead of simply returning it to be reused. This is usually done when an error is detected during a query.

server.route({
    method: 'GET',
    path: '/',
    handler: function (req, reply) {

        const cmd = 'SELECT * FROM stats;';

        req.pg.client.query(cmd, (err, result) => {

            if (err) {
                request.pg.kill = true;

                return reply(err);
            }

            reply('Number of rows: ', result.rows.length);
        });
    }
});

Outside of request handlers

Outside of request handlers you can get access to the Postgres connect method. We expose it from the plugin already bound to the connection string.

After plugin registration is complete you'll have access through the server.plugins api.

const hpg = server.plugins['hapi-node-postgres'];

hpg.connect((err, client, done) => {

      if (err) {
          // handle error case
      }

      // make queries with client

      // call done to return client to the connection pool
  });

Plugin registration

Register the plugin manually.

const plugin = {
    register: require('hapi-node-postgres'),
    options: {
        connectionString: 'postgres://username:[email protected]/database',
        native: true
    }
};

server.register(plugin, (err) => {

    if (err) {
        console.error('Failed loading "hapi-node-postgres" plugin');
    }
 });

Or include it in your composer manifest.

"plugins": {
    "hapi-node-postgres": {
        "connectionString": "postgres://username:[email protected]/database",
        "native": true
    }
}

The options passed to the plugin is an object where:

  • connectionString - a string representing the connection details for Postgres.
  • attach - a string representing the extension point event where the request is decorated with the pg attribute. Defaults to onPreHandler.
  • detach - a string representing the server event where the connection is cleaned up. Defaults to tail.
  • native - a boolean indicating if the native bindings should be used. Defaults to false. Native bindings offer 20-30% increase in parsing speed.

See the hapijs docs to learn more about request lifecycle events.

License

MIT

Don't forget

What you create with hapi-node-postgres is more important than hapi-node-postgres.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].