## Description This is the beta (v3) PR for the v2 PR [here](https://github.com/payloadcms/payload/pull/6857) Addresses #6800, #5108 - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) ## Checklist: - [x] I have added tests that prove my fix is effective or that my feature works - [x] Existing test suite passes locally with my changes
22 lines
616 B
TypeScript
22 lines
616 B
TypeScript
import type { ClientSession } from 'mongoose'
|
|
import type { PayloadRequest } from 'payload'
|
|
|
|
import type { MongooseAdapter } from './index.js'
|
|
|
|
/**
|
|
* returns the session belonging to the transaction of the req.session if exists
|
|
* @returns ClientSession
|
|
*/
|
|
export async function withSession(
|
|
db: MongooseAdapter,
|
|
req: PayloadRequest,
|
|
): Promise<{ session: ClientSession } | object> {
|
|
let transactionID = req.transactionID
|
|
|
|
if (transactionID instanceof Promise) {
|
|
transactionID = await req.transactionID
|
|
}
|
|
|
|
if (req) return db.sessions[transactionID] ? { session: db.sessions[transactionID] } : {}
|
|
}
|