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

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