Skip to content

Commit 4ff97f5

Browse files
committed
Add support for rowMode & custom types
1 parent 5b4bb7b commit 4ff97f5

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

index.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ const prepare = require('./pg').prepareValue
44
const EventEmitter = require('events').EventEmitter
55
const util = require('util')
66

7-
function Cursor (text, values) {
7+
function Cursor (text, values, config) {
88
EventEmitter.call(this)
99

10+
this._conf = config || { }
1011
this.text = text
1112
this.values = values ? values.map(prepare) : null
1213
this.connection = null
1314
this._queue = []
1415
this.state = 'initialized'
15-
this._result = new Result()
16+
this._result = new Result(this._conf.rowMode)
1617
this._cb = null
1718
this._rows = null
1819
}
@@ -44,8 +45,12 @@ Cursor.prototype.submit = function (connection) {
4445
this._shiftQueue()
4546
}
4647

48+
if (this._conf.types) {
49+
this._result._getTypeParser = this._conf.types.getTypeParser
50+
}
51+
4752
con.once('noData', ifNoData)
48-
con.once('rowDescription', function () {
53+
con.once('rowDescription', () => {
4954
con.removeListener('noData', ifNoData)
5055
})
5156
}
@@ -139,7 +144,6 @@ Cursor.prototype.end = function (cb) {
139144
this.connection.sync()
140145
}
141146
this.connection.stream.once('end', cb)
142-
console.log('calling end on connection')
143147
this.connection.end()
144148
}
145149

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"eslint-plugin-promise": "^3.5.0",
2020
"eslint-plugin-standard": "^3.0.1",
2121
"mocha": "^3.5.0",
22-
"pg": "~6.0.0"
22+
"pg": "6.x"
2323
},
2424
"dependencies": {}
2525
}

test/query-config.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict'
2+
const assert = require('assert')
3+
const Cursor = require('../')
4+
const pg = require('pg')
5+
6+
describe('query config passed to result', () => {
7+
it('passes rowMode to result', (done) => {
8+
const client = new pg.Client()
9+
client.connect()
10+
const text = 'SELECT generate_series as num FROM generate_series(0, 5)'
11+
const cursor = client.query(new Cursor(text, null, { rowMode: 'array' }))
12+
cursor.read(10, (err, rows) => {
13+
assert(!err)
14+
assert.deepEqual(rows, [[0], [1], [2], [3], [4], [5]])
15+
client.end()
16+
done()
17+
})
18+
})
19+
20+
it('passes types to result', (done) => {
21+
const client = new pg.Client()
22+
client.connect()
23+
const text = 'SELECT generate_series as num FROM generate_series(0, 2)'
24+
const types = {
25+
getTypeParser: () => () => 'foo'
26+
}
27+
const cursor = client.query(new Cursor(text, null, { types }))
28+
cursor.read(10, (err, rows) => {
29+
assert(!err)
30+
assert.deepEqual(rows, [{ num: 'foo' }, { num: 'foo' }, { num: 'foo' }])
31+
client.end()
32+
done()
33+
})
34+
35+
})
36+
})

0 commit comments

Comments
 (0)