Skip to content

Commit

Permalink
Option to use pre-shaped result rows; fixes #3042 (#3043)
Browse files Browse the repository at this point in the history
* Add property usePrebuiltEmptyResultObjects to Query constructor which generates pre-shaped result rows

* Remove option and test for prebuiltEmptyResultObject

* Remove errorneously added newline

* Move all logic for prebuilding objects to Result

* Move prebuilding to addFields

* Use a clone as clone-base

---------

Co-authored-by: HZ111 / Dev2 <hz111@wielick.nl>
  • Loading branch information
koenfaro90 and HZ111 / Dev2 authored Aug 15, 2023
1 parent 58865b2 commit b5c5e52
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/pg/lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Result {
if (this.rowAsArray) {
this.parseRow = this._parseRowAsArray
}
this._prebuiltEmptyResultObject = null
}

// adds a command complete message
Expand Down Expand Up @@ -60,14 +61,12 @@ class Result {
}

parseRow(rowData) {
var row = {}
var row = { ... this._prebuiltEmptyResultObject }
for (var i = 0, len = rowData.length; i < len; i++) {
var rawValue = rowData[i]
var field = this.fields[i].name
if (rawValue !== null) {
row[field] = this._parsers[i](rawValue)
} else {
row[field] = null
}
}
return row
Expand All @@ -94,6 +93,14 @@ class Result {
this._parsers[i] = types.getTypeParser(desc.dataTypeID, desc.format || 'text')
}
}
this._createPrebuiltEmptyResultObject()
}
_createPrebuiltEmptyResultObject() {
var row = {}
for (var i = 0; i < this.fields.length; i++) {
row[this.fields[i].name] = null
}
this._prebuiltEmptyResultObject = { ... row }
}
}

Expand Down

0 comments on commit b5c5e52

Please sign in to comment.