Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/pg-query-stream/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class QueryStream extends Readable implements Submittable {
cursor: any
_result: any

callback: Function
handleRowDescription: Function
handleDataRow: Function
handlePortalSuspended: Function
Expand All @@ -26,6 +27,13 @@ class QueryStream extends Readable implements Submittable {

super({ objectMode: true, autoDestroy: true, highWaterMark: batchSize || highWaterMark })
this.cursor = new Cursor(text, values, config)
this.cursor
.on('end', (result) => {
this.callback && this.callback(null, result)
})
.on('error', (err) => {
this.callback && this.callback(err)
})

// delegate Submittable callbacks to cursor
this.handleRowDescription = this.cursor.handleRowDescription.bind(this.cursor)
Expand Down
17 changes: 17 additions & 0 deletions packages/pg-query-stream/test/pool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Pool } from 'pg'
import QueryStream from '../src'

describe('pool', function () {
it('works', async function () {
const pool = new Pool()
const query = new QueryStream('SELECT * FROM generate_series(0, 10) num', [])
const q = pool.query(query)
query.on('data', (row) => {
// just consume the whole stream
})
await q
query.on('end', () => {
pool.end()
})
})
})
8 changes: 6 additions & 2 deletions packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,12 @@ class Client extends EventEmitter {
} else if (typeof config.submit === 'function') {
readTimeout = config.query_timeout || this.connectionParameters.query_timeout
result = query = config
if (typeof values === 'function') {
query.callback = query.callback || values
if (!query.callback) {
if (typeof values === 'function') {
query.callback = values
} else if (callback) {
query.callback = callback
}
}
} else {
readTimeout = config.query_timeout || this.connectionParameters.query_timeout
Expand Down