chore(index): return ids only (#12543)

This commit is contained in:
Carlos R. L. Rodrigues
2025-05-20 11:16:02 -03:00
committed by GitHub
parent fca5ad77b4
commit ebe5cc7acd
12 changed files with 92 additions and 16 deletions

View File

@@ -0,0 +1,8 @@
---
"@medusajs/modules-sdk": patch
"@medusajs/index": patch
"@medusajs/types": patch
"@medusajs/orchestration": patch
---
chore(index): return ids only

View File

@@ -175,7 +175,18 @@ medusaIntegrationTestRunner({
},
},
],
prices: expect.arrayContaining([]),
prices: expect.arrayContaining([
{
currency_code: "CAD",
amount: 20,
id: expect.any(String),
},
{
currency_code: "USD",
amount: 80,
id: expect.any(String),
},
]),
},
{
sku: "extra-variant-1",

View File

@@ -4,12 +4,12 @@ import {
batchProductVariantsWorkflow,
batchProductVariantsWorkflowId,
} from "@medusajs/core-flows"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
IFulfillmentModuleService,
IProductModuleService,
} from "@medusajs/types"
import { Modules } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
jest.setTimeout(50000)
@@ -165,14 +165,16 @@ medusaIntegrationTestRunner({
})
expect(product.variants).toHaveLength(2)
expect(product.variants).toEqual([
expect.objectContaining({
title: "variant1",
}),
expect.objectContaining({
title: "variant2",
}),
])
expect(product.variants).toEqual(
expect.arrayContaining([
expect.objectContaining({
title: "variant1",
}),
expect.objectContaining({
title: "variant2",
}),
])
)
})
})
})

View File

@@ -204,7 +204,7 @@ export class Query {
const mainEntity = queryOptions.entity
const fields = queryOptions.fields.map((field) => mainEntity + "." + field)
const fields = [mainEntity + ".id"]
const filters = queryOptions.filters
? { [mainEntity]: queryOptions.filters }
: ({} as any)
@@ -223,10 +223,17 @@ export class Query {
filters,
joinFilters,
pagination,
idsOnly: true,
})) as unknown as GraphResultSet<TEntry>
delete queryOptions.filters
const idFilters = {
id: indexResponse.data.map((item) => item.id),
} as any
queryOptions.filters = idFilters
const graphOptions: RemoteQueryInput<TEntry> = {
...queryOptions,
pagination: {

View File

@@ -45,6 +45,7 @@ type InternalParseExpandsParams = {
implodeMapping: InternalImplodeMapping[]
options?: RemoteJoinerOptions
initialData?: any[]
initialDataOnly?: boolean
}
export class RemoteJoiner {
@@ -922,6 +923,7 @@ export class RemoteJoiner {
implodeMapping,
options,
initialData,
initialDataOnly,
} = params
const { parsedExpands, aliasRealPathMap } = this.parseProperties({
@@ -932,7 +934,7 @@ export class RemoteJoiner {
implodeMapping,
})
if (initialData?.length) {
if (initialData?.length && initialDataOnly) {
this.createFilterFromInitialData({
initialData: options?.initialData as any,
parsedExpands,
@@ -1524,6 +1526,7 @@ export class RemoteJoiner {
implodeMapping,
options,
initialData: iniDataArray,
initialDataOnly: options?.initialDataOnly,
}
const parsedExpands = this.parseExpands(parseExpandsConfig)

View File

@@ -2,4 +2,4 @@ export * from "./common"
export * from "./index-service-entry-points"
export * from "./query-config"
export * from "./service"
export * from "./sotrage-provider"
export * from "./storage-provider"

View File

@@ -63,6 +63,7 @@ export type IndexQueryConfig<TEntry extends string> = {
filters?: IndexFilters<TEntry>
joinFilters?: IndexFilters<TEntry>
pagination?: Partial<IndexQueryInput<TEntry>["pagination"]>
idsOnly?: boolean
}
export type QueryFunctionReturnPagination = {

View File

@@ -86,6 +86,7 @@ export interface RemoteJoinerOptions {
throwIfKeyNotFound?: boolean
throwIfRelationNotFound?: boolean | string[]
initialData?: object | object[]
initialDataOnly?: boolean
}
export interface RemoteNestedExpands {

View File

@@ -536,6 +536,42 @@ describe("IndexModuleService query", function () {
])
})
it("should query all products ordered by price returning only ids", async () => {
const { data } = await module.query({
fields: ["product.*", "product.variants.*"],
idsOnly: true,
pagination: {
order: {
product: {
variants: {
prices: {
amount: "DESC",
},
},
},
},
},
})
expect(data).toEqual([
{
id: "prod_2",
variants: [],
},
{
id: "prod_1",
variants: [
{
id: "var_1",
},
{
id: "var_2",
},
],
},
])
})
it("should query products filtering by variant sku", async () => {
const { data, metadata } = await module.query({
fields: ["product.*", "product.variants.*", "product.variants.prices.*"],

View File

@@ -237,7 +237,7 @@ export class PostgresProvider implements IndexTypes.StorageProvider {
): Promise<IndexTypes.QueryResultSet<TEntry>> {
await this.#isReady_
const { fields = [], filters = {}, joinFilters = {} } = config
const { fields = [], filters = {}, joinFilters = {}, idsOnly } = config
const { take, skip, order: inputOrderBy = {} } = config.pagination ?? {}
const select = normalizeFieldsSelection(fields)
@@ -280,6 +280,7 @@ export class PostgresProvider implements IndexTypes.StorageProvider {
},
rawConfig: config,
requestedFields,
idsOnly,
})
const { sql, sqlCount } = qb.buildQuery({

View File

@@ -73,6 +73,7 @@ export class QueryBuilder {
private readonly requestedFields: {
[key: string]: any
}
private readonly idsOnly?: boolean
constructor(args: {
schema: IndexTypes.SchemaObjectRepresentation
@@ -84,6 +85,7 @@ export class QueryBuilder {
requestedFields: {
[key: string]: any
}
idsOnly?: boolean
}) {
this.schema = args.schema
this.entityMap = args.entityMap
@@ -96,6 +98,7 @@ export class QueryBuilder {
)
this.rawConfig = args.rawConfig
this.requestedFields = args.requestedFields
this.idsOnly = args.idsOnly ?? false
}
private getStructureKeys(structure) {
@@ -643,7 +646,7 @@ export class QueryBuilder {
if (parentProperty === "id") {
selectParts[currentAliasPath] = `${alias}.id`
} else {
} else if (!this.idsOnly) {
selectParts[currentAliasPath] = this.knex.raw(
`${alias}.data->'${parentProperty}'`
)
@@ -657,7 +660,10 @@ export class QueryBuilder {
return selectParts
}
selectParts[currentAliasPath] = `${alias}.data`
if (!this.idsOnly) {
selectParts[currentAliasPath] = `${alias}.data`
}
selectParts[currentAliasPath + ".id"] = `${alias}.id`
const children = this.getStructureKeys(structure)