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

Function buildToManyOrderBy

packages/sql/src/AbstractSqlDriver.ts:3362–3394  ·  view source on GitHub ↗
(
    qb: AnyQueryBuilder<T>,
    prop: EntityProperty<T>,
    path: string,
    ref: string | undefined,
    orderBy: QueryOrderMap<T>[],
  )

Source from the content-addressed store, hash-verified

3360 }
3361
3362 private buildToManyOrderBy<T extends object>(
3363 qb: AnyQueryBuilder<T>,
3364 prop: EntityProperty<T>,
3365 path: string,
3366 ref: string | undefined,
3367 orderBy: QueryOrderMap<T>[],
3368 ): void {
3369 const join = qb.getJoinForPath(path, { matchPopulateJoins: true });
3370 const propAlias = qb.getAliasForJoinPath(join ?? path, { matchPopulateJoins: true });
3371
3372 if (prop.kind === ReferenceKind.MANY_TO_MANY && prop.fixedOrder && join) {
3373 const alias = ref ? propAlias : join.ownerAlias;
3374 orderBy.push({ [`${alias}.${prop.fixedOrderColumn}`]: QueryOrder.ASC } as QueryOrderMap<T>);
3375 }
3376
3377 const effectiveOrderBy = QueryHelper.mergeOrderBy(prop.orderBy, prop.targetMeta?.orderBy);
3378
3379 for (const item of effectiveOrderBy) {
3380 for (const field of Utils.getObjectQueryKeys(item)) {
3381 const order = item[field as keyof typeof item];
3382
3383 if (RawQueryFragment.isKnownFragmentSymbol(field)) {
3384 const { sql, params } = RawQueryFragment.getKnownFragment(field)!;
3385 const sql2 = propAlias ? sql.replace(new RegExp(ALIAS_REPLACEMENT_RE, 'g'), propAlias) : sql;
3386 const key = raw(sql2, params);
3387 orderBy.push({ [key]: order } as QueryOrderMap<T>);
3388 continue;
3389 }
3390
3391 orderBy.push({ [`${propAlias}.${field}` as EntityKey]: order } as QueryOrderMap<T>);
3392 }
3393 }
3394 }
3395
3396 protected normalizeFields<T extends object>(fields: InternalField<T>[], prefix = ''): string[] {
3397 const ret: string[] = [];

Callers

nothing calls this directly

Calls 8

rawFunction · 0.90
pushMethod · 0.80
mergeOrderByMethod · 0.80
getObjectQueryKeysMethod · 0.80
isKnownFragmentSymbolMethod · 0.80
getKnownFragmentMethod · 0.80
getJoinForPathMethod · 0.65
getAliasForJoinPathMethod · 0.65

Tested by

no test coverage detected