From 4f6208521b19aa271cd1a1c4a127649e11a38f03 Mon Sep 17 00:00:00 2001 From: brianc Date: Tue, 28 Jun 2016 17:44:02 -0700 Subject: [PATCH 01/10] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1096dee9..c6c4e076 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg-cursor", - "version": "1.0.0", + "version": "1.0.1", "description": "", "main": "index.js", "directories": { From af84d5cd4bde99981ebef291fcf7da3d446722da Mon Sep 17 00:00:00 2001 From: Cris Vergara Date: Mon, 5 Dec 2016 17:18:25 -0500 Subject: [PATCH 02/10] Fix require for webpack compatibility --- pg.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pg.js b/pg.js index 96cd7f9a..c6be9ba6 100644 --- a/pg.js +++ b/pg.js @@ -1,13 +1,10 @@ -var path = require('path') -var pgPath; //support both pg & pg.js //this will eventually go away when i break native bindings //out into their own module try { - pgPath = path.dirname(require.resolve('pg')) + module.exports.Result = require('pg/lib/result.js') + module.exports.prepareValue = require('pg/lib/utils.js').prepareValue } catch(e) { - pgPath = path.dirname(require.resolve('pg.js')) + '/lib' + module.exports.Result = require('pg.js/lib/result.js') + module.exports.prepareValue = require('pg.js/lib/utils.js').prepareValue } - -module.exports.Result = require(path.join(pgPath, 'result.js')) -module.exports.prepareValue = require(path.join(pgPath, 'utils.js')).prepareValue From a3204168b728ef059d4d410786184abbf27a2452 Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Thu, 27 Apr 2017 11:42:19 -0500 Subject: [PATCH 03/10] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c6c4e076..3ce24326 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg-cursor", - "version": "1.0.1", + "version": "1.0.2", "description": "", "main": "index.js", "directories": { From 557e5f879d25cd82e7adc905c804f0841da8a03f Mon Sep 17 00:00:00 2001 From: Sam Beran Date: Thu, 27 Apr 2017 14:26:11 -0500 Subject: [PATCH 04/10] Return result accumulator in callback fixes issue: https://github.com/brianc/node-pg-cursor/issues/22 --- README.md | 3 ++- index.js | 3 ++- test/index.js | 11 +++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1cddfbfd..fe79bf74 100644 --- a/README.md +++ b/README.md @@ -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)) diff --git a/index.js b/index.js index 70a90f57..71c12522 100644 --- a/index.js +++ b/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)) diff --git a/test/index.js b/test/index.js index 8f04ccc2..af4f041c 100644 --- a/test/index.js +++ b/test/index.js @@ -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() + }) + }) }) From 4bf66e65dec960865ccd16a5e13b0c7c26766878 Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Thu, 27 Apr 2017 14:37:58 -0500 Subject: [PATCH 05/10] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3ce24326..9ff7cc9f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg-cursor", - "version": "1.0.2", + "version": "1.1.0", "description": "", "main": "index.js", "directories": { From 42af0144832219fce3b185342f5755c84ef2f0e2 Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Mon, 8 May 2017 16:49:24 -0500 Subject: [PATCH 06/10] Update travis.yml --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 061e3d15..3bd6f9c6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: node_js node_js: - - "0.10" - - "0.11" + - "6" env: - PGUSER=postgres From acae15de53a2dfb449cfe0dc4d1040f80dbd50da Mon Sep 17 00:00:00 2001 From: Sam Beran Date: Mon, 8 May 2017 16:16:17 -0500 Subject: [PATCH 07/10] Emit Query Events This change adds events to the `Cursor` object as per the [Query API](https://github.com/brianc/node-postgres/wiki/Query). --- index.js | 11 ++++++++++- test/index.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 71c12522..0c20e486 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,11 @@ var Result = require('./pg').Result var prepare = require('./pg').prepareValue +var EventEmitter = require('events').EventEmitter; +var util = require('util'); + +function Cursor (text, values) { + EventEmitter.call(this); -var Cursor = function(text, values) { this.text = text this.values = values ? values.map(prepare) : null this.connection = null @@ -12,6 +16,8 @@ var Cursor = function(text, values) { this._rows = null } +util.inherits(Cursor, EventEmitter) + Cursor.prototype.submit = function(connection) { this.connection = connection @@ -58,6 +64,7 @@ Cursor.prototype.handleRowDescription = function(msg) { Cursor.prototype.handleDataRow = function(msg) { var row = this._result.parseRow(msg.fields) + this.emit('row', row, this._result) this._rows.push(row) } @@ -87,6 +94,7 @@ Cursor.prototype.handlePortalSuspended = function() { Cursor.prototype.handleReadyForQuery = function() { this._sendRows() + this.emit('end', this._result) this.state = 'done' } @@ -107,6 +115,7 @@ Cursor.prototype.handleError = function(msg) { for(var i = 0; i < this._queue.length; i++) { this._queue.pop()[1](msg) } + this.emit('error', msg) //call sync to keep this connection from hanging this.connection.sync() } diff --git a/test/index.js b/test/index.js index af4f041c..cc97960e 100644 --- a/test/index.js +++ b/test/index.js @@ -127,4 +127,33 @@ describe('cursor', function() { done() }) }) + + it('emits row events', function(done) { + var cursor = this.pgCursor(text) + cursor.read(10) + cursor.on('row', (row, result) => result.addRow(row)) + cursor.on('end', (result) => { + assert.equal(result.rows.length, 6) + done() + }) + }) + + it('emits row events when cursor is closed manually', function(done) { + var cursor = this.pgCursor(text) + cursor.on('row', (row, result) => result.addRow(row)) + cursor.on('end', (result) => { + assert.equal(result.rows.length, 3) + done() + }) + + cursor.read(3, () => cursor.close()) + }) + + it('emits error events', function(done) { + var cursor = this.pgCursor('select asdfasdf') + cursor.on('error', function(err) { + assert(err) + done() + }) + }) }) From 2f480217cb599a8ebe2f3ad658eac35a07c8cc68 Mon Sep 17 00:00:00 2001 From: Sam Beran Date: Tue, 9 May 2017 09:16:45 -0500 Subject: [PATCH 08/10] fix: only dispatch error events if we have a listener --- index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 0c20e486..0827043e 100644 --- a/index.js +++ b/index.js @@ -115,7 +115,11 @@ Cursor.prototype.handleError = function(msg) { for(var i = 0; i < this._queue.length; i++) { this._queue.pop()[1](msg) } - this.emit('error', msg) + + if (this.eventNames().indexOf('error') >= 0) { + //only dispatch error events if we have a listener + this.emit('error', msg) + } //call sync to keep this connection from hanging this.connection.sync() } From 4427e31661df2019df004afded5d8e3cd8f467c0 Mon Sep 17 00:00:00 2001 From: Sam Beran Date: Tue, 9 May 2017 09:30:16 -0500 Subject: [PATCH 09/10] fix travis build env --- .travis.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.travis.yml b/.travis.yml index 3bd6f9c6..267b07b5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,13 @@ language: node_js +dist: trusty +sudo: false node_js: - "6" env: - PGUSER=postgres +services: + - postgresql +addons: + postgresql: "9.6" +before_script: + - psql -c 'create database travis;' -U postgres | true \ No newline at end of file From 6e462ffae62123d15c66481be4b2b122cf8473df Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Mon, 15 May 2017 23:16:55 -0500 Subject: [PATCH 10/10] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9ff7cc9f..f3a0d642 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg-cursor", - "version": "1.1.0", + "version": "1.2.0", "description": "", "main": "index.js", "directories": {