From c661e064885ca0a874a443b293f9bb48b2742cf7 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Wed, 14 May 2025 13:43:01 -0300 Subject: [PATCH] fix(index): query builder handle json array (#12480) * fix(index): query builder handle json array * fix sales channel event names --- .changeset/long-games-beg.md | 5 +++ .../__tests__/index/query-index.spec.ts | 33 +++++++++++++++++++ .../modules/index/src/utils/default-schema.ts | 2 +- .../modules/index/src/utils/query-builder.ts | 7 ++-- 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 .changeset/long-games-beg.md diff --git a/.changeset/long-games-beg.md b/.changeset/long-games-beg.md new file mode 100644 index 0000000000..94453770a1 --- /dev/null +++ b/.changeset/long-games-beg.md @@ -0,0 +1,5 @@ +--- +"@medusajs/index": patch +--- + +fix(index): query builder handle json array diff --git a/integration-tests/modules/__tests__/index/query-index.spec.ts b/integration-tests/modules/__tests__/index/query-index.spec.ts index 424e0d7195..0908fd0714 100644 --- a/integration-tests/modules/__tests__/index/query-index.spec.ts +++ b/integration-tests/modules/__tests__/index/query-index.spec.ts @@ -373,6 +373,39 @@ medusaIntegrationTestRunner({ }, ]) }) + + it("should use query.index to get products by an array of handles", async () => { + await populateData(api) + + const query = appContainer.resolve( + ContainerRegistrationKeys.QUERY + ) as RemoteQueryFunction + + const resultset = await fetchAndRetry( + async () => + await query.index({ + entity: "product", + fields: ["id"], + filters: { + handle: ["extra-product", "test-product"], + }, + pagination: { + take: 10, + skip: 0, + order: { + "variants.prices.amount": "DESC", + }, + }, + }), + ({ data }) => data.length > 0, + { + retries: 3, + waitSeconds: 3, + } + ) + + expect(resultset.data.length).toEqual(2) + }) }) }, }) diff --git a/packages/modules/index/src/utils/default-schema.ts b/packages/modules/index/src/utils/default-schema.ts index 1d900daf68..1f318df970 100644 --- a/packages/modules/index/src/utils/default-schema.ts +++ b/packages/modules/index/src/utils/default-schema.ts @@ -31,7 +31,7 @@ export const defaultSchema = ` currency_code: String } - type SalesChannel @Listeners(values: ["${Modules.SALES_CHANNEL}.sales_channel.created", "${Modules.SALES_CHANNEL}.sales_channel.updated", "${Modules.SALES_CHANNEL}.sales_channel.deleted"]) { + type SalesChannel @Listeners(values: ["${Modules.SALES_CHANNEL}.sales-channel.created", "${Modules.SALES_CHANNEL}.sales-channel.updated", "${Modules.SALES_CHANNEL}.sales-channel.deleted"]) { id: ID is_disabled: Boolean } diff --git a/packages/modules/index/src/utils/query-builder.ts b/packages/modules/index/src/utils/query-builder.ts index 5d34a41068..3ee0ec0f48 100644 --- a/packages/modules/index/src/utils/query-builder.ts +++ b/packages/modules/index/src/utils/query-builder.ts @@ -308,6 +308,7 @@ export class QueryBuilder { [targetField]: item === null ? null : item, }) ) + builder.whereRaw( `${aliasMapping[attr]}.data${nested} @> ANY(ARRAY[${inPlaceholders}]::JSONB[])`, jsonbValues @@ -355,11 +356,13 @@ export class QueryBuilder { [...value] ) } else { + const targetField = field[field.length - 1] as string + const jsonbValues = value.map((item) => - JSON.stringify({ [nested]: item === null ? null : item }) + JSON.stringify({ [targetField]: item === null ? null : item }) ) builder.whereRaw( - `${aliasMapping[attr]}.data IN ANY(ARRAY[${inPlaceholders}]::JSONB[])`, + `${aliasMapping[attr]}.data${nested} @> ANY(ARRAY[${inPlaceholders}]::JSONB[])`, jsonbValues ) }