Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for per per-query types #1825

Merged
merged 1 commit into from Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/client.js
Expand Up @@ -455,8 +455,9 @@ Client.prototype.query = function (config, values, callback) {
if (this.binary && !query.binary) {
query.binary = true
}
if (query._result) {
query._result._getTypeParser = this._types.getTypeParser.bind(this._types)

if (query._result && !query._result._types) {
query._result._types = this._types
}

if (!this._queryable) {
Expand Down
7 changes: 3 additions & 4 deletions lib/result.js
Expand Up @@ -12,13 +12,14 @@ var types = require('pg-types')
// result object returned from query
// in the 'end' event and also
// passed as second argument to provided callback
var Result = function (rowMode) {
var Result = function (rowMode, types) {
this.command = null
this.rowCount = null
this.oid = null
this.rows = []
this.fields = []
this._parsers = []
this._types = types
this.RowCtor = null
this.rowAsArray = rowMode === 'array'
if (this.rowAsArray) {
Expand Down Expand Up @@ -94,11 +95,9 @@ Result.prototype.addFields = function (fieldDescriptions) {
for (var i = 0; i < fieldDescriptions.length; i++) {
var desc = fieldDescriptions[i]
this.fields.push(desc)
var parser = this._getTypeParser(desc.dataTypeID, desc.format || 'text')
var parser = (this._types || types).getTypeParser(desc.dataTypeID, desc.format || 'text')
this._parsers.push(parser)
}
}

Result.prototype._getTypeParser = types.getTypeParser

module.exports = Result
28 changes: 23 additions & 5 deletions test/integration/client/custom-types-tests.js
Expand Up @@ -3,13 +3,13 @@ const helper = require('./test-helper')
const Client = helper.pg.Client
const suite = new helper.Suite()

const client = new Client({
types: {
getTypeParser: () => () => 'okay!'
}
})
const customTypes = {
getTypeParser: () => () => 'okay!'
}

suite.test('custom type parser in client config', (done) => {
const client = new Client({ types: customTypes })

client.connect()
.then(() => {
client.query('SELECT NOW() as val', assert.success(function (res) {
Expand All @@ -18,3 +18,21 @@ suite.test('custom type parser in client config', (done) => {
}))
})
})

// Custom type-parsers per query are not supported in native
if (!helper.args.native) {
suite.test('custom type parser in query', (done) => {
const client = new Client()

client.connect()
.then(() => {
client.query({
text: 'SELECT NOW() as val',
types: customTypes
}, assert.success(function (res) {
assert.equal(res.rows[0].val, 'okay!')
client.end().then(done)
}))
})
})
}