hapi-node-postgres icon indicating copy to clipboard operation
hapi-node-postgres copied to clipboard

Hapi 17 support

Open jedireza opened this issue 7 years ago • 3 comments

See: https://github.com/hapijs/hapi/issues/3658

jedireza avatar Feb 03 '18 03:02 jedireza

Have you any notice about this?

siarcarse avatar Feb 14 '18 15:02 siarcarse

Nothing new yet, I've been time poor. A PR is welcome if anyone gets time before I do.

jedireza avatar Feb 23 '18 03:02 jedireza

This is very rough but I needed this for a new project so maybe this will help someone get started...

'use strict';

const Hoek = require('hoek');
let Pg = require('pg');


const DEFAULTS = {
  connectionString: undefined,
  attach: 'onPreAuth',
  detach: 'response'
};


exports.plugin = {
  name: 'car-hapi-pg',
  register: function(server, options) {
    
    const config = Hoek.applyToDefaults(DEFAULTS, options);

    server.ext(config.attach, async (request, h) => {
      const client = new Pg.Client({connectionString: config.connectionString});
      await client.connect();
      console.log('connected to pg');
      request.pg = {
        client
      };

      return h.continue;
    });

    server.events.on(config.detach, (request, err) => {

      if (request.pg) {
        request.pg.client.end().then(() => console.log('client disconnected'));
      }
    });
  }
};

mikejerome avatar Feb 23 '18 22:02 mikejerome