MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / processOrderBy

Method processOrderBy

packages/sql/src/query/QueryBuilder.ts:1740–1788  ·  view source on GitHub ↗
(
    orderBy: QueryOrderMap<Entity> | QueryOrderMap<Entity>[],
    reset = true,
  )

Source from the content-addressed store, hash-verified

1738 }
1739
1740 private processOrderBy(
1741 orderBy: QueryOrderMap<Entity> | QueryOrderMap<Entity>[],
1742 reset = true,
1743 ): SelectQueryBuilder<Entity, RootAlias, Hint, Context, RawAliases, Fields, CTEs> {
1744 this.ensureNotFinalized();
1745
1746 if (reset) {
1747 this.#state.orderBy = [];
1748 }
1749
1750 const selectAliases = this.getSelectAliases();
1751
1752 Utils.asArray<QueryOrderMap<Entity>>(orderBy).forEach(orig => {
1753 // Shallow clone to avoid mutating the caller's object — safe because the clone
1754 // is only used within this loop iteration and `orig` is not referenced afterward.
1755 const o = { ...orig } as Dictionary;
1756
1757 // Wrap known select aliases in raw() so they bypass property validation and alias prefixing
1758 for (const key of Object.keys(o)) {
1759 if (selectAliases.has(key)) {
1760 o[raw('??', [key]) as any] = o[key];
1761 delete o[key];
1762 }
1763 }
1764
1765 this.helper.validateQueryOrder(o);
1766 const processed = QueryHelper.processWhere({
1767 where: o as FilterQuery<Entity>,
1768 entityName: this.mainAlias.entityName,
1769 metadata: this.metadata,
1770 platform: this.platform,
1771 aliasMap: this.getAliasMap(),
1772 aliased: [QueryType.SELECT, QueryType.COUNT].includes(this.type),
1773 convertCustomTypes: false,
1774 type: 'orderBy',
1775 });
1776 this.#state.orderBy.push(
1777 CriteriaNodeFactory.createNode<Entity>(this.metadata, this.mainAlias.entityName, processed).process(
1778 this as IQueryBuilder<Entity>,
1779 {
1780 matchPopulateJoins: true,
1781 type: 'orderBy',
1782 },
1783 ),
1784 );
1785 });
1786
1787 return this as SelectQueryBuilder<Entity, RootAlias, Hint, Context, RawAliases, Fields, CTEs>;
1788 }
1789
1790 /** Collect custom aliases from select fields (stored as 'resolved as alias' strings by select()). */
1791 private getSelectAliases(): Set<string> {

Callers 2

orderByMethod · 0.95
andOrderByMethod · 0.95

Calls 12

ensureNotFinalizedMethod · 0.95
getSelectAliasesMethod · 0.95
getAliasMapMethod · 0.95
rawFunction · 0.90
asArrayMethod · 0.80
validateQueryOrderMethod · 0.80
pushMethod · 0.80
createNodeMethod · 0.80
hasMethod · 0.65
processMethod · 0.65
keysMethod · 0.45
processWhereMethod · 0.45

Tested by

no test coverage detected