link-stack/packages/hapi-pg-promise/README.md

2.9 KiB

hapi-pg-promise

This is a plugin for hapi.js that decorates server and request with a pg-promise database instance.

Usage

import * as Hapi from "@hapi/hapi";
import PgPromisePlugin from "@digiresilience/hapi-pg-promise";

const server = new Hapi.Server();

// the validator must be registered before registering the plugin
await server.validator(Joi);

// register the plugin
await server.register({
  plugin: PgPromisePlugin,
  options: {
    // the only required parameter is the connection string
    connection: "postgresql://....",
    // ... and the pg-promise initialization options
    pgpInit: {...}
  },
});

Reference the pg-promise initialization options

Options consist of:

  • connection - pg-promise/pg connection string or object
  • pgpInit - the pg-promise initialization options (mutually exclusive with pgp)
  • pgp - an existing pre-initialized pg-promise instance (mutually exclusive with pgpInit)
  • logSql - a boolean that when true, causes pgp monitor to print all sql statements. !! WARNING !! setting to true could cause data leaks
  • decorateAs - an object containing..
    • pgp - a string. the plugin will decorate server and request with the pgp instance under this key (default: pgp)
    • db - a string. the plugin will decorate server and request with the db instance under this key (default: db)
    • if either of these is falsey, then the plugin will not decorate

Defaults are defined in src/index.ts

Credits

Copyright © 2020-present Center for Digital Resilience

Contributors

Abel Luck
Abel Luck

License

License: AGPL v3

GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.