Skip to content

Commit

Permalink
Update lint rules for pg-cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
brianc committed Dec 18, 2019
1 parent 37d1574 commit 423baa6
Show file tree
Hide file tree
Showing 14 changed files with 348 additions and 125 deletions.
20 changes: 11 additions & 9 deletions .eslintrc
@@ -1,19 +1,21 @@
{
"plugins": [
"node"
],
"extends": [
"standard",
"eslint:recommended",
"plugin:node/recommended"
],
"plugins": ["node"],
"extends": ["standard", "eslint:recommended", "plugin:node/recommended"],
"parserOptions": {
"ecmaVersion": 2017
},
"env": {
"node": true,
"es6": true
"es6": true,
"mocha": true
},
"rules": {
"space-before-function-paren": "off",
"node/no-unpublished-require": [
"error",
{
"allowModules": ["pg"]
}
]
}
}
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -10,7 +10,8 @@
"packages/*"
],
"scripts": {
"test": "yarn lerna exec --parallel yarn test"
"test": "yarn lerna exec --parallel yarn test",
"lint": "yarn lerna exec --parallel yarn lint"
},
"devDependencies": {
"lerna": "^3.19.0"
Expand Down
17 changes: 0 additions & 17 deletions packages/pg-cursor/.eslintrc

This file was deleted.

46 changes: 23 additions & 23 deletions packages/pg-cursor/index.js
Expand Up @@ -6,7 +6,7 @@ const util = require('util')

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 || {}
Expand All @@ -25,42 +25,42 @@ function Cursor(text, values, config) {

util.inherits(Cursor, EventEmitter)

Cursor.prototype._ifNoData = function() {
Cursor.prototype._ifNoData = function () {
this.state = 'idle'
this._shiftQueue()
}

Cursor.prototype._rowDescription = function() {
Cursor.prototype._rowDescription = function () {
if (this.connection) {
this.connection.removeListener('noData', this._ifNoData)
}
}

Cursor.prototype.submit = function(connection) {
Cursor.prototype.submit = function (connection) {
this.connection = connection
this._portal = 'C_' + nextUniqueID++

const con = connection

con.parse(
{
text: this.text,
text: this.text
},
true
)

con.bind(
{
portal: this._portal,
values: this.values,
values: this.values
},
true
)

con.describe(
{
type: 'P',
name: this._portal, // AWS Redshift requires a portal name
name: this._portal // AWS Redshift requires a portal name
},
true
)
Expand All @@ -75,13 +75,13 @@ Cursor.prototype.submit = function(connection) {
con.once('rowDescription', this._rowDescription)
}

Cursor.prototype._shiftQueue = function() {
Cursor.prototype._shiftQueue = function () {
if (this._queue.length) {
this._getRows.apply(this, this._queue.shift())
}
}

Cursor.prototype._closePortal = function() {
Cursor.prototype._closePortal = function () {
// because we opened a named portal to stream results
// we need to close the same named portal. Leaving a named portal
// open can lock tables for modification if inside a transaction.
Expand All @@ -90,19 +90,19 @@ Cursor.prototype._closePortal = function() {
this.connection.sync()
}

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
Expand All @@ -118,26 +118,26 @@ Cursor.prototype._sendRows = function() {
})
}

Cursor.prototype.handleCommandComplete = function(msg) {
Cursor.prototype.handleCommandComplete = function (msg) {
this._result.addCommandComplete(msg)
this._closePortal()
}

Cursor.prototype.handlePortalSuspended = function() {
Cursor.prototype.handlePortalSuspended = function () {
this._sendRows()
}

Cursor.prototype.handleReadyForQuery = function() {
Cursor.prototype.handleReadyForQuery = function () {
this._sendRows()
this.state = 'done'
this.emit('end', this._result)
}

Cursor.prototype.handleEmptyQuery = function() {
Cursor.prototype.handleEmptyQuery = function () {
this.connection.sync()
}

Cursor.prototype.handleError = function(msg) {
Cursor.prototype.handleError = function (msg) {
this.connection.removeListener('noData', this._ifNoData)
this.connection.removeListener('rowDescription', this._rowDescription)
this.state = 'error'
Expand All @@ -159,29 +159,29 @@ 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()
}

// users really shouldn't be calling 'end' here and terminating a connection to postgres
// via the low level connection.end api
Cursor.prototype.end = util.deprecate(function(cb) {
Cursor.prototype.end = util.deprecate(function (cb) {
if (this.state !== 'initialized') {
this.connection.sync()
}
this.connection.once('end', cb)
this.connection.end()
}, 'Cursor.end is deprecated. Call end on the client itself to end a connection to the database.')

Cursor.prototype.close = function(cb) {
Cursor.prototype.close = function (cb) {
if (this.state === 'done') {
if (cb) {
return setImmediate(cb)
Expand All @@ -192,13 +192,13 @@ Cursor.prototype.close = function(cb) {
this._closePortal()
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)
}
Expand Down
5 changes: 3 additions & 2 deletions packages/pg-cursor/package.json
Expand Up @@ -7,7 +7,8 @@
"test": "test"
},
"scripts": {
"test": " mocha && eslint ."
"test": "mocha && eslint .",
"lint": "eslint ."
},
"repository": {
"type": "git",
Expand All @@ -26,7 +27,7 @@
"prettier": {
"semi": false,
"printWidth": 120,
"trailingComma": "es5",
"trailingComma": "none",
"singleQuote": true
}
}
18 changes: 9 additions & 9 deletions packages/pg-cursor/test/close.js
Expand Up @@ -3,40 +3,40 @@ const Cursor = require('../')
const pg = require('pg')

const text = 'SELECT generate_series as num FROM generate_series(0, 50)'
describe('close', function() {
beforeEach(function(done) {
describe('close', function () {
beforeEach(function (done) {
const client = (this.client = new pg.Client())
client.connect(done)
client.on('drain', client.end.bind(client))
})

it('can close a finished cursor without a callback', function(done) {
it('can close a finished cursor without a callback', function (done) {
const cursor = new Cursor(text)
this.client.query(cursor)
this.client.query('SELECT NOW()', done)
cursor.read(100, function(err) {
cursor.read(100, function (err) {
assert.ifError(err)
cursor.close()
})
})

it('closes cursor early', function(done) {
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) {
cursor.read(25, function (err) {
assert.ifError(err)
cursor.close()
})
})

it('works with callback style', function(done) {
it('works with callback style', function (done) {
const cursor = new Cursor(text)
const client = this.client
client.query(cursor)
cursor.read(25, function(err) {
cursor.read(25, function (err) {
assert.ifError(err)
cursor.close(function(err) {
cursor.close(function (err) {
assert.ifError(err)
client.query('SELECT NOW()', done)
})
Expand Down
12 changes: 6 additions & 6 deletions packages/pg-cursor/test/error-handling.js
Expand Up @@ -29,11 +29,11 @@ describe('read callback does not fire sync', () => {
let after = false
cursor.read(1, function(err) {
assert(err, 'error should be returned')
assert.equal(after, true, 'should not call read sync')
assert.strictEqual(after, true, 'should not call read sync')
after = false
cursor.read(1, function(err) {
assert(err, 'error should be returned')
assert.equal(after, true, 'should not call read sync')
assert.strictEqual(after, true, 'should not call read sync')
client.end()
done()
})
Expand All @@ -49,13 +49,13 @@ describe('read callback does not fire sync', () => {
let after = false
cursor.read(1, function(err) {
assert(!err)
assert.equal(after, true, 'should not call read sync')
assert.strictEqual(after, true, 'should not call read sync')
cursor.read(1, function(err) {
assert(!err)
after = false
cursor.read(1, function(err) {
assert(!err)
assert.equal(after, true, 'should not call read sync')
assert.strictEqual(after, true, 'should not call read sync')
client.end()
done()
})
Expand All @@ -73,11 +73,11 @@ describe('proper cleanup', function() {
const cursor1 = client.query(new Cursor(text))
cursor1.read(8, function(err, rows) {
assert.ifError(err)
assert.equal(rows.length, 5)
assert.strictEqual(rows.length, 5)
const cursor2 = client.query(new Cursor(text))
cursor2.read(8, function(err, rows) {
assert.ifError(err)
assert.equal(rows.length, 5)
assert.strictEqual(rows.length, 5)
client.end()
done()
})
Expand Down

0 comments on commit 423baa6

Please sign in to comment.