Skip to content

Commit 4164686

Browse files
committed
meh
1 parent 414fac6 commit 4164686

File tree

8 files changed

+385
-635
lines changed

8 files changed

+385
-635
lines changed

.eslintrc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
2-
"extends": "eslint:recommended",
2+
"extends": ["eslint:recommended"],
33
"plugins": ["prettier"],
44
"rules": {
55
"prettier/prettier": "error",
6-
"no-new-func": "off"
6+
"prefer-const": "error",
7+
"no-var": "error"
78
},
89
"env": {
910
"es6": true,

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const prepare = require('pg/lib/utils.js').prepareValue
44
const EventEmitter = require('events').EventEmitter
55
const util = require('util')
66

7-
var nextUniqueID = 1 // concept borrowed from org.postgresql.core.v3.QueryExecutorImpl
7+
let nextUniqueID = 1 // concept borrowed from org.postgresql.core.v3.QueryExecutorImpl
88

99
function Cursor(text, values, config) {
1010
EventEmitter.call(this)
@@ -130,7 +130,7 @@ Cursor.prototype.handleError = function(msg) {
130130
this._cb(msg)
131131
}
132132
// dispatch error to all waiting callbacks
133-
for (var i = 0; i < this._queue.length; i++) {
133+
for (let i = 0; i < this._queue.length; i++) {
134134
this._queue.pop()[1](msg)
135135
}
136136

@@ -155,6 +155,7 @@ Cursor.prototype._getRows = function(rows, cb) {
155155
}
156156

157157
Cursor.prototype.end = function(cb) {
158+
console.log(this.state)
158159
if (this.state !== 'initialized') {
159160
this.connection.sync()
160161
}
@@ -177,6 +178,7 @@ Cursor.prototype.close = function(cb) {
177178
}
178179

179180
Cursor.prototype.read = function(rows, cb) {
181+
console.log('state', this.state)
180182
if (this.state === 'idle') {
181183
return this._getRows(rows, cb)
182184
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"devDependencies": {
1919
"eslint": "^6.5.1",
2020
"eslint-config-prettier": "^6.4.0",
21-
"eslint-plugin-import": "^2.7.0",
2221
"eslint-plugin-prettier": "^3.1.1",
2322
"mocha": "^6.2.2",
2423
"pg": "^7.12.1",

test/close.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
var assert = require('assert')
2-
var Cursor = require('../')
3-
var pg = require('pg')
1+
const assert = require('assert')
2+
const Cursor = require('../')
3+
const pg = require('pg')
44

5-
var text = 'SELECT generate_series as num FROM generate_series(0, 50)'
5+
const text = 'SELECT generate_series as num FROM generate_series(0, 50)'
66
describe('close', function() {
77
beforeEach(function(done) {
8-
var client = (this.client = new pg.Client())
8+
const client = (this.client = new pg.Client())
99
client.connect(done)
1010
client.on('drain', client.end.bind(client))
1111
})
1212

1313
it('closes cursor early', function(done) {
14-
var cursor = new Cursor(text)
14+
const cursor = new Cursor(text)
1515
this.client.query(cursor)
1616
this.client.query('SELECT NOW()', done)
1717
cursor.read(25, function(err) {
@@ -21,8 +21,8 @@ describe('close', function() {
2121
})
2222

2323
it('works with callback style', function(done) {
24-
var cursor = new Cursor(text)
25-
var client = this.client
24+
const cursor = new Cursor(text)
25+
const client = this.client
2626
client.query(cursor)
2727
cursor.read(25, function(err) {
2828
assert.ifError(err)

test/error-handling.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
'use strict'
2-
var assert = require('assert')
3-
var Cursor = require('../')
4-
var pg = require('pg')
2+
const assert = require('assert')
3+
const Cursor = require('../')
4+
const pg = require('pg')
55

6-
var text = 'SELECT generate_series as num FROM generate_series(0, 4)'
6+
const text = 'SELECT generate_series as num FROM generate_series(0, 4)'
77

88
describe('error handling', function() {
99
it('can continue after error', function(done) {
10-
var client = new pg.Client()
10+
const client = new pg.Client()
1111
client.connect()
12-
var cursor = client.query(new Cursor('asdfdffsdf'))
12+
const cursor = client.query(new Cursor('asdfdffsdf'))
1313
cursor.read(1, function(err) {
1414
assert(err)
1515
client.query('SELECT NOW()', function(err) {
@@ -23,9 +23,9 @@ describe('error handling', function() {
2323

2424
describe('read callback does not fire sync', () => {
2525
it('does not fire error callback sync', done => {
26-
var client = new pg.Client()
26+
const client = new pg.Client()
2727
client.connect()
28-
var cursor = client.query(new Cursor('asdfdffsdf'))
28+
const cursor = client.query(new Cursor('asdfdffsdf'))
2929
let after = false
3030
cursor.read(1, function(err) {
3131
assert(err, 'error should be returned')
@@ -43,9 +43,9 @@ describe('read callback does not fire sync', () => {
4343
})
4444

4545
it('does not fire result sync after finished', done => {
46-
var client = new pg.Client()
46+
const client = new pg.Client()
4747
client.connect()
48-
var cursor = client.query(new Cursor('SELECT NOW()'))
48+
const cursor = client.query(new Cursor('SELECT NOW()'))
4949
let after = false
5050
cursor.read(1, function(err) {
5151
assert(!err)
@@ -68,13 +68,13 @@ describe('read callback does not fire sync', () => {
6868

6969
describe('proper cleanup', function() {
7070
it('can issue multiple cursors on one client', function(done) {
71-
var client = new pg.Client()
71+
const client = new pg.Client()
7272
client.connect()
73-
var cursor1 = client.query(new Cursor(text))
73+
const cursor1 = client.query(new Cursor(text))
7474
cursor1.read(8, function(err, rows) {
7575
assert.ifError(err)
7676
assert.equal(rows.length, 5)
77-
var cursor2 = client.query(new Cursor(text))
77+
const cursor2 = client.query(new Cursor(text))
7878
cursor2.read(8, function(err, rows) {
7979
assert.ifError(err)
8080
assert.equal(rows.length, 5)

test/index.js

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
var assert = require('assert')
2-
var Cursor = require('../')
3-
var pg = require('pg')
1+
const assert = require('assert')
2+
const Cursor = require('../')
3+
const pg = require('pg')
44

5-
var text = 'SELECT generate_series as num FROM generate_series(0, 5)'
5+
const text = 'SELECT generate_series as num FROM generate_series(0, 5)'
66

77
describe('cursor', function() {
88
beforeEach(function(done) {
9-
var client = (this.client = new pg.Client())
9+
const client = (this.client = new pg.Client())
1010
client.connect(done)
1111

1212
this.pgCursor = function(text, values) {
13-
client.on('drain', client.end.bind(client))
1413
return client.query(new Cursor(text, values || []))
1514
}
1615
})
@@ -20,16 +19,16 @@ describe('cursor', function() {
2019
})
2120

2221
it('fetch 6 when asking for 10', function(done) {
23-
var cursor = this.pgCursor(text)
22+
const cursor = this.pgCursor(text)
2423
cursor.read(10, function(err, res) {
2524
assert.ifError(err)
2625
assert.equal(res.length, 6)
2726
done()
2827
})
2928
})
3029

31-
it('end before reading to end', function(done) {
32-
var cursor = this.pgCursor(text)
30+
it.only('end before reading to end', function(done) {
31+
const cursor = this.pgCursor(text)
3332
cursor.read(3, function(err, res) {
3433
assert.ifError(err)
3534
assert.equal(res.length, 3)
@@ -38,15 +37,15 @@ describe('cursor', function() {
3837
})
3938

4039
it('callback with error', function(done) {
41-
var cursor = this.pgCursor('select asdfasdf')
40+
const cursor = this.pgCursor('select asdfasdf')
4241
cursor.read(1, function(err) {
4342
assert(err)
4443
done()
4544
})
4645
})
4746

4847
it('read a partial chunk of data', function(done) {
49-
var cursor = this.pgCursor(text)
48+
const cursor = this.pgCursor(text)
5049
cursor.read(2, function(err, res) {
5150
assert.ifError(err)
5251
assert.equal(res.length, 2)
@@ -68,7 +67,7 @@ describe('cursor', function() {
6867
})
6968

7069
it('read return length 0 past the end', function(done) {
71-
var cursor = this.pgCursor(text)
70+
const cursor = this.pgCursor(text)
7271
cursor.read(2, function(err) {
7372
assert(!err)
7473
cursor.read(100, function(err, res) {
@@ -85,11 +84,11 @@ describe('cursor', function() {
8584

8685
it('read huge result', function(done) {
8786
this.timeout(10000)
88-
var text = 'SELECT generate_series as num FROM generate_series(0, 100000)'
89-
var values = []
90-
var cursor = this.pgCursor(text, values)
91-
var count = 0
92-
var read = function() {
87+
const text = 'SELECT generate_series as num FROM generate_series(0, 100000)'
88+
const values = []
89+
const cursor = this.pgCursor(text, values)
90+
let count = 0
91+
const read = function() {
9392
cursor.read(100, function(err, rows) {
9493
if (err) return done(err)
9594
if (!rows.length) {
@@ -107,9 +106,9 @@ describe('cursor', function() {
107106
})
108107

109108
it('normalizes parameter values', function(done) {
110-
var text = 'SELECT $1::json me'
111-
var values = [{ name: 'brian' }]
112-
var cursor = this.pgCursor(text, values)
109+
const text = 'SELECT $1::json me'
110+
const values = [{ name: 'brian' }]
111+
const cursor = this.pgCursor(text, values)
113112
cursor.read(1, function(err, rows) {
114113
if (err) return done(err)
115114
assert.equal(rows[0].me.name, 'brian')
@@ -122,7 +121,7 @@ describe('cursor', function() {
122121
})
123122

124123
it('returns result along with rows', function(done) {
125-
var cursor = this.pgCursor(text)
124+
const cursor = this.pgCursor(text)
126125
cursor.read(1, function(err, rows, result) {
127126
assert.ifError(err)
128127
assert.equal(rows.length, 1)
@@ -133,7 +132,7 @@ describe('cursor', function() {
133132
})
134133

135134
it('emits row events', function(done) {
136-
var cursor = this.pgCursor(text)
135+
const cursor = this.pgCursor(text)
137136
cursor.read(10)
138137
cursor.on('row', (row, result) => result.addRow(row))
139138
cursor.on('end', result => {
@@ -143,7 +142,7 @@ describe('cursor', function() {
143142
})
144143

145144
it('emits row events when cursor is closed manually', function(done) {
146-
var cursor = this.pgCursor(text)
145+
const cursor = this.pgCursor(text)
147146
cursor.on('row', (row, result) => result.addRow(row))
148147
cursor.on('end', result => {
149148
assert.equal(result.rows.length, 3)
@@ -154,19 +153,19 @@ describe('cursor', function() {
154153
})
155154

156155
it('emits error events', function(done) {
157-
var cursor = this.pgCursor('select asdfasdf')
156+
const cursor = this.pgCursor('select asdfasdf')
158157
cursor.on('error', function(err) {
159158
assert(err)
160159
done()
161160
})
162161
})
163162

164163
it('returns rowCount on insert', function(done) {
165-
var pgCursor = this.pgCursor
164+
const pgCursor = this.pgCursor
166165
this.client
167166
.query('CREATE TEMPORARY TABLE pg_cursor_test (foo VARCHAR(1), bar VARCHAR(1))')
168167
.then(function() {
169-
var cursor = pgCursor('insert into pg_cursor_test values($1, $2)', ['a', 'b'])
168+
const cursor = pgCursor('insert into pg_cursor_test values($1, $2)', ['a', 'b'])
170169
cursor.read(1, function(err, rows, result) {
171170
assert.ifError(err)
172171
assert.equal(rows.length, 0)

test/no-data-handling.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
var assert = require('assert')
2-
var pg = require('pg')
3-
var Cursor = require('../')
1+
const assert = require('assert')
2+
const pg = require('pg')
3+
const Cursor = require('../')
44

55
describe('queries with no data', function() {
66
beforeEach(function(done) {
7-
var client = (this.client = new pg.Client())
7+
const client = (this.client = new pg.Client())
88
client.connect(done)
99
})
1010

@@ -13,7 +13,7 @@ describe('queries with no data', function() {
1313
})
1414

1515
it('handles queries that return no data', function(done) {
16-
var cursor = new Cursor('CREATE TEMPORARY TABLE whatwhat (thing int)')
16+
const cursor = new Cursor('CREATE TEMPORARY TABLE whatwhat (thing int)')
1717
this.client.query(cursor)
1818
cursor.read(100, function(err, rows) {
1919
assert.ifError(err)
@@ -23,7 +23,7 @@ describe('queries with no data', function() {
2323
})
2424

2525
it('handles empty query', function(done) {
26-
var cursor = new Cursor('-- this is a comment')
26+
let cursor = new Cursor('-- this is a comment')
2727
cursor = this.client.query(cursor)
2828
cursor.read(100, function(err, rows) {
2929
assert.ifError(err)

0 commit comments

Comments
 (0)