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

Method joinReference

packages/sql/src/query/QueryBuilder.ts:3009–3141  ·  view source on GitHub ↗
(
    field: string | RawQueryFragment | NativeQueryBuilder | QueryBuilder,
    alias: string,
    cond: Dictionary,
    type: JoinType,
    path?: string,
    schema?: string,
    subquery?: string,
  )

Source from the content-addressed store, hash-verified

3007 }
3008
3009 private joinReference(
3010 field: string | RawQueryFragment | NativeQueryBuilder | QueryBuilder,
3011 alias: string,
3012 cond: Dictionary,
3013 type: JoinType,
3014 path?: string,
3015 schema?: string,
3016 subquery?: string,
3017 ): { prop: EntityProperty<Entity>; key: string } {
3018 this.ensureNotFinalized();
3019
3020 if (typeof field === 'object') {
3021 const prop = {
3022 name: '__subquery__',
3023 kind: ReferenceKind.MANY_TO_ONE,
3024 } as EntityProperty;
3025
3026 if (field instanceof QueryBuilder) {
3027 prop.type = Utils.className(field.mainAlias.entityName);
3028 prop.targetMeta = field.mainAlias.meta!;
3029 field = field.getNativeQuery();
3030 }
3031
3032 if (isRaw(field)) {
3033 field = this.platform.formatQuery(field.sql, field.params);
3034 }
3035
3036 const key = `${this.alias}.${prop.name}#${alias}`;
3037 this.#state.joins[key] = {
3038 prop,
3039 alias,
3040 type,
3041 cond,
3042 schema,
3043 subquery: field.toString(),
3044 ownerAlias: this.alias,
3045 } as any;
3046
3047 return { prop, key };
3048 }
3049
3050 if (!subquery && type.includes('lateral')) {
3051 throw new Error(`Lateral join can be used only with a sub-query.`);
3052 }
3053
3054 const [fromAlias, fromField] = this.helper.splitField(field as EntityKey<Entity>);
3055 const q = (str: string) => `'${str}'`;
3056
3057 if (!this.#state.aliases[fromAlias]) {
3058 throw new Error(
3059 `Trying to join ${q(fromField)} with alias ${q(fromAlias)}, but ${q(fromAlias)} is not a known alias. Available aliases are: ${Object.keys(this.#state.aliases).map(q).join(', ')}.`,
3060 );
3061 }
3062
3063 const entityName = this.#state.aliases[fromAlias].entityName;
3064 const meta = this.metadata.get(entityName);
3065 const prop = meta.properties[fromField];
3066

Callers 2

joinMethod · 0.95
joinAndSelectMethod · 0.95

Calls 15

ensureNotFinalizedMethod · 0.95
createAliasMethod · 0.95
getAliasMapMethod · 0.95
getAliasForJoinPathMethod · 0.95
getNextAliasMethod · 0.95
isRawFunction · 0.90
classNameMethod · 0.80
formatQueryMethod · 0.80
splitFieldMethod · 0.80
createNodeMethod · 0.80
joinOneToReferenceMethod · 0.80

Tested by

no test coverage detected