Skip to content

Commit 072c7c0

Browse files
committedFeb 15, 2015
init
0 parents  commit 072c7c0

7 files changed

+155
-0
lines changed
 

‎.editorconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
indent_style = space
7+
indent_size = 2
8+
charset = utf-8
9+
trim_trailing_whitespace = true

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
node_modules

‎LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Paul Serraino
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

‎index.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var pg = require('pg')
2+
//PGEvents = require('pg-events')
3+
4+
pg.connect('postgres://paulserraino@localhost/test', function (err, client, done) {
5+
if (err) throw err;
6+
console.log('connected');
7+
//var events = PGEvents(client);
8+
9+
//events.listen('users')
10+
//events.on('users:update', fn);
11+
//events.on('users:insert', fn);
12+
//events.on('users:delete', fn);
13+
14+
15+
});
16+
17+
var EventEmitter = require('events').EventEmitter;
18+
var inherits = require('inherits');
19+
20+
function PGEvents (client) {
21+
if (!(this instanceof PGEvents)) return new PGEvents(client);
22+
var self = this;
23+
24+
this.client = client;
25+
this.events = {};
26+
};
27+
28+
inherits(pgEvents, EventEmitter);
29+
30+
pgEvents.prototype.listen = function (table) {};
31+
pgEvents.prototype.notify = function (table) {};
32+
pgEvents.prototype.unlisten = function (table) {};
33+

‎install.sql

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
CREATE OR REPLACE FUNCTION notify_trigger() RETURNS trigger AS $$
2+
DECLARE
3+
_json_ text := '{}';
4+
BEGIN
5+
IF TG_OP = 'INSERT' THEN
6+
_json_ := '{"table": "' || TG_TABLE_NAME || '",
7+
"operation": "insert",
8+
"timestamp": "' || CURRENT_TIMESTAMP || '",
9+
"data": ' || row_to_json(NEW) || '}';
10+
11+
ELSIF TG_OP = 'UPDATE' THEN
12+
_json_ := '{"table:" "' || TG_TABLE_NAME || '",
13+
"operation": "update",
14+
"timestamp": "' || CURRENT_TIMESTAMP || '",
15+
"old_data": "' || row_to_json(OLD) || ',
16+
"data": ' || row_to_json(NEW) || '}';
17+
18+
ELSIF TG_OP = 'DELETE' THEN
19+
_json_ := '{"table": "' || TG_TABLE_NAME || '",
20+
"operation": "delete",
21+
"timestamp": "' || CURRENT_TIMESTAMP || '",
22+
"data": ' || row_to_json(OLD) || '}';
23+
END IF;
24+
25+
PERFORM pg_notify(TG_TABLE_NAME::text, _json_);
26+
27+
RETURN NULL;
28+
END;
29+
$$ LANGUAGE plpgsql;
30+
31+
CREATE OR REPLACE FUNCTION add_notify_trigger_to_table(table_name text)
32+
RETURNS VOID as $$
33+
BEGIN
34+
EXECUTE 'DROP TRIGGER IF EXISTS notify_trigger_event ON ' || table_name::regclass;
35+
36+
EXECUTE '
37+
CREATE TRIGGER notify_trigger_event
38+
AFTER INSERT OR UPDATE OR DELETE
39+
ON ' || table_name::regclass || '
40+
FOR EACH ROW
41+
EXECUTE PROCEDURE notify_trigger()';
42+
END;
43+
$$ LANGUAGE plpgsql;
44+
45+
CREATE OR REPLACE FUNCTION remove_notify_trigger_from_table(table_name text)
46+
RETURNS VOID AS $$
47+
BEGIN
48+
EXECUTE 'DROP TRIGGER IF EXISTS notify_trigger_event ON ' || table_name::regclass;
49+
END;
50+
$$ LANGUAGE plpgsql;

‎package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "pg-events",
3+
"version": "0.0.0",
4+
"description": "postgreSQL listen/notify events",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "./node_modules/mocha/bin/mocha tests/"
8+
},
9+
"keywords": [
10+
"pg",
11+
"listen",
12+
"notify",
13+
"events"
14+
],
15+
"author": {
16+
"name": "Paul Serraino",
17+
"url": "http://paulserraino.com"
18+
},
19+
"license": "MIT",
20+
"dependencies": {
21+
"inherits": "~2.0.1"
22+
},
23+
"devDependencies": {
24+
"mocha": "~2.1.0",
25+
"pg": "~4.2.0"
26+
},
27+
"engines": {
28+
"node": "0.10.x"
29+
}
30+
}

‎readme.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
```
3+
npm install pg-events
4+
```
5+
```
6+
psql -h localhost -d mydb < install.sql
7+
```
8+
9+
#License
10+
MIT

0 commit comments

Comments
 (0)
Please sign in to comment.