Skip to content

Commit e585975

Browse files
committed
Add support for per per-query types
The documentation states that you can pass custom type processors to query objects. See: https://node-postgres.com/features/queries#types This didn't actually work. This commit adds an initial implementation of per-query type-parsing. Caveats: * It does not work with pg-native. That would require a separate pull request to pg-native, and a rather significant change to how that library handles query results. * Per-query types do not "inherit" from types assigned to the Client, ala TypeOverrides.
1 parent fcd0f02 commit e585975

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

lib/client.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,9 @@ Client.prototype.query = function (config, values, callback) {
455455
if (this.binary && !query.binary) {
456456
query.binary = true
457457
}
458-
if (query._result) {
459-
query._result._getTypeParser = this._types.getTypeParser.bind(this._types)
458+
459+
if (query._result && !query._result._types) {
460+
query._result._types = this._types
460461
}
461462

462463
if (!this._queryable) {

lib/result.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ var types = require('pg-types')
1212
// result object returned from query
1313
// in the 'end' event and also
1414
// passed as second argument to provided callback
15-
var Result = function (rowMode) {
15+
var Result = function (rowMode, types) {
1616
this.command = null
1717
this.rowCount = null
1818
this.oid = null
1919
this.rows = []
2020
this.fields = []
2121
this._parsers = []
22+
this._types = types
2223
this.RowCtor = null
2324
this.rowAsArray = rowMode === 'array'
2425
if (this.rowAsArray) {
@@ -94,11 +95,9 @@ Result.prototype.addFields = function (fieldDescriptions) {
9495
for (var i = 0; i < fieldDescriptions.length; i++) {
9596
var desc = fieldDescriptions[i]
9697
this.fields.push(desc)
97-
var parser = this._getTypeParser(desc.dataTypeID, desc.format || 'text')
98+
var parser = (this._types || types).getTypeParser(desc.dataTypeID, desc.format || 'text')
9899
this._parsers.push(parser)
99100
}
100101
}
101102

102-
Result.prototype._getTypeParser = types.getTypeParser
103-
104103
module.exports = Result

test/integration/client/custom-types-tests.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ const helper = require('./test-helper')
33
const Client = helper.pg.Client
44
const suite = new helper.Suite()
55

6-
const client = new Client({
7-
types: {
8-
getTypeParser: () => () => 'okay!'
9-
}
10-
})
6+
const customTypes = {
7+
getTypeParser: () => () => 'okay!'
8+
}
119

1210
suite.test('custom type parser in client config', (done) => {
11+
const client = new Client({ types: customTypes })
12+
1313
client.connect()
1414
.then(() => {
1515
client.query('SELECT NOW() as val', assert.success(function (res) {
@@ -18,3 +18,21 @@ suite.test('custom type parser in client config', (done) => {
1818
}))
1919
})
2020
})
21+
22+
// Custom type-parsers per query are not supported in native
23+
if (!helper.args.native) {
24+
suite.test('custom type parser in query', (done) => {
25+
const client = new Client()
26+
27+
client.connect()
28+
.then(() => {
29+
client.query({
30+
text: 'SELECT NOW() as val',
31+
types: customTypes
32+
}, assert.success(function (res) {
33+
assert.equal(res.rows[0].val, 'okay!')
34+
client.end().then(done)
35+
}))
36+
})
37+
})
38+
}

0 commit comments

Comments
 (0)