diff --git a/index.js b/index.js index b01d43fd..2c33a230 100644 --- a/index.js +++ b/index.js @@ -4,15 +4,16 @@ const prepare = require('./pg').prepareValue const EventEmitter = require('events').EventEmitter const util = require('util') -function Cursor (text, values) { +function Cursor (text, values, config) { EventEmitter.call(this) + this._conf = config || { } this.text = text this.values = values ? values.map(prepare) : null this.connection = null this._queue = [] this.state = 'initialized' - this._result = new Result() + this._result = new Result(this._conf.rowMode) this._cb = null this._rows = null } @@ -44,8 +45,12 @@ Cursor.prototype.submit = function (connection) { this._shiftQueue() } + if (this._conf.types) { + this._result._getTypeParser = this._conf.types.getTypeParser + } + con.once('noData', ifNoData) - con.once('rowDescription', function () { + con.once('rowDescription', () => { con.removeListener('noData', ifNoData) }) } @@ -139,7 +144,6 @@ Cursor.prototype.end = function (cb) { this.connection.sync() } this.connection.stream.once('end', cb) - console.log('calling end on connection') this.connection.end() } diff --git a/package.json b/package.json index 885aec1f..ea99f0f8 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "eslint-plugin-promise": "^3.5.0", "eslint-plugin-standard": "^3.0.1", "mocha": "^3.5.0", - "pg": "~6.0.0" + "pg": "6.x" }, "dependencies": {} } diff --git a/test/query-config.js b/test/query-config.js new file mode 100644 index 00000000..3321f94e --- /dev/null +++ b/test/query-config.js @@ -0,0 +1,36 @@ +'use strict' +const assert = require('assert') +const Cursor = require('../') +const pg = require('pg') + +describe('query config passed to result', () => { + 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)' + const cursor = client.query(new Cursor(text, null, { rowMode: 'array' })) + cursor.read(10, (err, rows) => { + assert(!err) + assert.deepEqual(rows, [[0], [1], [2], [3], [4], [5]]) + client.end() + 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' + } + const cursor = client.query(new Cursor(text, null, { types })) + cursor.read(10, (err, rows) => { + assert(!err) + assert.deepEqual(rows, [{ num: 'foo' }, { num: 'foo' }, { num: 'foo' }]) + client.end() + done() + }) + + }) +})