Repository: typeorm/typeorm
File: src/query-builder/SelectQueryBuilder.ts (TypeScript)

Code before:

        return (
            " ORDER BY " +
            Object.keys(orderBys)
                .map((columnName) => {
                    const orderValue =
                        typeof orderBys[columnName] === "string"
                            ? orderBys[columnName]
                            : (orderBys[columnName] as any).order +
                              " " +
                              (orderBys[columnName] as any).nulls
                    const selection = this.expressionMap.selects.find(
                        (s) => s.selection === columnName,
                    )
                    if (
                        selection &&
                        !selection.aliasName &&
                        columnName.indexOf(".") !== -1
                    ) {
                        const criteriaParts = columnName.split(".")
                        const aliasName = criteriaParts[0]
                        const propertyPath = criteriaParts.slice(1).join(".")
                        const alias = this.expressionMap.aliases.find(
                            (alias) => alias.name === aliasName,
                        )
                        if (alias && alias.hasMetadata) {
                            const column =
                                alias.metadata.findColumnWithPropertyPath(
                                    propertyPath,
                                )
                            if (column) {
                                const orderAlias = DriverUtils.buildAlias(
                                    this.connection.driver,
                                    undefined,
                                    aliasName,
                                    column.databaseName,
                                )
                                return (
                                    this.escape(orderAlias) + " " + orderValue
                                )
                            }
                        }
                    }

                    return (
                        this.replacePropertyNames(columnName) + " " + orderValue
                    )
                })
                .join(", ")
        )
    }

Review comment (suggestion):
We could use optional chaining: `alias?.hasMetadata`

Code after:
                        .inverseRelation!.joinColumns.map((joinColumn) => {
                            // `post`.`id` = `post_category`.`postId`
                            return (
                                destinationTableAlias +
                                "." +
                                joinColumn.referencedColumn!.propertyPath +
                                "=" +
                                junctionAlias +
                                "." +
                                joinColumn.propertyPath
                            )
                        })
                        .join(" AND ")
                }

                return (
                    " " +
                    joinAttr.direction +
                    " JOIN " +
                    this.getTableName(junctionTableName) +
                    " " +
                    this.escape(junctionAlias) +
                    this.createTableLockExpression() +
                    " ON " +
                    this.replacePropertyNames(junctionCondition) +
                    " " +
                    joinAttr.direction +
                    " JOIN " +
                    this.getTableName(destinationTableName) +
                    " " +
                    this.escape(destinationTableAlias) +
                    this.createTableLockExpression() +
                    " ON " +
                    this.replacePropertyNames(
                        destinationCondition + appendedCondition,
                    )
                )
            }
        })

        return joins.join(" ")
    }

    /**
     * Creates "GROUP BY" part of SQL query.
     */
    protected createGroupByExpression() {
        if (!this.expressionMap.groupBys || !this.expressionMap.groupBys.length)
            return ""
        return (
            " GROUP BY " +