Socket
Socket
Sign inDemoInstall

hapi-piggy

Package Overview
Dependencies
95
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    hapi-piggy

A PostgreSQL plugin for HAPI, with a convenient key/val store abstraction layer.


Version published
Maintainers
1
Install size
3.78 MB
Created

Readme

Source

hapi-piggy Build Status npm version

A PostgreSQL plugin for HAPI, with a convenient key/val store abstraction layer.

Config

const defaultOptions = {
  connectionName: 'default',
  url: 'postgresql://localhost',
  connection: {
    ssl: true,
    max: 10,
    min: 4,
    idleTimeoutMillis: 30000
  }
};

Native bindings

hapi-piggy will use the native pg bindings if you have them installed as a peer.

Usage

This module exposes several server methods intended to make working with PostgreSQL a little easier for certain general use cases. It provides a key/val store-like abstraction layer, with a simplistic indexing model, and JSONB payloads.

/* Register server, config module... */
const uuid = require('uuid/v4');
const { createConnection, createStore, get, upsert } = server.methods.piggy;

createConnection()
  .then(client => {
    return createStore({
      client,
      table: 'example',
      indexes: ['lastName']
    });
  })
  .then(({ client }) => {
    return upsert({
      client,
      table: 'example',
      key: { lastName: 'Foo' },
      val: { firstName: 'Cool', lastName: 'Foo', car: 'Lambo' },
      options: { merge: true },
      generateKeyFn: () => uuid()
    });
  })
  .then(({ client }) => {
    return get({
      client,
      table: 'example',
      key: { lastName: 'Foo' }
    });
  })
  .then(({ client, key, val }) => {
    client.close();
    console.log(key, value);
    // 'uuid...', {firstName: 'Cool', lastName: 'Foo', car: 'Lambo'}
  });

Watching

You can easily set up a table that pushes notifications to a worker with the createWatchedTable or createStore with the watch boolean true and watchTable methods.

Watching a store looks like this:

const { createConnection, createStore, watchTable, set } = server.methods.piggy;

const watcher = function({ parsed }) {
  // do something with parsed payload
  // {key: 'foo'}
};

createConnection()
  .then(client => {
    return createStore({
      client,
      table: 'example',
      watch: true
    });
  })
  .then(({ client }) => {
    return watchTable({
      client,
      table: 'example',
      watcher
    });
  })
  .then(({ client }) => {
    return set({
      client,
      table: 'example',
      key: 'foo',
      val: 'bar'
    });
  })
  .then(({ client }) => {
    client.close();
  });

Methods

There are many, take a look at index.js.

Requirements

  • hapi >= 17.0.0
  • node.js >= 8.0
  • PostgresQL >= 9.4 (tested with 9.6)

Todo

  • Tests for this plugin (libpiggy will be tested alone)

Keywords

FAQs

Last updated on 06 May 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