pg-query-stream
Advanced tools
Comparing version 0.6.0 to 0.7.0
28
index.js
@@ -6,3 +6,5 @@ var util = require('util') | ||
var QueryStream = module.exports = function(text, values, options) { | ||
var self = this; | ||
var self = this | ||
this._reading = false | ||
this._closing = false | ||
options = options || { } | ||
@@ -17,8 +19,11 @@ Cursor.call(this, text, values) | ||
process.nextTick(function() { | ||
self.emit('close') | ||
}); | ||
}) | ||
self.emit('close') | ||
}) | ||
}) | ||
} | ||
util.inherits(QueryStream, Readable) | ||
//copy cursor prototype to QueryStream | ||
//so we can handle all the events emitted by the connection | ||
for(var key in Cursor.prototype) { | ||
@@ -32,6 +37,15 @@ if(key == 'read') { | ||
QueryStream.prototype.close = function() { | ||
this._closing = true | ||
var self = this | ||
Cursor.prototype.close.call(this, function(err) { | ||
if(err) return self.emit('error', err) | ||
process.nextTick(function() { | ||
self.push(null) | ||
}) | ||
}) | ||
} | ||
QueryStream.prototype._read = function(n) { | ||
if(this._reading) return false; | ||
if(this._reading || this._closing) return false | ||
this._reading = true | ||
@@ -47,3 +61,3 @@ var self = this | ||
}) | ||
return; | ||
return | ||
} | ||
@@ -50,0 +64,0 @@ self._reading = false |
{ | ||
"name": "pg-query-stream", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "Postgres query result returned as readable stream", | ||
@@ -34,5 +34,5 @@ "main": "index.js", | ||
"dependencies": { | ||
"pg-cursor": "0.2.0", | ||
"pg-cursor": "1.0.0", | ||
"readable-stream": "^1.0.27-1" | ||
} | ||
} |
@@ -7,4 +7,5 @@ var assert = require('assert') | ||
var QueryStream = require('../') | ||
var helper = require('./helper') | ||
require('./helper')('close', function(client) { | ||
helper('close', function(client) { | ||
it('emits close', function(done) { | ||
@@ -17,1 +18,20 @@ var stream = new QueryStream('SELECT * FROM generate_series(0, $1) num', [3], {batchSize: 2, highWaterMark: 2}) | ||
}) | ||
helper('early close', function(client) { | ||
it('can be closed early', function(done) { | ||
var stream = new QueryStream('SELECT * FROM generate_series(0, $1) num', [20000], {batchSize: 2, highWaterMark: 2}) | ||
var query = client.query(stream) | ||
var readCount = 0 | ||
query.on('readable', function() { | ||
readCount++ | ||
query.read() | ||
}) | ||
query.once('readable', function() { | ||
query.close() | ||
}) | ||
query.on('close', function() { | ||
assert(readCount < 10, 'should not have read more than 10 rows') | ||
done() | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12832
19
305
+ Addedpg-cursor@1.0.0(transitive)
- Removedpg-cursor@0.2.0(transitive)
Updatedpg-cursor@1.0.0