* 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)
| 93 | * @private |
| 94 | */ |
| 95 | formatResults(data) { |
| 96 | let result = this.instance; |
| 97 | |
| 98 | if (this.isBulkUpdateQuery() || this.isBulkDeleteQuery()) { |
| 99 | return data.affectedRows; |
| 100 | } |
| 101 | if (this.isUpsertQuery()) { |
| 102 | return [result, data.affectedRows === 1]; |
| 103 | } |
| 104 | if (this.isInsertQuery(data)) { |
| 105 | this.handleInsertQuery(data); |
| 106 | |
| 107 | if (!this.instance) { |
| 108 | // handle bulkCreate AI primary key |
| 109 | if ( |
| 110 | this.model |
| 111 | && this.model.autoIncrementAttribute |
| 112 | && this.model.autoIncrementAttribute === this.model.primaryKeyAttribute |
| 113 | && this.model.rawAttributes[this.model.primaryKeyAttribute] |
| 114 | ) { |
| 115 | // ONLY TRUE IF @auto_increment_increment is set to 1 !! |
| 116 | // Doesn't work with GALERA => each node will reserve increment (x for first server, x+1 for next node...) |
| 117 | const startId = data[this.getInsertIdField()]; |
| 118 | result = new Array(data.affectedRows); |
| 119 | const pkField = this.model.rawAttributes[this.model.primaryKeyAttribute].field; |
| 120 | for (let i = 0; i < data.affectedRows; i++) { |
| 121 | result[i] = { [pkField]: startId + i }; |
| 122 | } |
| 123 | return [result, data.affectedRows]; |
| 124 | } |
| 125 | |
| 126 | return [data[this.getInsertIdField()], data.affectedRows]; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (this.isSelectQuery()) { |
| 131 | this.handleJsonSelectQuery(data); |
| 132 | return this.handleSelectQuery(data); |
| 133 | } |
| 134 | if (this.isInsertQuery() || this.isUpdateQuery()) { |
| 135 | return [result, data.affectedRows]; |
| 136 | } |
| 137 | if (this.isCallQuery()) { |
| 138 | return data[0]; |
| 139 | } |
| 140 | if (this.isRawQuery()) { |
| 141 | const meta = data.meta; |
| 142 | delete data.meta; |
| 143 | return [data, meta]; |
| 144 | } |
| 145 | if (this.isShowIndexesQuery()) { |
| 146 | return this.handleShowIndexesQuery(data); |
| 147 | } |
| 148 | if (this.isForeignKeysQuery() || this.isShowConstraintsQuery()) { |
| 149 | return data; |
| 150 | } |
| 151 | if (this.isShowTablesQuery()) { |
| 152 | return this.handleShowTablesQuery(data); |
no test coverage detected