Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

erezlife/hapi-pg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hapi-pg

Wrap Hapi requests with access to a Postgres connection.

It gives you access to request.postgres.client within your handlers which you can then use to make Postgres requests. It also takes care of cleaning up the connection after the request.

Usage

var hapiPgOpts = {
  connectionString: 'tcp://postgres@localhost:5432/postgres'
};

server.register({
  register: require('hapi-pg'),
  options: hapiPgOpts
}, function (err) {
  if (err) {
    console.error(err);
    throw err;
  }
});

It can also take a function to dynamically resolve connection name based on the incoming request.

var hapiPgOpts = {
  connectionString: function(request) {
    return 'tcp://postgres@localhost:5432/' + request.query.database;
  }
};

server.register({
  register: require('hapi-pg'),
  options: hapiPgOpts
}, function (err) {
  if (err) {
    console.error(err);
    throw err;
  }
});

The plugin accepts an options object with the following properties:

  • connectionString database connection string or function that returns the connection string and accepts the incoming request as an argument.
  • attach - a string representing the extension point event that attaches the pg connection to the request. Default is onPreHandler.
  • detach - a string representing the extension point event where connection cleanup is performed. Default is tail.

License

MIT

About

Wrap requests with a pg connection.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •