* Process rows received from the DB. * Use parse function to parse the returned value * to sequelize format * * @param {Array} rows * @private
(rows)
| 294 | * @private |
| 295 | */ |
| 296 | _processRows(rows) { |
| 297 | let result = rows; |
| 298 | let attrsMap = {}; |
| 299 | |
| 300 | // When quoteIdentifiers is false we need to map the DB column names |
| 301 | // To the one in attribute list |
| 302 | if (this.sequelize.options.quoteIdentifiers === false) { |
| 303 | // Building the attribute map from this.options.attributes |
| 304 | // Needed in case of an aggregate function |
| 305 | attrsMap = _.reduce(this.options.attributes, (mp, v) => { |
| 306 | // Aggregate function is of form |
| 307 | // Fn {fn: 'min', min}, so we have the name in index one of the object |
| 308 | if (typeof v === 'object') { |
| 309 | v = v[1]; |
| 310 | } |
| 311 | const catalogv = this.sequelize.queryInterface.queryGenerator.getCatalogName(v); |
| 312 | mp[catalogv] = v; |
| 313 | return mp; |
| 314 | }, {}); |
| 315 | |
| 316 | |
| 317 | // Building the attribute map by matching the column names received |
| 318 | // from DB and the one in model.rawAttributes |
| 319 | if (this.model) { |
| 320 | this._getAttributeMap(attrsMap, this.model.rawAttributes); |
| 321 | } |
| 322 | |
| 323 | // If aliasesmapping exists we update the attribute map |
| 324 | if (this.options.aliasesMapping) { |
| 325 | const obj = Object.fromEntries(this.options.aliasesMapping); |
| 326 | rows = rows |
| 327 | .map(row => _.toPairs(row) |
| 328 | .reduce((acc, [key, value]) => { |
| 329 | const mapping = Object.values(obj).find(element => { |
| 330 | const catalogElement = this.sequelize.queryInterface.queryGenerator.getCatalogName(element); |
| 331 | return catalogElement === key; |
| 332 | }); |
| 333 | if (mapping) |
| 334 | acc[mapping || key] = value; |
| 335 | return acc; |
| 336 | }, {}) |
| 337 | ); |
| 338 | } |
| 339 | |
| 340 | // Modify the keys into the format that sequelize expects |
| 341 | result = rows.map(row => { |
| 342 | return _.mapKeys(row, (value, key) => { |
| 343 | const targetAttr = attrsMap[key]; |
| 344 | if (typeof targetAttr === 'string' && targetAttr !== key) { |
| 345 | return targetAttr; |
| 346 | } |
| 347 | return key; |
| 348 | }); |
| 349 | }); |
| 350 | } |
| 351 | |
| 352 | // We parse the value received from the DB based on its datatype |
| 353 | if (this.model) { |
no test coverage detected