chore: simplifies logic to decouple pagination and limit

This commit is contained in:
Jarrod Flesch
2022-12-01 08:17:00 -05:00
parent f7ce0c615d
commit dc4e0971a6
4 changed files with 8 additions and 51 deletions

View File

@@ -10,7 +10,6 @@ import { buildSortParam } from '../../mongoose/buildSortParam';
import replaceWithDraftIfAvailable from '../../versions/drafts/replaceWithDraftIfAvailable';
import { AccessResult } from '../../config/types';
import { afterRead } from '../../fields/hooks/afterRead';
import { findDocs } from './helpers/findDocs';
export type Arguments = {
collection: Collection
@@ -137,9 +136,9 @@ async function find<T extends TypeWithID = any>(incomingArgs: Arguments): Promis
locale,
});
const paginatedDocs = await findDocs<T>(Model, query, {
const fallbackLimit = pagination ? 10 : 0;
const paginatedDocs = await Model.paginate(query, {
page: page || 1,
limit: limit || 10,
sort: {
[sortProperty]: sortOrder,
},
@@ -148,6 +147,10 @@ async function find<T extends TypeWithID = any>(incomingArgs: Arguments): Promis
useEstimatedCount,
pagination,
useCustomCountFn: pagination ? undefined : () => Promise.resolve(1),
options: {
// limit must be set here to avoid being ignored when paginate is false
limit: typeof limit === 'number' ? limit : fallbackLimit,
},
});
let result: PaginatedDocs<T> = {

View File

@@ -10,7 +10,6 @@ import { PaginatedDocs } from '../../mongoose/types';
import { TypeWithVersion } from '../../versions/types';
import { afterRead } from '../../fields/hooks/afterRead';
import { buildVersionCollectionFields } from '../../versions/buildCollectionFields';
import { findDocs } from './helpers/findDocs';
export type Arguments = {
collection: Collection
@@ -99,7 +98,7 @@ async function findVersions<T extends TypeWithVersion<T> = any>(args: Arguments)
locale,
});
const paginatedDocs = await findDocs<T>(VersionsModel, query, {
const paginatedDocs = await VersionsModel.paginate(query, {
page: page || 1,
limit: limit || 10,
sort: {

View File

@@ -1,44 +0,0 @@
import { PaginatedDocs } from '../../../mongoose/types';
import { CollectionModel } from '../../config/types';
import { Arguments } from '../find';
type FindDocsArguments = ({
page: Arguments['page']
limit: Arguments['limit']
sort: {
[key: string]: string,
}
}) & ({
pagination?: false
} | {
pagination: true
lean?: boolean
leanWithId?: boolean
useEstimatedCount?: boolean
useCustomCountFn?: (() => Promise<number>) | undefined;
})
export async function findDocs<T>(Model: CollectionModel, query: Record<string, unknown>, args: FindDocsArguments): Promise<PaginatedDocs<T>> {
// /////////////////////////////////////
// Model.paginate ignores limit when paginate is true
// pass limit=0 or pagination=false to skip
// /////////////////////////////////////
if (args.limit !== 0 && args.pagination) {
return Model.paginate(query, args);
}
const docs = await Model.find(query, undefined, args);
return {
docs,
totalDocs: docs.length,
totalPages: 1,
page: undefined,
nextPage: null,
prevPage: null,
pagingCounter: 0,
hasNextPage: null,
hasPrevPage: null,
limit: args.limit || null,
};
}

View File

@@ -10,7 +10,6 @@ import { TypeWithVersion } from '../../versions/types';
import { SanitizedGlobalConfig } from '../config/types';
import { afterRead } from '../../fields/hooks/afterRead';
import { buildVersionGlobalFields } from '../../versions/buildGlobalFields';
import { findDocs } from '../../collections/operations/helpers/findDocs';
export type Arguments = {
globalConfig: SanitizedGlobalConfig
@@ -97,7 +96,7 @@ async function findVersions<T extends TypeWithVersion<T> = any>(args: Arguments)
locale,
});
const paginatedDocs = await findDocs<T>(VersionsModel, query, {
const paginatedDocs = await VersionsModel.paginate(query, {
page: page || 1,
limit: limit || 10,
sort: {