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

Function buildFields

packages/sql/src/AbstractSqlDriver.ts:3450–3547  ·  view source on GitHub ↗
(
    meta: EntityMetadata<T>,
    populate: PopulateOptions<T>[],
    joinedProps: PopulateOptions<T>[],
    qb: AnyQueryBuilder<T>,
    alias: string,
    options: Pick<FindOptions<T, any, any, any>, 'strategy' | 'fields' | 'exclude'>,
    schema?: string,
  )

Source from the content-addressed store, hash-verified

3448 }
3449
3450 protected buildFields<T extends object>(
3451 meta: EntityMetadata<T>,
3452 populate: PopulateOptions<T>[],
3453 joinedProps: PopulateOptions<T>[],
3454 qb: AnyQueryBuilder<T>,
3455 alias: string,
3456 options: Pick<FindOptions<T, any, any, any>, 'strategy' | 'fields' | 'exclude'>,
3457 schema?: string,
3458 ): InternalField<T>[] {
3459 const lazyProps = meta.props.filter(prop => prop.lazy && !populate.some(p => this.isPopulated(meta, prop, p)));
3460 const hasLazyFormulas = meta.props.some(p => p.lazy && p.formula);
3461 const requiresSQLConversion = meta.props.some(p => p.customType?.convertToJSValueSQL && p.persist !== false);
3462 const hasExplicitFields = !!options.fields;
3463 const ret: InternalField<T>[] = [];
3464 let addFormulas = false;
3465
3466 // handle root entity properties first, this is used for both strategies in the same way
3467 if (options.fields) {
3468 for (const field of this.normalizeFields(options.fields as string[])) {
3469 if (field === '*') {
3470 ret.push('*');
3471 continue;
3472 }
3473
3474 const parts = field.split('.');
3475 const rootPropName = parts.shift()!; // first one is the `prop`
3476 const prop = QueryHelper.findProperty<T>(rootPropName, {
3477 metadata: this.metadata,
3478 platform: this.platform,
3479 entityName: meta.class,
3480 where: {} as FilterQuery<T>,
3481 aliasMap: qb.getAliasMap(),
3482 });
3483
3484 this.processField(meta, prop, parts.join('.'), ret);
3485 }
3486
3487 if (!options.fields.includes('*') && !options.fields.includes(`${qb.alias}.*`)) {
3488 ret.unshift(...meta.primaryKeys.filter(pk => !options.fields!.includes(pk)));
3489 }
3490
3491 if (
3492 meta.root.inheritanceType === 'sti' &&
3493 !options.fields.includes(`${qb.alias}.${meta.root.discriminatorColumn!}`)
3494 ) {
3495 ret.push(meta.root.discriminatorColumn!);
3496 }
3497 } else if (!Utils.isEmpty(options.exclude) || lazyProps.some(p => !p.formula && (p.kind !== '1:1' || p.owner))) {
3498 const props = meta.props.filter(prop =>
3499 this.platform.shouldHaveColumn(prop, populate, options.exclude as string[], false, false),
3500 );
3501 ret.push(...props.filter(p => !lazyProps.includes(p)).map(p => p.name));
3502 addFormulas = true;
3503 } else if (hasLazyFormulas || requiresSQLConversion) {
3504 ret.push('*');
3505 addFormulas = true;
3506 } else {
3507 ret.push('*');

Callers

nothing calls this directly

Calls 15

rawFunction · 0.90
filterMethod · 0.80
isPopulatedMethod · 0.80
pushMethod · 0.80
getAliasMapMethod · 0.80
createFormulaTableMethod · 0.80
joinMethod · 0.65
uniqueMethod · 0.65
findPropertyMethod · 0.45
isEmptyMethod · 0.45

Tested by

no test coverage detected