Skip to content

Commit 43dcf84

Browse files
damnsamnGermanJablor1tsuu
authored
fix(db-mongodb): findVersions/findGlobalVersions not respecting limit of 0 (#14573)
### What? Updated `db-mongodb`'s `findVersions()` and `findGlobalVersions()` functions to treat the `limit` property the same as the `find()` does. ### Why? Currently, passing `limit: 0` to `findVersions` or `findGlobalVersions` results in an empty array of documents. The reason being is the logical AND present in these files when evaluating the `limit` property, which will resolve to false when `limit === 0`, meaning that the nested `if (limit === 0)` statement can never be reached ### How? I've copied the approach from `find()`. This also defaults the `limit` value to 0 as in `find()`, which may/not be desirable? I opted for consistency across the functions rather than leaving the defaults undefined and checking the types instead. --------- Co-authored-by: German Jablonski <43938777+GermanJablo@users.noreply.github.com> Co-authored-by: Sasha <64744993+r1tsuu@users.noreply.github.com>
1 parent 73a18dc commit 43dcf84

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

packages/db-mongodb/src/findGlobalVersions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const findGlobalVersions: FindGlobalVersions = async function findGlobalV
1616
this: MongooseAdapter,
1717
{
1818
global: globalSlug,
19-
limit,
19+
limit = 0,
2020
locale,
2121
page,
2222
pagination,
@@ -101,7 +101,7 @@ export const findGlobalVersions: FindGlobalVersions = async function findGlobalV
101101
}
102102
}
103103

104-
if (limit && limit >= 0) {
104+
if (limit >= 0) {
105105
paginationOptions.limit = limit
106106
// limit must also be set here, it's ignored when pagination is false
107107

packages/db-mongodb/src/findVersions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const findVersions: FindVersions = async function findVersions(
1616
this: MongooseAdapter,
1717
{
1818
collection: collectionSlug,
19-
limit,
19+
limit = 0,
2020
locale,
2121
page,
2222
pagination,
@@ -109,7 +109,7 @@ export const findVersions: FindVersions = async function findVersions(
109109
}
110110
}
111111

112-
if (limit && limit >= 0) {
112+
if (limit >= 0) {
113113
paginationOptions.limit = limit
114114
// limit must also be set here, it's ignored when pagination is false
115115

test/_community/payload-types.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export interface Config {
6969
collections: {
7070
posts: Post;
7171
media: Media;
72+
'payload-kv': PayloadKv;
7273
users: User;
7374
'payload-locked-documents': PayloadLockedDocument;
7475
'payload-preferences': PayloadPreference;
@@ -78,6 +79,7 @@ export interface Config {
7879
collectionsSelect: {
7980
posts: PostsSelect<false> | PostsSelect<true>;
8081
media: MediaSelect<false> | MediaSelect<true>;
82+
'payload-kv': PayloadKvSelect<false> | PayloadKvSelect<true>;
8183
users: UsersSelect<false> | UsersSelect<true>;
8284
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
8385
'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>;
@@ -188,6 +190,23 @@ export interface Media {
188190
};
189191
};
190192
}
193+
/**
194+
* This interface was referenced by `Config`'s JSON-Schema
195+
* via the `definition` "payload-kv".
196+
*/
197+
export interface PayloadKv {
198+
id: string;
199+
key: string;
200+
data:
201+
| {
202+
[k: string]: unknown;
203+
}
204+
| unknown[]
205+
| string
206+
| number
207+
| boolean
208+
| null;
209+
}
191210
/**
192211
* This interface was referenced by `Config`'s JSON-Schema
193212
* via the `definition` "users".
@@ -334,6 +353,14 @@ export interface MediaSelect<T extends boolean = true> {
334353
};
335354
};
336355
}
356+
/**
357+
* This interface was referenced by `Config`'s JSON-Schema
358+
* via the `definition` "payload-kv_select".
359+
*/
360+
export interface PayloadKvSelect<T extends boolean = true> {
361+
key?: T;
362+
data?: T;
363+
}
337364
/**
338365
* This interface was referenced by `Config`'s JSON-Schema
339366
* via the `definition` "users_select".

test/versions/int.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
autosaveWithMultiSelectCollectionSlug,
2525
draftCollectionSlug,
2626
draftGlobalSlug,
27+
draftUnlimitedGlobalSlug,
2728
localizedCollectionSlug,
2829
localizedGlobalSlug,
2930
versionCollectionSlug,
@@ -529,6 +530,23 @@ describe('Versions', () => {
529530
draftsDescending.docs[draftsDescending.docs.length - 1]!,
530531
)
531532
})
533+
534+
it('should findVersions with limit: 0', async () => {
535+
const doc = await payload.create({
536+
collection: draftCollectionSlug,
537+
data: { description: 'a', title: 'test-doc' },
538+
})
539+
540+
for (let i = 0; i < 100; i++) {
541+
await payload.update({ collection: draftCollectionSlug, id: doc.id, data: {} })
542+
}
543+
const res = await payload.findVersions({
544+
collection: draftCollectionSlug,
545+
limit: 0,
546+
where: { parent: { equals: doc.id } },
547+
})
548+
expect(res.docs).toHaveLength(101)
549+
})
532550
})
533551

534552
describe('Restore', () => {
@@ -2203,6 +2221,20 @@ describe('Versions', () => {
22032221

22042222
expect(version.id).toStrictEqual(globalVersionID)
22052223
})
2224+
2225+
it('should findGlobalVersions with limit: 0', async () => {
2226+
await payload.db.deleteVersions({ globalSlug: draftUnlimitedGlobalSlug, where: {} })
2227+
for (let i = 0; i < 100; i++) {
2228+
await payload.updateGlobal({ slug: draftUnlimitedGlobalSlug, data: { title: 'global' } })
2229+
}
2230+
2231+
const res = await payload.findGlobalVersions({
2232+
slug: draftUnlimitedGlobalSlug,
2233+
limit: 0,
2234+
})
2235+
2236+
expect(res.docs).toHaveLength(100)
2237+
})
22062238
})
22072239

22082240
describe('Update', () => {

0 commit comments

Comments
 (0)