Skip to content

Commit 71d8a00

Browse files
fix(index): index enum fields (medusajs#13428)
medusajs#13372 What: - It handles enum fields corretly when added to filterable fields
1 parent 637d4cf commit 71d8a00

File tree

6 files changed

+57
-6
lines changed

6 files changed

+57
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/index": patch
3+
---
4+
5+
fix(index): index enum fields

integration-tests/modules/__tests__/index/query-index.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,49 @@ medusaIntegrationTestRunner({
514514
expect(resultset.data.length).toEqual(1)
515515
expect(resultset.data[0].origin_country).toEqual("USA")
516516
})
517+
518+
it("should use query.index to filter enum field", async () => {
519+
const products = await populateData(api)
520+
521+
const brandModule = appContainer.resolve("brand")
522+
const link = appContainer.resolve(ContainerRegistrationKeys.LINK)
523+
const brand = await brandModule.createBrands({
524+
name: "Medusa Brand",
525+
})
526+
527+
await link.create({
528+
[Modules.PRODUCT]: {
529+
product_id: products.find((p) => p.title === "Extra product").id,
530+
},
531+
brand: {
532+
brand_id: brand.id,
533+
},
534+
})
535+
536+
const query = appContainer.resolve(
537+
ContainerRegistrationKeys.QUERY
538+
) as RemoteQueryFunction
539+
540+
const resultset = await fetchAndRetry(
541+
async () =>
542+
await query.index({
543+
entity: "product",
544+
fields: ["id"],
545+
filters: {
546+
status: "published",
547+
brand: {
548+
status: "active",
549+
},
550+
},
551+
}),
552+
({ data }) => data.length > 0,
553+
{
554+
retries: 3,
555+
waitSeconds: 3,
556+
}
557+
)
558+
expect(resultset.data.length).toEqual(1)
559+
})
517560
})
518561
},
519562
})

integration-tests/modules/src/links/product-brand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const link =
1212
},
1313
{
1414
linkable: BrandModule.linkable.brand.id,
15-
filterable: ["id", "name"],
15+
filterable: ["id", "name", "status"],
1616
isList: false,
1717
}
1818
)

integration-tests/modules/src/modules/brand/migrations/Migration20250805184935.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Migration } from "@mikro-orm/migrations"
33
export class Migration20250805184935 extends Migration {
44
override async up(): Promise<void> {
55
this.addSql(
6-
`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"));`
6+
`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"));`
77
)
88
this.addSql(
99
`CREATE INDEX IF NOT EXISTS "IDX_brand_deleted_at" ON "brand" (deleted_at) WHERE deleted_at IS NULL;`

integration-tests/modules/src/modules/brand/models/brand.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import { model } from "@medusajs/utils"
33
export const Brand = model.define("brand", {
44
id: model.id({ prefix: "brand" }).primaryKey(),
55
name: model.text(),
6+
status: model.enum(["active", "inactive"]).default("active"),
67
})

packages/modules/index/src/utils/build-config.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,11 +1180,13 @@ function buildSchemaFromFilterableLinks(
11801180
return
11811181
}
11821182

1183-
const isEnum =
1184-
fieldRef.type?.astNode?.kind === GraphQLUtils.Kind.ENUM_TYPE_DEFINITION
1185-
const fieldType = isEnum ? "String" : fieldRef.type.toString()
1183+
const fieldType = fieldRef.type.toString()
11861184
const isArray = fieldType.startsWith("[")
1187-
const currentType = fieldType.replace(/\[|\]|\!/g, "")
1185+
let currentType = fieldType.replace(/\[|\]|\!/g, "")
1186+
const isEnum = currentType.endsWith("Enum")
1187+
if (isEnum) {
1188+
currentType = "String"
1189+
}
11881190

11891191
return isArray ? `[${currentType}]` : currentType
11901192
}

0 commit comments

Comments
 (0)