mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
Add support for rowMode & custom types
This commit is contained in:
parent
5b4bb7b615
commit
4ff97f54bf
12
index.js
12
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()
|
||||
}
|
||||
|
||||
|
||||
@ -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": {}
|
||||
}
|
||||
|
||||
36
test/query-config.js
Normal file
36
test/query-config.js
Normal file
@ -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()
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user