Skip to content

Commit 4d1bc67

Browse files
committed
SelectHandle now emits initialization errors
1 parent caddb16 commit 4d1bc67

File tree

6 files changed

+101
-57
lines changed

6 files changed

+101
-57
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ The `LivePG.prototype.select()` method returns an instance of the `SelectHandle`
8888

8989
The `SelectHandle` class inherits from `EventEmitter`, providing an `update` event on each result set change with two arguments: `diff` and `data`. `diff` contains a description of which rows have been `added`, `moved`, `removed`, and `copied`. `data` contains an array of the full result set.
9090

91+
An `error` event is emitted from the `SelectHandle` for initialization errors.
92+
9193
## Getting started with the examples
9294

9395
1. Run `npm install` to download dependent packages.

lib/LivePG.js

+51-37
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ var LivePG = (function (_EventEmitter) {
164164
var handle = new SelectHandle(this, queryHash);
165165

166166
// Perform initialization asynchronously
167-
this._initSelect(query, params, triggers, queryHash, handle)['catch'](this._error);
167+
this._initSelect(query, params, triggers, queryHash, handle)['catch'](function (error) {
168+
return handle.emit('error', error);
169+
});
168170

169171
return handle;
170172
}
@@ -272,7 +274,7 @@ var LivePG = (function (_EventEmitter) {
272274

273275
// Initial results from cache
274276
handle.emit('update', { removed: null, moved: null, copied: null, added: queryBuffer.data }, queryBuffer.data);
275-
context$2$0.next = 54;
277+
context$2$0.next = 61;
276278
break;
277279

278280
case 8:
@@ -290,109 +292,121 @@ var LivePG = (function (_EventEmitter) {
290292
case 11:
291293
pgHandle = context$2$0.sent;
292294
tablesUsed = undefined;
295+
context$2$0.prev = 13;
293296

294297
if (!(queryHash in this.tablesUsedCache)) {
295-
context$2$0.next = 17;
298+
context$2$0.next = 18;
296299
break;
297300
}
298301

299302
tablesUsed = this.tablesUsedCache[queryHash];
300-
context$2$0.next = 21;
303+
context$2$0.next = 22;
301304
break;
302305

303-
case 17:
304-
context$2$0.next = 19;
306+
case 18:
307+
context$2$0.next = 20;
305308
return common.getQueryDetails(pgHandle.client, query);
306309

307-
case 19:
310+
case 20:
308311
tablesUsed = context$2$0.sent;
309312

310313
this.tablesUsedCache[queryHash] = tablesUsed;
311314

312-
case 21:
315+
case 22:
313316
_iteratorNormalCompletion3 = true;
314317
_didIteratorError3 = false;
315318
_iteratorError3 = undefined;
316-
context$2$0.prev = 24;
319+
context$2$0.prev = 25;
317320
_iterator3 = _getIterator(tablesUsed);
318321

319-
case 26:
322+
case 27:
320323
if (_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done) {
321-
context$2$0.next = 38;
324+
context$2$0.next = 39;
322325
break;
323326
}
324327

325328
table = _step3.value;
326329

327330
if (table in this.allTablesUsed) {
328-
context$2$0.next = 34;
331+
context$2$0.next = 35;
329332
break;
330333
}
331334

332335
this.allTablesUsed[table] = [queryHash];
333-
context$2$0.next = 32;
336+
context$2$0.next = 33;
334337
return common.createTableTrigger(pgHandle.client, table, this.channel);
335338

336-
case 32:
337-
context$2$0.next = 35;
339+
case 33:
340+
context$2$0.next = 36;
338341
break;
339342

340-
case 34:
343+
case 35:
341344
if (this.allTablesUsed[table].indexOf(queryHash) === -1) {
342345
this.allTablesUsed[table].push(queryHash);
343346
}
344347

345-
case 35:
348+
case 36:
346349
_iteratorNormalCompletion3 = true;
347-
context$2$0.next = 26;
350+
context$2$0.next = 27;
348351
break;
349352

350-
case 38:
351-
context$2$0.next = 44;
353+
case 39:
354+
context$2$0.next = 45;
352355
break;
353356

354-
case 40:
355-
context$2$0.prev = 40;
356-
context$2$0.t1 = context$2$0['catch'](24);
357+
case 41:
358+
context$2$0.prev = 41;
359+
context$2$0.t1 = context$2$0['catch'](25);
357360
_didIteratorError3 = true;
358361
_iteratorError3 = context$2$0.t1;
359362

360-
case 44:
361-
context$2$0.prev = 44;
363+
case 45:
362364
context$2$0.prev = 45;
365+
context$2$0.prev = 46;
363366

364367
if (!_iteratorNormalCompletion3 && _iterator3['return']) {
365368
_iterator3['return']();
366369
}
367370

368-
case 47:
369-
context$2$0.prev = 47;
371+
case 48:
372+
context$2$0.prev = 48;
370373

371374
if (!_didIteratorError3) {
372-
context$2$0.next = 50;
375+
context$2$0.next = 51;
373376
break;
374377
}
375378

376379
throw _iteratorError3;
377380

378-
case 50:
379-
return context$2$0.finish(47);
380-
381381
case 51:
382-
return context$2$0.finish(44);
382+
return context$2$0.finish(48);
383383

384384
case 52:
385+
return context$2$0.finish(45);
385386

386-
pgHandle.done();
387+
case 53:
387388

388389
// Retrieve initial results
389390
this.waitingToUpdate.push(queryHash);
391+
context$2$0.next = 60;
392+
break;
390393

391-
case 54:
394+
case 56:
395+
context$2$0.prev = 56;
396+
context$2$0.t2 = context$2$0['catch'](13);
397+
398+
pgHandle.done();
399+
throw context$2$0.t2;
400+
401+
case 60:
402+
403+
pgHandle.done();
404+
405+
case 61:
392406
case 'end':
393407
return context$2$0.stop();
394408
}
395-
}, null, this, [[24, 40, 44, 52], [45,, 47, 51]]);
409+
}, null, this, [[13, 56], [25, 41, 45, 53], [46,, 48, 52]]);
396410
}
397411
}, {
398412
key: '_updateQuery',
@@ -437,9 +451,9 @@ var LivePG = (function (_EventEmitter) {
437451

438452
case 17:
439453
context$2$0.prev = 17;
440-
context$2$0.t2 = context$2$0['catch'](13);
454+
context$2$0.t3 = context$2$0['catch'](13);
441455
_didIteratorError4 = true;
442-
_iteratorError4 = context$2$0.t2;
456+
_iteratorError4 = context$2$0.t3;
443457

444458
case 21:
445459
context$2$0.prev = 21;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name" : "pg-live-select",
3-
"version" : "0.0.8",
3+
"version" : "0.0.9",
44
"description" : "Live updating PostgreSQL SELECT statements",
55
"main" : "lib/LivePG.js",
66
"license" : "MIT",

src/LivePG.es6

+24-17
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class LivePG extends EventEmitter {
112112

113113
// Perform initialization asynchronously
114114
this._initSelect(query, params, triggers, queryHash, handle)
115-
.catch(this._error)
115+
.catch(error => handle.emit('error', error))
116116

117117
return handle
118118
}
@@ -156,28 +156,35 @@ class LivePG extends EventEmitter {
156156

157157
let pgHandle = await common.getClient(this.connStr)
158158
let tablesUsed
159-
if(queryHash in this.tablesUsedCache) {
160-
tablesUsed = this.tablesUsedCache[queryHash]
161-
}
162-
else {
163-
tablesUsed = await common.getQueryDetails(pgHandle.client, query)
164-
this.tablesUsedCache[queryHash] = tablesUsed
165-
}
166159

167-
for(let table of tablesUsed) {
168-
if(!(table in this.allTablesUsed)) {
169-
this.allTablesUsed[table] = [ queryHash ]
170-
await common.createTableTrigger(pgHandle.client, table, this.channel)
160+
try {
161+
if(queryHash in this.tablesUsedCache) {
162+
tablesUsed = this.tablesUsedCache[queryHash]
171163
}
172-
else if(this.allTablesUsed[table].indexOf(queryHash) === -1) {
173-
this.allTablesUsed[table].push(queryHash)
164+
else {
165+
tablesUsed = await common.getQueryDetails(pgHandle.client, query)
166+
this.tablesUsedCache[queryHash] = tablesUsed
174167
}
168+
169+
for(let table of tablesUsed) {
170+
if(!(table in this.allTablesUsed)) {
171+
this.allTablesUsed[table] = [ queryHash ]
172+
await common.createTableTrigger(pgHandle.client, table, this.channel)
173+
}
174+
else if(this.allTablesUsed[table].indexOf(queryHash) === -1) {
175+
this.allTablesUsed[table].push(queryHash)
176+
}
177+
}
178+
179+
// Retrieve initial results
180+
this.waitingToUpdate.push(queryHash)
181+
}
182+
catch(error) {
183+
pgHandle.done()
184+
throw error
175185
}
176186

177187
pgHandle.done()
178-
179-
// Retrieve initial results
180-
this.waitingToUpdate.push(queryHash)
181188
}
182189
}
183190

test/fixtures/variousQueries.es6

+7
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,13 @@ exports.cases.stopped = {
546546
]
547547
}
548548

549+
exports.cases.relation_error = {
550+
query: `SELECT score FROM scores_invalid ORDER BY score DESC`,
551+
events: [
552+
{ error: /error: relation "scores_relation_error_invalid" does not exist/ }
553+
]
554+
}
555+
549556
let newName = randomString.alphaLower(BIG_PAYLOAD_LENGTH)
550557
exports.cases.bigPayload = {
551558
query: `SELECT big_name FROM big_payload ORDER BY id ASC`,

test/variousQueries.es6

+16-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ exports.variousQueries = function(test) {
2323
.catch(error => console.error(error))
2424
.then(result => {
2525
var updateLog = [] // Cache for any updates to this query
26+
var errorLog = []
2627
var nextLogPos = 0 // Length at last action performed
27-
var select = liveDb.select(query).on('update',
28-
(diff, data) => updateLog.push({ diff, data }))
28+
var select = liveDb.select(query)
29+
.on('update', (diff, data) => updateLog.push({ diff, data }))
30+
.on('error', error => errorLog.push(error))
2931

3032
// For each event, check values or perform action, then continue
3133
var processEvents = (callback, index) => {
@@ -72,6 +74,18 @@ exports.variousQueries = function(test) {
7274
processEvents(callback, index + 1)
7375
}
7476
break
77+
case 'error':
78+
if(errorLog.length === 0) {
79+
// No error yet, wait longer
80+
setTimeout(() => {
81+
processEvents(callback, index)
82+
}, 100)
83+
}
84+
else {
85+
test.ok(errorLog[0].toString().match(data) !== null)
86+
// Move to next event
87+
processEvents(callback, index + 1)
88+
}
7589
case 'unchanged':
7690
setTimeout(() => {
7791
test.equal(updateLog.length, nextLogPos,

0 commit comments

Comments
 (0)