Skip to content

Commit 783e01a

Browse files
committed
Initial commit
0 parents  commit 783e01a

File tree

6 files changed

+116
-0
lines changed

6 files changed

+116
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.sw?
2+
npm-debug.log
3+
node_modules

ReadMe.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Unreleased, please come back later.

bin/postgres

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env node
2+
3+
var PostgresHook = require('../lib/postgres').PostgresHook;
4+
5+
var postgres = new PostgresHook({
6+
"name": "the-postgres-hook",
7+
"debug": true,
8+
"host": '127.0.0.1',
9+
"port": 5672,
10+
"vhost": '/',
11+
"login": 'guest',
12+
"password": 'guest',
13+
"ssl": false,
14+
"exchange": 'events',
15+
"topics": ['#']
16+
});
17+
18+
postgres.start();

config.json.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"database": "pg://brian:1234@localhost/postgres"
3+
}

lib/postgres.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
3+
hook.io hook for interfacing with postgres
4+
5+
*/
6+
7+
8+
var Hook = require('hook.io').Hook,
9+
pg = require('pg'),
10+
util = require('util');
11+
12+
var PostgresHook = exports.PostgresHook = function(options){
13+
var self = this;
14+
15+
Hook.call(this, options);
16+
17+
self.options = options;
18+
19+
self.on('hook::ready', function(){
20+
self._start();
21+
});
22+
23+
self.on('*::postgres::query', function(sql, callback) {
24+
self._query(sql, callback);
25+
});
26+
};
27+
28+
// Postgres inherits from Hook
29+
util.inherits(PostgresHook, Hook);
30+
31+
PostgresHook.prototype._start = function() {
32+
var self = this;
33+
34+
self.client = new pg.Client(self.options.database);
35+
self.client.connect();
36+
37+
self.client.on('error', function(err) {
38+
console.dir(err);
39+
self._error(err);
40+
});
41+
};
42+
43+
PostgresHook.prototype._query = function(sql, callback) {
44+
var self = this;
45+
46+
var query = self.client.apply(sql); // handle strings, arrays, or objects with apply
47+
var rows = [];
48+
49+
query.on('row', function(row) {
50+
rows.push(row);
51+
});
52+
53+
query.on('end', function() {
54+
callback(null, rows);
55+
});
56+
57+
query.on('error', function(err) {
58+
callback(err, null);
59+
});
60+
};
61+
62+
PostgresHook.prototype._error = function(err) {
63+
var self = this;
64+
self.emit('postgres::error', err);
65+
};

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"authors": [
3+
"Paul Bellamy <paul.a.bellamy@gmail.com>"
4+
],
5+
"name": "hookio-postgres",
6+
"description": "hook.io hook to interface with a postgres database",
7+
"version": "0.1.0",
8+
"repository": {
9+
"type": "git",
10+
"url": "git://github.com/paulbellamy/hookio-postgres.git"
11+
},
12+
"bin": {
13+
"hookio-postgres": "./bin/postgres"
14+
},
15+
"main" : "lib/postgres",
16+
"engines": {
17+
"node": ">= v0.4.7"
18+
},
19+
"dependencies": {
20+
"hook.io": "0.8.x",
21+
"colors": "0.5.x",
22+
"async": "0.1.x",
23+
"pg": "0.6.x"
24+
},
25+
"devDependencies": {}
26+
}

0 commit comments

Comments
 (0)