fix(link-modules, utils): remove limits on queries when primary-key is provided (#5732)
This commit is contained in:
1 parent
3bcf08c0cb
commit
de8f748674
4 files changed
+35
-6
No files matched your search
@@ -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,11 +11,12 @@ import {
|
|||||||
import {
|
import {
|
||||||
InjectManager,
|
InjectManager,
|
||||||
InjectTransactionManager,
|
InjectTransactionManager,
|
||||||
|
isDefined,
|
||||||
|
mapObjectTo,
|
||||||
MapToConfig,
|
MapToConfig,
|
||||||
MedusaContext,
|
MedusaContext,
|
||||||
MedusaError,
|
MedusaError,
|
||||||
ModulesSdkUtils,
|
ModulesSdkUtils,
|
||||||
mapObjectTo,
|
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { LinkService } from "@services"
|
import { LinkService } from "@services"
|
||||||
import { shouldForceTransaction } from "../utils"
|
import { shouldForceTransaction } from "../utils"
|
||||||
@@ -126,6 +127,10 @@ export default class LinkModuleService<TLink> implements ILinkModule {
|
|||||||
config: FindConfig<unknown> = {},
|
config: FindConfig<unknown> = {},
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
): Promise<unknown[]> {
|
): Promise<unknown[]> {
|
||||||
|
if (!isDefined(config.take)) {
|
||||||
|
config.take = null
|
||||||
|
}
|
||||||
|
|
||||||
const rows = await this.linkService_.list(filters, config, sharedContext)
|
const rows = await this.linkService_.list(filters, config, sharedContext)
|
||||||
|
|
||||||
return await this.baseRepository_.serialize<object[]>(rows)
|
return await this.baseRepository_.serialize<object[]>(rows)
|
||||||
@@ -137,6 +142,10 @@ export default class LinkModuleService<TLink> implements ILinkModule {
|
|||||||
config: FindConfig<unknown> = {},
|
config: FindConfig<unknown> = {},
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
): Promise<[unknown[], number]> {
|
): Promise<[unknown[], number]> {
|
||||||
|
if (!isDefined(config.take)) {
|
||||||
|
config.take = null
|
||||||
|
}
|
||||||
|
|
||||||
const [rows, count] = await this.linkService_.listAndCount(
|
const [rows, count] = await this.linkService_.listAndCount(
|
||||||
filters,
|
filters,
|
||||||
config,
|
config,
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ describe("Product service", function () {
|
|||||||
},
|
},
|
||||||
options: {
|
options: {
|
||||||
fields: undefined,
|
fields: undefined,
|
||||||
limit: 15,
|
limit: undefined,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
populate: [],
|
populate: [],
|
||||||
withDeleted: undefined,
|
withDeleted: undefined,
|
||||||
@@ -44,7 +44,7 @@ describe("Product service", function () {
|
|||||||
},
|
},
|
||||||
options: {
|
options: {
|
||||||
fields: undefined,
|
fields: undefined,
|
||||||
limit: 15,
|
limit: undefined,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
populate: [],
|
populate: [],
|
||||||
withDeleted: undefined,
|
withDeleted: undefined,
|
||||||
|
|||||||
@@ -1,15 +1,29 @@
|
|||||||
import { DAL, FindConfig } from "@medusajs/types"
|
import { DAL, FindConfig } from "@medusajs/types"
|
||||||
|
|
||||||
import { deduplicate, isObject } from "../common"
|
import { deduplicate, isDefined, isObject } from "../common"
|
||||||
import { SoftDeletableFilterKey } from "../dal"
|
import { SoftDeletableFilterKey } from "../dal"
|
||||||
|
|
||||||
export function buildQuery<T = any, TDto = any>(
|
export function buildQuery<T = any, TDto = any>(
|
||||||
filters: Record<string, any> = {},
|
filters: Record<string, any> = {},
|
||||||
config: FindConfig<TDto> = {}
|
config: FindConfig<TDto> & { primaryKeyFields?: string | string[] } = {}
|
||||||
): DAL.FindOptions<T> {
|
): DAL.FindOptions<T> {
|
||||||
const where: DAL.FilterQuery<T> = {}
|
const where: DAL.FilterQuery<T> = {}
|
||||||
buildWhere(filters, where)
|
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> = {
|
const findOptions: DAL.OptionsQuery<T, any> = {
|
||||||
populate: deduplicate(config.relations ?? []),
|
populate: deduplicate(config.relations ?? []),
|
||||||
fields: config.select as string[],
|
fields: config.select as string[],
|
||||||
@@ -17,7 +31,7 @@ export function buildQuery<T = any, TDto = any>(
|
|||||||
(Number.isSafeInteger(config.take) && config.take! >= 0) ||
|
(Number.isSafeInteger(config.take) && config.take! >= 0) ||
|
||||||
null === config.take
|
null === config.take
|
||||||
? config.take ?? undefined
|
? config.take ?? undefined
|
||||||
: 15,
|
: defaultLimit,
|
||||||
offset:
|
offset:
|
||||||
(Number.isSafeInteger(config.skip) && config.skip! >= 0) ||
|
(Number.isSafeInteger(config.skip) && config.skip! >= 0) ||
|
||||||
null === config.skip
|
null === config.skip
|
||||||
|
|||||||
Reference in new issue
Block a user