Skip to content

Commit ebe5cc7

Browse files
chore(index): return ids only (medusajs#12543)
1 parent fca5ad7 commit ebe5cc7

File tree

12 files changed

+92
-16
lines changed

12 files changed

+92
-16
lines changed

.changeset/shaggy-pillows-cough.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@medusajs/modules-sdk": patch
3+
"@medusajs/index": patch
4+
"@medusajs/types": patch
5+
"@medusajs/orchestration": patch
6+
---
7+
8+
chore(index): return ids only

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,18 @@ medusaIntegrationTestRunner({
175175
},
176176
},
177177
],
178-
prices: expect.arrayContaining([]),
178+
prices: expect.arrayContaining([
179+
{
180+
currency_code: "CAD",
181+
amount: 20,
182+
id: expect.any(String),
183+
},
184+
{
185+
currency_code: "USD",
186+
amount: 80,
187+
id: expect.any(String),
188+
},
189+
]),
179190
},
180191
{
181192
sku: "extra-variant-1",

integration-tests/modules/__tests__/product/workflows/batch-products.spec.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import {
44
batchProductVariantsWorkflow,
55
batchProductVariantsWorkflowId,
66
} from "@medusajs/core-flows"
7+
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
78
import {
89
IFulfillmentModuleService,
910
IProductModuleService,
1011
} from "@medusajs/types"
1112
import { Modules } from "@medusajs/utils"
12-
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
1313

1414
jest.setTimeout(50000)
1515

@@ -165,14 +165,16 @@ medusaIntegrationTestRunner({
165165
})
166166

167167
expect(product.variants).toHaveLength(2)
168-
expect(product.variants).toEqual([
169-
expect.objectContaining({
170-
title: "variant1",
171-
}),
172-
expect.objectContaining({
173-
title: "variant2",
174-
}),
175-
])
168+
expect(product.variants).toEqual(
169+
expect.arrayContaining([
170+
expect.objectContaining({
171+
title: "variant1",
172+
}),
173+
expect.objectContaining({
174+
title: "variant2",
175+
}),
176+
])
177+
)
176178
})
177179
})
178180
})

packages/core/modules-sdk/src/remote-query/query.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export class Query {
204204

205205
const mainEntity = queryOptions.entity
206206

207-
const fields = queryOptions.fields.map((field) => mainEntity + "." + field)
207+
const fields = [mainEntity + ".id"]
208208
const filters = queryOptions.filters
209209
? { [mainEntity]: queryOptions.filters }
210210
: ({} as any)
@@ -223,10 +223,17 @@ export class Query {
223223
filters,
224224
joinFilters,
225225
pagination,
226+
idsOnly: true,
226227
})) as unknown as GraphResultSet<TEntry>
227228

228229
delete queryOptions.filters
229230

231+
const idFilters = {
232+
id: indexResponse.data.map((item) => item.id),
233+
} as any
234+
235+
queryOptions.filters = idFilters
236+
230237
const graphOptions: RemoteQueryInput<TEntry> = {
231238
...queryOptions,
232239
pagination: {

packages/core/orchestration/src/joiner/remote-joiner.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type InternalParseExpandsParams = {
4545
implodeMapping: InternalImplodeMapping[]
4646
options?: RemoteJoinerOptions
4747
initialData?: any[]
48+
initialDataOnly?: boolean
4849
}
4950

5051
export class RemoteJoiner {
@@ -922,6 +923,7 @@ export class RemoteJoiner {
922923
implodeMapping,
923924
options,
924925
initialData,
926+
initialDataOnly,
925927
} = params
926928

927929
const { parsedExpands, aliasRealPathMap } = this.parseProperties({
@@ -932,7 +934,7 @@ export class RemoteJoiner {
932934
implodeMapping,
933935
})
934936

935-
if (initialData?.length) {
937+
if (initialData?.length && initialDataOnly) {
936938
this.createFilterFromInitialData({
937939
initialData: options?.initialData as any,
938940
parsedExpands,
@@ -1524,6 +1526,7 @@ export class RemoteJoiner {
15241526
implodeMapping,
15251527
options,
15261528
initialData: iniDataArray,
1529+
initialDataOnly: options?.initialDataOnly,
15271530
}
15281531

15291532
const parsedExpands = this.parseExpands(parseExpandsConfig)

packages/core/types/src/index-data/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export * from "./common"
22
export * from "./index-service-entry-points"
33
export * from "./query-config"
44
export * from "./service"
5-
export * from "./sotrage-provider"
5+
export * from "./storage-provider"

packages/core/types/src/index-data/query-config/query-input-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export type IndexQueryConfig<TEntry extends string> = {
6363
filters?: IndexFilters<TEntry>
6464
joinFilters?: IndexFilters<TEntry>
6565
pagination?: Partial<IndexQueryInput<TEntry>["pagination"]>
66+
idsOnly?: boolean
6667
}
6768

6869
export type QueryFunctionReturnPagination = {

packages/core/types/src/index-data/sotrage-provider.ts renamed to packages/core/types/src/index-data/storage-provider.ts

File renamed without changes.

packages/core/types/src/joiner/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export interface RemoteJoinerOptions {
8686
throwIfKeyNotFound?: boolean
8787
throwIfRelationNotFound?: boolean | string[]
8888
initialData?: object | object[]
89+
initialDataOnly?: boolean
8990
}
9091

9192
export interface RemoteNestedExpands {

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,42 @@ describe("IndexModuleService query", function () {
536536
])
537537
})
538538

539+
it("should query all products ordered by price returning only ids", async () => {
540+
const { data } = await module.query({
541+
fields: ["product.*", "product.variants.*"],
542+
idsOnly: true,
543+
pagination: {
544+
order: {
545+
product: {
546+
variants: {
547+
prices: {
548+
amount: "DESC",
549+
},
550+
},
551+
},
552+
},
553+
},
554+
})
555+
556+
expect(data).toEqual([
557+
{
558+
id: "prod_2",
559+
variants: [],
560+
},
561+
{
562+
id: "prod_1",
563+
variants: [
564+
{
565+
id: "var_1",
566+
},
567+
{
568+
id: "var_2",
569+
},
570+
],
571+
},
572+
])
573+
})
574+
539575
it("should query products filtering by variant sku", async () => {
540576
const { data, metadata } = await module.query({
541577
fields: ["product.*", "product.variants.*", "product.variants.prices.*"],

0 commit comments

Comments
 (0)