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: make req partial and optional in DB / Local API operations #9935

Merged
merged 1 commit into from
Dec 19, 2024
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
10 changes: 6 additions & 4 deletions packages/db-mongodb/src/count.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import type { CountOptions } from 'mongodb'
import type { Count, PayloadRequest } from 'payload'
import type { Count } from 'payload'

import { flattenWhereToOperators } from 'payload'

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

import { withSession } from './withSession.js'
import { getSession } from './utilities/getSession.js'

export const count: Count = async function count(
this: MongooseAdapter,
{ collection, locale, req = {} as PayloadRequest, where },
{ collection, locale, req, where },
) {
const Model = this.collections[collection]
const options: CountOptions = await withSession(this, req)
const options: CountOptions = {
session: await getSession(this, req),
}

let hasNearConstraint = false

Expand Down
10 changes: 6 additions & 4 deletions packages/db-mongodb/src/countGlobalVersions.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import type { CountOptions } from 'mongodb'
import type { CountGlobalVersions, PayloadRequest } from 'payload'
import type { CountGlobalVersions } from 'payload'

import { flattenWhereToOperators } from 'payload'

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

import { withSession } from './withSession.js'
import { getSession } from './utilities/getSession.js'

export const countGlobalVersions: CountGlobalVersions = async function countGlobalVersions(
this: MongooseAdapter,
{ global, locale, req = {} as PayloadRequest, where },
{ global, locale, req, where },
) {
const Model = this.versions[global]
const options: CountOptions = await withSession(this, req)
const options: CountOptions = {
session: await getSession(this, req),
}

let hasNearConstraint = false

Expand Down
10 changes: 6 additions & 4 deletions packages/db-mongodb/src/countVersions.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import type { CountOptions } from 'mongodb'
import type { CountVersions, PayloadRequest } from 'payload'
import type { CountVersions } from 'payload'

import { flattenWhereToOperators } from 'payload'

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

import { withSession } from './withSession.js'
import { getSession } from './utilities/getSession.js'

export const countVersions: CountVersions = async function countVersions(
this: MongooseAdapter,
{ collection, locale, req = {} as PayloadRequest, where },
{ collection, locale, req, where },
) {
const Model = this.versions[collection]
const options: CountOptions = await withSession(this, req)
const options: CountOptions = {
session: await getSession(this, req),
}

let hasNearConstraint = false

Expand Down
12 changes: 8 additions & 4 deletions packages/db-mongodb/src/create.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import type { Create, Document, PayloadRequest } from 'payload'
import type { CreateOptions } from 'mongoose'
import type { Create, Document } from 'payload'

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

import { getSession } from './utilities/getSession.js'
import { handleError } from './utilities/handleError.js'
import { sanitizeRelationshipIDs } from './utilities/sanitizeRelationshipIDs.js'
import { withSession } from './withSession.js'

export const create: Create = async function create(
this: MongooseAdapter,
{ collection, data, req = {} as PayloadRequest },
{ collection, data, req },
) {
const Model = this.collections[collection]
const options = await withSession(this, req)
const options: CreateOptions = {
session: await getSession(this, req),
}

let doc

const sanitizedData = sanitizeRelationshipIDs({
Expand Down
11 changes: 7 additions & 4 deletions packages/db-mongodb/src/createGlobal.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { CreateGlobal, PayloadRequest } from 'payload'
import type { CreateOptions } from 'mongoose'
import type { CreateGlobal } from 'payload'

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

import { getSession } from './utilities/getSession.js'
import { sanitizeInternalFields } from './utilities/sanitizeInternalFields.js'
import { sanitizeRelationshipIDs } from './utilities/sanitizeRelationshipIDs.js'
import { withSession } from './withSession.js'

export const createGlobal: CreateGlobal = async function createGlobal(
this: MongooseAdapter,
{ slug, data, req = {} as PayloadRequest },
{ slug, data, req },
) {
const Model = this.globals

Expand All @@ -21,7 +22,9 @@ export const createGlobal: CreateGlobal = async function createGlobal(
fields: this.payload.config.globals.find((globalConfig) => globalConfig.slug === slug).fields,
})

const options = await withSession(this, req)
const options: CreateOptions = {
session: await getSession(this, req),
}

let [result] = (await Model.create([global], options)) as any

Expand Down
17 changes: 8 additions & 9 deletions packages/db-mongodb/src/createGlobalVersion.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import {
buildVersionGlobalFields,
type CreateGlobalVersion,
type Document,
type PayloadRequest,
} from 'payload'
import type { CreateOptions } from 'mongoose'

import { buildVersionGlobalFields, type CreateGlobalVersion, type Document } from 'payload'

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

import { getSession } from './utilities/getSession.js'
import { sanitizeRelationshipIDs } from './utilities/sanitizeRelationshipIDs.js'
import { withSession } from './withSession.js'

export const createGlobalVersion: CreateGlobalVersion = async function createGlobalVersion(
this: MongooseAdapter,
Expand All @@ -18,14 +15,16 @@ export const createGlobalVersion: CreateGlobalVersion = async function createGlo
globalSlug,
parent,
publishedLocale,
req = {} as PayloadRequest,
req,
snapshot,
updatedAt,
versionData,
},
) {
const VersionModel = this.versions[globalSlug]
const options = await withSession(this, req)
const options: CreateOptions = {
session: await getSession(this, req),
}

const data = sanitizeRelationshipIDs({
config: this.payload.config,
Expand Down
17 changes: 8 additions & 9 deletions packages/db-mongodb/src/createVersion.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type { CreateOptions } from 'mongoose'

import { Types } from 'mongoose'
import {
buildVersionCollectionFields,
type CreateVersion,
type Document,
type PayloadRequest,
} from 'payload'
import { buildVersionCollectionFields, type CreateVersion, type Document } from 'payload'

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

import { getSession } from './utilities/getSession.js'
import { sanitizeRelationshipIDs } from './utilities/sanitizeRelationshipIDs.js'
import { withSession } from './withSession.js'

export const createVersion: CreateVersion = async function createVersion(
this: MongooseAdapter,
Expand All @@ -19,14 +16,16 @@ export const createVersion: CreateVersion = async function createVersion(
createdAt,
parent,
publishedLocale,
req = {} as PayloadRequest,
req,
snapshot,
updatedAt,
versionData,
},
) {
const VersionModel = this.versions[collectionSlug]
const options = await withSession(this, req)
const options: CreateOptions = {
session: await getSession(this, req),
}

const data = sanitizeRelationshipIDs({
config: this.payload.config,
Expand Down
12 changes: 6 additions & 6 deletions packages/db-mongodb/src/deleteMany.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { DeleteMany, PayloadRequest } from 'payload'
import type { DeleteOptions } from 'mongodb'
import type { DeleteMany } from 'payload'

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

import { withSession } from './withSession.js'
import { getSession } from './utilities/getSession.js'

export const deleteMany: DeleteMany = async function deleteMany(
this: MongooseAdapter,
{ collection, req = {} as PayloadRequest, where },
{ collection, req, where },
) {
const Model = this.collections[collection]
const options = {
...(await withSession(this, req)),
lean: true,
const options: DeleteOptions = {
session: await getSession(this, req),
}

const query = await Model.buildQuery({
Expand Down
25 changes: 13 additions & 12 deletions packages/db-mongodb/src/deleteOne.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import type { DeleteOne, Document, PayloadRequest } from 'payload'
import type { QueryOptions } from 'mongoose'
import type { DeleteOne, Document } from 'payload'

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

import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'
import { getSession } from './utilities/getSession.js'
import { sanitizeInternalFields } from './utilities/sanitizeInternalFields.js'
import { withSession } from './withSession.js'

export const deleteOne: DeleteOne = async function deleteOne(
this: MongooseAdapter,
{ collection, req = {} as PayloadRequest, select, where },
{ collection, req, select, where },
) {
const Model = this.collections[collection]
const options = await withSession(this, req)
const options: QueryOptions = {
projection: buildProjectionFromSelect({
adapter: this,
fields: this.payload.collections[collection].config.flattenedFields,
select,
}),
session: await getSession(this, req),
}

const query = await Model.buildQuery({
payload: this.payload,
where,
})

const doc = await Model.findOneAndDelete(query, {
...options,
projection: buildProjectionFromSelect({
adapter: this,
fields: this.payload.collections[collection].config.flattenedFields,
select,
}),
}).lean()
const doc = await Model.findOneAndDelete(query, options).lean()

let result: Document = JSON.parse(JSON.stringify(doc))

Expand Down
14 changes: 6 additions & 8 deletions packages/db-mongodb/src/deleteVersions.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import type { DeleteVersions, PayloadRequest } from 'payload'
import type { DeleteVersions } from 'payload'

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

import { withSession } from './withSession.js'
import { getSession } from './utilities/getSession.js'

export const deleteVersions: DeleteVersions = async function deleteVersions(
this: MongooseAdapter,
{ collection, locale, req = {} as PayloadRequest, where },
{ collection, locale, req, where },
) {
const VersionsModel = this.versions[collection]
const options = {
...(await withSession(this, req)),
lean: true,
}

const session = await getSession(this, req)

const query = await VersionsModel.buildQuery({
locale,
payload: this.payload,
where,
})

await VersionsModel.deleteMany(query, options)
await VersionsModel.deleteMany(query, { session })
}
15 changes: 9 additions & 6 deletions packages/db-mongodb/src/find.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PaginateOptions } from 'mongoose'
import type { Find, PayloadRequest } from 'payload'
import type { Find } from 'payload'

import { flattenWhereToOperators } from 'payload'

Expand All @@ -8,8 +8,8 @@ import type { MongooseAdapter } from './index.js'
import { buildSortParam } from './queries/buildSortParam.js'
import { buildJoinAggregation } from './utilities/buildJoinAggregation.js'
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'
import { getSession } from './utilities/getSession.js'
import { sanitizeInternalFields } from './utilities/sanitizeInternalFields.js'
import { withSession } from './withSession.js'

export const find: Find = async function find(
this: MongooseAdapter,
Expand All @@ -21,15 +21,16 @@ export const find: Find = async function find(
page,
pagination,
projection,
req = {} as PayloadRequest,
req,
select,
sort: sortArg,
where,
},
) {
const Model = this.collections[collection]
const collectionConfig = this.payload.collections[collection].config
const options = await withSession(this, req)

const session = await getSession(this, req)

let hasNearConstraint = false

Expand Down Expand Up @@ -60,7 +61,9 @@ export const find: Find = async function find(
const paginationOptions: PaginateOptions = {
lean: true,
leanWithId: true,
options,
options: {
session,
},
page,
pagination,
projection,
Expand Down Expand Up @@ -92,8 +95,8 @@ export const find: Find = async function find(
paginationOptions.useCustomCountFn = () => {
return Promise.resolve(
Model.countDocuments(query, {
...options,
hint: { _id: 1 },
session,
}),
)
}
Expand Down
11 changes: 6 additions & 5 deletions packages/db-mongodb/src/findGlobal.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import type { FindGlobal, PayloadRequest } from 'payload'
import type { QueryOptions } from 'mongoose'
import type { FindGlobal } from 'payload'

import { combineQueries } from 'payload'

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

import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'
import { getSession } from './utilities/getSession.js'
import { sanitizeInternalFields } from './utilities/sanitizeInternalFields.js'
import { withSession } from './withSession.js'

export const findGlobal: FindGlobal = async function findGlobal(
this: MongooseAdapter,
{ slug, locale, req = {} as PayloadRequest, select, where },
{ slug, locale, req, select, where },
) {
const Model = this.globals
const options = {
...(await withSession(this, req)),
const options: QueryOptions = {
lean: true,
select: buildProjectionFromSelect({
adapter: this,
fields: this.payload.globals.config.find((each) => each.slug === slug).flattenedFields,
select,
}),
session: await getSession(this, req),
}

const query = await Model.buildQuery({
Expand Down
Loading