Skip to content

Commit 557e5f8

Browse files
committed
Return result accumulator in callback
fixes issue: brianc/node-pg-cursor#22
1 parent a320416 commit 557e5f8

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ pg.connect(function(err, client, done) {
5959

6060
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)
6161

62-
#### cursor#read(int rowCount, function callback(Error err, Array rows)
62+
#### cursor#read(int rowCount, function callback(Error err, Array rows, Result result)
6363

6464
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.
6565

6666
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.
6767

68+
`result` is a special [https://github.com/brianc/node-postgres/wiki/Query#result-object](Result) object that can be used to accumulate rows.
6869

6970
#### cursor#close(function callback(Error err))
7071

index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ Cursor.prototype._sendRows = function() {
7070
//within the call to this callback
7171
this._cb = null
7272
if(cb) {
73-
cb(null, this._rows)
73+
this._result.rows = this._rows
74+
cb(null, this._rows, this._result)
7475
}
7576
this._rows = []
7677
}.bind(this))

test/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,15 @@ describe('cursor', function() {
116116
})
117117
})
118118
})
119+
120+
it('returns result along with rows', function(done) {
121+
var cursor = this.pgCursor(text)
122+
cursor.read(1, function(err, rows, result) {
123+
assert.ifError(err)
124+
assert.equal(rows.length, 1)
125+
assert.strictEqual(rows, result.rows)
126+
assert.deepEqual(result.fields.map(f => f.name), ['num'])
127+
done()
128+
})
129+
})
119130
})

0 commit comments

Comments
 (0)