Skip to content

Commit de8f748

Browse files
authored
fix(link-modules, utils): remove limits on queries when primary-key is provided (medusajs#5732)
1 parent 3bcf08c commit de8f748

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@medusajs/link-modules": patch
3+
"@medusajs/utils": patch
4+
---
5+
6+
fix(link-modules, utils): remove limtis if primary key exists in filter for buildquery and no limit is present

packages/link-modules/src/services/link-module-service.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import {
1111
import {
1212
InjectManager,
1313
InjectTransactionManager,
14+
isDefined,
15+
mapObjectTo,
1416
MapToConfig,
1517
MedusaContext,
1618
MedusaError,
1719
ModulesSdkUtils,
18-
mapObjectTo,
1920
} from "@medusajs/utils"
2021
import { LinkService } from "@services"
2122
import { shouldForceTransaction } from "../utils"
@@ -126,6 +127,10 @@ export default class LinkModuleService<TLink> implements ILinkModule {
126127
config: FindConfig<unknown> = {},
127128
@MedusaContext() sharedContext: Context = {}
128129
): Promise<unknown[]> {
130+
if (!isDefined(config.take)) {
131+
config.take = null
132+
}
133+
129134
const rows = await this.linkService_.list(filters, config, sharedContext)
130135

131136
return await this.baseRepository_.serialize<object[]>(rows)
@@ -137,6 +142,10 @@ export default class LinkModuleService<TLink> implements ILinkModule {
137142
config: FindConfig<unknown> = {},
138143
@MedusaContext() sharedContext: Context = {}
139144
): Promise<[unknown[], number]> {
145+
if (!isDefined(config.take)) {
146+
config.take = null
147+
}
148+
140149
const [rows, count] = await this.linkService_.listAndCount(
141150
filters,
142151
config,

packages/product/src/services/__tests__/product.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe("Product service", function () {
1919
},
2020
options: {
2121
fields: undefined,
22-
limit: 15,
22+
limit: undefined,
2323
offset: 0,
2424
populate: [],
2525
withDeleted: undefined,
@@ -44,7 +44,7 @@ describe("Product service", function () {
4444
},
4545
options: {
4646
fields: undefined,
47-
limit: 15,
47+
limit: undefined,
4848
offset: 0,
4949
populate: [],
5050
withDeleted: undefined,

packages/utils/src/modules-sdk/build-query.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
import { DAL, FindConfig } from "@medusajs/types"
22

3-
import { deduplicate, isObject } from "../common"
3+
import { deduplicate, isDefined, isObject } from "../common"
44
import { SoftDeletableFilterKey } from "../dal"
55

66
export function buildQuery<T = any, TDto = any>(
77
filters: Record<string, any> = {},
8-
config: FindConfig<TDto> = {}
8+
config: FindConfig<TDto> & { primaryKeyFields?: string | string[] } = {}
99
): DAL.FindOptions<T> {
1010
const where: DAL.FilterQuery<T> = {}
1111
buildWhere(filters, where)
1212

13+
const primaryKeyFieldArray = isDefined(config.primaryKeyFields)
14+
? !Array.isArray(config.primaryKeyFields)
15+
? [config.primaryKeyFields]
16+
: config.primaryKeyFields
17+
: ["id"]
18+
19+
const whereHasPrimaryKeyFields = primaryKeyFieldArray.some(
20+
(pkField) => !!where[pkField]
21+
)
22+
23+
const defaultLimit = whereHasPrimaryKeyFields ? undefined : 15
24+
25+
delete config.primaryKeyFields
26+
1327
const findOptions: DAL.OptionsQuery<T, any> = {
1428
populate: deduplicate(config.relations ?? []),
1529
fields: config.select as string[],
1630
limit:
1731
(Number.isSafeInteger(config.take) && config.take! >= 0) ||
1832
null === config.take
1933
? config.take ?? undefined
20-
: 15,
34+
: defaultLimit,
2135
offset:
2236
(Number.isSafeInteger(config.skip) && config.skip! >= 0) ||
2337
null === config.skip

0 commit comments

Comments
 (0)