mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
Return result accumulator in callback
fixes issue: https://github.com/brianc/node-pg-cursor/issues/22
This commit is contained in:
parent
a3204168b7
commit
557e5f879d
@ -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))
|
||||
|
||||
|
||||
3
index.js
3
index.js
@ -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))
|
||||
|
||||
@ -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()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user