fix(index): Apply various fixes to the index engine (#12501)

This commit is contained in:
Carlos R. L. Rodrigues
2025-05-19 15:14:25 -03:00
committed by GitHub
parent 32be40a2c0
commit 59bbff62d8
20 changed files with 194 additions and 133 deletions

View File

@@ -7,6 +7,7 @@ import {
} from "@medusajs/framework/utils"
import { Knex } from "@mikro-orm/knex"
import { OrderBy, QueryFormat, QueryOptions, Select } from "@types"
import { getPivotTableName, normalizeTableName } from "./normalze-table-name"
function escapeJsonPathString(val: string): string {
// Escape for JSONPath string
@@ -23,6 +24,10 @@ function buildSafeJsonPathQuery(
jsonPathOperator = "=="
} else if (operator.toUpperCase().includes("LIKE")) {
jsonPathOperator = "like_regex"
} else if (operator === "IS") {
jsonPathOperator = "=="
} else if (operator === "IS NOT") {
jsonPathOperator = "!="
}
if (typeof value === "string") {
@@ -32,6 +37,10 @@ function buildSafeJsonPathQuery(
val = val.replace(/%/g, ".*").replace(/_/g, ".")
}
value = `"${escapeJsonPathString(val)}"`
} else {
if ((operator === "IS" || operator === "IS NOT") && value === null) {
value = "null"
}
}
return `$.${field} ${jsonPathOperator} ${value}`
@@ -530,14 +539,14 @@ export class QueryBuilder {
aliasMapping[currentAliasPath] = alias
if (level > 0) {
const cName = entity.ref.entity.toLowerCase()
const cName = normalizeTableName(entity.ref.entity)
let joinTable = `cat_${cName} AS ${alias}`
if (entity.isInverse || parEntity.isInverse) {
const pName =
`${entity.ref.entity}${parEntity.ref.entity}`.toLowerCase()
const pivotTable = `cat_pivot_${pName}`
const pivotTable = getPivotTableName(pName)
joinBuilder.leftJoin(
`${pivotTable} AS ${alias}_ref`,
@@ -552,7 +561,7 @@ export class QueryBuilder {
} else {
const pName =
`${parEntity.ref.entity}${entity.ref.entity}`.toLowerCase()
const pivotTable = `cat_pivot_${pName}`
const pivotTable = getPivotTableName(pName)
joinBuilder.leftJoin(
`${pivotTable} AS ${alias}_ref`,
@@ -704,11 +713,9 @@ export class QueryBuilder {
public buildQuery({
hasPagination = true,
hasCount = false,
returnIdOnly = false,
}: {
hasPagination?: boolean
hasCount?: boolean
returnIdOnly?: boolean
}): { sql: string; sqlCount?: string } {
const selectOnlyStructure = this.selector.select
const structure = this.requestedFields
@@ -816,7 +823,10 @@ export class QueryBuilder {
}
innerQueryBuilder.from(
`cat_${rootEntity} AS ${this.getShortAlias(aliasMapping, rootKey)}`
`cat_${normalizeTableName(rootEntity)} AS ${this.getShortAlias(
aliasMapping,
rootKey
)}`
)
joinParts.forEach((joinPart) => {
@@ -887,7 +897,10 @@ export class QueryBuilder {
const innerQueryAlias = "paginated_ids"
outerQueryBuilder.from(
`cat_${rootEntity} AS ${this.getShortAlias(aliasMapping, rootKey)}`
`cat_${normalizeTableName(rootEntity)} AS ${this.getShortAlias(
aliasMapping,
rootKey
)}`
)
outerQueryBuilder.joinRaw(
@@ -909,13 +922,11 @@ export class QueryBuilder {
outerQueryBuilder.joinRaw(joinPart)
})
const finalSelectParts = !returnIdOnly
? this.buildSelectParts(
selectOnlyStructure[rootKey] as Select,
rootKey,
aliasMapping
)
: { [`${rootKey}.id`]: `${rootAlias}.id` }
const finalSelectParts = this.buildSelectParts(
selectOnlyStructure[rootKey] as Select,
rootKey,
aliasMapping
)
outerQueryBuilder.select(finalSelectParts)