Skip to content

Commit

Permalink
Initial Source files
Browse files Browse the repository at this point in the history
  • Loading branch information
kabirbaidhya committed Jun 28, 2016
0 parents commit a6f150e
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .babelrc
@@ -0,0 +1,4 @@
{
"presets": ["es2015"],
"plugins": []
}
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
dist/
config.yml
node_modules
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Kabir Baidhya

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
10 changes: 10 additions & 0 deletions README.md
@@ -0,0 +1,10 @@
# pglistend
[![npm version](https://img.shields.io/npm/v/pglistend.svg?style=flat-square)](https://www.npmjs.com/package/pglistend) [![npm downloads](https://img.shields.io/npm/dt/pglistend.svg?style=flat-square)](https://www.npmjs.com/package/pglistend) [![Code Climate](https://img.shields.io/codeclimate/github/kabirbaidhya/pglistend.svg?style=flat-square)](https://codeclimate.com/github/kabirbaidhya/pglistend)

A lightweight PostgreSQL LISTEN Daemon built on using Nodejs and Systemd.

## Installation

```bash
$ npm install -g pglistend
```
3 changes: 3 additions & 0 deletions bin/pglisten
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../dist/index');
2 changes: 2 additions & 0 deletions index.js
@@ -0,0 +1,2 @@
// Just in case the user tries to require() the package.
throw new Error('This package is a CLI tool and was not supposed to be used with require() programatically');
42 changes: 42 additions & 0 deletions package.json
@@ -0,0 +1,42 @@
{
"name": "pglistend",
"version": "0.0.0",
"description": "pglistend - PostgreSQL LISTEN Daemon using Node.js/Systemd",
"scripts": {
"start": "nodemon --exec babel-node src/index.js --config=./config.yml",
"clean": "rm -rf dist/",
"build": "npm run clean && babel src/ -d dist/",
"prepublish": "npm run build"
},
"keywords": [
"postgresql",
"listen",
"notify",
"daemon",
"pglistend"
],
"author": "Kabir Baidhya <kabirbaidhya@gmail.com>",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/kabirbaidhya/pglistend.git"
},
"dependencies": {
"pg": "^6.0.1",
"yamljs": "^0.2.8",
"yargs": "^4.7.1"
},
"devDependencies": {
"babel-cli": "^6.10.1",
"babel-preset-es2015": "^6.9.0",
"nodemon": "^1.9.2"
},
"preferGlobal": true,
"bin": {
"pglisten": "bin/pglisten"
},
"files": [
"bin/",
"dist/"
]
}
31 changes: 31 additions & 0 deletions src/Listener.js
@@ -0,0 +1,31 @@
import {Client} from 'pg';

class Listener {

constructor(config) {
this.config = config;
}

listen() {
const {connection, channel} = this.config;
let client = new Client(connection);

client.connect();
console.log('Connection established');

client.on('notification', (n) => {
try {
let payload = JSON.parse(n.payload);

console.log(n.channel, payload);
} catch(e) {
console.error('Error parsing the JSON payload. Please make sure the payload sent by postgresql NOTIFY is a valid JSON');
}
});

console.log('Started Listening to channel ' + channel);
client.query('LISTEN ' + channel);
}
}

export default Listener;
13 changes: 13 additions & 0 deletions src/index.js
@@ -0,0 +1,13 @@
import {Client} from 'pg';
import Yaml from 'yamljs';
import yargs from 'yargs';
import Listener from './Listener';

let argv = yargs.usage('Usage: $0 --config=[filename]')
.demand(['config']).argv;

let config = Yaml.load(argv.config);
let listener = new Listener(config);

// Start listening
listener.listen();

0 comments on commit a6f150e

Please sign in to comment.