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

Method finalize

packages/sql/src/query/QueryBuilder.ts:3701–3784  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3699 }
3700
3701 private finalize(): void {
3702 if (this.#state.finalized) {
3703 return;
3704 }
3705
3706 if (!this.#state.type) {
3707 this.select('*');
3708 }
3709
3710 const meta = this.mainAlias.meta as EntityMetadata<Entity>;
3711 this.applyDiscriminatorCondition();
3712 this.applyTPTJoins();
3713 this.addTPTParentFields();
3714 this.applyTPTPolymorphicJoins();
3715 this.processPopulateHint();
3716 this.processNestedJoins();
3717
3718 if (meta && (this.#state.fields?.includes('*') || this.#state.fields?.includes(`${this.mainAlias.aliasName}.*`))) {
3719 const schema = this.getSchema(this.mainAlias);
3720
3721 // Create a column mapping with unquoted aliases - quoting should be handled by the user via `quote` helper
3722 // For TPT, use helper to resolve correct alias per property (inherited props use parent alias)
3723 const quotedMainAlias = this.platform.quoteIdentifier(this.mainAlias.aliasName).toString();
3724 const columns = meta.createColumnMappingObject(
3725 prop => this.helper.getTPTAliasForProperty(prop.name, this.mainAlias.aliasName),
3726 quotedMainAlias,
3727 );
3728
3729 meta.props
3730 .filter(prop => prop.formula && (!prop.lazy || this.#state.flags.has(QueryFlag.INCLUDE_LAZY_FORMULAS)))
3731 .map(prop => {
3732 const aliased = this.platform.quoteIdentifier(prop.fieldNames[0]);
3733 const table = this.helper.createFormulaTable(quotedMainAlias, meta, schema);
3734 return `${this.driver.evaluateFormula(prop.formula!, columns, table)} as ${aliased}`;
3735 })
3736 .filter(
3737 field =>
3738 !this.#state.fields!.some(f => {
3739 if (isRaw(f)) {
3740 return f.sql === field && f.params.length === 0;
3741 }
3742
3743 return f === field;
3744 }),
3745 )
3746 .forEach(field => this.#state.fields!.push(raw(field)));
3747 }
3748
3749 QueryHelper.processObjectParams(this.#state.data);
3750 QueryHelper.processObjectParams(this.#state.cond);
3751 QueryHelper.processObjectParams(this.#state.having);
3752
3753 // automatically enable paginate flag when we detect to-many joins, but only if there is no `group by` clause
3754 if (
3755 !this.#state.flags.has(QueryFlag.DISABLE_PAGINATE) &&
3756 this.#state.groupBy.length === 0 &&
3757 this.hasToManyJoins()
3758 ) {

Callers 1

getNativeQueryMethod · 0.95

Calls 15

selectMethod · 0.95
applyTPTJoinsMethod · 0.95
addTPTParentFieldsMethod · 0.95
processPopulateHintMethod · 0.95
processNestedJoinsMethod · 0.95
getSchemaMethod · 0.95
hasToManyJoinsMethod · 0.95
wrapPaginateSubQueryMethod · 0.95
preparePartitionLimitMethod · 0.95
wrapModifySubQueryMethod · 0.95

Tested by

no test coverage detected