File tree 5 files changed +15
-6
lines changed
5 files changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -19,14 +19,16 @@ var DEFAULTS = {
19
19
removeNotifyTrigger : 'remove_notify_trigger_to_table'
20
20
} ,
21
21
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
23
24
} ;
24
25
25
26
function PGObserver ( opts ) {
26
27
if ( ! ( this instanceof PGObserver ) ) return new PGObserver ( opts ) ;
27
28
this . client = opts . client || new pg . Client ( opts . conString ) ;
28
29
if ( opts . names ) opts . names = _ . extend ( _ . clone ( DEFAULTS . names ) , opts . names ) ;
29
30
opts = _ . extend ( _ . clone ( DEFAULTS ) , opts ) ;
31
+ if ( opts . sendRecordId === true ) opts . sendRecordId = 'id' ;
30
32
this . options = opts ;
31
33
} ;
32
34
Original file line number Diff line number Diff line change @@ -12,12 +12,15 @@ BEGIN
12
12
"operation": "' || TG_OP || ' ",
13
13
"timestamp": "' || CURRENT_TIMESTAMP || ' "' ;
14
14
15
+ {{ var id = it .sendRecordId ; }}
15
16
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); {{?}}
17
19
END IF;
18
20
19
21
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); {{?}}
21
24
END IF;
22
25
23
26
data := data || ' }' ;
Original file line number Diff line number Diff line change @@ -27,6 +27,10 @@ var dispatcher = require('pg-dispatch')({
27
27
// },
28
28
// operationEvents: true, // false to NOT send events on each operation
29
29
// 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.
30
34
});
31
35
32
36
dispatcher .install (function (err ) {
Original file line number Diff line number Diff line change 1
1
DROP TABLE IF EXISTS users;
2
2
CREATE TABLE IF NOT EXISTS users(
3
- user_id serial primary key ,
3
+ id serial primary key ,
4
4
name varchar (80 ),
5
5
created_at timestamp not null default now()
6
6
);
Original file line number Diff line number Diff line change 1
1
var assert = require ( 'assert' )
2
2
var pg = require ( 'pg' )
3
- var PGEvents = require ( '../' )
3
+ var PGEvents = require ( '../index ' )
4
4
var connect = require ( './connect' )
5
5
6
6
before ( function ( done ) {
@@ -74,7 +74,6 @@ describe('PG Events', function() {
74
74
validCache ( self . cache ) ;
75
75
assert . equal ( self . cache . operation , 'UPDATE' ) ;
76
76
assert . notDeepEqual ( self . cache . old_data , { } , 'cache old_data is empty' ) ;
77
- self . cache = { } ;
78
77
79
78
done ( ) ;
80
79
} , 100 ) ;
@@ -83,6 +82,7 @@ describe('PG Events', function() {
83
82
84
83
it ( 'should not send message when no changes' , function ( done ) {
85
84
var self = this ;
85
+ this . cache = { } ;
86
86
this . client . query ( "UPDATE users SET name='cat' WHERE name='cat'" , function ( error ) {
87
87
if ( error ) return done ( error ) ;
88
88
You can’t perform that action at this time.
0 commit comments