Skip to content

Commit a955392

Browse files
authored
fix: add missed pagination property to findVersions and findGlobalVersions and handle it properly (#13763)
* The `pagination` property was missing in `findVersions` and `findGlobalVersions` Local API operations, although the actual functions did have it - https://github.com/payloadcms/payload/blob/1b93c4becc9f85684bc2d79795615aee101988fd/packages/payload/src/collections/operations/findVersions.ts#L25 * The handling of the `pagination` property in those functions was broken, this PR fixes it.
1 parent 731c4fb commit a955392

File tree

9 files changed

+179
-5
lines changed

9 files changed

+179
-5
lines changed

packages/payload/src/collections/operations/findVersions.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,15 @@ export const findVersionsOperation = async <TData extends TypeWithVersion<TData>
9797
// Find
9898
// /////////////////////////////////////
9999

100+
const usePagination = pagination && limit !== 0
101+
const sanitizedLimit = limit ?? (usePagination ? 10 : 0)
102+
const sanitizedPage = page || 1
103+
100104
const paginatedDocs = await payload.db.findVersions<TData>({
101105
collection: collectionConfig.slug,
102-
limit: limit ?? 10,
106+
limit: sanitizedLimit,
103107
locale: locale!,
104-
page: page || 1,
108+
page: sanitizedPage,
105109
pagination,
106110
req,
107111
select,

packages/payload/src/collections/operations/local/findVersions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ export type Options<TSlug extends CollectionSlug> = {
6161
* @default 1
6262
*/
6363
page?: number
64+
/**
65+
* Set to `false` to return all documents and avoid querying for document counts which introduces some overhead.
66+
* You can also combine that property with a specified `limit` to limit documents but avoid the count query.
67+
*/
68+
pagination?: boolean
6469
/**
6570
* Specify [populate](https://payloadcms.com/docs/queries/select#populate) to control which fields to include to the result from populated documents.
6671
*/
@@ -114,6 +119,7 @@ export async function findVersionsLocal<TSlug extends CollectionSlug>(
114119
limit,
115120
overrideAccess = true,
116121
page,
122+
pagination = true,
117123
populate,
118124
select,
119125
showHiddenFields,
@@ -136,6 +142,7 @@ export async function findVersionsLocal<TSlug extends CollectionSlug>(
136142
limit,
137143
overrideAccess,
138144
page,
145+
pagination,
139146
populate,
140147
req: await createLocalReq(options as CreateLocalReqOptions, payload),
141148
select,

packages/payload/src/globals/operations/findVersions.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,15 @@ export const findVersionsOperation = async <T extends TypeWithVersion<T>>(
7979
// Find
8080
// /////////////////////////////////////
8181

82+
const usePagination = pagination && limit !== 0
83+
const sanitizedLimit = limit ?? (usePagination ? 10 : 0)
84+
const sanitizedPage = page || 1
85+
8286
const paginatedDocs = await payload.db.findGlobalVersions<T>({
8387
global: globalConfig.slug,
84-
limit: limit ?? 10,
88+
limit: sanitizedLimit,
8589
locale: locale!,
86-
page: page || 1,
90+
page: sanitizedPage,
8791
pagination,
8892
req,
8993
select,

packages/payload/src/globals/operations/local/findVersions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ export type Options<TSlug extends GlobalSlug> = {
5353
* @default 1
5454
*/
5555
page?: number
56+
/**
57+
* Set to `false` to return all documents and avoid querying for document counts which introduces some overhead.
58+
* You can also combine that property with a specified `limit` to limit documents but avoid the count query.
59+
*/
60+
pagination?: boolean
5661
/**
5762
* Specify [populate](https://payloadcms.com/docs/queries/select#populate) to control which fields to include to the result from populated documents.
5863
*/
@@ -101,6 +106,7 @@ export async function findGlobalVersionsLocal<TSlug extends GlobalSlug>(
101106
limit,
102107
overrideAccess = true,
103108
page,
109+
pagination = true,
104110
populate,
105111
select,
106112
showHiddenFields,
@@ -120,6 +126,7 @@ export async function findGlobalVersionsLocal<TSlug extends GlobalSlug>(
120126
limit,
121127
overrideAccess,
122128
page,
129+
pagination,
123130
populate,
124131
req: await createLocalReq(options as CreateLocalReqOptions, payload),
125132
select,

test/versions/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import AutosaveGlobal from './globals/Autosave.js'
2525
import AutosaveWithDraftButtonGlobal from './globals/AutosaveWithDraftButton.js'
2626
import DisablePublishGlobal from './globals/DisablePublish.js'
2727
import DraftGlobal from './globals/Draft.js'
28+
import DraftUnlimitedGlobal from './globals/DraftUnlimited.js'
2829
import DraftWithMaxGlobal from './globals/DraftWithMax.js'
2930
import LocalizedGlobal from './globals/LocalizedGlobal.js'
3031
import { MaxVersions } from './globals/MaxVersions.js'
@@ -66,6 +67,7 @@ export default buildConfigWithDefaults({
6667
DisablePublishGlobal,
6768
LocalizedGlobal,
6869
MaxVersions,
70+
DraftUnlimitedGlobal,
6971
],
7072
indexSortableFields: true,
7173
localization: {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import type { GlobalConfig } from 'payload'
2+
3+
import { draftUnlimitedGlobalSlug } from '../slugs.js'
4+
5+
const DraftUnlimitedGlobal: GlobalConfig = {
6+
slug: draftUnlimitedGlobalSlug,
7+
label: 'Draft Unlimited Global',
8+
admin: {
9+
preview: () => 'https://payloadcms.com',
10+
components: {
11+
views: {
12+
edit: {
13+
version: {
14+
actions: ['/elements/GlobalVersionButton/index.js'],
15+
},
16+
versions: {
17+
actions: ['/elements/GlobalVersionsButton/index.js'],
18+
},
19+
},
20+
},
21+
},
22+
},
23+
versions: {
24+
max: 0,
25+
drafts: {
26+
schedulePublish: true,
27+
},
28+
},
29+
access: {
30+
read: ({ req: { user } }) => {
31+
if (user) {
32+
return true
33+
}
34+
35+
return {
36+
or: [
37+
{
38+
_status: {
39+
equals: 'published',
40+
},
41+
},
42+
{
43+
_status: {
44+
exists: false,
45+
},
46+
},
47+
],
48+
}
49+
},
50+
},
51+
fields: [
52+
{
53+
name: 'title',
54+
type: 'text',
55+
required: true,
56+
localized: true,
57+
},
58+
],
59+
}
60+
61+
export default DraftUnlimitedGlobal

test/versions/int.spec.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,44 @@ describe('Versions', () => {
573573
expect(restoredVersion.title).toStrictEqual('v1')
574574
})
575575

576+
it('findVersions - pagination should work correctly', async () => {
577+
const post = await payload.create({
578+
collection: 'draft-posts',
579+
data: { description: 'a', title: 'title' },
580+
})
581+
for (let i = 0; i < 100; i++) {
582+
await payload.update({ collection: 'draft-posts', id: post.id, data: {} })
583+
}
584+
const res = await payload.findVersions({
585+
collection: 'draft-posts',
586+
where: { parent: { equals: post.id } },
587+
})
588+
expect(res.totalDocs).toBe(101)
589+
expect(res.docs).toHaveLength(10)
590+
const resPaginationFalse = await payload.findVersions({
591+
collection: 'draft-posts',
592+
where: { parent: { equals: post.id } },
593+
pagination: false,
594+
})
595+
const resPaginationFalse2 = await payload.find({
596+
collection: 'draft-posts',
597+
// where: { parent: { equals: post.id } },
598+
pagination: false,
599+
})
600+
601+
expect(resPaginationFalse.docs).toHaveLength(101)
602+
expect(resPaginationFalse.totalDocs).toBe(101)
603+
604+
const resPaginationFalseLimit0 = await payload.findVersions({
605+
collection: 'draft-posts',
606+
where: { parent: { equals: post.id } },
607+
pagination: false,
608+
limit: 0,
609+
})
610+
expect(resPaginationFalseLimit0.docs).toHaveLength(101)
611+
expect(resPaginationFalseLimit0.totalDocs).toBe(101)
612+
})
613+
576614
describe('Update', () => {
577615
it('should allow a draft to be patched', async () => {
578616
const originalTitle = 'Here is a published post'
@@ -1802,6 +1840,31 @@ describe('Versions', () => {
18021840
expect(version_1_deleted).toBeFalsy()
18031841
})
18041842

1843+
it('findGlobalVersions - pagination should work correctly', async () => {
1844+
for (let i = 0; i < 100; i++) {
1845+
await payload.updateGlobal({ slug: 'draft-unlimited-global', data: { title: 'title' } })
1846+
}
1847+
const res = await payload.findGlobalVersions({
1848+
slug: 'draft-unlimited-global',
1849+
})
1850+
expect(res.totalDocs).toBe(100)
1851+
expect(res.docs).toHaveLength(10)
1852+
const resPaginationFalse = await payload.findGlobalVersions({
1853+
slug: 'draft-unlimited-global',
1854+
pagination: false,
1855+
})
1856+
expect(resPaginationFalse.docs).toHaveLength(100)
1857+
expect(resPaginationFalse.totalDocs).toBe(100)
1858+
1859+
const resPaginationFalseLimit0 = await payload.findGlobalVersions({
1860+
slug: 'draft-unlimited-global',
1861+
pagination: false,
1862+
limit: 0,
1863+
})
1864+
expect(resPaginationFalseLimit0.docs).toHaveLength(100)
1865+
expect(resPaginationFalseLimit0.totalDocs).toBe(100)
1866+
})
1867+
18051868
describe('Read', () => {
18061869
it('should allow a version to be retrieved by ID', async () => {
18071870
const version = await payload.findGlobalVersionByID({

test/versions/payload-types.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export interface Config {
128128
'disable-publish-global': DisablePublishGlobal;
129129
'localized-global': LocalizedGlobal;
130130
'max-versions': MaxVersion;
131+
'draft-unlimited-global': DraftUnlimitedGlobal;
131132
};
132133
globalsSelect: {
133134
'autosave-global': AutosaveGlobalSelect<false> | AutosaveGlobalSelect<true>;
@@ -137,6 +138,7 @@ export interface Config {
137138
'disable-publish-global': DisablePublishGlobalSelect<false> | DisablePublishGlobalSelect<true>;
138139
'localized-global': LocalizedGlobalSelect<false> | LocalizedGlobalSelect<true>;
139140
'max-versions': MaxVersionsSelect<false> | MaxVersionsSelect<true>;
141+
'draft-unlimited-global': DraftUnlimitedGlobalSelect<false> | DraftUnlimitedGlobalSelect<true>;
140142
};
141143
locale: 'en' | 'es' | 'de';
142144
user: User & {
@@ -1348,6 +1350,17 @@ export interface MaxVersion {
13481350
updatedAt?: string | null;
13491351
createdAt?: string | null;
13501352
}
1353+
/**
1354+
* This interface was referenced by `Config`'s JSON-Schema
1355+
* via the `definition` "draft-unlimited-global".
1356+
*/
1357+
export interface DraftUnlimitedGlobal {
1358+
id: string;
1359+
title: string;
1360+
_status?: ('draft' | 'published') | null;
1361+
updatedAt?: string | null;
1362+
createdAt?: string | null;
1363+
}
13511364
/**
13521365
* This interface was referenced by `Config`'s JSON-Schema
13531366
* via the `definition` "autosave-global_select".
@@ -1426,6 +1439,17 @@ export interface MaxVersionsSelect<T extends boolean = true> {
14261439
createdAt?: T;
14271440
globalType?: T;
14281441
}
1442+
/**
1443+
* This interface was referenced by `Config`'s JSON-Schema
1444+
* via the `definition` "draft-unlimited-global_select".
1445+
*/
1446+
export interface DraftUnlimitedGlobalSelect<T extends boolean = true> {
1447+
title?: T;
1448+
_status?: T;
1449+
updatedAt?: T;
1450+
createdAt?: T;
1451+
globalType?: T;
1452+
}
14291453
/**
14301454
* This interface was referenced by `Config`'s JSON-Schema
14311455
* via the `definition` "TaskSchedulePublish".
@@ -1447,7 +1471,7 @@ export interface TaskSchedulePublish {
14471471
relationTo: 'draft-posts-with-change-hook';
14481472
value: string | DraftPostsWithChangeHook;
14491473
} | null);
1450-
global?: 'draft-global' | null;
1474+
global?: ('draft-global' | 'draft-unlimited-global') | null;
14511475
user?: (string | null) | User;
14521476
};
14531477
output?: unknown;

test/versions/slugs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export const autosaveWithDraftButtonGlobal = 'autosave-with-draft-button-global'
4848

4949
export const draftGlobalSlug = 'draft-global'
5050

51+
export const draftUnlimitedGlobalSlug = 'draft-unlimited-global'
52+
5153
export const draftWithMaxGlobalSlug = 'draft-with-max-global'
5254

5355
export const globalSlugs = [autoSaveGlobalSlug, draftGlobalSlug]

0 commit comments

Comments
 (0)