feat: decouples limit from pagination, allows for no limit query
This commit is contained in:
@@ -10,6 +10,7 @@ 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
|
||||
@@ -136,7 +137,7 @@ async function find<T extends TypeWithID = any>(incomingArgs: Arguments): Promis
|
||||
locale,
|
||||
});
|
||||
|
||||
const optionsToExecute = {
|
||||
const paginatedDocs = await findDocs<T>(Model, query, {
|
||||
page: page || 1,
|
||||
limit: limit || 10,
|
||||
sort: {
|
||||
@@ -147,18 +148,16 @@ async function find<T extends TypeWithID = any>(incomingArgs: Arguments): Promis
|
||||
useEstimatedCount,
|
||||
pagination,
|
||||
useCustomCountFn: pagination ? undefined : () => Promise.resolve(1),
|
||||
};
|
||||
});
|
||||
|
||||
const paginatedDocs = await Model.paginate(query, optionsToExecute);
|
||||
|
||||
let result = {
|
||||
let result: PaginatedDocs<T> = {
|
||||
...paginatedDocs,
|
||||
docs: paginatedDocs.docs.map((doc) => {
|
||||
const sanitizedDoc = JSON.parse(JSON.stringify(doc));
|
||||
sanitizedDoc.id = sanitizedDoc._id;
|
||||
return sanitizeInternalFields(sanitizedDoc);
|
||||
}),
|
||||
} as PaginatedDocs<T>;
|
||||
};
|
||||
|
||||
// /////////////////////////////////////
|
||||
// Replace documents with drafts if available
|
||||
|
||||
@@ -10,6 +10,7 @@ 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
|
||||
@@ -98,7 +99,7 @@ async function findVersions<T extends TypeWithVersion<T> = any>(args: Arguments)
|
||||
locale,
|
||||
});
|
||||
|
||||
const optionsToExecute = {
|
||||
const paginatedDocs = await findDocs<T>(VersionsModel, query, {
|
||||
page: page || 1,
|
||||
limit: limit || 10,
|
||||
sort: {
|
||||
@@ -107,9 +108,7 @@ async function findVersions<T extends TypeWithVersion<T> = any>(args: Arguments)
|
||||
lean: true,
|
||||
leanWithId: true,
|
||||
useEstimatedCount,
|
||||
};
|
||||
|
||||
const paginatedDocs = await VersionsModel.paginate(query, optionsToExecute);
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
// beforeRead - Collection
|
||||
|
||||
44
src/collections/operations/helpers/findDocs.ts
Normal file
44
src/collections/operations/helpers/findDocs.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
@@ -10,6 +10,7 @@ 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
|
||||
@@ -96,7 +97,7 @@ async function findVersions<T extends TypeWithVersion<T> = any>(args: Arguments)
|
||||
locale,
|
||||
});
|
||||
|
||||
const optionsToExecute = {
|
||||
const paginatedDocs = await findDocs<T>(VersionsModel, query, {
|
||||
page: page || 1,
|
||||
limit: limit || 10,
|
||||
sort: {
|
||||
@@ -105,9 +106,7 @@ async function findVersions<T extends TypeWithVersion<T> = any>(args: Arguments)
|
||||
lean: true,
|
||||
leanWithId: true,
|
||||
useEstimatedCount,
|
||||
};
|
||||
|
||||
const paginatedDocs = await VersionsModel.paginate(query, optionsToExecute);
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
// afterRead - Fields
|
||||
|
||||
Reference in New Issue
Block a user