chore(product, modules-sdk): Add missing index for select-in strategy + allow to pass top level strategy to force the behaviour (#12508)

**What**
- Add missing index for query that falls in select in strategy, specifically the heaviest one with variant IN filtering
- Allow to force the strategy to be sent to the entry point module when using the graph API. It is useful with big dataset where filtering is enough without pagination and select in can offer better performances

e.g
```ts
       await query.graph({
            entity: 'product',
            fields,
            filters,
            strategy: 'select-in' // <-- this will force the module to use receive this value and apply it accordingly 
        })
```
This commit is contained in:
Adrien de Peretti
2025-05-19 17:57:58 +02:00
committed by GitHub
parent 397d353af6
commit 52bd9f9a53
9 changed files with 70 additions and 1 deletions
+7
View File
@@ -0,0 +1,7 @@
---
"@medusajs/product": patch
"@medusajs/modules-sdk": patch
"@medusajs/types": patch
---
chore(product, modules-sdk): Add missing index for select-in strategy + allow to pass top level strategy to force the behaviour
@@ -40,6 +40,28 @@ describe("toRemoteQuery", () => {
})
})
it("should transform a query with strategy", () => {
const format = toRemoteQuery(
{
entity: "product",
fields: ["id", "handle", "description"],
strategy: "select-in",
},
entitiesMap
)
expect(format).toEqual({
product: {
__fields: ["id", "handle", "description"],
__args: {
options: {
strategy: "select-in",
},
},
},
})
})
it("should transform a query with pagination", () => {
const format = toRemoteQuery(
{
@@ -159,6 +159,7 @@ export class Query {
queryOptions,
this.#remoteQuery.getEntitiesMap()
)
let response:
| any[]
| { rows: any[]; metadata: RemoteQueryFunctionReturnPagination }
@@ -313,6 +313,7 @@ export class RemoteQuery {
"sort",
"order",
"withDeleted",
"options",
]
const availableOptionsAlias = new Map([
["limit", "take"],
@@ -37,6 +37,7 @@ export function toRemoteQuery<const TEntity extends string>(
pagination?: Partial<RemoteQueryInput<TEntity>["pagination"]>
context?: Record<string, any>
withDeleted?: boolean
strategy?: "joined" | "select-in"
},
entitiesMap: Map<string, any>
): RemoteQueryGraph<TEntity> {
@@ -46,6 +47,7 @@ export function toRemoteQuery<const TEntity extends string>(
filters = {},
context = {},
withDeleted,
strategy,
} = config
const joinerQuery: Record<string, any> = {
@@ -130,6 +132,12 @@ export function toRemoteQuery<const TEntity extends string>(
}
}
if (strategy) {
joinerQuery[entity][ARGUMENTS] ??= {} as any
joinerQuery[entity][ARGUMENTS]["options"] ??= {} as any
joinerQuery[entity][ARGUMENTS]["options"]["strategy"] = strategy
}
if (withDeleted) {
joinerQuery[entity][ARGUMENTS] ??= {} as any
joinerQuery[entity][ARGUMENTS]["withDeleted"] = true
@@ -71,6 +71,10 @@ export type RemoteQueryInput<TEntry extends string> = {
* Apply a `withDeleted` flag on the retrieved data to retrieve soft deleted items.
*/
withDeleted?: boolean
/**
* Strategy will be send to the entry module called method
*/
strategy?: "joined" | "select-in"
}
export type RemoteQueryGraph<TEntry extends string> = {
@@ -1582,6 +1582,15 @@
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_product_variant_deleted_at\" ON \"product_variant\" (deleted_at) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_product_variant_id_product_id",
"columnNames": [],
"composite": false,
"constraint": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_product_variant_id_product_id\" ON \"product_variant\" (id, product_id) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_product_variant_sku_unique",
"columnNames": [],
@@ -0,0 +1,13 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20250516081326 extends Migration {
override async up(): Promise<void> {
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_product_variant_id_product_id" ON "product_variant" (id, product_id) WHERE deleted_at IS NULL;`);
}
override async down(): Promise<void> {
this.addSql(`drop index if exists "IDX_product_variant_id_product_id";`);
}
}
@@ -35,10 +35,14 @@ const ProductVariant = model
}),
})
.indexes([
{
name: "IDX_product_variant_id_product_id",
on: ["id", "product_id"],
where: "deleted_at IS NULL",
},
{
name: "IDX_product_variant_product_id",
on: ["product_id"],
unique: false,
where: "deleted_at IS NULL",
},
{