feat(db-*): add updateMany method to database adapter (#11441)
This PR adds a new `payload.db.updateMany` method, which is a more performant way to update multiple documents compared to using `payload.update`.
This commit is contained in:
@@ -28,6 +28,7 @@ import { devUser } from '../credentials.js'
|
||||
import { initPayloadInt } from '../helpers/initPayloadInt.js'
|
||||
import { isMongoose } from '../helpers/isMongoose.js'
|
||||
import removeFiles from '../helpers/removeFiles.js'
|
||||
import { seed } from './seed.js'
|
||||
import { errorOnUnnamedFieldsSlug, postsSlug } from './shared.js'
|
||||
|
||||
const filename = fileURLToPath(import.meta.url)
|
||||
@@ -43,9 +44,17 @@ process.env.PAYLOAD_CONFIG_PATH = path.join(dirname, 'config.ts')
|
||||
|
||||
describe('database', () => {
|
||||
beforeAll(async () => {
|
||||
process.env.SEED_IN_CONFIG_ONINIT = 'false' // Makes it so the payload config onInit seed is not run. Otherwise, the seed would be run unnecessarily twice for the initial test run - once for beforeEach and once for onInit
|
||||
;({ payload, restClient } = await initPayloadInt(dirname))
|
||||
payload.db.migrationDir = path.join(dirname, './migrations')
|
||||
|
||||
await seed(payload)
|
||||
|
||||
await restClient.login({
|
||||
slug: 'users',
|
||||
credentials: devUser,
|
||||
})
|
||||
|
||||
const loginResult = await payload.login({
|
||||
collection: 'users',
|
||||
data: {
|
||||
@@ -794,6 +803,7 @@ describe('database', () => {
|
||||
data: {
|
||||
title,
|
||||
},
|
||||
depth: 0,
|
||||
disableTransaction: true,
|
||||
})
|
||||
})
|
||||
@@ -876,6 +886,80 @@ describe('database', () => {
|
||||
|
||||
expect(result.point).toEqual([5, 10])
|
||||
})
|
||||
|
||||
it('ensure updateMany updates all docs and respects where query', async () => {
|
||||
await payload.db.deleteMany({
|
||||
collection: postsSlug,
|
||||
where: {
|
||||
id: {
|
||||
exists: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
await payload.create({
|
||||
collection: postsSlug,
|
||||
data: {
|
||||
title: 'notupdated',
|
||||
},
|
||||
})
|
||||
|
||||
// Create 5 posts
|
||||
for (let i = 0; i < 5; i++) {
|
||||
await payload.create({
|
||||
collection: postsSlug,
|
||||
data: {
|
||||
title: `v1 ${i}`,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const result = await payload.db.updateMany({
|
||||
collection: postsSlug,
|
||||
data: {
|
||||
title: 'updated',
|
||||
},
|
||||
where: {
|
||||
title: {
|
||||
not_equals: 'notupdated',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(result?.length).toBe(5)
|
||||
expect(result?.[0]?.title).toBe('updated')
|
||||
expect(result?.[4]?.title).toBe('updated')
|
||||
|
||||
// Ensure all posts minus the one we don't want updated are updated
|
||||
const { docs } = await payload.find({
|
||||
collection: postsSlug,
|
||||
depth: 0,
|
||||
pagination: false,
|
||||
where: {
|
||||
title: {
|
||||
equals: 'updated',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(docs).toHaveLength(5)
|
||||
expect(docs?.[0]?.title).toBe('updated')
|
||||
expect(docs?.[4]?.title).toBe('updated')
|
||||
|
||||
const { docs: notUpdatedDocs } = await payload.find({
|
||||
collection: postsSlug,
|
||||
depth: 0,
|
||||
pagination: false,
|
||||
where: {
|
||||
title: {
|
||||
not_equals: 'updated',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(notUpdatedDocs).toHaveLength(1)
|
||||
expect(notUpdatedDocs?.[0]?.title).toBe('notupdated')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Error Handler', () => {
|
||||
|
||||
Reference in New Issue
Block a user