Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(link-modules, utils): remove limits on queries when primary-key is provided #5732

Merged
merged 5 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/mighty-windows-perform.md
Original file line number Diff line number Diff line change
@@ -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
11 changes: 10 additions & 1 deletion packages/link-modules/src/services/link-module-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -126,6 +127,10 @@ export default class LinkModuleService<TLink> implements ILinkModule {
config: FindConfig<unknown> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<unknown[]> {
if (!isDefined(config.take)) {
config.take = null
}

const rows = await this.linkService_.list(filters, config, sharedContext)

return await this.baseRepository_.serialize<object[]>(rows)
Expand All @@ -137,6 +142,10 @@ export default class LinkModuleService<TLink> implements ILinkModule {
config: FindConfig<unknown> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[unknown[], number]> {
if (!isDefined(config.take)) {
config.take = null
}

const [rows, count] = await this.linkService_.listAndCount(
filters,
config,
Expand Down
4 changes: 2 additions & 2 deletions packages/product/src/services/__tests__/product.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("Product service", function () {
},
options: {
fields: undefined,
limit: 15,
limit: undefined,
offset: 0,
populate: [],
withDeleted: undefined,
Expand All @@ -44,7 +44,7 @@ describe("Product service", function () {
},
options: {
fields: undefined,
limit: 15,
limit: undefined,
offset: 0,
populate: [],
withDeleted: undefined,
Expand Down
20 changes: 17 additions & 3 deletions packages/utils/src/modules-sdk/build-query.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
import { DAL, FindConfig } from "@medusajs/types"

import { deduplicate, isObject } from "../common"
import { deduplicate, isDefined, isObject } from "../common"
import { SoftDeletableFilterKey } from "../dal"

export function buildQuery<T = any, TDto = any>(
filters: Record<string, any> = {},
config: FindConfig<TDto> = {}
config: FindConfig<TDto> & { primaryKeyFields?: string | string[] } = {}
): DAL.FindOptions<T> {
const where: DAL.FilterQuery<T> = {}
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<T, any> = {
populate: deduplicate(config.relations ?? []),
fields: config.select as string[],
limit:
(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
Expand Down