chore(index): return ids only (#12543)
This commit is contained in:
1 parent
fca5ad77b4
commit
ebe5cc7acd
12 files changed
+84
-8
No files matched your search
@@ -0,0 +1,8 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/modules-sdk": patch
|
||||||
|
"@medusajs/index": patch
|
||||||
|
"@medusajs/types": patch
|
||||||
|
"@medusajs/orchestration": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
chore(index): return ids only
|
||||||
@@ -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",
|
sku: "extra-variant-1",
|
||||||
|
|||||||
@@ -4,12 +4,12 @@ import {
|
|||||||
batchProductVariantsWorkflow,
|
batchProductVariantsWorkflow,
|
||||||
batchProductVariantsWorkflowId,
|
batchProductVariantsWorkflowId,
|
||||||
} from "@medusajs/core-flows"
|
} from "@medusajs/core-flows"
|
||||||
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
||||||
import {
|
import {
|
||||||
IFulfillmentModuleService,
|
IFulfillmentModuleService,
|
||||||
IProductModuleService,
|
IProductModuleService,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { Modules } from "@medusajs/utils"
|
import { Modules } from "@medusajs/utils"
|
||||||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
|
||||||
|
|
||||||
jest.setTimeout(50000)
|
jest.setTimeout(50000)
|
||||||
|
|
||||||
@@ -165,7 +165,8 @@ medusaIntegrationTestRunner({
|
|||||||
})
|
})
|
||||||
|
|
||||||
expect(product.variants).toHaveLength(2)
|
expect(product.variants).toHaveLength(2)
|
||||||
expect(product.variants).toEqual([
|
expect(product.variants).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
title: "variant1",
|
title: "variant1",
|
||||||
}),
|
}),
|
||||||
@@ -173,6 +174,7 @@ medusaIntegrationTestRunner({
|
|||||||
title: "variant2",
|
title: "variant2",
|
||||||
}),
|
}),
|
||||||
])
|
])
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -204,7 +204,7 @@ export class Query {
|
|||||||
|
|
||||||
const mainEntity = queryOptions.entity
|
const mainEntity = queryOptions.entity
|
||||||
|
|
||||||
const fields = queryOptions.fields.map((field) => mainEntity + "." + field)
|
const fields = [mainEntity + ".id"]
|
||||||
const filters = queryOptions.filters
|
const filters = queryOptions.filters
|
||||||
? { [mainEntity]: queryOptions.filters }
|
? { [mainEntity]: queryOptions.filters }
|
||||||
: ({} as any)
|
: ({} as any)
|
||||||
@@ -223,10 +223,17 @@ export class Query {
|
|||||||
filters,
|
filters,
|
||||||
joinFilters,
|
joinFilters,
|
||||||
pagination,
|
pagination,
|
||||||
|
idsOnly: true,
|
||||||
})) as unknown as GraphResultSet<TEntry>
|
})) as unknown as GraphResultSet<TEntry>
|
||||||
|
|
||||||
delete queryOptions.filters
|
delete queryOptions.filters
|
||||||
|
|
||||||
|
const idFilters = {
|
||||||
|
id: indexResponse.data.map((item) => item.id),
|
||||||
|
} as any
|
||||||
|
|
||||||
|
queryOptions.filters = idFilters
|
||||||
|
|
||||||
const graphOptions: RemoteQueryInput<TEntry> = {
|
const graphOptions: RemoteQueryInput<TEntry> = {
|
||||||
...queryOptions,
|
...queryOptions,
|
||||||
pagination: {
|
pagination: {
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ type InternalParseExpandsParams = {
|
|||||||
implodeMapping: InternalImplodeMapping[]
|
implodeMapping: InternalImplodeMapping[]
|
||||||
options?: RemoteJoinerOptions
|
options?: RemoteJoinerOptions
|
||||||
initialData?: any[]
|
initialData?: any[]
|
||||||
|
initialDataOnly?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export class RemoteJoiner {
|
export class RemoteJoiner {
|
||||||
@@ -922,6 +923,7 @@ export class RemoteJoiner {
|
|||||||
implodeMapping,
|
implodeMapping,
|
||||||
options,
|
options,
|
||||||
initialData,
|
initialData,
|
||||||
|
initialDataOnly,
|
||||||
} = params
|
} = params
|
||||||
|
|
||||||
const { parsedExpands, aliasRealPathMap } = this.parseProperties({
|
const { parsedExpands, aliasRealPathMap } = this.parseProperties({
|
||||||
@@ -932,7 +934,7 @@ export class RemoteJoiner {
|
|||||||
implodeMapping,
|
implodeMapping,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (initialData?.length) {
|
if (initialData?.length && initialDataOnly) {
|
||||||
this.createFilterFromInitialData({
|
this.createFilterFromInitialData({
|
||||||
initialData: options?.initialData as any,
|
initialData: options?.initialData as any,
|
||||||
parsedExpands,
|
parsedExpands,
|
||||||
@@ -1524,6 +1526,7 @@ export class RemoteJoiner {
|
|||||||
implodeMapping,
|
implodeMapping,
|
||||||
options,
|
options,
|
||||||
initialData: iniDataArray,
|
initialData: iniDataArray,
|
||||||
|
initialDataOnly: options?.initialDataOnly,
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsedExpands = this.parseExpands(parseExpandsConfig)
|
const parsedExpands = this.parseExpands(parseExpandsConfig)
|
||||||
|
|||||||
@@ -2,4 +2,4 @@ export * from "./common"
|
|||||||
export * from "./index-service-entry-points"
|
export * from "./index-service-entry-points"
|
||||||
export * from "./query-config"
|
export * from "./query-config"
|
||||||
export * from "./service"
|
export * from "./service"
|
||||||
export * from "./sotrage-provider"
|
export * from "./storage-provider"
|
||||||
@@ -63,6 +63,7 @@ export type IndexQueryConfig<TEntry extends string> = {
|
|||||||
filters?: IndexFilters<TEntry>
|
filters?: IndexFilters<TEntry>
|
||||||
joinFilters?: IndexFilters<TEntry>
|
joinFilters?: IndexFilters<TEntry>
|
||||||
pagination?: Partial<IndexQueryInput<TEntry>["pagination"]>
|
pagination?: Partial<IndexQueryInput<TEntry>["pagination"]>
|
||||||
|
idsOnly?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export type QueryFunctionReturnPagination = {
|
export type QueryFunctionReturnPagination = {
|
||||||
|
|||||||
File renamed without changes.
@@ -86,6 +86,7 @@ export interface RemoteJoinerOptions {
|
|||||||
throwIfKeyNotFound?: boolean
|
throwIfKeyNotFound?: boolean
|
||||||
throwIfRelationNotFound?: boolean | string[]
|
throwIfRelationNotFound?: boolean | string[]
|
||||||
initialData?: object | object[]
|
initialData?: object | object[]
|
||||||
|
initialDataOnly?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RemoteNestedExpands {
|
export interface RemoteNestedExpands {
|
||||||
|
|||||||
@@ -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 () => {
|
it("should query products filtering by variant sku", async () => {
|
||||||
const { data, metadata } = await module.query({
|
const { data, metadata } = await module.query({
|
||||||
fields: ["product.*", "product.variants.*", "product.variants.prices.*"],
|
fields: ["product.*", "product.variants.*", "product.variants.prices.*"],
|
||||||
|
|||||||
@@ -237,7 +237,7 @@ export class PostgresProvider implements IndexTypes.StorageProvider {
|
|||||||
): Promise<IndexTypes.QueryResultSet<TEntry>> {
|
): Promise<IndexTypes.QueryResultSet<TEntry>> {
|
||||||
await this.#isReady_
|
await this.#isReady_
|
||||||
|
|
||||||
const { fields = [], filters = {}, joinFilters = {} } = config
|
const { fields = [], filters = {}, joinFilters = {}, idsOnly } = config
|
||||||
const { take, skip, order: inputOrderBy = {} } = config.pagination ?? {}
|
const { take, skip, order: inputOrderBy = {} } = config.pagination ?? {}
|
||||||
|
|
||||||
const select = normalizeFieldsSelection(fields)
|
const select = normalizeFieldsSelection(fields)
|
||||||
@@ -280,6 +280,7 @@ export class PostgresProvider implements IndexTypes.StorageProvider {
|
|||||||
},
|
},
|
||||||
rawConfig: config,
|
rawConfig: config,
|
||||||
requestedFields,
|
requestedFields,
|
||||||
|
idsOnly,
|
||||||
})
|
})
|
||||||
|
|
||||||
const { sql, sqlCount } = qb.buildQuery({
|
const { sql, sqlCount } = qb.buildQuery({
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ export class QueryBuilder {
|
|||||||
private readonly requestedFields: {
|
private readonly requestedFields: {
|
||||||
[key: string]: any
|
[key: string]: any
|
||||||
}
|
}
|
||||||
|
private readonly idsOnly?: boolean
|
||||||
|
|
||||||
constructor(args: {
|
constructor(args: {
|
||||||
schema: IndexTypes.SchemaObjectRepresentation
|
schema: IndexTypes.SchemaObjectRepresentation
|
||||||
@@ -84,6 +85,7 @@ export class QueryBuilder {
|
|||||||
requestedFields: {
|
requestedFields: {
|
||||||
[key: string]: any
|
[key: string]: any
|
||||||
}
|
}
|
||||||
|
idsOnly?: boolean
|
||||||
}) {
|
}) {
|
||||||
this.schema = args.schema
|
this.schema = args.schema
|
||||||
this.entityMap = args.entityMap
|
this.entityMap = args.entityMap
|
||||||
@@ -96,6 +98,7 @@ export class QueryBuilder {
|
|||||||
)
|
)
|
||||||
this.rawConfig = args.rawConfig
|
this.rawConfig = args.rawConfig
|
||||||
this.requestedFields = args.requestedFields
|
this.requestedFields = args.requestedFields
|
||||||
|
this.idsOnly = args.idsOnly ?? false
|
||||||
}
|
}
|
||||||
|
|
||||||
private getStructureKeys(structure) {
|
private getStructureKeys(structure) {
|
||||||
@@ -643,7 +646,7 @@ export class QueryBuilder {
|
|||||||
|
|
||||||
if (parentProperty === "id") {
|
if (parentProperty === "id") {
|
||||||
selectParts[currentAliasPath] = `${alias}.id`
|
selectParts[currentAliasPath] = `${alias}.id`
|
||||||
} else {
|
} else if (!this.idsOnly) {
|
||||||
selectParts[currentAliasPath] = this.knex.raw(
|
selectParts[currentAliasPath] = this.knex.raw(
|
||||||
`${alias}.data->'${parentProperty}'`
|
`${alias}.data->'${parentProperty}'`
|
||||||
)
|
)
|
||||||
@@ -657,7 +660,10 @@ export class QueryBuilder {
|
|||||||
return selectParts
|
return selectParts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!this.idsOnly) {
|
||||||
selectParts[currentAliasPath] = `${alias}.data`
|
selectParts[currentAliasPath] = `${alias}.data`
|
||||||
|
}
|
||||||
|
|
||||||
selectParts[currentAliasPath + ".id"] = `${alias}.id`
|
selectParts[currentAliasPath + ".id"] = `${alias}.id`
|
||||||
|
|
||||||
const children = this.getStructureKeys(structure)
|
const children = this.getStructureKeys(structure)
|
||||||
|
|||||||
Reference in new issue
Block a user