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 10:39:58 -03:00
committed by GitHub
parent 637d4cf7ef
commit 71d8a0031f
6 changed files with 57 additions and 6 deletions

View File

@@ -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)
})
})
},
})

View File

@@ -12,7 +12,7 @@ const link =
},
{
linkable: BrandModule.linkable.brand.id,
filterable: ["id", "name"],
filterable: ["id", "name", "status"],
isList: false,
}
)

View File

@@ -3,7 +3,7 @@ import { Migration } from "@mikro-orm/migrations"
export class Migration20250805184935 extends Migration {
override async up(): Promise<void> {
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;`

View File

@@ -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"),
})