* High level function that handles the results of a query execution. * * * Example: * query.formatResults([ * { * id: 1, // this is from the main table * attr2: 'snafu', // this is from the main table * Tasks.id: 1, // this is from t
(data)
| 104 | * @private |
| 105 | */ |
| 106 | formatResults(data) { |
| 107 | let result = this.instance; |
| 108 | |
| 109 | if (this.isInsertQuery(data)) { |
| 110 | this.handleInsertQuery(data); |
| 111 | |
| 112 | if (!this.instance) { |
| 113 | // handle bulkCreate AI primary key |
| 114 | if ( |
| 115 | data.constructor.name === 'ResultSetHeader' |
| 116 | && this.model |
| 117 | && this.model.autoIncrementAttribute |
| 118 | && this.model.autoIncrementAttribute === this.model.primaryKeyAttribute |
| 119 | && this.model.rawAttributes[this.model.primaryKeyAttribute] |
| 120 | ) { |
| 121 | const startId = data[this.getInsertIdField()]; |
| 122 | result = []; |
| 123 | for (let i = startId; i < startId + data.affectedRows; i++) { |
| 124 | result.push({ [this.model.rawAttributes[this.model.primaryKeyAttribute].field]: i }); |
| 125 | } |
| 126 | } else { |
| 127 | result = data[this.getInsertIdField()]; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (this.isSelectQuery()) { |
| 133 | return this.handleSelectQuery(data); |
| 134 | } |
| 135 | if (this.isShowTablesQuery()) { |
| 136 | return this.handleShowTablesQuery(data); |
| 137 | } |
| 138 | if (this.isDescribeQuery()) { |
| 139 | result = {}; |
| 140 | |
| 141 | for (const _result of data) { |
| 142 | const enumRegex = /^enum/i; |
| 143 | result[_result.Field] = { |
| 144 | type: enumRegex.test(_result.Type) ? _result.Type.replace(enumRegex, 'ENUM') : _result.Type.toUpperCase(), |
| 145 | allowNull: _result.Null === 'YES', |
| 146 | defaultValue: _result.Default, |
| 147 | primaryKey: _result.Key === 'PRI', |
| 148 | autoIncrement: Object.prototype.hasOwnProperty.call(_result, 'Extra') |
| 149 | && _result.Extra.toLowerCase() === 'auto_increment', |
| 150 | comment: _result.Comment ? _result.Comment : null |
| 151 | }; |
| 152 | } |
| 153 | return result; |
| 154 | } |
| 155 | if (this.isShowIndexesQuery()) { |
| 156 | return this.handleShowIndexesQuery(data); |
| 157 | } |
| 158 | if (this.isCallQuery()) { |
| 159 | return data[0]; |
| 160 | } |
| 161 | if (this.isBulkUpdateQuery() || this.isBulkDeleteQuery()) { |
| 162 | return data.affectedRows; |
| 163 | } |
no test coverage detected