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

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

No files matched your search

@@ -232,7 +232,7 @@ export class Query {
pagination: {
// We pass through `take` to force the `select-in` query strategy
// There might be a better way to do this, but for now this should do
take: queryOptions.pagination?.take,
take: queryOptions.pagination?.take ?? indexResponse.data.length,
},
}
@@ -23,14 +23,34 @@ describe("IndexQueryConfig", () => {
expectTypeOf<IndexConfig["filters"]>().toEqualTypeOf<
| {
id?: string | string[] | OperatorMap<string>
title?: string | string[] | OperatorMap<string>
id?: string | string[] | OperatorMap<string | string[] | null> | null
title?:
| string
| string[]
| OperatorMap<string | string[] | null>
| null
variants?: {
id?: string | string[] | OperatorMap<string>
product_id?: string | string[] | OperatorMap<string>
sku?: string | string[] | OperatorMap<string>
id?:
| string
| string[]
| OperatorMap<string | string[] | null>
| null
product_id?:
| string
| string[]
| OperatorMap<string | string[] | null>
| null
sku?:
| string
| string[]
| OperatorMap<string | string[] | null>
| null
prices?: {
amount?: number | number[] | OperatorMap<number>
amount?:
| number
| number[]
| OperatorMap<number | number[] | null>
| null
}
}
}
@@ -1,12 +1,12 @@
export type IndexOperatorMap<T> = {
$eq: T
$lt: T
$lte: T
$gt: T
$gte: T
$ne: T
$in: T
$is: T
$like: T
$ilike: T
$eq?: T
$lt?: T
$lte?: T
$gt?: T
$gte?: T
$ne?: T
$in?: T
$is?: T
$like?: T
$ilike?: T
}
@@ -19,8 +19,12 @@ type ExtractFiltersOperators<
? never
: Key extends ExcludedProps
? never
: TypeOnly<T[Key]> extends string | number | boolean | Date
? TypeOnly<T[Key]> | TypeOnly<T[Key]>[] | OperatorMap<TypeOnly<T[Key]>>
: TypeOnly<T[Key]> extends string | number | boolean | Date | null
?
| TypeOnly<T[Key]>
| TypeOnly<T[Key]>[]
| OperatorMap<TypeOnly<T[Key]> | TypeOnly<T[Key]>[] | null>
| null
: TypeOnly<T[Key]> extends Array<infer R>
? TypeOnly<R> extends { __typename: any }
? IndexFilters<Key & string, T, [Key & string, ...Exclusion], Depth[Lim]>
@@ -63,7 +63,6 @@ export type IndexQueryConfig<TEntry extends string> = {
filters?: IndexFilters<TEntry>
joinFilters?: IndexFilters<TEntry>
pagination?: Partial<IndexQueryInput<TEntry>["pagination"]>
keepFilteredEntities?: boolean
}
export type QueryFunctionReturnPagination = {
@@ -0,0 +1,26 @@
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"
)
})
})
@@ -0,0 +1,39 @@
import { camelToSnakeCase } from "./camel-to-snake-case"
import { simpleHash } from "./simple-hash"
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()
}
+4 -3
View File
@@ -4,6 +4,7 @@ export * from "./array-intersection"
export * from "./build-query"
export * from "./build-regexp-if-valid"
export * from "./camel-to-snake-case"
export * from "./compress-name"
export * from "./container"
export * from "./convert-item-response-to-update-request"
export * from "./create-container-like"
@@ -80,11 +81,11 @@ export * from "./to-handle"
export * from "./to-kebab-case"
export * from "./to-pascal-case"
export * from "./to-unix-slash"
export * from "./try-convert-to-number"
export * from "./trim-zeros"
export * from "./try-convert-to-boolean"
export * from "./try-convert-to-number"
export * from "./unflatten-object-keys"
export * from "./upper-case-first"
export * from "./validate-handle"
export * from "./wrap-handler"
export * from "./validate-module-name"
export * from "./try-convert-to-boolean"
export * from "./wrap-handler"
+1 -2
View File
@@ -44,8 +44,7 @@ export function buildModuleResourceEventName({
action: string
}): string {
const kebabCaseName = lowerCaseFirst(kebabCase(objectName))
const conventionalePrefix = prefix && lowerCaseFirst(kebabCase(prefix))
return `${prefix ? `${conventionalePrefix}.` : ""}${kebabCaseName}.${action}`
return `${prefix ? `${prefix}.` : ""}${kebabCaseName}.${action}`
}
/**