fix(db-mongodb): db.find default limit to 0 (#8376)

Fixes an error anytime a `db.find` is called on documents with joins
without a set `limit` by defaulting the limit to 0.
This commit is contained in:
Dan Ribbens
2024-09-23 16:31:50 -04:00
committed by GitHub
parent 19e2f10f4b
commit dc69e2c0f6
3 changed files with 31 additions and 4 deletions

View File

@@ -15,7 +15,7 @@ export const find: Find = async function find(
{
collection,
joins = {},
limit,
limit = 0,
locale,
page,
pagination,

View File

@@ -111,9 +111,10 @@ export const buildJoinAggregation = async ({
input: `$${as}.docs`,
},
}, // Slicing the docs to match the limit
[`${as}.hasNextPage`]: {
$gt: [{ $size: `$${as}.docs` }, limitJoin || Number.MAX_VALUE],
}, // Boolean indicating if more docs than limit
[`${as}.hasNextPage`]: limitJoin
? { $gt: [{ $size: `$${as}.docs` }, limitJoin] }
: false,
// Boolean indicating if more docs than limit
},
},
)

View File

@@ -117,6 +117,9 @@ describe('Joins Field', () => {
it('should populate joins using find', async () => {
const result = await payload.find({
collection: 'categories',
where: {
id: { equals: category.id },
},
})
const [categoryWithPosts] = result.docs
@@ -126,6 +129,29 @@ describe('Joins Field', () => {
expect(categoryWithPosts.group.relatedPosts.docs[0].title).toBe('test 14')
})
it('should not error when deleting documents with joins', async () => {
const category = await payload.create({
collection: 'categories',
data: {
name: 'category with post',
},
})
const post = await createPost({
category: category.id,
})
const result = await payload.delete({
collection: 'categories',
// id: category.id,
where: {
id: { equals: category.id },
},
})
expect(result.docs[0].id).toStrictEqual(category.id)
})
describe('Joins with localization', () => {
let localizedCategory: Category