MCPcopy
hub / github.com/sequelize/sequelize / formatResults

Method formatResults

lib/dialects/snowflake/query.js:98–196  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

96 * @private
97 */
98 formatResults(data) {
99 let result = this.instance;
100
101 if (this.isInsertQuery(data)) {
102 this.handleInsertQuery(data);
103
104 if (!this.instance) {
105 // handle bulkCreate AI primary key
106 if (
107 data.constructor.name === 'ResultSetHeader'
108 && this.model
109 && this.model.autoIncrementAttribute
110 && this.model.autoIncrementAttribute === this.model.primaryKeyAttribute
111 && this.model.rawAttributes[this.model.primaryKeyAttribute]
112 ) {
113 const startId = data[this.getInsertIdField()];
114 result = [];
115 for (let i = startId; i < startId + data.affectedRows; i++) {
116 result.push({ [this.model.rawAttributes[this.model.primaryKeyAttribute].field]: i });
117 }
118 } else {
119 result = data[this.getInsertIdField()];
120 }
121 }
122 }
123
124 if (this.isSelectQuery()) {
125 // Snowflake will treat tables as case-insensitive, so fix the case
126 // of the returned values to match attributes
127 if (this.options.raw === false && this.sequelize.options.quoteIdentifiers === false) {
128 const sfAttrMap = _.reduce(this.model.rawAttributes, (m, v, k) => {
129 m[k.toUpperCase()] = k;
130 return m;
131 }, {});
132
133 data = data.map(data => _.reduce(data, (prev, value, key) => {
134 if ( value !== undefined && sfAttrMap[key] ) {
135 prev[sfAttrMap[key]] = value;
136 delete prev[key];
137 }
138 return prev;
139 }, data));
140 }
141
142 this.options.fieldMap = _.mapKeys(this.options.fieldMap, (v, k) => { return k.toUpperCase(); });
143
144 return this.handleSelectQuery(data);
145 }
146
147 if (this.isShowTablesQuery()) {
148 return this.handleShowTablesQuery(data);
149 }
150
151 if (this.isDescribeQuery()) {
152 result = {};
153
154 for (const _result of data) {
155 result[_result.Field] = {

Callers 1

runMethod · 0.95

Calls 15

handleInsertQueryMethod · 0.95
getInsertIdFieldMethod · 0.95
handleShowTablesQueryMethod · 0.95
isShowIndexesQueryMethod · 0.95
isForeignKeysQueryMethod · 0.95
isInsertQueryMethod · 0.80
isSelectQueryMethod · 0.80
handleSelectQueryMethod · 0.80
isShowTablesQueryMethod · 0.80
isDescribeQueryMethod · 0.80
isCallQueryMethod · 0.80

Tested by

no test coverage detected