Setup changes-feed-like trigger functions in PostgreSQL
JavaScript
Latest commit e0c0d53 Apr 9, 2015 @klaemo 0.2.0

readme.md

postgres-triggers Build Status

NPM

Create trigger functions for changes (INSERT, UPDATE, DELETE) in tables. The triggers will return a JSON object with some information and the changed row.

Heavily inspired by this blog post by @fritzy.

Because of the use of json_build_object() you'll need Postgres 9.4 or higher.

This module is in its early stages. Feedback and PRs welcome!

Install

npm i [-g] postgres-triggers

Usage (CLI)

postgres-triggers postgres://foo@localhost:5432/db table1 tbl2Name:idColumn ...

Usage (API)

const triggers = require('postgres-triggers')

triggers({
    db: 'postgres://foo@localhost:5432/db',
    // three ways of specifying table name and id column
    // default idColumn: 'id'
    tables: [
      'tbl1', 'tblName:idColumn', { name: 'tableName', id: 'idColumn'}
    ],
    channel: 'table_update' // optional
}, function(err) {
  if (err) throw err
  console.log('done')
})

Trigger Payload

{
  table: 'table-name',
  id: 'id-of-the-change-row',
  type: 'insert|update|delete',
  row: { id: 'baz', foo: 'bar', ... }
}

TODO

  • allow removal of triggers

Tests

You need to give it a database connection string to be able to run the tests.

POSTGRES=postgres://postgres@localhost:5432/postgres npm test

License

MIT