feat: add upsert to database interface and adapters (#8397)

- Adds the upsert method to the database interface
- Adds a mongodb specific option to extend the updateOne to accept
mongoDB Query Options (to pass `upsert: true`)
- Added upsert method to all database adapters
- Uses db.upsert in the payload preferences update operation

Includes a test using payload-preferences
This commit is contained in:
Dan Ribbens
2024-09-25 09:23:54 -04:00
committed by GitHub
parent 06ea67a184
commit 82ba1930e5
10 changed files with 101 additions and 24 deletions

View File

@@ -101,7 +101,7 @@ describe('Auth', () => {
describe('logged in', () => {
let token: string | undefined
let loggedInUser: User | undefined
let loggedInUser: undefined | User
beforeAll(async () => {
const response = await restClient.POST(`/${slug}/login`, {
@@ -396,6 +396,52 @@ describe('Auth', () => {
expect(result.docs).toHaveLength(1)
})
it('should only have one preference per user per key', async () => {
await restClient.POST(`/payload-preferences/${key}`, {
body: JSON.stringify({
value: { property: 'test', property2: 'test' },
}),
headers: {
Authorization: `JWT ${token}`,
},
})
await restClient.POST(`/payload-preferences/${key}`, {
body: JSON.stringify({
value: { property: 'updated', property2: 'updated' },
}),
headers: {
Authorization: `JWT ${token}`,
},
})
const result = await payload.find({
collection: 'payload-preferences',
depth: 0,
where: {
and: [
{
key: { equals: key },
},
{
'user.relationTo': {
equals: 'users',
},
},
{
'user.value': {
equals: loggedInUser.id,
},
},
],
},
})
expect(result.docs[0].value.property).toStrictEqual('updated')
expect(result.docs[0].value.property2).toStrictEqual('updated')
expect(result.docs).toHaveLength(1)
})
it('should delete', async () => {
const response = await restClient.DELETE(`/payload-preferences/${key}`, {
headers: {