Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(db-mongodb): add support for query options in update operations #9191

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion packages/db-mongodb/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import type { CollationOptions, TransactionOptions } from 'mongodb'
import type { MongoMemoryReplSet } from 'mongodb-memory-server'
import type { ClientSession, Connection, ConnectOptions, QueryOptions } from 'mongoose'
import type { BaseDatabaseAdapter, DatabaseAdapterObj, Payload, UpdateOneArgs } from 'payload'
import type {
BaseDatabaseAdapter,
DatabaseAdapterObj,
Payload,
TypeWithID,
TypeWithVersion,
UpdateGlobalArgs,
UpdateGlobalVersionArgs,
UpdateOneArgs,
UpdateVersionArgs,
} from 'payload'

import fs from 'fs'
import mongoose from 'mongoose'
Expand Down Expand Up @@ -135,7 +145,16 @@ declare module 'payload' {
}[]
sessions: Record<number | string, ClientSession>
transactionOptions: TransactionOptions
updateGlobal: <T extends Record<string, unknown>>(
args: { options?: QueryOptions } & UpdateGlobalArgs<T>,
) => Promise<T>
updateGlobalVersion: <T extends TypeWithID = TypeWithID>(
args: { options?: QueryOptions } & UpdateGlobalVersionArgs<T>,
) => Promise<TypeWithVersion<T>>
updateOne: (args: { options?: QueryOptions } & UpdateOneArgs) => Promise<Document>
updateVersion: <T extends TypeWithID = TypeWithID>(
args: { options?: QueryOptions } & UpdateVersionArgs<T>,
) => Promise<TypeWithVersion<T>>
versions: {
[slug: string]: CollectionModel
}
Expand Down
6 changes: 4 additions & 2 deletions packages/db-mongodb/src/updateGlobal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { QueryOptions } from 'mongoose'
import type { PayloadRequest, UpdateGlobal } from 'payload'

import type { MongooseAdapter } from './index.js'
Expand All @@ -9,12 +10,13 @@ import { withSession } from './withSession.js'

export const updateGlobal: UpdateGlobal = async function updateGlobal(
this: MongooseAdapter,
{ slug, data, req = {} as PayloadRequest, select },
{ slug, data, options: optionsArgs = {}, req = {} as PayloadRequest, select },
) {
const Model = this.globals
const fields = this.payload.config.globals.find((global) => global.slug === slug).fields

const options = {
const options: QueryOptions = {
...optionsArgs,
...(await withSession(this, req)),
lean: true,
new: true,
Expand Down
6 changes: 5 additions & 1 deletion packages/db-mongodb/src/updateGlobalVersion.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { QueryOptions } from 'mongoose'

import {
buildVersionGlobalFields,
type PayloadRequest,
Expand All @@ -17,6 +19,7 @@ export async function updateGlobalVersion<T extends TypeWithID>(
id,
global: globalSlug,
locale,
options: optionsArgs = {},
req = {} as PayloadRequest,
select,
versionData,
Expand All @@ -30,7 +33,8 @@ export async function updateGlobalVersion<T extends TypeWithID>(
this.payload.config.globals.find((global) => global.slug === globalSlug),
)

const options = {
const options: QueryOptions = {
...optionsArgs,
...(await withSession(this, req)),
lean: true,
new: true,
Expand Down
16 changes: 14 additions & 2 deletions packages/db-mongodb/src/updateVersion.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { QueryOptions } from 'mongoose'

import { buildVersionCollectionFields, type PayloadRequest, type UpdateVersion } from 'payload'

import type { MongooseAdapter } from './index.js'
Expand All @@ -8,7 +10,16 @@ import { withSession } from './withSession.js'

export const updateVersion: UpdateVersion = async function updateVersion(
this: MongooseAdapter,
{ id, collection, locale, req = {} as PayloadRequest, select, versionData, where },
{
id,
collection,
locale,
options: optionsArgs = {},
req = {} as PayloadRequest,
select,
versionData,
where,
},
) {
const VersionModel = this.versions[collection]
const whereToUse = where || { id: { equals: id } }
Expand All @@ -17,7 +28,8 @@ export const updateVersion: UpdateVersion = async function updateVersion(
this.payload.collections[collection].config,
)

const options = {
const options: QueryOptions = {
...optionsArgs,
...(await withSession(this, req)),
lean: true,
new: true,
Expand Down
12 changes: 12 additions & 0 deletions packages/payload/src/database/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ export type FindGlobalArgs = {
export type UpdateGlobalVersionArgs<T = TypeWithID> = {
global: string
locale?: string
/**
* Additional database adapter specific options to pass to the query
*/
options?: Record<string, unknown>
req: PayloadRequest
select?: SelectType
versionData: T
Expand Down Expand Up @@ -316,6 +320,10 @@ export type CreateGlobal = <T extends Record<string, unknown> = any>(

export type UpdateGlobalArgs<T extends Record<string, unknown> = any> = {
data: T
/**
* Additional database adapter specific options to pass to the query
*/
options?: Record<string, unknown>
req: PayloadRequest
select?: SelectType
slug: string
Expand Down Expand Up @@ -380,6 +388,10 @@ export type DeleteVersions = (args: DeleteVersionsArgs) => Promise<void>
export type UpdateVersionArgs<T = TypeWithID> = {
collection: string
locale?: string
/**
* Additional database adapter specific options to pass to the query
*/
options?: Record<string, unknown>
req: PayloadRequest
select?: SelectType
versionData: T
Expand Down
Loading