Merge pull request #23 from sberan/cursor-result

Return result accumulator in callback
This commit is contained in:
Brian C 2017-04-27 14:37:49 -05:00 committed by GitHub
commit 3cad54e061
3 changed files with 15 additions and 2 deletions

View File

@ -59,12 +59,13 @@ pg.connect(function(err, client, done) {
Creates an instance of a query cursor. Pass this instance to node-postgres [`client#query`](https://github.com/brianc/node-postgres/wiki/Client#wiki-method-query-parameterized)
#### cursor#read(int rowCount, function callback(Error err, Array rows)
#### cursor#read(int rowCount, function callback(Error err, Array rows, Result result)
Read `rowCount` rows from the cursor instance. The `callback` will be called when the rows are available, loaded into memory, parsed, and converted to JavaScript types.
If the cursor has read to the end of the result sets all subsequent calls to `cursor#read` will return a 0 length array of rows. I'm open to other ways to signal the end of a cursor, but this has worked out well for me so far.
`result` is a special [https://github.com/brianc/node-postgres/wiki/Query#result-object](Result) object that can be used to accumulate rows.
#### cursor#close(function callback(Error err))

View File

@ -70,7 +70,8 @@ Cursor.prototype._sendRows = function() {
//within the call to this callback
this._cb = null
if(cb) {
cb(null, this._rows)
this._result.rows = this._rows
cb(null, this._rows, this._result)
}
this._rows = []
}.bind(this))

View File

@ -116,4 +116,15 @@ describe('cursor', function() {
})
})
})
it('returns result along with rows', function(done) {
var cursor = this.pgCursor(text)
cursor.read(1, function(err, rows, result) {
assert.ifError(err)
assert.equal(rows.length, 1)
assert.strictEqual(rows, result.rows)
assert.deepEqual(result.fields.map(f => f.name), ['num'])
done()
})
})
})