Merge pull request #57 from brianc/bmc/lint-changes

Fix for pg@7.x
This commit is contained in:
Brian C 2019-10-25 18:17:44 -05:00 committed by GitHub
commit 6d47026083
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 1500 additions and 1990 deletions

View File

@ -1,9 +1,14 @@
{
"extends": "standard",
"env": {
"mocha": true
},
"extends": ["eslint:recommended"],
"plugins": ["prettier"],
"rules": {
"no-new-func": "off"
"prettier/prettier": "error",
"prefer-const": "error",
"no-var": "error"
},
"env": {
"es6": true,
"node": true,
"mocha": true
}
}

View File

@ -2,9 +2,9 @@ language: node_js
dist: trusty
sudo: false
node_js:
- "4.2"
- "6"
- "8"
- "10"
- "12"
env:
- PGUSER=postgres
services:

View File

@ -1,13 +1,14 @@
.PHONY: publish-patch test
.PHONY: test
test:
npm test
.PHONY: patch
patch: test
npm version patch -m "Bump version"
git push origin master --tags
npm publish
.PHONY: minor
minor: test
npm version minor -m "Bump version"
git push origin master --tags

View File

@ -4,9 +4,9 @@ const prepare = require('pg/lib/utils.js').prepareValue
const EventEmitter = require('events').EventEmitter
const util = require('util')
var nextUniqueID = 1 // concept borrowed from org.postgresql.core.v3.QueryExecutorImpl
let nextUniqueID = 1 // concept borrowed from org.postgresql.core.v3.QueryExecutorImpl
function Cursor (text, values, config) {
function Cursor(text, values, config) {
EventEmitter.call(this)
this._conf = config || {}
@ -15,7 +15,7 @@ function Cursor (text, values, config) {
this.connection = null
this._queue = []
this.state = 'initialized'
this._result = new Result(this._conf.rowMode)
this._result = new Result(this._conf.rowMode, this._conf.types)
this._cb = null
this._rows = null
this._portal = null
@ -23,25 +23,34 @@ function Cursor (text, values, config) {
util.inherits(Cursor, EventEmitter)
Cursor.prototype.submit = function (connection) {
Cursor.prototype.submit = function(connection) {
this.connection = connection
this._portal = 'C_' + (nextUniqueID++)
this._portal = 'C_' + nextUniqueID++
const con = connection
con.parse({
text: this.text
}, true)
con.parse(
{
text: this.text,
},
true
)
con.bind({
portal: this._portal,
values: this.values
}, true)
con.bind(
{
portal: this._portal,
values: this.values,
},
true
)
con.describe({
type: 'P',
name: this._portal // AWS Redshift requires a portal name
}, true)
con.describe(
{
type: 'P',
name: this._portal, // AWS Redshift requires a portal name
},
true
)
con.flush()
@ -60,25 +69,25 @@ Cursor.prototype.submit = function (connection) {
})
}
Cursor.prototype._shiftQueue = function () {
Cursor.prototype._shiftQueue = function() {
if (this._queue.length) {
this._getRows.apply(this, this._queue.shift())
}
}
Cursor.prototype.handleRowDescription = function (msg) {
Cursor.prototype.handleRowDescription = function(msg) {
this._result.addFields(msg.fields)
this.state = 'idle'
this._shiftQueue()
}
Cursor.prototype.handleDataRow = function (msg) {
Cursor.prototype.handleDataRow = function(msg) {
const row = this._result.parseRow(msg.fields)
this.emit('row', row, this._result)
this._rows.push(row)
}
Cursor.prototype._sendRows = function () {
Cursor.prototype._sendRows = function() {
this.state = 'idle'
setImmediate(() => {
const cb = this._cb
@ -94,26 +103,26 @@ Cursor.prototype._sendRows = function () {
})
}
Cursor.prototype.handleCommandComplete = function (msg) {
Cursor.prototype.handleCommandComplete = function(msg) {
this._result.addCommandComplete(msg)
this.connection.sync()
}
Cursor.prototype.handlePortalSuspended = function () {
Cursor.prototype.handlePortalSuspended = function() {
this._sendRows()
}
Cursor.prototype.handleReadyForQuery = function () {
Cursor.prototype.handleReadyForQuery = function() {
this._sendRows()
this.emit('end', this._result)
this.state = 'done'
}
Cursor.prototype.handleEmptyQuery = function () {
Cursor.prototype.handleEmptyQuery = function() {
this.connection.sync()
}
Cursor.prototype.handleError = function (msg) {
Cursor.prototype.handleError = function(msg) {
this.state = 'error'
this._error = msg
// satisfy any waiting callback
@ -121,7 +130,7 @@ Cursor.prototype.handleError = function (msg) {
this._cb(msg)
}
// dispatch error to all waiting callbacks
for (var i = 0; i < this._queue.length; i++) {
for (let i = 0; i < this._queue.length; i++) {
this._queue.pop()[1](msg)
}
@ -133,19 +142,19 @@ Cursor.prototype.handleError = function (msg) {
this.connection.sync()
}
Cursor.prototype._getRows = function (rows, cb) {
Cursor.prototype._getRows = function(rows, cb) {
this.state = 'busy'
this._cb = cb
this._rows = []
const msg = {
portal: this._portal,
rows: rows
rows: rows,
}
this.connection.execute(msg, true)
this.connection.flush()
}
Cursor.prototype.end = function (cb) {
Cursor.prototype.end = function(cb) {
if (this.state !== 'initialized') {
this.connection.sync()
}
@ -153,7 +162,7 @@ Cursor.prototype.end = function (cb) {
this.connection.end()
}
Cursor.prototype.close = function (cb) {
Cursor.prototype.close = function(cb) {
if (this.state === 'done') {
return setImmediate(cb)
}
@ -161,13 +170,13 @@ Cursor.prototype.close = function (cb) {
this.connection.sync()
this.state = 'done'
if (cb) {
this.connection.once('closeComplete', function () {
this.connection.once('closeComplete', function() {
cb()
})
}
}
Cursor.prototype.read = function (rows, cb) {
Cursor.prototype.read = function(rows, cb) {
if (this.state === 'idle') {
return this._getRows(rows, cb)
}

1807
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
{
"name": "pg-cursor",
"version": "2.0.0",
"description": "",
"description": "Query cursor extension for node-postgres",
"main": "index.js",
"directories": {
"test": "test"
@ -16,14 +16,17 @@
"author": "Brian M. Carlson",
"license": "MIT",
"devDependencies": {
"eslint": "^4.4.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-node": "^5.1.1",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1",
"mocha": "^3.5.0",
"pg": "6.x"
"eslint": "^6.5.1",
"eslint-config-prettier": "^6.4.0",
"eslint-plugin-prettier": "^3.1.1",
"mocha": "^6.2.2",
"pg": "7.x",
"prettier": "^1.18.2"
},
"dependencies": {}
"prettier": {
"semi": false,
"printWidth": 120,
"trailingComma": "es5",
"singleQuote": true
}
}

View File

@ -1,32 +1,32 @@
var assert = require('assert')
var Cursor = require('../')
var pg = require('pg')
const assert = require('assert')
const Cursor = require('../')
const pg = require('pg')
var text = 'SELECT generate_series as num FROM generate_series(0, 50)'
describe('close', function () {
beforeEach(function (done) {
var client = this.client = new pg.Client()
const text = 'SELECT generate_series as num FROM generate_series(0, 50)'
describe('close', function() {
beforeEach(function(done) {
const client = (this.client = new pg.Client())
client.connect(done)
client.on('drain', client.end.bind(client))
})
it('closes cursor early', function (done) {
var cursor = new Cursor(text)
it('closes cursor early', function(done) {
const cursor = new Cursor(text)
this.client.query(cursor)
this.client.query('SELECT NOW()', done)
cursor.read(25, function (err, res) {
cursor.read(25, function(err) {
assert.ifError(err)
cursor.close()
})
})
it('works with callback style', function (done) {
var cursor = new Cursor(text)
var client = this.client
it('works with callback style', function(done) {
const cursor = new Cursor(text)
const client = this.client
client.query(cursor)
cursor.read(25, function (err, res) {
cursor.read(25, function(err) {
assert.ifError(err)
cursor.close(function (err) {
cursor.close(function(err) {
assert.ifError(err)
client.query('SELECT NOW()', done)
})

View File

@ -1,18 +1,18 @@
'use strict'
var assert = require('assert')
var Cursor = require('../')
var pg = require('pg')
const assert = require('assert')
const Cursor = require('../')
const pg = require('pg')
var text = 'SELECT generate_series as num FROM generate_series(0, 4)'
const text = 'SELECT generate_series as num FROM generate_series(0, 4)'
describe('error handling', function () {
it('can continue after error', function (done) {
var client = new pg.Client()
describe('error handling', function() {
it('can continue after error', function(done) {
const client = new pg.Client()
client.connect()
var cursor = client.query(new Cursor('asdfdffsdf'))
cursor.read(1, function (err) {
const cursor = client.query(new Cursor('asdfdffsdf'))
cursor.read(1, function(err) {
assert(err)
client.query('SELECT NOW()', function (err, res) {
client.query('SELECT NOW()', function(err) {
assert.ifError(err)
client.end()
done()
@ -22,16 +22,16 @@ describe('error handling', function () {
})
describe('read callback does not fire sync', () => {
it('does not fire error callback sync', (done) => {
var client = new pg.Client()
it('does not fire error callback sync', done => {
const client = new pg.Client()
client.connect()
var cursor = client.query(new Cursor('asdfdffsdf'))
const cursor = client.query(new Cursor('asdfdffsdf'))
let after = false
cursor.read(1, function (err) {
cursor.read(1, function(err) {
assert(err, 'error should be returned')
assert.equal(after, true, 'should not call read sync')
after = false
cursor.read(1, function (err) {
cursor.read(1, function(err) {
assert(err, 'error should be returned')
assert.equal(after, true, 'should not call read sync')
client.end()
@ -42,18 +42,18 @@ describe('read callback does not fire sync', () => {
after = true
})
it('does not fire result sync after finished', (done) => {
var client = new pg.Client()
it('does not fire result sync after finished', done => {
const client = new pg.Client()
client.connect()
var cursor = client.query(new Cursor('SELECT NOW()'))
const cursor = client.query(new Cursor('SELECT NOW()'))
let after = false
cursor.read(1, function (err) {
cursor.read(1, function(err) {
assert(!err)
assert.equal(after, true, 'should not call read sync')
cursor.read(1, function (err) {
cursor.read(1, function(err) {
assert(!err)
after = false
cursor.read(1, function (err) {
cursor.read(1, function(err) {
assert(!err)
assert.equal(after, true, 'should not call read sync')
client.end()
@ -66,16 +66,16 @@ describe('read callback does not fire sync', () => {
})
})
describe('proper cleanup', function () {
it('can issue multiple cursors on one client', function (done) {
var client = new pg.Client()
describe('proper cleanup', function() {
it('can issue multiple cursors on one client', function(done) {
const client = new pg.Client()
client.connect()
var cursor1 = client.query(new Cursor(text))
cursor1.read(8, function (err, rows) {
const cursor1 = client.query(new Cursor(text))
cursor1.read(8, function(err, rows) {
assert.ifError(err)
assert.equal(rows.length, 5)
var cursor2 = client.query(new Cursor(text))
cursor2.read(8, function (err, rows) {
const cursor2 = client.query(new Cursor(text))
cursor2.read(8, function(err, rows) {
assert.ifError(err)
assert.equal(rows.length, 5)
client.end()

View File

@ -1,62 +1,61 @@
var assert = require('assert')
var Cursor = require('../')
var pg = require('pg')
const assert = require('assert')
const Cursor = require('../')
const pg = require('pg')
var text = 'SELECT generate_series as num FROM generate_series(0, 5)'
const text = 'SELECT generate_series as num FROM generate_series(0, 5)'
describe('cursor', function () {
beforeEach(function (done) {
var client = this.client = new pg.Client()
describe('cursor', function() {
beforeEach(function(done) {
const client = (this.client = new pg.Client())
client.connect(done)
this.pgCursor = function (text, values) {
client.on('drain', client.end.bind(client))
this.pgCursor = function(text, values) {
return client.query(new Cursor(text, values || []))
}
})
afterEach(function () {
afterEach(function() {
this.client.end()
})
it('fetch 6 when asking for 10', function (done) {
var cursor = this.pgCursor(text)
cursor.read(10, function (err, res) {
it('fetch 6 when asking for 10', function(done) {
const cursor = this.pgCursor(text)
cursor.read(10, function(err, res) {
assert.ifError(err)
assert.equal(res.length, 6)
done()
})
})
it('end before reading to end', function (done) {
var cursor = this.pgCursor(text)
cursor.read(3, function (err, res) {
it('end before reading to end', function(done) {
const cursor = this.pgCursor(text)
cursor.read(3, function(err, res) {
assert.ifError(err)
assert.equal(res.length, 3)
cursor.end(done)
done()
})
})
it('callback with error', function (done) {
var cursor = this.pgCursor('select asdfasdf')
cursor.read(1, function (err) {
it('callback with error', function(done) {
const cursor = this.pgCursor('select asdfasdf')
cursor.read(1, function(err) {
assert(err)
done()
})
})
it('read a partial chunk of data', function (done) {
var cursor = this.pgCursor(text)
cursor.read(2, function (err, res) {
it('read a partial chunk of data', function(done) {
const cursor = this.pgCursor(text)
cursor.read(2, function(err, res) {
assert.ifError(err)
assert.equal(res.length, 2)
cursor.read(3, function (err, res) {
cursor.read(3, function(err, res) {
assert(!err)
assert.equal(res.length, 3)
cursor.read(1, function (err, res) {
cursor.read(1, function(err, res) {
assert(!err)
assert.equal(res.length, 1)
cursor.read(1, function (err, res) {
cursor.read(1, function(err, res) {
assert(!err)
assert.ifError(err)
assert.strictEqual(res.length, 0)
@ -67,14 +66,14 @@ describe('cursor', function () {
})
})
it('read return length 0 past the end', function (done) {
var cursor = this.pgCursor(text)
cursor.read(2, function (err, res) {
it('read return length 0 past the end', function(done) {
const cursor = this.pgCursor(text)
cursor.read(2, function(err) {
assert(!err)
cursor.read(100, function (err, res) {
cursor.read(100, function(err, res) {
assert(!err)
assert.equal(res.length, 4)
cursor.read(100, function (err, res) {
cursor.read(100, function(err, res) {
assert(!err)
assert.equal(res.length, 0)
done()
@ -83,14 +82,14 @@ describe('cursor', function () {
})
})
it('read huge result', function (done) {
it('read huge result', function(done) {
this.timeout(10000)
var text = 'SELECT generate_series as num FROM generate_series(0, 100000)'
var values = []
var cursor = this.pgCursor(text, values)
var count = 0
var read = function () {
cursor.read(100, function (err, rows) {
const text = 'SELECT generate_series as num FROM generate_series(0, 100000)'
const values = []
const cursor = this.pgCursor(text, values)
let count = 0
const read = function() {
cursor.read(100, function(err, rows) {
if (err) return done(err)
if (!rows.length) {
assert.equal(count, 100001)
@ -106,14 +105,14 @@ describe('cursor', function () {
read()
})
it('normalizes parameter values', function (done) {
var text = 'SELECT $1::json me'
var values = [{ name: 'brian' }]
var cursor = this.pgCursor(text, values)
cursor.read(1, function (err, rows) {
it('normalizes parameter values', function(done) {
const text = 'SELECT $1::json me'
const values = [{ name: 'brian' }]
const cursor = this.pgCursor(text, values)
cursor.read(1, function(err, rows) {
if (err) return done(err)
assert.equal(rows[0].me.name, 'brian')
cursor.read(1, function (err, rows) {
cursor.read(1, function(err, rows) {
assert(!err)
assert.equal(rows.length, 0)
done()
@ -121,9 +120,9 @@ describe('cursor', function () {
})
})
it('returns result along with rows', function (done) {
var cursor = this.pgCursor(text)
cursor.read(1, function (err, rows, result) {
it('returns result along with rows', function(done) {
const cursor = this.pgCursor(text)
cursor.read(1, function(err, rows, result) {
assert.ifError(err)
assert.equal(rows.length, 1)
assert.strictEqual(rows, result.rows)
@ -132,20 +131,20 @@ describe('cursor', function () {
})
})
it('emits row events', function (done) {
var cursor = this.pgCursor(text)
it('emits row events', function(done) {
const cursor = this.pgCursor(text)
cursor.read(10)
cursor.on('row', (row, result) => result.addRow(row))
cursor.on('end', (result) => {
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)
it('emits row events when cursor is closed manually', function(done) {
const cursor = this.pgCursor(text)
cursor.on('row', (row, result) => result.addRow(row))
cursor.on('end', (result) => {
cursor.on('end', result => {
assert.equal(result.rows.length, 3)
done()
})
@ -153,25 +152,27 @@ describe('cursor', function () {
cursor.read(3, () => cursor.close())
})
it('emits error events', function (done) {
var cursor = this.pgCursor('select asdfasdf')
cursor.on('error', function (err) {
it('emits error events', function(done) {
const cursor = this.pgCursor('select asdfasdf')
cursor.on('error', function(err) {
assert(err)
done()
})
})
it('returns rowCount on insert', function (done) {
var pgCursor = this.pgCursor
this.client.query('CREATE TEMPORARY TABLE pg_cursor_test (foo VARCHAR(1), bar VARCHAR(1))')
.then(function () {
var cursor = pgCursor('insert into pg_cursor_test values($1, $2)', ['a', 'b'])
cursor.read(1, function (err, rows, result) {
it('returns rowCount on insert', function(done) {
const pgCursor = this.pgCursor
this.client
.query('CREATE TEMPORARY TABLE pg_cursor_test (foo VARCHAR(1), bar VARCHAR(1))')
.then(function() {
const cursor = pgCursor('insert into pg_cursor_test values($1, $2)', ['a', 'b'])
cursor.read(1, function(err, rows, result) {
assert.ifError(err)
assert.equal(rows.length, 0)
assert.equal(result.rowCount, 1)
done()
})
}).catch(done)
})
.catch(done)
})
})

View File

@ -1,31 +1,31 @@
var assert = require('assert')
var pg = require('pg')
var Cursor = require('../')
const assert = require('assert')
const pg = require('pg')
const Cursor = require('../')
describe('queries with no data', function () {
beforeEach(function (done) {
var client = this.client = new pg.Client()
describe('queries with no data', function() {
beforeEach(function(done) {
const client = (this.client = new pg.Client())
client.connect(done)
})
afterEach(function () {
afterEach(function() {
this.client.end()
})
it('handles queries that return no data', function (done) {
var cursor = new Cursor('CREATE TEMPORARY TABLE whatwhat (thing int)')
it('handles queries that return no data', function(done) {
const cursor = new Cursor('CREATE TEMPORARY TABLE whatwhat (thing int)')
this.client.query(cursor)
cursor.read(100, function (err, rows) {
cursor.read(100, function(err, rows) {
assert.ifError(err)
assert.equal(rows.length, 0)
done()
})
})
it('handles empty query', function (done) {
var cursor = new Cursor('-- this is a comment')
it('handles empty query', function(done) {
let cursor = new Cursor('-- this is a comment')
cursor = this.client.query(cursor)
cursor.read(100, function (err, rows) {
cursor.read(100, function(err, rows) {
assert.ifError(err)
assert.equal(rows.length, 0)
done()

View File

@ -5,7 +5,7 @@ const pg = require('pg')
const text = 'SELECT generate_series as num FROM generate_series(0, 50)'
function poolQueryPromise (pool, readRowCount) {
function poolQueryPromise(pool, readRowCount) {
return new Promise((resolve, reject) => {
pool.connect((err, client, done) => {
if (err) {
@ -13,7 +13,7 @@ function poolQueryPromise (pool, readRowCount) {
return reject(err)
}
const cursor = client.query(new Cursor(text))
cursor.read(readRowCount, (err, res) => {
cursor.read(readRowCount, err => {
if (err) {
done(err)
return reject(err)
@ -31,16 +31,16 @@ function poolQueryPromise (pool, readRowCount) {
})
}
describe('pool', function () {
beforeEach(function () {
this.pool = new pg.Pool({max: 1})
describe('pool', function() {
beforeEach(function() {
this.pool = new pg.Pool({ max: 1 })
})
afterEach(function () {
afterEach(function() {
this.pool.end()
})
it('closes cursor early, single pool query', function (done) {
it('closes cursor early, single pool query', function(done) {
poolQueryPromise(this.pool, 25)
.then(() => done())
.catch(err => {
@ -49,7 +49,7 @@ describe('pool', function () {
})
})
it('closes cursor early, saturated pool', function (done) {
it('closes cursor early, saturated pool', function(done) {
const promises = []
for (let i = 0; i < 10; i++) {
promises.push(poolQueryPromise(this.pool, 25))
@ -62,7 +62,7 @@ describe('pool', function () {
})
})
it('closes exhausted cursor, single pool query', function (done) {
it('closes exhausted cursor, single pool query', function(done) {
poolQueryPromise(this.pool, 100)
.then(() => done())
.catch(err => {
@ -71,7 +71,7 @@ describe('pool', function () {
})
})
it('closes exhausted cursor, saturated pool', function (done) {
it('closes exhausted cursor, saturated pool', function(done) {
const promises = []
for (let i = 0; i < 10; i++) {
promises.push(poolQueryPromise(this.pool, 100))

View File

@ -4,7 +4,7 @@ const Cursor = require('../')
const pg = require('pg')
describe('query config passed to result', () => {
it('passes rowMode to result', (done) => {
it('passes rowMode to result', done => {
const client = new pg.Client()
client.connect()
const text = 'SELECT generate_series as num FROM generate_series(0, 5)'
@ -17,12 +17,12 @@ describe('query config passed to result', () => {
})
})
it('passes types to result', (done) => {
it('passes types to result', done => {
const client = new pg.Client()
client.connect()
const text = 'SELECT generate_series as num FROM generate_series(0, 2)'
const types = {
getTypeParser: () => () => 'foo'
getTypeParser: () => () => 'foo',
}
const cursor = client.query(new Cursor(text, null, { types }))
cursor.read(10, (err, rows) => {

1298
yarn.lock Normal file

File diff suppressed because it is too large Load Diff