Socket
Socket
Sign inDemoInstall

hapi-node-postgres-7

Package Overview
Dependencies
39
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    hapi-node-postgres-7

Hapijs plugin for using postgres (with native binding)


Version published
Weekly downloads
1
Maintainers
1
Created
Weekly downloads
 

Readme

Source

hapijs-node-postgres

hapi-node-postgres-7

Install the plugin

npm install --save hapi-node-postgres-7

Use native mode

if you would like to use the native mode you'll have to manually npm install pg-native I use the pure Javascript driver it's protable and pretty fast.

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-7'];
    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

Keywords

FAQs

Last updated on 23 Jan 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc