Skip to content

Commit 2dc1a47

Browse files
committedMar 1, 2015
option to send only ID field in notifications
1 parent 3468f3c commit 2dc1a47

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed
 

‎index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ var DEFAULTS = {
1919
removeNotifyTrigger: 'remove_notify_trigger_to_table'
2020
},
2121
operationEvents: true,
22-
checkUpdates: true // only notify if update changes record, can be set to false if record comparison is expensive
22+
checkUpdates: true, // only notify if update changes record, can be set to false if record comparison is expensive
23+
sendRecordId: false // send only record IDs, true - to send column 'id', string to send column with a given name
2324
};
2425

2526
function PGObserver (opts) {
2627
if (!(this instanceof PGObserver)) return new PGObserver(opts);
2728
this.client = opts.client || new pg.Client(opts.conString);
2829
if (opts.names) opts.names = _.extend(_.clone(DEFAULTS.names), opts.names);
2930
opts = _.extend(_.clone(DEFAULTS), opts);
31+
if (opts.sendRecordId === true) opts.sendRecordId = 'id';
3032
this.options = opts;
3133
};
3234

‎install.sql

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ BEGIN
1212
"operation": "' || TG_OP || '",
1313
"timestamp": "' || CURRENT_TIMESTAMP || '"';
1414

15+
{{ var id = it.sendRecordId; }}
1516
IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
16-
data := data || ', "data": ' || row_to_json(NEW);
17+
data := data || ', "data": ' ||
18+
{{?id}} '{ "{{=id}}": ' || NEW.{{=id}}::text || ' }'; {{??}} row_to_json(NEW); {{?}}
1719
END IF;
1820

1921
IF TG_OP = 'DELETE' OR TG_OP = 'UPDATE' THEN
20-
data := data || ', "old_data": ' || row_to_json(OLD);
22+
data := data || ', "old_data": ' ||
23+
{{?id}} '{ "{{=id}}": ' || OLD.{{=id}}::text || ' }'; {{??}} row_to_json(OLD); {{?}}
2124
END IF;
2225

2326
data := data || '}';

‎readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ var dispatcher = require('pg-dispatch')({
2727
// },
2828
// operationEvents: true, // false to NOT send events on each operation
2929
// checkUpdates: true // only notify if update changes record
30+
// sendRecordId: false // send only record IDs, true - to send column 'id',
31+
// string to send column with a given name.
32+
// this column should be a number.
33+
// by default the whole record data is sent.
3034
});
3135

3236
dispatcher.install(function(err) {

‎tests/setup.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
DROP TABLE IF EXISTS users;
22
CREATE TABLE IF NOT EXISTS users(
3-
user_id serial primary key,
3+
id serial primary key,
44
name varchar(80),
55
created_at timestamp not null default now()
66
);

‎tests/tests.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var assert = require('assert')
22
var pg = require('pg')
3-
var PGEvents = require('../')
3+
var PGEvents = require('../index')
44
var connect = require('./connect')
55

66
before(function (done) {
@@ -74,7 +74,6 @@ describe('PG Events', function() {
7474
validCache(self.cache);
7575
assert.equal(self.cache.operation, 'UPDATE');
7676
assert.notDeepEqual(self.cache.old_data, {}, 'cache old_data is empty');
77-
self.cache = {};
7877

7978
done();
8079
}, 100);
@@ -83,6 +82,7 @@ describe('PG Events', function() {
8382

8483
it('should not send message when no changes', function(done) {
8584
var self = this;
85+
this.cache = {};
8686
this.client.query("UPDATE users SET name='cat' WHERE name='cat'", function (error) {
8787
if (error) return done(error);
8888

0 commit comments

Comments
 (0)
Please sign in to comment.