mirror of
https://github.com/brianc/node-postgres.git
synced 2026-02-01 16:47:23 +00:00
commit
6d47026083
15
.eslintrc
15
.eslintrc
@ -1,9 +1,14 @@
|
|||||||
{
|
{
|
||||||
"extends": "standard",
|
"extends": ["eslint:recommended"],
|
||||||
"env": {
|
"plugins": ["prettier"],
|
||||||
"mocha": true
|
|
||||||
},
|
|
||||||
"rules": {
|
"rules": {
|
||||||
"no-new-func": "off"
|
"prettier/prettier": "error",
|
||||||
|
"prefer-const": "error",
|
||||||
|
"no-var": "error"
|
||||||
|
},
|
||||||
|
"env": {
|
||||||
|
"es6": true,
|
||||||
|
"node": true,
|
||||||
|
"mocha": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,9 +2,9 @@ language: node_js
|
|||||||
dist: trusty
|
dist: trusty
|
||||||
sudo: false
|
sudo: false
|
||||||
node_js:
|
node_js:
|
||||||
- "4.2"
|
|
||||||
- "6"
|
|
||||||
- "8"
|
- "8"
|
||||||
|
- "10"
|
||||||
|
- "12"
|
||||||
env:
|
env:
|
||||||
- PGUSER=postgres
|
- PGUSER=postgres
|
||||||
services:
|
services:
|
||||||
|
|||||||
5
Makefile
5
Makefile
@ -1,13 +1,14 @@
|
|||||||
.PHONY: publish-patch test
|
.PHONY: test
|
||||||
|
|
||||||
test:
|
test:
|
||||||
npm test
|
npm test
|
||||||
|
|
||||||
|
.PHONY: patch
|
||||||
patch: test
|
patch: test
|
||||||
npm version patch -m "Bump version"
|
npm version patch -m "Bump version"
|
||||||
git push origin master --tags
|
git push origin master --tags
|
||||||
npm publish
|
npm publish
|
||||||
|
|
||||||
|
.PHONY: minor
|
||||||
minor: test
|
minor: test
|
||||||
npm version minor -m "Bump version"
|
npm version minor -m "Bump version"
|
||||||
git push origin master --tags
|
git push origin master --tags
|
||||||
|
|||||||
73
index.js
73
index.js
@ -4,9 +4,9 @@ const prepare = require('pg/lib/utils.js').prepareValue
|
|||||||
const EventEmitter = require('events').EventEmitter
|
const EventEmitter = require('events').EventEmitter
|
||||||
const util = require('util')
|
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)
|
EventEmitter.call(this)
|
||||||
|
|
||||||
this._conf = config || {}
|
this._conf = config || {}
|
||||||
@ -15,7 +15,7 @@ function Cursor (text, values, config) {
|
|||||||
this.connection = null
|
this.connection = null
|
||||||
this._queue = []
|
this._queue = []
|
||||||
this.state = 'initialized'
|
this.state = 'initialized'
|
||||||
this._result = new Result(this._conf.rowMode)
|
this._result = new Result(this._conf.rowMode, this._conf.types)
|
||||||
this._cb = null
|
this._cb = null
|
||||||
this._rows = null
|
this._rows = null
|
||||||
this._portal = null
|
this._portal = null
|
||||||
@ -23,25 +23,34 @@ function Cursor (text, values, config) {
|
|||||||
|
|
||||||
util.inherits(Cursor, EventEmitter)
|
util.inherits(Cursor, EventEmitter)
|
||||||
|
|
||||||
Cursor.prototype.submit = function (connection) {
|
Cursor.prototype.submit = function(connection) {
|
||||||
this.connection = connection
|
this.connection = connection
|
||||||
this._portal = 'C_' + (nextUniqueID++)
|
this._portal = 'C_' + nextUniqueID++
|
||||||
|
|
||||||
const con = connection
|
const con = connection
|
||||||
|
|
||||||
con.parse({
|
con.parse(
|
||||||
text: this.text
|
{
|
||||||
}, true)
|
text: this.text,
|
||||||
|
},
|
||||||
|
true
|
||||||
|
)
|
||||||
|
|
||||||
con.bind({
|
con.bind(
|
||||||
portal: this._portal,
|
{
|
||||||
values: this.values
|
portal: this._portal,
|
||||||
}, true)
|
values: this.values,
|
||||||
|
},
|
||||||
|
true
|
||||||
|
)
|
||||||
|
|
||||||
con.describe({
|
con.describe(
|
||||||
type: 'P',
|
{
|
||||||
name: this._portal // AWS Redshift requires a portal name
|
type: 'P',
|
||||||
}, true)
|
name: this._portal, // AWS Redshift requires a portal name
|
||||||
|
},
|
||||||
|
true
|
||||||
|
)
|
||||||
|
|
||||||
con.flush()
|
con.flush()
|
||||||
|
|
||||||
@ -60,25 +69,25 @@ Cursor.prototype.submit = function (connection) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
Cursor.prototype._shiftQueue = function () {
|
Cursor.prototype._shiftQueue = function() {
|
||||||
if (this._queue.length) {
|
if (this._queue.length) {
|
||||||
this._getRows.apply(this, this._queue.shift())
|
this._getRows.apply(this, this._queue.shift())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Cursor.prototype.handleRowDescription = function (msg) {
|
Cursor.prototype.handleRowDescription = function(msg) {
|
||||||
this._result.addFields(msg.fields)
|
this._result.addFields(msg.fields)
|
||||||
this.state = 'idle'
|
this.state = 'idle'
|
||||||
this._shiftQueue()
|
this._shiftQueue()
|
||||||
}
|
}
|
||||||
|
|
||||||
Cursor.prototype.handleDataRow = function (msg) {
|
Cursor.prototype.handleDataRow = function(msg) {
|
||||||
const row = this._result.parseRow(msg.fields)
|
const row = this._result.parseRow(msg.fields)
|
||||||
this.emit('row', row, this._result)
|
this.emit('row', row, this._result)
|
||||||
this._rows.push(row)
|
this._rows.push(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
Cursor.prototype._sendRows = function () {
|
Cursor.prototype._sendRows = function() {
|
||||||
this.state = 'idle'
|
this.state = 'idle'
|
||||||
setImmediate(() => {
|
setImmediate(() => {
|
||||||
const cb = this._cb
|
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._result.addCommandComplete(msg)
|
||||||
this.connection.sync()
|
this.connection.sync()
|
||||||
}
|
}
|
||||||
|
|
||||||
Cursor.prototype.handlePortalSuspended = function () {
|
Cursor.prototype.handlePortalSuspended = function() {
|
||||||
this._sendRows()
|
this._sendRows()
|
||||||
}
|
}
|
||||||
|
|
||||||
Cursor.prototype.handleReadyForQuery = function () {
|
Cursor.prototype.handleReadyForQuery = function() {
|
||||||
this._sendRows()
|
this._sendRows()
|
||||||
this.emit('end', this._result)
|
this.emit('end', this._result)
|
||||||
this.state = 'done'
|
this.state = 'done'
|
||||||
}
|
}
|
||||||
|
|
||||||
Cursor.prototype.handleEmptyQuery = function () {
|
Cursor.prototype.handleEmptyQuery = function() {
|
||||||
this.connection.sync()
|
this.connection.sync()
|
||||||
}
|
}
|
||||||
|
|
||||||
Cursor.prototype.handleError = function (msg) {
|
Cursor.prototype.handleError = function(msg) {
|
||||||
this.state = 'error'
|
this.state = 'error'
|
||||||
this._error = msg
|
this._error = msg
|
||||||
// satisfy any waiting callback
|
// satisfy any waiting callback
|
||||||
@ -121,7 +130,7 @@ Cursor.prototype.handleError = function (msg) {
|
|||||||
this._cb(msg)
|
this._cb(msg)
|
||||||
}
|
}
|
||||||
// dispatch error to all waiting callbacks
|
// 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)
|
this._queue.pop()[1](msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,19 +142,19 @@ Cursor.prototype.handleError = function (msg) {
|
|||||||
this.connection.sync()
|
this.connection.sync()
|
||||||
}
|
}
|
||||||
|
|
||||||
Cursor.prototype._getRows = function (rows, cb) {
|
Cursor.prototype._getRows = function(rows, cb) {
|
||||||
this.state = 'busy'
|
this.state = 'busy'
|
||||||
this._cb = cb
|
this._cb = cb
|
||||||
this._rows = []
|
this._rows = []
|
||||||
const msg = {
|
const msg = {
|
||||||
portal: this._portal,
|
portal: this._portal,
|
||||||
rows: rows
|
rows: rows,
|
||||||
}
|
}
|
||||||
this.connection.execute(msg, true)
|
this.connection.execute(msg, true)
|
||||||
this.connection.flush()
|
this.connection.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
Cursor.prototype.end = function (cb) {
|
Cursor.prototype.end = function(cb) {
|
||||||
if (this.state !== 'initialized') {
|
if (this.state !== 'initialized') {
|
||||||
this.connection.sync()
|
this.connection.sync()
|
||||||
}
|
}
|
||||||
@ -153,7 +162,7 @@ Cursor.prototype.end = function (cb) {
|
|||||||
this.connection.end()
|
this.connection.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
Cursor.prototype.close = function (cb) {
|
Cursor.prototype.close = function(cb) {
|
||||||
if (this.state === 'done') {
|
if (this.state === 'done') {
|
||||||
return setImmediate(cb)
|
return setImmediate(cb)
|
||||||
}
|
}
|
||||||
@ -161,13 +170,13 @@ Cursor.prototype.close = function (cb) {
|
|||||||
this.connection.sync()
|
this.connection.sync()
|
||||||
this.state = 'done'
|
this.state = 'done'
|
||||||
if (cb) {
|
if (cb) {
|
||||||
this.connection.once('closeComplete', function () {
|
this.connection.once('closeComplete', function() {
|
||||||
cb()
|
cb()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Cursor.prototype.read = function (rows, cb) {
|
Cursor.prototype.read = function(rows, cb) {
|
||||||
if (this.state === 'idle') {
|
if (this.state === 'idle') {
|
||||||
return this._getRows(rows, cb)
|
return this._getRows(rows, cb)
|
||||||
}
|
}
|
||||||
|
|||||||
1807
package-lock.json
generated
1807
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
23
package.json
23
package.json
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "pg-cursor",
|
"name": "pg-cursor",
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"description": "",
|
"description": "Query cursor extension for node-postgres",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"directories": {
|
"directories": {
|
||||||
"test": "test"
|
"test": "test"
|
||||||
@ -16,14 +16,17 @@
|
|||||||
"author": "Brian M. Carlson",
|
"author": "Brian M. Carlson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^4.4.0",
|
"eslint": "^6.5.1",
|
||||||
"eslint-config-standard": "^10.2.1",
|
"eslint-config-prettier": "^6.4.0",
|
||||||
"eslint-plugin-import": "^2.7.0",
|
"eslint-plugin-prettier": "^3.1.1",
|
||||||
"eslint-plugin-node": "^5.1.1",
|
"mocha": "^6.2.2",
|
||||||
"eslint-plugin-promise": "^3.5.0",
|
"pg": "7.x",
|
||||||
"eslint-plugin-standard": "^3.0.1",
|
"prettier": "^1.18.2"
|
||||||
"mocha": "^3.5.0",
|
|
||||||
"pg": "6.x"
|
|
||||||
},
|
},
|
||||||
"dependencies": {}
|
"prettier": {
|
||||||
|
"semi": false,
|
||||||
|
"printWidth": 120,
|
||||||
|
"trailingComma": "es5",
|
||||||
|
"singleQuote": true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,32 +1,32 @@
|
|||||||
var assert = require('assert')
|
const assert = require('assert')
|
||||||
var Cursor = require('../')
|
const Cursor = require('../')
|
||||||
var pg = require('pg')
|
const pg = require('pg')
|
||||||
|
|
||||||
var text = 'SELECT generate_series as num FROM generate_series(0, 50)'
|
const text = 'SELECT generate_series as num FROM generate_series(0, 50)'
|
||||||
describe('close', function () {
|
describe('close', function() {
|
||||||
beforeEach(function (done) {
|
beforeEach(function(done) {
|
||||||
var client = this.client = new pg.Client()
|
const client = (this.client = new pg.Client())
|
||||||
client.connect(done)
|
client.connect(done)
|
||||||
client.on('drain', client.end.bind(client))
|
client.on('drain', client.end.bind(client))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('closes cursor early', function (done) {
|
it('closes cursor early', function(done) {
|
||||||
var cursor = new Cursor(text)
|
const cursor = new Cursor(text)
|
||||||
this.client.query(cursor)
|
this.client.query(cursor)
|
||||||
this.client.query('SELECT NOW()', done)
|
this.client.query('SELECT NOW()', done)
|
||||||
cursor.read(25, function (err, res) {
|
cursor.read(25, function(err) {
|
||||||
assert.ifError(err)
|
assert.ifError(err)
|
||||||
cursor.close()
|
cursor.close()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('works with callback style', function (done) {
|
it('works with callback style', function(done) {
|
||||||
var cursor = new Cursor(text)
|
const cursor = new Cursor(text)
|
||||||
var client = this.client
|
const client = this.client
|
||||||
client.query(cursor)
|
client.query(cursor)
|
||||||
cursor.read(25, function (err, res) {
|
cursor.read(25, function(err) {
|
||||||
assert.ifError(err)
|
assert.ifError(err)
|
||||||
cursor.close(function (err) {
|
cursor.close(function(err) {
|
||||||
assert.ifError(err)
|
assert.ifError(err)
|
||||||
client.query('SELECT NOW()', done)
|
client.query('SELECT NOW()', done)
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,18 +1,18 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
var assert = require('assert')
|
const assert = require('assert')
|
||||||
var Cursor = require('../')
|
const Cursor = require('../')
|
||||||
var pg = require('pg')
|
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 () {
|
describe('error handling', function() {
|
||||||
it('can continue after error', function (done) {
|
it('can continue after error', function(done) {
|
||||||
var client = new pg.Client()
|
const client = new pg.Client()
|
||||||
client.connect()
|
client.connect()
|
||||||
var cursor = client.query(new Cursor('asdfdffsdf'))
|
const cursor = client.query(new Cursor('asdfdffsdf'))
|
||||||
cursor.read(1, function (err) {
|
cursor.read(1, function(err) {
|
||||||
assert(err)
|
assert(err)
|
||||||
client.query('SELECT NOW()', function (err, res) {
|
client.query('SELECT NOW()', function(err) {
|
||||||
assert.ifError(err)
|
assert.ifError(err)
|
||||||
client.end()
|
client.end()
|
||||||
done()
|
done()
|
||||||
@ -22,16 +22,16 @@ describe('error handling', function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('read callback does not fire sync', () => {
|
describe('read callback does not fire sync', () => {
|
||||||
it('does not fire error callback sync', (done) => {
|
it('does not fire error callback sync', done => {
|
||||||
var client = new pg.Client()
|
const client = new pg.Client()
|
||||||
client.connect()
|
client.connect()
|
||||||
var cursor = client.query(new Cursor('asdfdffsdf'))
|
const cursor = client.query(new Cursor('asdfdffsdf'))
|
||||||
let after = false
|
let after = false
|
||||||
cursor.read(1, function (err) {
|
cursor.read(1, function(err) {
|
||||||
assert(err, 'error should be returned')
|
assert(err, 'error should be returned')
|
||||||
assert.equal(after, true, 'should not call read sync')
|
assert.equal(after, true, 'should not call read sync')
|
||||||
after = false
|
after = false
|
||||||
cursor.read(1, function (err) {
|
cursor.read(1, function(err) {
|
||||||
assert(err, 'error should be returned')
|
assert(err, 'error should be returned')
|
||||||
assert.equal(after, true, 'should not call read sync')
|
assert.equal(after, true, 'should not call read sync')
|
||||||
client.end()
|
client.end()
|
||||||
@ -42,18 +42,18 @@ describe('read callback does not fire sync', () => {
|
|||||||
after = true
|
after = true
|
||||||
})
|
})
|
||||||
|
|
||||||
it('does not fire result sync after finished', (done) => {
|
it('does not fire result sync after finished', done => {
|
||||||
var client = new pg.Client()
|
const client = new pg.Client()
|
||||||
client.connect()
|
client.connect()
|
||||||
var cursor = client.query(new Cursor('SELECT NOW()'))
|
const cursor = client.query(new Cursor('SELECT NOW()'))
|
||||||
let after = false
|
let after = false
|
||||||
cursor.read(1, function (err) {
|
cursor.read(1, function(err) {
|
||||||
assert(!err)
|
assert(!err)
|
||||||
assert.equal(after, true, 'should not call read sync')
|
assert.equal(after, true, 'should not call read sync')
|
||||||
cursor.read(1, function (err) {
|
cursor.read(1, function(err) {
|
||||||
assert(!err)
|
assert(!err)
|
||||||
after = false
|
after = false
|
||||||
cursor.read(1, function (err) {
|
cursor.read(1, function(err) {
|
||||||
assert(!err)
|
assert(!err)
|
||||||
assert.equal(after, true, 'should not call read sync')
|
assert.equal(after, true, 'should not call read sync')
|
||||||
client.end()
|
client.end()
|
||||||
@ -66,16 +66,16 @@ describe('read callback does not fire sync', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('proper cleanup', function () {
|
describe('proper cleanup', function() {
|
||||||
it('can issue multiple cursors on one client', function (done) {
|
it('can issue multiple cursors on one client', function(done) {
|
||||||
var client = new pg.Client()
|
const client = new pg.Client()
|
||||||
client.connect()
|
client.connect()
|
||||||
var cursor1 = client.query(new Cursor(text))
|
const cursor1 = client.query(new Cursor(text))
|
||||||
cursor1.read(8, function (err, rows) {
|
cursor1.read(8, function(err, rows) {
|
||||||
assert.ifError(err)
|
assert.ifError(err)
|
||||||
assert.equal(rows.length, 5)
|
assert.equal(rows.length, 5)
|
||||||
var cursor2 = client.query(new Cursor(text))
|
const cursor2 = client.query(new Cursor(text))
|
||||||
cursor2.read(8, function (err, rows) {
|
cursor2.read(8, function(err, rows) {
|
||||||
assert.ifError(err)
|
assert.ifError(err)
|
||||||
assert.equal(rows.length, 5)
|
assert.equal(rows.length, 5)
|
||||||
client.end()
|
client.end()
|
||||||
|
|||||||
127
test/index.js
127
test/index.js
@ -1,62 +1,61 @@
|
|||||||
var assert = require('assert')
|
const assert = require('assert')
|
||||||
var Cursor = require('../')
|
const Cursor = require('../')
|
||||||
var pg = require('pg')
|
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 () {
|
describe('cursor', function() {
|
||||||
beforeEach(function (done) {
|
beforeEach(function(done) {
|
||||||
var client = this.client = new pg.Client()
|
const client = (this.client = new pg.Client())
|
||||||
client.connect(done)
|
client.connect(done)
|
||||||
|
|
||||||
this.pgCursor = function (text, values) {
|
this.pgCursor = function(text, values) {
|
||||||
client.on('drain', client.end.bind(client))
|
|
||||||
return client.query(new Cursor(text, values || []))
|
return client.query(new Cursor(text, values || []))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function() {
|
||||||
this.client.end()
|
this.client.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('fetch 6 when asking for 10', function (done) {
|
it('fetch 6 when asking for 10', function(done) {
|
||||||
var cursor = this.pgCursor(text)
|
const cursor = this.pgCursor(text)
|
||||||
cursor.read(10, function (err, res) {
|
cursor.read(10, function(err, res) {
|
||||||
assert.ifError(err)
|
assert.ifError(err)
|
||||||
assert.equal(res.length, 6)
|
assert.equal(res.length, 6)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('end before reading to end', function (done) {
|
it('end before reading to end', function(done) {
|
||||||
var cursor = this.pgCursor(text)
|
const cursor = this.pgCursor(text)
|
||||||
cursor.read(3, function (err, res) {
|
cursor.read(3, function(err, res) {
|
||||||
assert.ifError(err)
|
assert.ifError(err)
|
||||||
assert.equal(res.length, 3)
|
assert.equal(res.length, 3)
|
||||||
cursor.end(done)
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('callback with error', function (done) {
|
it('callback with error', function(done) {
|
||||||
var cursor = this.pgCursor('select asdfasdf')
|
const cursor = this.pgCursor('select asdfasdf')
|
||||||
cursor.read(1, function (err) {
|
cursor.read(1, function(err) {
|
||||||
assert(err)
|
assert(err)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('read a partial chunk of data', function (done) {
|
it('read a partial chunk of data', function(done) {
|
||||||
var cursor = this.pgCursor(text)
|
const cursor = this.pgCursor(text)
|
||||||
cursor.read(2, function (err, res) {
|
cursor.read(2, function(err, res) {
|
||||||
assert.ifError(err)
|
assert.ifError(err)
|
||||||
assert.equal(res.length, 2)
|
assert.equal(res.length, 2)
|
||||||
cursor.read(3, function (err, res) {
|
cursor.read(3, function(err, res) {
|
||||||
assert(!err)
|
assert(!err)
|
||||||
assert.equal(res.length, 3)
|
assert.equal(res.length, 3)
|
||||||
cursor.read(1, function (err, res) {
|
cursor.read(1, function(err, res) {
|
||||||
assert(!err)
|
assert(!err)
|
||||||
assert.equal(res.length, 1)
|
assert.equal(res.length, 1)
|
||||||
cursor.read(1, function (err, res) {
|
cursor.read(1, function(err, res) {
|
||||||
assert(!err)
|
assert(!err)
|
||||||
assert.ifError(err)
|
assert.ifError(err)
|
||||||
assert.strictEqual(res.length, 0)
|
assert.strictEqual(res.length, 0)
|
||||||
@ -67,14 +66,14 @@ describe('cursor', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('read return length 0 past the end', function (done) {
|
it('read return length 0 past the end', function(done) {
|
||||||
var cursor = this.pgCursor(text)
|
const cursor = this.pgCursor(text)
|
||||||
cursor.read(2, function (err, res) {
|
cursor.read(2, function(err) {
|
||||||
assert(!err)
|
assert(!err)
|
||||||
cursor.read(100, function (err, res) {
|
cursor.read(100, function(err, res) {
|
||||||
assert(!err)
|
assert(!err)
|
||||||
assert.equal(res.length, 4)
|
assert.equal(res.length, 4)
|
||||||
cursor.read(100, function (err, res) {
|
cursor.read(100, function(err, res) {
|
||||||
assert(!err)
|
assert(!err)
|
||||||
assert.equal(res.length, 0)
|
assert.equal(res.length, 0)
|
||||||
done()
|
done()
|
||||||
@ -83,14 +82,14 @@ describe('cursor', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('read huge result', function (done) {
|
it('read huge result', function(done) {
|
||||||
this.timeout(10000)
|
this.timeout(10000)
|
||||||
var text = 'SELECT generate_series as num FROM generate_series(0, 100000)'
|
const text = 'SELECT generate_series as num FROM generate_series(0, 100000)'
|
||||||
var values = []
|
const values = []
|
||||||
var cursor = this.pgCursor(text, values)
|
const cursor = this.pgCursor(text, values)
|
||||||
var count = 0
|
let count = 0
|
||||||
var read = function () {
|
const read = function() {
|
||||||
cursor.read(100, function (err, rows) {
|
cursor.read(100, function(err, rows) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
if (!rows.length) {
|
if (!rows.length) {
|
||||||
assert.equal(count, 100001)
|
assert.equal(count, 100001)
|
||||||
@ -106,14 +105,14 @@ describe('cursor', function () {
|
|||||||
read()
|
read()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('normalizes parameter values', function (done) {
|
it('normalizes parameter values', function(done) {
|
||||||
var text = 'SELECT $1::json me'
|
const text = 'SELECT $1::json me'
|
||||||
var values = [{ name: 'brian' }]
|
const values = [{ name: 'brian' }]
|
||||||
var cursor = this.pgCursor(text, values)
|
const cursor = this.pgCursor(text, values)
|
||||||
cursor.read(1, function (err, rows) {
|
cursor.read(1, function(err, rows) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
assert.equal(rows[0].me.name, 'brian')
|
assert.equal(rows[0].me.name, 'brian')
|
||||||
cursor.read(1, function (err, rows) {
|
cursor.read(1, function(err, rows) {
|
||||||
assert(!err)
|
assert(!err)
|
||||||
assert.equal(rows.length, 0)
|
assert.equal(rows.length, 0)
|
||||||
done()
|
done()
|
||||||
@ -121,9 +120,9 @@ describe('cursor', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns result along with rows', function (done) {
|
it('returns result along with rows', function(done) {
|
||||||
var cursor = this.pgCursor(text)
|
const cursor = this.pgCursor(text)
|
||||||
cursor.read(1, function (err, rows, result) {
|
cursor.read(1, function(err, rows, result) {
|
||||||
assert.ifError(err)
|
assert.ifError(err)
|
||||||
assert.equal(rows.length, 1)
|
assert.equal(rows.length, 1)
|
||||||
assert.strictEqual(rows, result.rows)
|
assert.strictEqual(rows, result.rows)
|
||||||
@ -132,20 +131,20 @@ describe('cursor', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('emits row events', function (done) {
|
it('emits row events', function(done) {
|
||||||
var cursor = this.pgCursor(text)
|
const cursor = this.pgCursor(text)
|
||||||
cursor.read(10)
|
cursor.read(10)
|
||||||
cursor.on('row', (row, result) => result.addRow(row))
|
cursor.on('row', (row, result) => result.addRow(row))
|
||||||
cursor.on('end', (result) => {
|
cursor.on('end', result => {
|
||||||
assert.equal(result.rows.length, 6)
|
assert.equal(result.rows.length, 6)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('emits row events when cursor is closed manually', function (done) {
|
it('emits row events when cursor is closed manually', function(done) {
|
||||||
var cursor = this.pgCursor(text)
|
const cursor = this.pgCursor(text)
|
||||||
cursor.on('row', (row, result) => result.addRow(row))
|
cursor.on('row', (row, result) => result.addRow(row))
|
||||||
cursor.on('end', (result) => {
|
cursor.on('end', result => {
|
||||||
assert.equal(result.rows.length, 3)
|
assert.equal(result.rows.length, 3)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
@ -153,25 +152,27 @@ describe('cursor', function () {
|
|||||||
cursor.read(3, () => cursor.close())
|
cursor.read(3, () => cursor.close())
|
||||||
})
|
})
|
||||||
|
|
||||||
it('emits error events', function (done) {
|
it('emits error events', function(done) {
|
||||||
var cursor = this.pgCursor('select asdfasdf')
|
const cursor = this.pgCursor('select asdfasdf')
|
||||||
cursor.on('error', function (err) {
|
cursor.on('error', function(err) {
|
||||||
assert(err)
|
assert(err)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns rowCount on insert', function (done) {
|
it('returns rowCount on insert', function(done) {
|
||||||
var pgCursor = this.pgCursor
|
const pgCursor = this.pgCursor
|
||||||
this.client.query('CREATE TEMPORARY TABLE pg_cursor_test (foo VARCHAR(1), bar VARCHAR(1))')
|
this.client
|
||||||
.then(function () {
|
.query('CREATE TEMPORARY TABLE pg_cursor_test (foo VARCHAR(1), bar VARCHAR(1))')
|
||||||
var cursor = pgCursor('insert into pg_cursor_test values($1, $2)', ['a', 'b'])
|
.then(function() {
|
||||||
cursor.read(1, function (err, rows, result) {
|
const cursor = pgCursor('insert into pg_cursor_test values($1, $2)', ['a', 'b'])
|
||||||
|
cursor.read(1, function(err, rows, result) {
|
||||||
assert.ifError(err)
|
assert.ifError(err)
|
||||||
assert.equal(rows.length, 0)
|
assert.equal(rows.length, 0)
|
||||||
assert.equal(result.rowCount, 1)
|
assert.equal(result.rowCount, 1)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
}).catch(done)
|
})
|
||||||
|
.catch(done)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,31 +1,31 @@
|
|||||||
var assert = require('assert')
|
const assert = require('assert')
|
||||||
var pg = require('pg')
|
const pg = require('pg')
|
||||||
var Cursor = require('../')
|
const Cursor = require('../')
|
||||||
|
|
||||||
describe('queries with no data', function () {
|
describe('queries with no data', function() {
|
||||||
beforeEach(function (done) {
|
beforeEach(function(done) {
|
||||||
var client = this.client = new pg.Client()
|
const client = (this.client = new pg.Client())
|
||||||
client.connect(done)
|
client.connect(done)
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function() {
|
||||||
this.client.end()
|
this.client.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('handles queries that return no data', function (done) {
|
it('handles queries that return no data', function(done) {
|
||||||
var cursor = new Cursor('CREATE TEMPORARY TABLE whatwhat (thing int)')
|
const cursor = new Cursor('CREATE TEMPORARY TABLE whatwhat (thing int)')
|
||||||
this.client.query(cursor)
|
this.client.query(cursor)
|
||||||
cursor.read(100, function (err, rows) {
|
cursor.read(100, function(err, rows) {
|
||||||
assert.ifError(err)
|
assert.ifError(err)
|
||||||
assert.equal(rows.length, 0)
|
assert.equal(rows.length, 0)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('handles empty query', function (done) {
|
it('handles empty query', function(done) {
|
||||||
var cursor = new Cursor('-- this is a comment')
|
let cursor = new Cursor('-- this is a comment')
|
||||||
cursor = this.client.query(cursor)
|
cursor = this.client.query(cursor)
|
||||||
cursor.read(100, function (err, rows) {
|
cursor.read(100, function(err, rows) {
|
||||||
assert.ifError(err)
|
assert.ifError(err)
|
||||||
assert.equal(rows.length, 0)
|
assert.equal(rows.length, 0)
|
||||||
done()
|
done()
|
||||||
|
|||||||
20
test/pool.js
20
test/pool.js
@ -5,7 +5,7 @@ const pg = require('pg')
|
|||||||
|
|
||||||
const text = 'SELECT generate_series as num FROM generate_series(0, 50)'
|
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) => {
|
return new Promise((resolve, reject) => {
|
||||||
pool.connect((err, client, done) => {
|
pool.connect((err, client, done) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@ -13,7 +13,7 @@ function poolQueryPromise (pool, readRowCount) {
|
|||||||
return reject(err)
|
return reject(err)
|
||||||
}
|
}
|
||||||
const cursor = client.query(new Cursor(text))
|
const cursor = client.query(new Cursor(text))
|
||||||
cursor.read(readRowCount, (err, res) => {
|
cursor.read(readRowCount, err => {
|
||||||
if (err) {
|
if (err) {
|
||||||
done(err)
|
done(err)
|
||||||
return reject(err)
|
return reject(err)
|
||||||
@ -31,16 +31,16 @@ function poolQueryPromise (pool, readRowCount) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('pool', function () {
|
describe('pool', function() {
|
||||||
beforeEach(function () {
|
beforeEach(function() {
|
||||||
this.pool = new pg.Pool({max: 1})
|
this.pool = new pg.Pool({ max: 1 })
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function() {
|
||||||
this.pool.end()
|
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)
|
poolQueryPromise(this.pool, 25)
|
||||||
.then(() => done())
|
.then(() => done())
|
||||||
.catch(err => {
|
.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 = []
|
const promises = []
|
||||||
for (let i = 0; i < 10; i++) {
|
for (let i = 0; i < 10; i++) {
|
||||||
promises.push(poolQueryPromise(this.pool, 25))
|
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)
|
poolQueryPromise(this.pool, 100)
|
||||||
.then(() => done())
|
.then(() => done())
|
||||||
.catch(err => {
|
.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 = []
|
const promises = []
|
||||||
for (let i = 0; i < 10; i++) {
|
for (let i = 0; i < 10; i++) {
|
||||||
promises.push(poolQueryPromise(this.pool, 100))
|
promises.push(poolQueryPromise(this.pool, 100))
|
||||||
|
|||||||
@ -4,7 +4,7 @@ const Cursor = require('../')
|
|||||||
const pg = require('pg')
|
const pg = require('pg')
|
||||||
|
|
||||||
describe('query config passed to result', () => {
|
describe('query config passed to result', () => {
|
||||||
it('passes rowMode to result', (done) => {
|
it('passes rowMode to result', done => {
|
||||||
const client = new pg.Client()
|
const client = new pg.Client()
|
||||||
client.connect()
|
client.connect()
|
||||||
const text = 'SELECT generate_series as num FROM generate_series(0, 5)'
|
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()
|
const client = new pg.Client()
|
||||||
client.connect()
|
client.connect()
|
||||||
const text = 'SELECT generate_series as num FROM generate_series(0, 2)'
|
const text = 'SELECT generate_series as num FROM generate_series(0, 2)'
|
||||||
const types = {
|
const types = {
|
||||||
getTypeParser: () => () => 'foo'
|
getTypeParser: () => () => 'foo',
|
||||||
}
|
}
|
||||||
const cursor = client.query(new Cursor(text, null, { types }))
|
const cursor = client.query(new Cursor(text, null, { types }))
|
||||||
cursor.read(10, (err, rows) => {
|
cursor.read(10, (err, rows) => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user