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

Method buildFields

packages/mongodb/src/MongoDriver.ts:750–796  ·  view source on GitHub ↗
(
    entityName: EntityName<T>,
    populate: PopulateOptions<T>[],
    fields?: readonly EntityField<T, P>[],
    exclude?: string[],
  )

Source from the content-addressed store, hash-verified

748 }
749
750 protected buildFields<T extends object, P extends string = never>(
751 entityName: EntityName<T>,
752 populate: PopulateOptions<T>[],
753 fields?: readonly EntityField<T, P>[],
754 exclude?: string[],
755 ): string[] | undefined {
756 const meta = this.metadata.get(entityName);
757 const lazyProps = meta.props.filter(prop => prop.lazy && !populate.some(p => this.isPopulated(meta, prop, p)));
758 const ret: string[] = [];
759
760 if (fields) {
761 for (let field of fields) {
762 /* v8 ignore next */
763 if (Utils.isPlainObject(field)) {
764 continue;
765 }
766
767 if (field.toString().includes('.')) {
768 field = field.toString().substring(0, field.toString().indexOf('.')) as EntityField<T, P>;
769 }
770
771 let prop = meta.properties[field as EntityKey<T>];
772
773 /* v8 ignore next */
774 if (prop) {
775 if (!prop.fieldNames) {
776 continue;
777 }
778
779 prop = prop.serializedPrimaryKey ? meta.getPrimaryProps()[0] : prop;
780 ret.push(prop.fieldNames[0]);
781 } else if (field === '*') {
782 const props = meta.props.filter(prop => this.platform.shouldHaveColumn(prop, populate));
783 ret.push(...Utils.flatten(props.filter(p => !lazyProps.includes(p)).map(p => p.fieldNames)));
784 } else {
785 ret.push(field as keyof T & string);
786 }
787 }
788
789 ret.unshift(...meta.primaryKeys.filter(pk => !fields.includes(pk)));
790 } else if (!Utils.isEmpty(exclude) || lazyProps.some(p => !p.formula)) {
791 const props = meta.props.filter(prop => this.platform.shouldHaveColumn(prop, populate, exclude));
792 ret.push(...Utils.flatten(props.filter(p => !lazyProps.includes(p)).map(p => p.fieldNames)));
793 }
794
795 return ret.length > 0 ? ret : undefined;
796 }
797
798 private handleVersionProperty<T extends object>(
799 entityName: EntityName<T>,

Callers 5

streamMethod · 0.95
findMethod · 0.95
findOneMethod · 0.95
countFunction · 0.45

Calls 11

filterMethod · 0.80
isPopulatedMethod · 0.80
isPlainObjectMethod · 0.80
getPrimaryPropsMethod · 0.80
pushMethod · 0.80
getMethod · 0.65
toStringMethod · 0.45
shouldHaveColumnMethod · 0.45
flattenMethod · 0.45
mapMethod · 0.45
isEmptyMethod · 0.45

Tested by

no test coverage detected