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

Function mapPropToFieldNames

packages/sql/src/AbstractSqlDriver.ts:2872–2930  ·  view source on GitHub ↗

* @internal

(
    qb: AnyQueryBuilder<T>,
    prop: EntityProperty<T>,
    tableAlias: string,
    meta: EntityMetadata<T>,
    schema?: string,
    explicitFields?: readonly InternalField<T>[],
  )

Source from the content-addressed store, hash-verified

2870 * @internal
2871 */
2872 mapPropToFieldNames<T extends object>(
2873 qb: AnyQueryBuilder<T>,
2874 prop: EntityProperty<T>,
2875 tableAlias: string,
2876 meta: EntityMetadata<T>,
2877 schema?: string,
2878 explicitFields?: readonly InternalField<T>[],
2879 ): InternalField<T>[] {
2880 if (prop.kind === ReferenceKind.EMBEDDED && !prop.object) {
2881 return Object.entries(prop.embeddedProps).flatMap(([name, childProp]) => {
2882 const childFields = explicitFields ? Utils.extractChildElements(explicitFields as string[], prop.name) : [];
2883
2884 if (
2885 !this.shouldHaveColumn(
2886 prop.targetMeta!,
2887 { ...childProp, name },
2888 [],
2889 childFields.length > 0 ? childFields : undefined,
2890 )
2891 ) {
2892 return [];
2893 }
2894
2895 return this.mapPropToFieldNames<T>(qb, childProp, tableAlias, meta, schema, childFields);
2896 });
2897 }
2898
2899 const aliased = this.platform.quoteIdentifier(`${tableAlias}__${prop.fieldNames[0]}`);
2900 const sourceAlias = qb.helper.getTPTAliasForProperty(prop.name, tableAlias);
2901
2902 if (prop.customTypes?.some(type => !!type?.convertToJSValueSQL)) {
2903 return prop.fieldNames.map((col, idx) => {
2904 if (!prop.customTypes[idx]?.convertToJSValueSQL) {
2905 return col;
2906 }
2907
2908 const prefixed = this.platform.quoteIdentifier(`${sourceAlias}.${col}`);
2909 const aliased = this.platform.quoteIdentifier(`${tableAlias}__${col}`);
2910
2911 return raw(`${prop.customTypes[idx].convertToJSValueSQL(prefixed, this.platform)} as ${aliased}`);
2912 });
2913 }
2914
2915 if (prop.customType?.convertToJSValueSQL) {
2916 const prefixed = this.platform.quoteIdentifier(`${sourceAlias}.${prop.fieldNames[0]}`);
2917 return [raw(`${prop.customType.convertToJSValueSQL(prefixed, this.platform)} as ${aliased}`)];
2918 }
2919
2920 if (prop.formula) {
2921 const quotedAlias = this.platform.quoteIdentifier(tableAlias).toString();
2922 const table = this.createFormulaTable(quotedAlias, meta, schema);
2923 const columns = meta.createColumnMappingObject(tableAlias);
2924 return [raw(`${this.evaluateFormula(prop.formula, columns, table)} as ${aliased}`)];
2925 }
2926
2927 return prop.fieldNames.map(fieldName => {
2928 return raw('?? as ??', [`${sourceAlias}.${fieldName}`, `${tableAlias}__${fieldName}`]);
2929 });

Callers

nothing calls this directly

Calls 11

rawFunction · 0.90
entriesMethod · 0.80
extractChildElementsMethod · 0.80
createFormulaTableMethod · 0.80
shouldHaveColumnMethod · 0.45
quoteIdentifierMethod · 0.45
mapMethod · 0.45
convertToJSValueSQLMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected