fix(index): index enum fields (#13428)

https://github.com/medusajs/medusa/issues/13372

What:
 - It handles enum fields corretly when added to filterable fields
This commit is contained in:
Carlos R. L. Rodrigues
2025-09-07 13:39:58 +00:00
committed by GitHub
parent 637d4cf7ef
commit 71d8a0031f
6 changed files with 57 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/index": patch
---
fix(index): index enum fields
@@ -514,6 +514,49 @@ medusaIntegrationTestRunner({
expect(resultset.data.length).toEqual(1) expect(resultset.data.length).toEqual(1)
expect(resultset.data[0].origin_country).toEqual("USA") 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)
})
}) })
}, },
}) })
@@ -12,7 +12,7 @@ const link =
}, },
{ {
linkable: BrandModule.linkable.brand.id, linkable: BrandModule.linkable.brand.id,
filterable: ["id", "name"], filterable: ["id", "name", "status"],
isList: false, isList: false,
} }
) )
@@ -3,7 +3,7 @@ import { Migration } from "@mikro-orm/migrations"
export class Migration20250805184935 extends Migration { export class Migration20250805184935 extends Migration {
override async up(): Promise<void> { override async up(): Promise<void> {
this.addSql( 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( this.addSql(
`CREATE INDEX IF NOT EXISTS "IDX_brand_deleted_at" ON "brand" (deleted_at) WHERE deleted_at IS NULL;` `CREATE INDEX IF NOT EXISTS "IDX_brand_deleted_at" ON "brand" (deleted_at) WHERE deleted_at IS NULL;`
@@ -3,4 +3,5 @@ import { model } from "@medusajs/utils"
export const Brand = model.define("brand", { export const Brand = model.define("brand", {
id: model.id({ prefix: "brand" }).primaryKey(), id: model.id({ prefix: "brand" }).primaryKey(),
name: model.text(), name: model.text(),
status: model.enum(["active", "inactive"]).default("active"),
}) })
@@ -1180,11 +1180,13 @@ function buildSchemaFromFilterableLinks(
return return
} }
const isEnum = const fieldType = fieldRef.type.toString()
fieldRef.type?.astNode?.kind === GraphQLUtils.Kind.ENUM_TYPE_DEFINITION
const fieldType = isEnum ? "String" : fieldRef.type.toString()
const isArray = fieldType.startsWith("[") 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 return isArray ? `[${currentType}]` : currentType
} }