Skip to content

Commit

Permalink
feat(db-mongodb): support query options in db update operations (#9191)
Browse files Browse the repository at this point in the history
The mongodb adapter `updateOne` method accepts an `options` argument
that allows query options to be passed to mongoose. This parameter was
added in #8397 to support the
`upsert` operation.

This `options` parameter can also be useful when using the database
adaptor directly without going through the local api. It is true that
the Mongoose models could be used directly in such situations, but the
adapter methods include a lot of useful functionality, like for instance
the sanitization of document and relationship ids, so it is desirable to
be able to use the adapter functions while still being able to provide
mongoose query options (e.g. `{timestamps: false}`).

This PR adds the same options parameter to the other update methods of
the mongodb adapter.
  • Loading branch information
franciscolourenco authored Nov 14, 2024
1 parent 315b4e5 commit 2d2d020
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
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 @@ -285,6 +285,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 @@ -318,6 +322,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 @@ -382,6 +390,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

0 comments on commit 2d2d020

Please sign in to comment.