Skip to content
This repository has been archived by the owner on Dec 27, 2019. It is now read-only.

get fields at the start of the stream #51

Open
gajus opened this issue Mar 1, 2019 · 2 comments
Open

get fields at the start of the stream #51

gajus opened this issue Mar 1, 2019 · 2 comments

Comments

@gajus
Copy link

gajus commented Mar 1, 2019

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
Copy link
Author

gajus commented Mar 1, 2019

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
Copy link
Author

gajus commented Mar 1, 2019

As a quick workaround, I have inlined pg-query-stream.

This is the solution that I have used:

gajus/slonik@fce4d22

As suggested in this issue, each date event emits row and fields data.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant