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

@@ -1,26 +0,0 @@
import { compressName } from "../compress-name"
describe("compressName", () => {
it("should remove consecutive duplicate names in sequence if it exceds 58 chars", () => {
const name =
"product_product_variant_id_order_order_id_long_long_long_long_name"
const result = compressName(name)
expect(result).toBe("product_variant_id_order_id_long_name33bb7b344")
})
it("should remove duplicate names and truncate name to append a hash", () => {
const name = "product_product_variant_id_order_order_id"
const result = compressName(name, 30)
expect(result).toBe("product_variant_id_ord91d0cda8")
})
it("handles very long names with truncation and appends hash when necessary", () => {
const name =
"CustomModuleImplementationContainingAReallyBigNameThatExceedsPosgresLimitToNameATableModule"
const result = compressName(name)
expect(result).toHaveLength(58)
expect(result).toBe(
"cust_modu_impl_cont_area_big_name_that_exce_posg_1f1cc72da"
)
})
})

View File

@@ -1,38 +0,0 @@
import { camelToSnakeCase, simpleHash } from "@medusajs/framework/utils"
export function compressName(name: string, limit = 58) {
if (name.length <= limit) {
return name.toLowerCase()
}
const hash = simpleHash(name).toLowerCase()
const hashLength = hash.length
// Remove consecutive duplicates
const parts = name.toLowerCase().split("_")
const deduplicatedParts: string[] = []
for (let i = 0; i < parts.length; i++) {
if (i === 0 || parts[i] !== parts[i - 1]) {
deduplicatedParts.push(parts[i])
}
}
let result = deduplicatedParts.join("_")
// If still greater than the limit, truncate each part to 4 characters
if (result.length > limit) {
const allParts = camelToSnakeCase(name).split("_")
result = allParts.map((part) => part.substring(0, 4)).join("_")
}
name = result
const nameLength = name.length
const diff = nameLength + hashLength - limit
if (diff > 0) {
return name.slice(0, nameLength - diff) + hash
}
return (name + hash).toLowerCase()
}

View File

@@ -4,13 +4,13 @@ import {
} from "@medusajs/framework/types"
import {
composeTableName,
compressName,
mikroOrmSoftDeletableFilterOptions,
simpleHash,
SoftDeletableFilterKey,
} from "@medusajs/framework/utils"
import { EntitySchema } from "@mikro-orm/core"
import { compressName } from "./compress-name"
function getClass(...properties) {
return class LinkModel {