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

Method select

packages/sql/src/query/QueryBuilder.ts:773–806  ·  view source on GitHub ↗
(
    fields: unknown,
    distinct = false,
  )

Source from the content-addressed store, hash-verified

771 CTEs
772 >;
773 select(
774 fields: unknown,
775 distinct = false,
776 ): SelectQueryBuilder<Entity, RootAlias, Hint, Context, RawAliases, any, CTEs> {
777 this.ensureNotFinalized();
778 this.#state.fields = Utils.asArray(fields as Field<Entity, RootAlias, Context>[]).flatMap(f => {
779 if (typeof f !== 'string') {
780 // Normalize sql.ref('prop') and sql.ref('prop').as('alias') to string form
781 if (isRaw(f) && f.sql === '??' && f.params.length === 1) {
782 return this.resolveNestedPath(String(f.params[0]));
783 }
784
785 if (isRaw(f) && f.sql === '?? as ??' && f.params.length === 2) {
786 return `${this.resolveNestedPath(String(f.params[0]))} as ${String(f.params[1])}`;
787 }
788
789 return f;
790 }
791
792 const asMatch = FIELD_ALIAS_RE.exec(f);
793
794 if (asMatch) {
795 return `${this.resolveNestedPath(asMatch[1].trim())} as ${asMatch[2]}`;
796 }
797
798 return this.resolveNestedPath(f);
799 }) as any;
800
801 if (distinct) {
802 this.#state.flags.add(QueryFlag.DISTINCT);
803 }
804
805 return this.init(QueryType.SELECT) as any;
806 }
807
808 /**
809 * Adds fields to an existing SELECT query.

Callers 5

addSelectMethod · 0.95
joinAndSelectMethod · 0.95
whereMethod · 0.95
finalizeMethod · 0.95
wrapPaginateSubQueryMethod · 0.95

Calls 6

ensureNotFinalizedMethod · 0.95
resolveNestedPathMethod · 0.95
initMethod · 0.95
isRawFunction · 0.90
asArrayMethod · 0.80
addMethod · 0.80

Tested by

no test coverage detected