Skip to content

Commit 194e1fa

Browse files
committed
add valuemapper to bind method
1 parent fb83255 commit 194e1fa

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

packages/pg-batch-query/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { Submittable, Connection, QueryResult } from 'pg'
22
const Result = require('pg/lib/result.js')
3-
const EventEmitter = require('events').EventEmitter
4-
3+
const utils = require('pg/lib/utils.js')
54
let nextUniqueID = 1 // concept borrowed from org.postgresql.core.v3.QueryExecutorImpl
65

76
interface BatchQueryConfig {
87
name?: string
98
text: string
10-
values?: string[][]
9+
values?: any[][]
1110
}
1211

1312
class BatchQuery implements Submittable {
@@ -58,7 +57,8 @@ class BatchQuery implements Submittable {
5857
this.connection.bind({
5958
statement: this.name,
6059
values: val,
61-
portal: this._portal
60+
portal: this._portal,
61+
valueMapper: utils.prepareValue,
6262
}, true)
6363

6464
// maybe we could avoid this for non-select queries

packages/pg-batch-query/test/test-query.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'assert'
2-
import BatchQuery from '../'
2+
import BatchQuery from '../src'
33
import pg from 'pg'
44

55
describe('batch query', function () {
@@ -54,4 +54,20 @@ describe('batch query', function () {
5454
const resp = await this.client.query('SELECT SUM(value) from bar')
5555
assert.strictEqual(resp.rows[0]['sum'], '3')
5656
})
57+
58+
it('If query is for an array', async function() {
59+
await this.client.query('INSERT INTO foo (name) VALUES ($1)', ['first'])
60+
await this.client.query('INSERT INTO foo (name) VALUES ($1)', ['second'])
61+
const responses = await this.client.query(new BatchQuery({
62+
text: `SELECT * from foo where name = ANY($1)`,
63+
values: [
64+
[['first', 'third']],
65+
[['second', 'fourth']]
66+
],
67+
name: 'optional'
68+
})).execute()
69+
assert.equal(responses.length, 2)
70+
for ( const response of responses) {
71+
assert.strictEqual(response.rowCount, 1)
72+
}})
5773
})

0 commit comments

Comments
 (0)