From de8f748674bfd19b3dbadb9695d9080aa91940de Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Mon, 27 Nov 2023 15:42:25 +0000 Subject: [PATCH] fix(link-modules, utils): remove limits on queries when primary-key is provided (#5732) --- .changeset/mighty-windows-perform.md | 6 ++++++ .../src/services/link-module-service.ts | 11 +++++++++- .../src/services/__tests__/product.spec.ts | 4 ++-- packages/utils/src/modules-sdk/build-query.ts | 20 ++++++++++++++++--- 4 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 .changeset/mighty-windows-perform.md diff --git a/.changeset/mighty-windows-perform.md b/.changeset/mighty-windows-perform.md new file mode 100644 index 0000000000000..46050fc9acad1 --- /dev/null +++ b/.changeset/mighty-windows-perform.md @@ -0,0 +1,6 @@ +--- +"@medusajs/link-modules": patch +"@medusajs/utils": patch +--- + +fix(link-modules, utils): remove limtis if primary key exists in filter for buildquery and no limit is present diff --git a/packages/link-modules/src/services/link-module-service.ts b/packages/link-modules/src/services/link-module-service.ts index 1569afdc79113..692e7ae987733 100644 --- a/packages/link-modules/src/services/link-module-service.ts +++ b/packages/link-modules/src/services/link-module-service.ts @@ -11,11 +11,12 @@ import { import { InjectManager, InjectTransactionManager, + isDefined, + mapObjectTo, MapToConfig, MedusaContext, MedusaError, ModulesSdkUtils, - mapObjectTo, } from "@medusajs/utils" import { LinkService } from "@services" import { shouldForceTransaction } from "../utils" @@ -126,6 +127,10 @@ export default class LinkModuleService implements ILinkModule { config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} ): Promise { + if (!isDefined(config.take)) { + config.take = null + } + const rows = await this.linkService_.list(filters, config, sharedContext) return await this.baseRepository_.serialize(rows) @@ -137,6 +142,10 @@ export default class LinkModuleService implements ILinkModule { config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} ): Promise<[unknown[], number]> { + if (!isDefined(config.take)) { + config.take = null + } + const [rows, count] = await this.linkService_.listAndCount( filters, config, diff --git a/packages/product/src/services/__tests__/product.spec.ts b/packages/product/src/services/__tests__/product.spec.ts index 0f2e1d12d1a91..6d2820a19e9c7 100644 --- a/packages/product/src/services/__tests__/product.spec.ts +++ b/packages/product/src/services/__tests__/product.spec.ts @@ -19,7 +19,7 @@ describe("Product service", function () { }, options: { fields: undefined, - limit: 15, + limit: undefined, offset: 0, populate: [], withDeleted: undefined, @@ -44,7 +44,7 @@ describe("Product service", function () { }, options: { fields: undefined, - limit: 15, + limit: undefined, offset: 0, populate: [], withDeleted: undefined, diff --git a/packages/utils/src/modules-sdk/build-query.ts b/packages/utils/src/modules-sdk/build-query.ts index 36b6b316a94ac..912725d58999e 100644 --- a/packages/utils/src/modules-sdk/build-query.ts +++ b/packages/utils/src/modules-sdk/build-query.ts @@ -1,15 +1,29 @@ import { DAL, FindConfig } from "@medusajs/types" -import { deduplicate, isObject } from "../common" +import { deduplicate, isDefined, isObject } from "../common" import { SoftDeletableFilterKey } from "../dal" export function buildQuery( filters: Record = {}, - config: FindConfig = {} + config: FindConfig & { primaryKeyFields?: string | string[] } = {} ): DAL.FindOptions { const where: DAL.FilterQuery = {} buildWhere(filters, where) + const primaryKeyFieldArray = isDefined(config.primaryKeyFields) + ? !Array.isArray(config.primaryKeyFields) + ? [config.primaryKeyFields] + : config.primaryKeyFields + : ["id"] + + const whereHasPrimaryKeyFields = primaryKeyFieldArray.some( + (pkField) => !!where[pkField] + ) + + const defaultLimit = whereHasPrimaryKeyFields ? undefined : 15 + + delete config.primaryKeyFields + const findOptions: DAL.OptionsQuery = { populate: deduplicate(config.relations ?? []), fields: config.select as string[], @@ -17,7 +31,7 @@ export function buildQuery( (Number.isSafeInteger(config.take) && config.take! >= 0) || null === config.take ? config.take ?? undefined - : 15, + : defaultLimit, offset: (Number.isSafeInteger(config.skip) && config.skip! >= 0) || null === config.skip