MCPcopy
hub / github.com/sequelize/sequelize / getQueryOrders

Method getQueryOrders

src/dialects/abstract/query-generator.js:2220–2290  ·  view source on GitHub ↗
(options, model, subQuery)

Source from the content-addressed store, hash-verified

2218 }
2219
2220 getQueryOrders(options, model, subQuery) {
2221 const mainQueryOrder = [];
2222 const subQueryOrder = [];
2223
2224 if (Array.isArray(options.order)) {
2225 for (let order of options.order) {
2226
2227 // wrap if not array
2228 if (!Array.isArray(order)) {
2229 order = [order];
2230 }
2231
2232 if (
2233 subQuery
2234 && Array.isArray(order)
2235 && order[0]
2236 && !(order[0] instanceof Association)
2237 && !(typeof order[0] === 'function' && order[0].prototype instanceof Model)
2238 && !(typeof order[0].model === 'function' && order[0].model.prototype instanceof Model)
2239 && !(typeof order[0] === 'string' && model && model.associations !== undefined && model.associations[order[0]])
2240 ) {
2241 const field = model.rawAttributes[order[0]] ? model.rawAttributes[order[0]].field : order[0];
2242 const subQueryAlias = this._getAliasForField(this.quoteIdentifier(model.name), field, options);
2243
2244 let parent = null;
2245 let orderToQuote = [];
2246
2247 // we need to ensure that the parent is null if we use the subquery alias, else we'll get an exception since
2248 // "model_name"."alias" doesn't exist - only "alias" does. we also need to ensure that we preserve order direction
2249 // by pushing order[1] to the subQueryOrder as well - in case it doesn't exist, we want to push "ASC"
2250 if (subQueryAlias === null) {
2251 orderToQuote = order;
2252 parent = model;
2253 } else {
2254 orderToQuote = [subQueryAlias, order.length > 1 ? order[1] : 'ASC'];
2255 parent = null;
2256 }
2257
2258 subQueryOrder.push(this.quote(orderToQuote, parent, '->'));
2259 }
2260
2261 // Handle case where renamed attributes are used to order by,
2262 // see https://github.com/sequelize/sequelize/issues/8739
2263 // need to check if either of the attribute options match the order
2264 if (options.attributes && model) {
2265 const aliasedAttribute = options.attributes.find(attr => Array.isArray(attr)
2266 && attr[1]
2267 && (attr[0] === order[0] || attr[1] === order[0]));
2268
2269 if (aliasedAttribute) {
2270 const modelName = this.quoteIdentifier(model.name);
2271 const alias = this._getAliasForField(modelName, aliasedAttribute[1], options);
2272
2273 order[0] = new Utils.Col(alias || aliasedAttribute[1]);
2274 }
2275 }
2276
2277 mainQueryOrder.push(this.quote(order, model, '->'));

Callers 4

selectQueryMethod · 0.95
addLimitAndOffsetMethod · 0.80
addLimitAndOffsetMethod · 0.80

Calls 3

_getAliasForFieldMethod · 0.95
quoteIdentifierMethod · 0.95
quoteMethod · 0.95

Tested by

no test coverage detected