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

@@ -1180,7 +1180,9 @@ function buildSchemaFromFilterableLinks(
return
}
const fieldType = fieldRef.type.toString()
const isEnum =
fieldRef.type?.astNode?.kind === GraphQLUtils.Kind.ENUM_TYPE_DEFINITION
const fieldType = isEnum ? "String" : fieldRef.type.toString()
const isArray = fieldType.startsWith("[")
const currentType = fieldType.replace(/\[|\]|\!/g, "")

View File

@@ -1,6 +1,7 @@
import { IndexTypes } from "@medusajs/framework/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { schemaObjectRepresentationPropertiesToOmit } from "@types"
import { getPivotTableName, normalizeTableName } from "./normalze-table-name"
export async function createPartitions(
schemaObjectRepresentation: IndexTypes.SchemaObjectRepresentation,
@@ -18,7 +19,7 @@ export async function createPartitions(
schemaObjectRepresentation[key].listeners.length > 0
)
.map((key) => {
const cName = key.toLowerCase()
const cName = normalizeTableName(key)
if (createdPartitions.has(cName)) {
return []
@@ -35,7 +36,8 @@ export async function createPartitions(
continue
}
const pName = `cat_pivot_${parent.ref.entity}${key}`.toLowerCase()
const pName = getPivotTableName(`${parent.ref.entity}${key}`)
if (createdPartitions.has(pName)) {
continue
}
@@ -63,7 +65,7 @@ export async function createPartitions(
schemaObjectRepresentation[key].listeners.length > 0
)
.map((key) => {
const cName = key.toLowerCase()
const cName = normalizeTableName(key)
const part: string[] = []
part.push(
@@ -79,7 +81,8 @@ export async function createPartitions(
continue
}
const pName = `cat_pivot_${parent.ref.entity}${key}`.toLowerCase()
const pName = getPivotTableName(`${parent.ref.entity}${key}`)
part.push(
`CREATE INDEX CONCURRENTLY IF NOT EXISTS "IDX_${pName}_child_id" ON ${activeSchema}${pName} ("child_id")`
)

View File

@@ -6,3 +6,4 @@ export * from "./sync/configuration"
export * from "./index-metadata-status"
export * from "./gql-to-types"
export * from "./default-schema"
export * from "./normalze-table-name"

View File

@@ -0,0 +1,10 @@
import { compressName } from "@medusajs/framework/utils"
export function normalizeTableName(name: string): string {
return compressName(name.toLowerCase(), 58).replace(/[^a-z0-9_]/g, "_")
}
export function getPivotTableName(tableName: string) {
const compressedName = normalizeTableName(tableName)
return `cat_pivot_${compressedName}`
}

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)