diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index d1230d2b3f..908d22129c 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -14,6 +14,7 @@ import Validator from 'validator'; import { customAlphabet } from 'nanoid'; import DOMPurify from 'isomorphic-dompurify'; import { v4 as uuidv4 } from 'uuid'; +import { extractLimitAndOffset } from '../helpers' import { NcError } from '../helpers/catchError'; import getAst from '../helpers/getAst'; @@ -1512,31 +1513,12 @@ class BaseModelSqlv2 { } _getListArgs(args: XcFilterWithAlias): XcFilter { - const obj: XcFilter = {}; + const obj: XcFilter = extractLimitAndOffset(args); obj.where = args.filter || args.where || args.w || ''; obj.having = args.having || args.h || ''; obj.shuffle = args.shuffle || args.r || ''; obj.condition = args.condition || args.c || {}; obj.conditionGraph = args.conditionGraph || {}; - - // use default value if invalid limit - // for example, if limit is not a number, it will be ignored - // if limit is less than 1, it will be ignored - const limit = +(args.limit || args.l); - obj.limit = Math.max( - Math.min( - limit && limit > 0 && Number.isInteger(limit) - ? limit - : this.config.limitDefault, - this.config.limitMax, - ), - this.config.limitMin, - ); - - // skip any invalid offset, ignore negative and non-integer values - const offset = +(args.offset || args.o) || 0; - obj.offset = Math.max(Number.isInteger(offset) ? offset : 0, 0); - obj.fields = args.fields || args.f; obj.sort = args.sort || args.s; return obj; diff --git a/packages/nocodb/src/helpers/PagedResponse.ts b/packages/nocodb/src/helpers/PagedResponse.ts index 1600d749dd..cd9da62d45 100644 --- a/packages/nocodb/src/helpers/PagedResponse.ts +++ b/packages/nocodb/src/helpers/PagedResponse.ts @@ -1,11 +1,6 @@ +import { extractLimitAndOffset } from '.'; import type { PaginatedType } from 'nocodb-sdk'; -const config: any = { - limitDefault: Math.max(+process.env.DB_QUERY_LIMIT_DEFAULT || 25, 1), - limitMin: Math.max(+process.env.DB_QUERY_LIMIT_MIN || 1, 1), - limitMax: Math.max(+process.env.DB_QUERY_LIMIT_MAX || 1000, 1), -}; - export class PagedResponseImpl { constructor( list: T[], @@ -17,12 +12,7 @@ export class PagedResponseImpl { o?: number; } = {}, ) { - const limit = Math.max( - Math.min(args.limit || args.l || config.limitDefault, config.limitMax), - config.limitMin, - ); - - const offset = Math.max(+(args.offset || args.o) || 0, 0); + const { offset, limit } = extractLimitAndOffset(args); let count = args.count ?? null; @@ -44,4 +34,5 @@ export class PagedResponseImpl { list: Array; pageInfo: PaginatedType; + errors?: any[]; } diff --git a/packages/nocodb/src/helpers/extractLimitAndOffset.ts b/packages/nocodb/src/helpers/extractLimitAndOffset.ts new file mode 100644 index 0000000000..1cf5072aa3 --- /dev/null +++ b/packages/nocodb/src/helpers/extractLimitAndOffset.ts @@ -0,0 +1,39 @@ +const config = { + limitDefault: Math.max(+process.env.DB_QUERY_LIMIT_DEFAULT || 25, 1), + limitMin: Math.max(+process.env.DB_QUERY_LIMIT_MIN || 1, 1), + limitMax: Math.max(+process.env.DB_QUERY_LIMIT_MAX || 1000, 1), +}; + +export function extractLimitAndOffset( + args: { + limit?: number | string; + offset?: number | string; + l?: number | string; + o?: number | string; + } = {}, +) { + const obj: { + limit?: number; + offset?: number; + } = {}; + + // use default value if invalid limit + // for example, if limit is not a number, it will be ignored + // if limit is less than 1, it will be ignored + const limit = +(args.limit || args.l); + obj.limit = Math.max( + Math.min( + limit && limit > 0 && Number.isInteger(limit) + ? limit + : config.limitDefault, + config.limitMax, + ), + config.limitMin, + ); + + // skip any invalid offset, ignore negative and non-integer values + const offset = +(args.offset || args.o) || 0; + obj.offset = Math.max(Number.isInteger(offset) ? offset : 0, 0); + + return obj; +} diff --git a/packages/nocodb/src/helpers/index.ts b/packages/nocodb/src/helpers/index.ts index ee77b070e2..adf2eeecba 100644 --- a/packages/nocodb/src/helpers/index.ts +++ b/packages/nocodb/src/helpers/index.ts @@ -2,5 +2,6 @@ import { populateMeta } from './populateMeta'; export * from './columnHelpers'; export * from './apiHelpers'; export * from './cacheHelpers'; +export * from './extractLimitAndOffset'; export { populateMeta };