feat: atomic number field updates (#13118)

Based on https://github.com/payloadcms/payload/pull/13060 which should
be merged first
This PR adds ability to update number fields atomically, which could be
important with parallel writes. For now we support this only via
`payload.db.updateOne`.

For example:
```js
// increment by 10
const res = await payload.db.updateOne({
  data: {
    number: {
      $inc: 10,
    },
  },
  collection: 'posts',
  where: { id: { equals: post.id } },
})

// decrement by 3
const res2 = await payload.db.updateOne({
  data: {
    number: {
      $inc: -3,
    },
  },
  collection: 'posts',
  where: { id: { equals: post.id } },
})
```
This commit is contained in:
Sasha
2025-07-16 07:53:45 +03:00
committed by GitHub
parent 2a59c5bf8c
commit 841bf891d0
8 changed files with 106 additions and 10 deletions

View File

@@ -2836,6 +2836,34 @@ describe('database', () => {
expect(res.arrayWithIDs[0].text).toBe('some text')
})
it('should allow incremental number update', async () => {
const post = await payload.create({ collection: 'posts', data: { number: 1, title: 'post' } })
const res = await payload.db.updateOne({
data: {
number: {
$inc: 10,
},
},
collection: 'posts',
where: { id: { equals: post.id } },
})
expect(res.number).toBe(11)
const res2 = await payload.db.updateOne({
data: {
number: {
$inc: -3,
},
},
collection: 'posts',
where: { id: { equals: post.id } },
})
expect(res2.number).toBe(8)
})
it('should support x3 nesting blocks', async () => {
const res = await payload.create({
collection: 'posts',