get fields at the start of the stream
gajus opened this issue · 2 comments
gajus commented
This is how I currently consume the stream:
const query = new QueryStream('SELECT * FROM foo');
const queryStream = connect.query(query);
queryStream.on('data', (row) => {
console.log(row);
});
However, is there a way to get the field description at the beginning of the stream?
Something similar to what pg provides for query results, i.e.
type FieldType = {|
+columnID: number,
+dataTypeID: number,
+dataTypeModifier: number,
+dataTypeSize: number,
+format: string,
+name: string,
+tableID: number
|};
gajus commented
Having looked at the source code, pg-query-stream
is a wrapper around pg-cursor
. Meanwhile, pg-cursor
does have access to result object.
It could be implemented simply as:
_read (size) {
if (this._reading || this._closed) {
return false
}
this._reading = true
const readAmount = Math.max(size, this.batchSize)
- this.cursor.read(readAmount, (err, rows) => {
+ this.cursor.read(readAmount, (err, rows, result) => {
if (this._closed) {
return
}
if (err) {
return this.emit('error', err)
}
// if we get a 0 length array we've read to the end of the cursor
if (!rows.length) {
this._closed = true
setImmediate(() => this.emit('close'))
return this.push(null)
}
// push each row into the stream
this._reading = false
for (var i = 0; i < rows.length; i++) {
- this.push(rows[i])
+ this.push({row: rows[i], fields: result.fields})
}
})
}
gajus commented
As a quick workaround, I have inlined pg-query-stream.
This is the solution that I have used:
As suggested in this issue, each date event emits row and fields data.