| 576 | callback: (refs: RefsForContext<TContext>) => TSelectValue, |
| 577 | ): QueryBuilder<WithResult<TContext, ResultTypeFromSelectValue<TSelectValue>>> |
| 578 | select( |
| 579 | callback: ( |
| 580 | refs: RefsForContext<TContext>, |
| 581 | ) => SelectObject | ScalarSelectValue, |
| 582 | ) { |
| 583 | const aliases = this._getCurrentAliases() |
| 584 | const refProxy = createRefProxy(aliases) as RefsForContext<TContext> |
| 585 | let selectObject = callback(refProxy) |
| 586 | |
| 587 | // Returning a top-level alias directly is equivalent to spreading it. |
| 588 | // Leaf refs like `row.name` must remain scalar selections. |
| 589 | if (isRefProxy(selectObject) && selectObject.__path.length === 1) { |
| 590 | const sentinelKey = `__SPREAD_SENTINEL__${selectObject.__path[0]}__0` |
| 591 | selectObject = { [sentinelKey]: true } |
| 592 | } |
| 593 | |
| 594 | const select = buildNestedSelect(selectObject, aliases) |
| 595 | |
| 596 | return new BaseQueryBuilder({ |
| 597 | ...this.query, |
| 598 | select: select, |
| 599 | fnSelect: undefined, // remove the fnSelect clause if it exists |
| 600 | }) as any |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Sort the query results by one or more columns |