forked from joaosp/pg-then
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
301 lines (250 loc) · 7.32 KB
/
index.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
'use strict'
const Rx = global.Rx || require('rx');
const Rxo = Rx.Observable;
const QueryStream = require('pg-query-stream')
//const pg = require('pg');
const slice = [].slice
const deasync = require('deasync')
const _ = require('lodash')
const moment = require('moment')
let QI = 0; // debug query index
module.exports = {
Client,
Pool
}
function _stream(_client, text, value, options) {
const stream = new Rx.Subject();
if(text.toString) text = text.toString();
if(this.opts.debug) console.log('stream:', text, value||'', options||'')
if(!this.opts.noMoment) {
const tmp = parseMoment(text, value);
text = tmp[0];
value = tmp[1];
}
const query = new QueryStream(text, value, options)
const source = _client.query(query)
source.on('end', cleanup)
source.on('error', onError)
source.on('close', cleanup)
source.on('data', onData)
function onData(x) {
stream.onNext(x);
}
function onError(err) {
stream.onError(err);
cleanup();
}
function cleanup(x) {
source.removeListener('data', onData)
source.removeListener('end', cleanup)
source.removeListener('error', onError)
source.removeListener('close', cleanup)
stream.onCompleted()
}
return stream.asObservable();
}
function _transaction(queryList, client) {
// Same as Client's version
queryList = ['BEGIN'].concat(queryList);
queryList.push('COMMIT');
//console.log('queryList', queryList)
//if(this.opts.debug) console.log("starting transaction");
let lastResponse = null;
return Rxo.fromArray(queryList).reduce((acc, x, i) => {
if(this.opts.debug) {
if(typeof x === 'function')
console.log('transaction ' + i + ': function');
else if(!!x.subscribe) console.log('transaction ' + i + ': Rx');
else console.log('transaction ' + i + ':', JSON.stringify(x));
}
return acc.first().concatMap( prev => {
if(i < queryList.length && prev) {
lastResponse = prev;
}
if(!x) return Rxo.just(null);
if(typeof x === 'string') return this._query(x);
else if(x.subscribe) return x;
else if(typeof x === 'function') {
const r = x(prev);
if(typeof r === 'string') return this._query(r);
else if(r && r.subscribe) return r;
else return this._query(r); // klex
}
else return this._query(x); // klex
})
}, Rxo.just(null))
.merge(1)
.catch( x => {
lastResponse = null;
var err = JSON.stringify(x);
if(this.opts.debug)
console.log('Transaction error:', x.message, err);
return this._query('ROLLBACK').flatMap(_=> Rxo.throw( {message:x.message, detail:err} ));
}).first().map(x => lastResponse); // use last response before commit
}
function _query(client, args) {
if(!client) return Rxo.throw(new Error("Client not reached"));
let query = args.length > 0 ? args[0] : null;
if(!query) throw new Error('No query given');
if(query && typeof query !== 'string') {
if(query instanceof QueryStream) null; // ignore QS
else if(typeof query.toString === 'function') {
query = args[0] = query.toString(); // klex support
}
else return Rxo.throw(new Error('Invalid object for query:', typeof query, query));
}
if(!this.opts.noMoment) {
const tmp = parseMoment(args[0], args[1]);
args[0] = query = tmp[0];
args[1] = tmp[1];
}
QI++;
client.rxquery =
client.rxquery || Rxo.fromNodeCallback(client.query, client);
const ret = Rxo.defer(x => {
if(this.opts.debug) console.log(QI + ' query:', args);
var invokedrx = client.rxquery.apply(client, args);
if(this.opts.debug) invokedrx = invokedrx.do(
x => console.log(QI + ' done:', x.rows),
x => console.log(QI + ' error:', x)
)
return invokedrx;
})
return ret;
}
function replaceNow(m) {
const unix = m ? m.utc().unix() : moment().utc().unix();
const x = "to_timestamp(" + unix + ")"
return x;
}
function parseMoment(q, p) {
// query helper
q = q.replace(/\$NOW/g, function() { return replaceNow() });
if(!p || !Array.isArray(p)) return [q, p];
// Find moment params objects and replace it with timestamp inputs
let reduceParams = 0;
p = _.reduce(p, (acc, y, i) => {
const index = i + 1;
const rindex = index - reduceParams;
// shift params back
if(reduceParams>0) q = q.replace('$'+(index), '$'+(rindex));
if(y && y instanceof moment) {
q = q.replace('$'+(rindex), replaceNow(y));
reduceParams++;
return acc; // replace param with null
}
acc.push(y);
return acc;
}, [])
return [q, p];
}
/**
* Pool
*/
let i = 0;
function Pool(config, opts) {
if (!(this instanceof Pool)) {
return new Pool(config, opts)
}
this.config = config;
this.opts = opts || {};
const r = {
connect: this._connect.bind(this)
, query: this._query.bind(this)
, stream: this._stream.bind(this)
, transaction: this.__transaction.bind(this),
}
return r;
}
Pool.prototype.__transaction = function(x) {
if(x) throw new Error('Invalid use of Pools transacion: method returns function and query method');
this.tclient = this.tclient || Client(this.config, this.opts);
var execute = (x) => this.tclient.transaction(x);
execute.query = this.tclient.query;
return execute;
}
Pool.prototype._connect = function() {
if(!this.pg) {
this.pg = require('pg');
if(!!this.opts.native) this.pg = this.pg.native;
}
return Rxo.create((obs) => {
let _done = x=>x;
this.pg.connect(this.config, (error, client, done) => {
_done = done;
//console.log(error, client, done)
if (error) {
done(error)
obs.onError(error) // Todo: Error obj needed?
obs.onCompleted();
return
}
obs.onNext({ client, done })
obs.onCompleted();
})
return dispose=>_done();
});
}
Pool.prototype._query = function() {
const args = slice.call(arguments);
let _pool;
return this._connect().flatMap(pool => {
_pool = pool;
return _query.call(this, pool.client, args);
}).do(
() => null,
() => {
_pool.done()
},
() => {
_pool.done()
} )
}
Pool.prototype._stream = function(text, value, options) {
let _pool;
return this._connect().flatMap(pool => {
_pool = pool;
return _stream.call(this, pool.client, text, value, options);
}).do(
() => null,
() => {
_pool.done()
},
() => {
_pool.done()
} )
}
/**
* Client
*/
function Client(config, opts) {
if (!(this instanceof Client)) {
return new Client(config, opts)
}
let pg = require('pg');
if(!!opts.native) pg = pg.native;
const _client = this._client = new pg.Client(config)
let connected = false
this._client.connect((error) => {
if (error) {
throw error
}
connected = true
})
deasync.loopWhile(function(){ return !connected });
this.opts = opts || {};
return { query: this._query.bind(this),
stream: this._stream.bind(this),
end: this._end.bind(this),
transaction: _transaction.bind(this) }
}
Client.prototype._query = function() {
const args = slice.call(arguments)
return _query.call(this, this._client, args);
}
Client.prototype._stream = function(text, value, options) {
return _stream.call(this, this._client, text, value, options);
}
Client.prototype._end = function() {
this._client.end()
}