From 2d2d020c29a7873ed6fdf06f5e3b309ebb25b22f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Louren=C3=A7o?= <208149+franciscolourenco@users.noreply.github.com> Date: Thu, 14 Nov 2024 21:15:03 +0100 Subject: [PATCH] feat(db-mongodb): support query options in db update operations (#9191) The mongodb adapter `updateOne` method accepts an `options` argument that allows query options to be passed to mongoose. This parameter was added in https://github.com/payloadcms/payload/pull/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. --- packages/db-mongodb/src/index.ts | 21 ++++++++++++++++++- packages/db-mongodb/src/updateGlobal.ts | 6 ++++-- .../db-mongodb/src/updateGlobalVersion.ts | 6 +++++- packages/db-mongodb/src/updateVersion.ts | 16 ++++++++++++-- packages/payload/src/database/types.ts | 12 +++++++++++ 5 files changed, 55 insertions(+), 6 deletions(-) diff --git a/packages/db-mongodb/src/index.ts b/packages/db-mongodb/src/index.ts index a8e74a1b1..3c77aa8ca 100644 --- a/packages/db-mongodb/src/index.ts +++ b/packages/db-mongodb/src/index.ts @@ -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' @@ -135,7 +145,16 @@ declare module 'payload' { }[] sessions: Record transactionOptions: TransactionOptions + updateGlobal: >( + args: { options?: QueryOptions } & UpdateGlobalArgs, + ) => Promise + updateGlobalVersion: ( + args: { options?: QueryOptions } & UpdateGlobalVersionArgs, + ) => Promise> updateOne: (args: { options?: QueryOptions } & UpdateOneArgs) => Promise + updateVersion: ( + args: { options?: QueryOptions } & UpdateVersionArgs, + ) => Promise> versions: { [slug: string]: CollectionModel } diff --git a/packages/db-mongodb/src/updateGlobal.ts b/packages/db-mongodb/src/updateGlobal.ts index 4016a33ee..0b0f36fc9 100644 --- a/packages/db-mongodb/src/updateGlobal.ts +++ b/packages/db-mongodb/src/updateGlobal.ts @@ -1,3 +1,4 @@ +import type { QueryOptions } from 'mongoose' import type { PayloadRequest, UpdateGlobal } from 'payload' import type { MongooseAdapter } from './index.js' @@ -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, diff --git a/packages/db-mongodb/src/updateGlobalVersion.ts b/packages/db-mongodb/src/updateGlobalVersion.ts index 4588ef595..a83a85ad4 100644 --- a/packages/db-mongodb/src/updateGlobalVersion.ts +++ b/packages/db-mongodb/src/updateGlobalVersion.ts @@ -1,3 +1,5 @@ +import type { QueryOptions } from 'mongoose' + import { buildVersionGlobalFields, type PayloadRequest, @@ -17,6 +19,7 @@ export async function updateGlobalVersion( id, global: globalSlug, locale, + options: optionsArgs = {}, req = {} as PayloadRequest, select, versionData, @@ -30,7 +33,8 @@ export async function updateGlobalVersion( this.payload.config.globals.find((global) => global.slug === globalSlug), ) - const options = { + const options: QueryOptions = { + ...optionsArgs, ...(await withSession(this, req)), lean: true, new: true, diff --git a/packages/db-mongodb/src/updateVersion.ts b/packages/db-mongodb/src/updateVersion.ts index f199d4c7f..b79f6727c 100644 --- a/packages/db-mongodb/src/updateVersion.ts +++ b/packages/db-mongodb/src/updateVersion.ts @@ -1,3 +1,5 @@ +import type { QueryOptions } from 'mongoose' + import { buildVersionCollectionFields, type PayloadRequest, type UpdateVersion } from 'payload' import type { MongooseAdapter } from './index.js' @@ -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 } } @@ -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, diff --git a/packages/payload/src/database/types.ts b/packages/payload/src/database/types.ts index 7d060a1eb..bb7d2273d 100644 --- a/packages/payload/src/database/types.ts +++ b/packages/payload/src/database/types.ts @@ -285,6 +285,10 @@ export type FindGlobalArgs = { export type UpdateGlobalVersionArgs = { global: string locale?: string + /** + * Additional database adapter specific options to pass to the query + */ + options?: Record req: PayloadRequest select?: SelectType versionData: T @@ -318,6 +322,10 @@ export type CreateGlobal = = any>( export type UpdateGlobalArgs = any> = { data: T + /** + * Additional database adapter specific options to pass to the query + */ + options?: Record req: PayloadRequest select?: SelectType slug: string @@ -382,6 +390,10 @@ export type DeleteVersions = (args: DeleteVersionsArgs) => Promise export type UpdateVersionArgs = { collection: string locale?: string + /** + * Additional database adapter specific options to pass to the query + */ + options?: Record req: PayloadRequest select?: SelectType versionData: T