Skip to content

Commit c21dac1

Browse files
authored
perf(db-*): add option to disable returning modified documents in db methods (#11393)
This PR adds a new `returning` option to various db adapter methods. Setting it to `false` where the return value is not used will lead to performance gains, as we don't have to do additional db calls to fetch the updated document and then sanitize it.
1 parent b3e7a9d commit c21dac1

22 files changed

+232
-35
lines changed

packages/db-mongodb/src/create.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { CreateOptions } from 'mongoose'
2-
import type { Create, Document } from 'payload'
2+
import type { Create } from 'payload'
33

44
import type { MongooseAdapter } from './index.js'
55

@@ -9,7 +9,7 @@ import { transform } from './utilities/transform.js'
99

1010
export const create: Create = async function create(
1111
this: MongooseAdapter,
12-
{ collection, data, req },
12+
{ collection, data, req, returning },
1313
) {
1414
const Model = this.collections[collection]
1515
const options: CreateOptions = {
@@ -34,6 +34,9 @@ export const create: Create = async function create(
3434
} catch (error) {
3535
handleError({ collection, error, req })
3636
}
37+
if (returning === false) {
38+
return null
39+
}
3740

3841
doc = doc.toObject()
3942

packages/db-mongodb/src/createGlobal.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { transform } from './utilities/transform.js'
88

99
export const createGlobal: CreateGlobal = async function createGlobal(
1010
this: MongooseAdapter,
11-
{ slug, data, req },
11+
{ slug, data, req, returning },
1212
) {
1313
const Model = this.globals
1414

@@ -25,6 +25,9 @@ export const createGlobal: CreateGlobal = async function createGlobal(
2525
}
2626

2727
let [result] = (await Model.create([data], options)) as any
28+
if (returning === false) {
29+
return null
30+
}
2831

2932
result = result.toObject()
3033

packages/db-mongodb/src/createGlobalVersion.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const createGlobalVersion: CreateGlobalVersion = async function createGlo
1616
parent,
1717
publishedLocale,
1818
req,
19+
returning,
1920
snapshot,
2021
updatedAt,
2122
versionData,
@@ -75,6 +76,10 @@ export const createGlobalVersion: CreateGlobalVersion = async function createGlo
7576
options,
7677
)
7778

79+
if (returning === false) {
80+
return null
81+
}
82+
7883
doc = doc.toObject()
7984

8085
transform({

packages/db-mongodb/src/createVersion.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const createVersion: CreateVersion = async function createVersion(
1616
parent,
1717
publishedLocale,
1818
req,
19+
returning,
1920
snapshot,
2021
updatedAt,
2122
versionData,
@@ -86,6 +87,10 @@ export const createVersion: CreateVersion = async function createVersion(
8687
options,
8788
)
8889

90+
if (returning === false) {
91+
return null
92+
}
93+
8994
doc = doc.toObject()
9095

9196
transform({

packages/db-mongodb/src/deleteOne.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { QueryOptions } from 'mongoose'
1+
import type { MongooseUpdateQueryOptions } from 'mongoose'
22
import type { DeleteOne } from 'payload'
33

44
import type { MongooseAdapter } from './index.js'
@@ -10,10 +10,10 @@ import { transform } from './utilities/transform.js'
1010

1111
export const deleteOne: DeleteOne = async function deleteOne(
1212
this: MongooseAdapter,
13-
{ collection, req, select, where },
13+
{ collection, req, returning, select, where },
1414
) {
1515
const Model = this.collections[collection]
16-
const options: QueryOptions = {
16+
const options: MongooseUpdateQueryOptions = {
1717
projection: buildProjectionFromSelect({
1818
adapter: this,
1919
fields: this.payload.collections[collection].config.flattenedFields,
@@ -29,6 +29,11 @@ export const deleteOne: DeleteOne = async function deleteOne(
2929
where,
3030
})
3131

32+
if (returning === false) {
33+
await Model.deleteOne(query, options)?.lean()
34+
return null
35+
}
36+
3237
const doc = await Model.findOneAndDelete(query, options)?.lean()
3338

3439
if (!doc) {

packages/db-mongodb/src/updateGlobal.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { QueryOptions } from 'mongoose'
1+
import type { MongooseUpdateQueryOptions } from 'mongoose'
22
import type { UpdateGlobal } from 'payload'
33

44
import type { MongooseAdapter } from './index.js'
@@ -9,12 +9,12 @@ import { transform } from './utilities/transform.js'
99

1010
export const updateGlobal: UpdateGlobal = async function updateGlobal(
1111
this: MongooseAdapter,
12-
{ slug, data, options: optionsArgs = {}, req, select },
12+
{ slug, data, options: optionsArgs = {}, req, returning, select },
1313
) {
1414
const Model = this.globals
1515
const fields = this.payload.config.globals.find((global) => global.slug === slug).fields
1616

17-
const options: QueryOptions = {
17+
const options: MongooseUpdateQueryOptions = {
1818
...optionsArgs,
1919
lean: true,
2020
new: true,
@@ -28,6 +28,11 @@ export const updateGlobal: UpdateGlobal = async function updateGlobal(
2828

2929
transform({ adapter: this, data, fields, globalSlug: slug, operation: 'write' })
3030

31+
if (returning === false) {
32+
await Model.updateOne({ globalType: slug }, data, options)
33+
return null
34+
}
35+
3136
const result: any = await Model.findOneAndUpdate({ globalType: slug }, data, options)
3237

3338
transform({ adapter: this, data: result, fields, globalSlug: slug, operation: 'read' })

packages/db-mongodb/src/updateGlobalVersion.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { QueryOptions } from 'mongoose'
1+
import type { MongooseUpdateQueryOptions } from 'mongoose'
22

33
import { buildVersionGlobalFields, type TypeWithID, type UpdateGlobalVersionArgs } from 'payload'
44

@@ -17,6 +17,7 @@ export async function updateGlobalVersion<T extends TypeWithID>(
1717
locale,
1818
options: optionsArgs = {},
1919
req,
20+
returning,
2021
select,
2122
versionData,
2223
where,
@@ -28,7 +29,7 @@ export async function updateGlobalVersion<T extends TypeWithID>(
2829
const currentGlobal = this.payload.config.globals.find((global) => global.slug === globalSlug)
2930
const fields = buildVersionGlobalFields(this.payload.config, currentGlobal)
3031
const flattenedFields = buildVersionGlobalFields(this.payload.config, currentGlobal, true)
31-
const options: QueryOptions = {
32+
const options: MongooseUpdateQueryOptions = {
3233
...optionsArgs,
3334
lean: true,
3435
new: true,
@@ -49,6 +50,11 @@ export async function updateGlobalVersion<T extends TypeWithID>(
4950

5051
transform({ adapter: this, data: versionData, fields, operation: 'write' })
5152

53+
if (returning === false) {
54+
await VersionModel.updateOne(query, versionData, options)
55+
return null
56+
}
57+
5258
const doc = await VersionModel.findOneAndUpdate(query, versionData, options)
5359

5460
if (!doc) {

packages/db-mongodb/src/updateOne.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { QueryOptions } from 'mongoose'
1+
import type { MongooseUpdateQueryOptions } from 'mongoose'
22
import type { UpdateOne } from 'payload'
33

44
import type { MongooseAdapter } from './index.js'
@@ -11,12 +11,22 @@ import { transform } from './utilities/transform.js'
1111

1212
export const updateOne: UpdateOne = async function updateOne(
1313
this: MongooseAdapter,
14-
{ id, collection, data, locale, options: optionsArgs = {}, req, select, where: whereArg },
14+
{
15+
id,
16+
collection,
17+
data,
18+
locale,
19+
options: optionsArgs = {},
20+
req,
21+
returning,
22+
select,
23+
where: whereArg,
24+
},
1525
) {
1626
const where = id ? { id: { equals: id } } : whereArg
1727
const Model = this.collections[collection]
1828
const fields = this.payload.collections[collection].config.fields
19-
const options: QueryOptions = {
29+
const options: MongooseUpdateQueryOptions = {
2030
...optionsArgs,
2131
lean: true,
2232
new: true,
@@ -41,7 +51,12 @@ export const updateOne: UpdateOne = async function updateOne(
4151
transform({ adapter: this, data, fields, operation: 'write' })
4252

4353
try {
44-
result = await Model.findOneAndUpdate(query, data, options)
54+
if (returning === false) {
55+
await Model.updateOne(query, data, options)
56+
return null
57+
} else {
58+
result = await Model.findOneAndUpdate(query, data, options)
59+
}
4560
} catch (error) {
4661
handleError({ collection, error, req })
4762
}

packages/db-mongodb/src/updateVersion.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { QueryOptions } from 'mongoose'
1+
import type { MongooseUpdateQueryOptions } from 'mongoose'
22

33
import { buildVersionCollectionFields, type UpdateVersion } from 'payload'
44

@@ -11,7 +11,7 @@ import { transform } from './utilities/transform.js'
1111

1212
export const updateVersion: UpdateVersion = async function updateVersion(
1313
this: MongooseAdapter,
14-
{ id, collection, locale, options: optionsArgs = {}, req, select, versionData, where },
14+
{ id, collection, locale, options: optionsArgs = {}, req, returning, select, versionData, where },
1515
) {
1616
const VersionModel = this.versions[collection]
1717
const whereToUse = where || { id: { equals: id } }
@@ -26,7 +26,7 @@ export const updateVersion: UpdateVersion = async function updateVersion(
2626
true,
2727
)
2828

29-
const options: QueryOptions = {
29+
const options: MongooseUpdateQueryOptions = {
3030
...optionsArgs,
3131
lean: true,
3232
new: true,
@@ -47,6 +47,11 @@ export const updateVersion: UpdateVersion = async function updateVersion(
4747

4848
transform({ adapter: this, data: versionData, fields, operation: 'write' })
4949

50+
if (returning === false) {
51+
await VersionModel.updateOne(query, versionData, options)
52+
return null
53+
}
54+
5055
const doc = await VersionModel.findOneAndUpdate(query, versionData, options)
5156

5257
if (!doc) {

packages/db-mongodb/src/upsert.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ import type { MongooseAdapter } from './index.js'
44

55
export const upsert: Upsert = async function upsert(
66
this: MongooseAdapter,
7-
{ collection, data, locale, req, select, where },
7+
{ collection, data, locale, req, returning, select, where },
88
) {
9-
return this.updateOne({ collection, data, locale, options: { upsert: true }, req, select, where })
9+
return this.updateOne({
10+
collection,
11+
data,
12+
locale,
13+
options: { upsert: true },
14+
req,
15+
returning,
16+
select,
17+
where,
18+
})
1019
}

0 commit comments

Comments
 (0)