feat: add limit property to bulk update operation (#8656)
Adds `limit` to `payload.update`(bulk) / REST
This commit is contained in:
@@ -10,9 +10,10 @@ import type { CollectionRouteHandler } from '../types.js'
|
||||
import { headersWithCors } from '../../../utilities/headersWithCors.js'
|
||||
|
||||
export const update: CollectionRouteHandler = async ({ collection, req }) => {
|
||||
const { depth, draft, where } = req.query as {
|
||||
const { depth, draft, limit, where } = req.query as {
|
||||
depth?: string
|
||||
draft?: string
|
||||
limit?: string
|
||||
where?: Where
|
||||
}
|
||||
|
||||
@@ -21,6 +22,7 @@ export const update: CollectionRouteHandler = async ({ collection, req }) => {
|
||||
data: req.data,
|
||||
depth: isNumber(depth) ? Number(depth) : undefined,
|
||||
draft: draft === 'true',
|
||||
limit: isNumber(limit) ? Number(limit) : undefined,
|
||||
req,
|
||||
where,
|
||||
})
|
||||
|
||||
@@ -45,6 +45,7 @@ export type ByIDOptions<TSlug extends CollectionSlug> = {
|
||||
|
||||
export type ManyOptions<TSlug extends CollectionSlug> = {
|
||||
id?: never
|
||||
limit?: number
|
||||
where: Where
|
||||
} & BaseOptions<TSlug>
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ export type Arguments<TSlug extends CollectionSlug> = {
|
||||
depth?: number
|
||||
disableVerificationEmail?: boolean
|
||||
draft?: boolean
|
||||
limit?: number
|
||||
overrideAccess?: boolean
|
||||
overrideLock?: boolean
|
||||
overwriteExistingFiles?: boolean
|
||||
@@ -78,6 +79,7 @@ export const updateOperation = async <TSlug extends CollectionSlug>(
|
||||
collection,
|
||||
depth,
|
||||
draft: draftArg = false,
|
||||
limit = 0,
|
||||
overrideAccess,
|
||||
overrideLock,
|
||||
overwriteExistingFiles = false,
|
||||
@@ -136,6 +138,7 @@ export const updateOperation = async <TSlug extends CollectionSlug>(
|
||||
|
||||
const query = await payload.db.queryDrafts<DataFromCollectionSlug<TSlug>>({
|
||||
collection: collectionConfig.slug,
|
||||
limit,
|
||||
locale,
|
||||
pagination: false,
|
||||
req,
|
||||
@@ -146,7 +149,7 @@ export const updateOperation = async <TSlug extends CollectionSlug>(
|
||||
} else {
|
||||
const query = await payload.db.find({
|
||||
collection: collectionConfig.slug,
|
||||
limit: 0,
|
||||
limit,
|
||||
locale,
|
||||
pagination: false,
|
||||
req,
|
||||
|
||||
@@ -154,6 +154,36 @@ describe('collections-rest', () => {
|
||||
expect(docs.pop().description).toEqual(description)
|
||||
})
|
||||
|
||||
it('should bulk update with limit', async () => {
|
||||
const ids = []
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const post = await createPost({ description: `to-update` })
|
||||
ids.push(post.id)
|
||||
}
|
||||
|
||||
const description = 'updated-description'
|
||||
const response = await restClient.PATCH(`/${slug}`, {
|
||||
body: JSON.stringify({
|
||||
description,
|
||||
}),
|
||||
query: { limit: 2, where: { id: { in: ids } } },
|
||||
})
|
||||
const { docs, errors } = await response.json()
|
||||
|
||||
expect(errors).toHaveLength(0)
|
||||
expect(response.status).toEqual(200)
|
||||
expect(docs).toHaveLength(2)
|
||||
expect(docs[0].description).toEqual(description)
|
||||
expect(docs.pop().description).toEqual(description)
|
||||
|
||||
const { docs: resDocs } = await payload.find({
|
||||
limit: 10,
|
||||
collection: slug,
|
||||
where: { id: { in: ids } },
|
||||
})
|
||||
expect(resDocs.at(-1).description).toEqual('to-update')
|
||||
})
|
||||
|
||||
it('should not bulk update with a bad query', async () => {
|
||||
for (let i = 0; i < 2; i++) {
|
||||
await createPost({ description: `desc ${i}` })
|
||||
|
||||
Reference in New Issue
Block a user