Skip to content

Commit f63b23b

Browse files
committed
fix(pg): use a Buffer when parsing binary
The call to parseArray was not working as expected because the value was being sent as a string instead of a Buffer. The binary parsers in pg-types all assume the incoming value is a Buffer.
1 parent da19ba1 commit f63b23b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

packages/pg/lib/result.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class Result {
6666
const rawValue = rowData[i]
6767
const field = this.fields[i].name
6868
if (rawValue !== null) {
69-
row[field] = this._parsers[i](rawValue)
69+
const v = this.fields[i].format === 'binary' ? Buffer.from(rawValue) : rawValue
70+
row[field] = this._parsers[i](v)
7071
} else {
7172
row[field] = null
7273
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const helper = require('../test-helper')
2+
const assert = require('assert')
3+
4+
const suite = new helper.Suite()
5+
6+
suite.testAsync('allows you to switch between format modes for arrays', async () => {
7+
const client = new helper.pg.Client()
8+
await client.connect()
9+
10+
const r1 = await client.query({
11+
text: 'SELECT CAST($1 AS INT[]) as a',
12+
values: [[1, 2, 8]],
13+
binary: false,
14+
})
15+
assert.deepEqual([1, 2, 8], r1.rows[0].a)
16+
17+
const r2 = await client.query({
18+
text: 'SELECT CAST($1 AS INT[]) as a',
19+
values: [[4, 5, 6]],
20+
binary: true,
21+
})
22+
assert.deepEqual([4, 5, 6], r2.rows[0].a)
23+
24+
await client.end()
25+
})

0 commit comments

Comments
 (0)