-
Notifications
You must be signed in to change notification settings - Fork 3
/
mpg.js
151 lines (117 loc) · 3.79 KB
/
mpg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
var pgPromise = require('pg-promise');
var PgTableObserver = require('pg-table-observer').default;
var PgQueryObserver = require('pg-query-observer').default;
// Initialization
var pgp;
var db;
var query_observer;
var table_observer;
function init(connection, channel) {
if(!connection) {
connection = process.env.PG_URL ? process.env.PG_URL : 'postgres://localhost/postgres';
}
if(!channel) {
channel = process.env.PG_CHANNEL ? process.env.PG_CHANNEL : 'default_channel';
}
// pg-promise connection
pgp = pgPromise({});
try {
db = Promise.await(pgp(connection));
}
catch(err) {
console.error('meteor-pg: failed to connect to', connection);
throw err;
}
// PgQueryObserver
query_observer = new PgQueryObserver(db, channel);
table_observer = query_observer.table_observer;
// Automatic cleanup
function cleanupAndExit() {
query_observer.cleanup().then(() => {
pgp.end();
process.exit();
})
}
process.on('SIGTERM', cleanupAndExit);
process.on('SIGINT', cleanupAndExit);
}
init(); // For now just init automatically
// select function
function live_select(sub, collection, query, params, triggers) {
if(!query_observer) throw new Error('Query observer not initialized yet');
try {
let handle = Promise.await(query_observer.notify(query, params, triggers, diff => {
// console.log(diff);
if(diff.removed) {
diff.removed.forEach(_id => {
sub.removed(collection, _id);
});
}
if(diff.changed) {
diff.changed.forEach(changed => {
let _id = changed._id;
sub.changed(collection, _id, changed);
});
}
if(diff.added) {
diff.added.forEach(added => {
let _id = added._id;
sub.added(collection, _id, added);
});
}
}));
// Add initial rows
let rows = handle.getRows();
rows.forEach(added => {
let _id = added._id;
sub.added(collection, _id, added);
});
// onStop handler
sub.onStop(() => {
// console.log("Stopped");
handle.stop();
});
}
catch(err) {
// console.error(err);
sub.error(err);
}
}
function select(collection, query, params, triggers) {
// Usage: (inside Publish function)
// return mpg.select(collection, query, params, triggers)
return {
_publishCursor: function(sub) {
live_select(sub, collection, query, params, triggers);
},
observeChanges: function(callbacks) {
console.log("Not implemented yet");
// console.log("observeChanges called");
// console.log(callbacks);
},
};
}
// mpg object
var mpg = {
pgp, db, query_observer, table_observer, select,
// await all query functions
connect() { return Promise.await(db.connect.apply(db, arguments)) },
query() { return Promise.await(db.query.apply(db, arguments)) },
none() { return Promise.await(db.none.apply(db, arguments)) },
one() { return Promise.await(db.one.apply(db, arguments)) },
many() { return Promise.await(db.many.apply(db, arguments)) },
oneOrNone() { return Promise.await(db.oneOrNone.apply(db, arguments)) },
manyOrNone() { return Promise.await(db.manyOrNone.apply(db, arguments)) },
any() { return Promise.await(db.any.apply(db, arguments)) },
result() { return Promise.await(db.result.apply(db, arguments)) },
stream() { return Promise.await(db.stream.apply(db, arguments)) },
func() { return Promise.await(db.func.apply(db, arguments)) },
proc() { return Promise.await(db.proc.apply(db, arguments)) },
map() { return Promise.await(db.map.apply(db, arguments)) },
each() { return Promise.await(db.each.apply(db, arguments)) },
task() { return Promise.await(db.task.apply(db, arguments)) },
tx() { return Promise.await(db.tx.apply(db, arguments)) },
};
// Export
module.exports = mpg;