From 71d8a0031f2b75de24c4c4e80ef3314661020420 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Sun, 7 Sep 2025 10:39:58 -0300 Subject: [PATCH] fix(index): index enum fields (#13428) https://github.com/medusajs/medusa/issues/13372 What: - It handles enum fields corretly when added to filterable fields --- .changeset/chilled-pillows-destroy.md | 5 +++ .../__tests__/index/query-index.spec.ts | 43 +++++++++++++++++++ .../modules/src/links/product-brand.ts | 2 +- .../migrations/Migration20250805184935.ts | 2 +- .../modules/src/modules/brand/models/brand.ts | 1 + .../modules/index/src/utils/build-config.ts | 10 +++-- 6 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 .changeset/chilled-pillows-destroy.md diff --git a/.changeset/chilled-pillows-destroy.md b/.changeset/chilled-pillows-destroy.md new file mode 100644 index 0000000000..faf523dac9 --- /dev/null +++ b/.changeset/chilled-pillows-destroy.md @@ -0,0 +1,5 @@ +--- +"@medusajs/index": patch +--- + +fix(index): index enum fields diff --git a/integration-tests/modules/__tests__/index/query-index.spec.ts b/integration-tests/modules/__tests__/index/query-index.spec.ts index 8f2ebb8861..8a17383dc0 100644 --- a/integration-tests/modules/__tests__/index/query-index.spec.ts +++ b/integration-tests/modules/__tests__/index/query-index.spec.ts @@ -514,6 +514,49 @@ medusaIntegrationTestRunner({ expect(resultset.data.length).toEqual(1) expect(resultset.data[0].origin_country).toEqual("USA") }) + + it("should use query.index to filter enum field", async () => { + const products = await populateData(api) + + const brandModule = appContainer.resolve("brand") + const link = appContainer.resolve(ContainerRegistrationKeys.LINK) + const brand = await brandModule.createBrands({ + name: "Medusa Brand", + }) + + await link.create({ + [Modules.PRODUCT]: { + product_id: products.find((p) => p.title === "Extra product").id, + }, + brand: { + brand_id: brand.id, + }, + }) + + const query = appContainer.resolve( + ContainerRegistrationKeys.QUERY + ) as RemoteQueryFunction + + const resultset = await fetchAndRetry( + async () => + await query.index({ + entity: "product", + fields: ["id"], + filters: { + status: "published", + brand: { + status: "active", + }, + }, + }), + ({ data }) => data.length > 0, + { + retries: 3, + waitSeconds: 3, + } + ) + expect(resultset.data.length).toEqual(1) + }) }) }, }) diff --git a/integration-tests/modules/src/links/product-brand.ts b/integration-tests/modules/src/links/product-brand.ts index 29a76062eb..1c0f7b6f79 100644 --- a/integration-tests/modules/src/links/product-brand.ts +++ b/integration-tests/modules/src/links/product-brand.ts @@ -12,7 +12,7 @@ const link = }, { linkable: BrandModule.linkable.brand.id, - filterable: ["id", "name"], + filterable: ["id", "name", "status"], isList: false, } ) diff --git a/integration-tests/modules/src/modules/brand/migrations/Migration20250805184935.ts b/integration-tests/modules/src/modules/brand/migrations/Migration20250805184935.ts index 3ee115b029..5131737d69 100644 --- a/integration-tests/modules/src/modules/brand/migrations/Migration20250805184935.ts +++ b/integration-tests/modules/src/modules/brand/migrations/Migration20250805184935.ts @@ -3,7 +3,7 @@ import { Migration } from "@mikro-orm/migrations" export class Migration20250805184935 extends Migration { override async up(): Promise { this.addSql( - `create table if not exists "brand" ("id" text not null, "name" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "brand_pkey" primary key ("id"));` + `create table if not exists "brand" ("id" text not null, "name" text not null, "status" text default 'active', "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "brand_pkey" primary key ("id"));` ) this.addSql( `CREATE INDEX IF NOT EXISTS "IDX_brand_deleted_at" ON "brand" (deleted_at) WHERE deleted_at IS NULL;` diff --git a/integration-tests/modules/src/modules/brand/models/brand.ts b/integration-tests/modules/src/modules/brand/models/brand.ts index 6c3795fb0a..b69651c31f 100644 --- a/integration-tests/modules/src/modules/brand/models/brand.ts +++ b/integration-tests/modules/src/modules/brand/models/brand.ts @@ -3,4 +3,5 @@ import { model } from "@medusajs/utils" export const Brand = model.define("brand", { id: model.id({ prefix: "brand" }).primaryKey(), name: model.text(), + status: model.enum(["active", "inactive"]).default("active"), }) diff --git a/packages/modules/index/src/utils/build-config.ts b/packages/modules/index/src/utils/build-config.ts index 2ad8cd861f..515bc155cc 100644 --- a/packages/modules/index/src/utils/build-config.ts +++ b/packages/modules/index/src/utils/build-config.ts @@ -1180,11 +1180,13 @@ function buildSchemaFromFilterableLinks( return } - const isEnum = - fieldRef.type?.astNode?.kind === GraphQLUtils.Kind.ENUM_TYPE_DEFINITION - const fieldType = isEnum ? "String" : fieldRef.type.toString() + const fieldType = fieldRef.type.toString() const isArray = fieldType.startsWith("[") - const currentType = fieldType.replace(/\[|\]|\!/g, "") + let currentType = fieldType.replace(/\[|\]|\!/g, "") + const isEnum = currentType.endsWith("Enum") + if (isEnum) { + currentType = "String" + } return isArray ? `[${currentType}]` : currentType }