From b5c5e52aa06da916454f693944fedeb7cc3e9a18 Mon Sep 17 00:00:00 2001 From: Koen Date: Tue, 15 Aug 2023 17:42:54 +0200 Subject: [PATCH] Option to use pre-shaped result rows; fixes #3042 (#3043) * 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 --- packages/pg/lib/result.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/pg/lib/result.js b/packages/pg/lib/result.js index 350609743..187c0d016 100644 --- a/packages/pg/lib/result.js +++ b/packages/pg/lib/result.js @@ -21,6 +21,7 @@ class Result { if (this.rowAsArray) { this.parseRow = this._parseRowAsArray } + this._prebuiltEmptyResultObject = null } // adds a command complete message @@ -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 @@ -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 } } }