diff --git a/packages/db-mongodb/src/count.ts b/packages/db-mongodb/src/count.ts index fd0aa284d20..e88fb5e59c6 100644 --- a/packages/db-mongodb/src/count.ts +++ b/packages/db-mongodb/src/count.ts @@ -15,10 +15,6 @@ export const count: Count = async function count( ) { const { collectionConfig, Model } = getCollection({ adapter: this, collectionSlug }) - const options: CountOptions = { - session: await getSession(this, req), - } - let hasNearConstraint = false if (where) { @@ -37,6 +33,10 @@ export const count: Count = async function count( // useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters. const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0 + const options: CountOptions = { + session: await getSession(this, req), + } + if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) { // Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding // a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents, diff --git a/packages/db-mongodb/src/countGlobalVersions.ts b/packages/db-mongodb/src/countGlobalVersions.ts index c91f1c8fd85..0d4ff89a298 100644 --- a/packages/db-mongodb/src/countGlobalVersions.ts +++ b/packages/db-mongodb/src/countGlobalVersions.ts @@ -15,10 +15,6 @@ export const countGlobalVersions: CountGlobalVersions = async function countGlob ) { const { globalConfig, Model } = getGlobal({ adapter: this, globalSlug, versions: true }) - const options: CountOptions = { - session: await getSession(this, req), - } - let hasNearConstraint = false if (where) { @@ -36,6 +32,10 @@ export const countGlobalVersions: CountGlobalVersions = async function countGlob // useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters. const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0 + const options: CountOptions = { + session: await getSession(this, req), + } + if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) { // Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding // a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents, diff --git a/packages/db-mongodb/src/countVersions.ts b/packages/db-mongodb/src/countVersions.ts index bb31931cb00..d3ba5690160 100644 --- a/packages/db-mongodb/src/countVersions.ts +++ b/packages/db-mongodb/src/countVersions.ts @@ -19,10 +19,6 @@ export const countVersions: CountVersions = async function countVersions( versions: true, }) - const options: CountOptions = { - session: await getSession(this, req), - } - let hasNearConstraint = false if (where) { @@ -40,6 +36,10 @@ export const countVersions: CountVersions = async function countVersions( // useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters. const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0 + const options: CountOptions = { + session: await getSession(this, req), + } + if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) { // Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding // a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents, diff --git a/packages/db-mongodb/src/create.ts b/packages/db-mongodb/src/create.ts index 82e65fcd702..9c96cd32467 100644 --- a/packages/db-mongodb/src/create.ts +++ b/packages/db-mongodb/src/create.ts @@ -15,12 +15,6 @@ export const create: Create = async function create( ) { const { collectionConfig, customIDType, Model } = getCollection({ adapter: this, collectionSlug }) - const options: CreateOptions = { - session: await getSession(this, req), - // Timestamps are manually added by the write transform - timestamps: false, - } - let doc if (!data.createdAt) { @@ -47,6 +41,12 @@ export const create: Create = async function create( } } + const options: CreateOptions = { + session: await getSession(this, req), + // Timestamps are manually added by the write transform + timestamps: false, + } + try { ;[doc] = await Model.create([data], options) } catch (error) { diff --git a/packages/db-mongodb/src/createGlobalVersion.ts b/packages/db-mongodb/src/createGlobalVersion.ts index 0469d919e39..64104d6421d 100644 --- a/packages/db-mongodb/src/createGlobalVersion.ts +++ b/packages/db-mongodb/src/createGlobalVersion.ts @@ -22,12 +22,6 @@ export const createGlobalVersion: CreateGlobalVersion = async function createGlo ) { const { globalConfig, Model } = getGlobal({ adapter: this, globalSlug, versions: true }) - const options = { - session: await getSession(this, req), - // Timestamps are manually added by the write transform - timestamps: false, - } - const data = { autosave, createdAt, @@ -50,6 +44,12 @@ export const createGlobalVersion: CreateGlobalVersion = async function createGlo operation: 'write', }) + const options = { + session: await getSession(this, req), + // Timestamps are manually added by the write transform + timestamps: false, + } + let [doc] = await Model.create([data], options, req) await Model.updateMany( diff --git a/packages/db-mongodb/src/createVersion.ts b/packages/db-mongodb/src/createVersion.ts index a6a7b5c2c20..aba93c496ad 100644 --- a/packages/db-mongodb/src/createVersion.ts +++ b/packages/db-mongodb/src/createVersion.ts @@ -27,12 +27,6 @@ export const createVersion: CreateVersion = async function createVersion( versions: true, }) - const options = { - session: await getSession(this, req), - // Timestamps are manually added by the write transform - timestamps: false, - } - const data = { autosave, createdAt, @@ -56,6 +50,12 @@ export const createVersion: CreateVersion = async function createVersion( operation: 'write', }) + const options = { + session: await getSession(this, req), + // Timestamps are manually added by the write transform + timestamps: false, + } + let [doc] = await Model.create([data], options, req) const parentQuery = { diff --git a/packages/db-mongodb/src/deleteMany.ts b/packages/db-mongodb/src/deleteMany.ts index a40fc7fb610..ebaee1df3f5 100644 --- a/packages/db-mongodb/src/deleteMany.ts +++ b/packages/db-mongodb/src/deleteMany.ts @@ -14,10 +14,6 @@ export const deleteMany: DeleteMany = async function deleteMany( ) { const { collectionConfig, Model } = getCollection({ adapter: this, collectionSlug }) - const options: DeleteOptions = { - session: await getSession(this, req), - } - const query = await buildQuery({ adapter: this, collectionSlug, @@ -25,5 +21,9 @@ export const deleteMany: DeleteMany = async function deleteMany( where, }) + const options: DeleteOptions = { + session: await getSession(this, req), + } + await Model.deleteMany(query, options) } diff --git a/packages/db-mongodb/src/deleteOne.ts b/packages/db-mongodb/src/deleteOne.ts index 1731642780f..713b8d9fea0 100644 --- a/packages/db-mongodb/src/deleteOne.ts +++ b/packages/db-mongodb/src/deleteOne.ts @@ -15,6 +15,13 @@ export const deleteOne: DeleteOne = async function deleteOne( ) { const { collectionConfig, Model } = getCollection({ adapter: this, collectionSlug }) + const query = await buildQuery({ + adapter: this, + collectionSlug, + fields: collectionConfig.flattenedFields, + where, + }) + const options: MongooseUpdateQueryOptions = { projection: buildProjectionFromSelect({ adapter: this, @@ -24,13 +31,6 @@ export const deleteOne: DeleteOne = async function deleteOne( session: await getSession(this, req), } - const query = await buildQuery({ - adapter: this, - collectionSlug, - fields: collectionConfig.flattenedFields, - where, - }) - if (returning === false) { await Model.deleteOne(query, options)?.lean() return null diff --git a/packages/db-mongodb/src/deleteVersions.ts b/packages/db-mongodb/src/deleteVersions.ts index 1d311b4f34c..97436494d78 100644 --- a/packages/db-mongodb/src/deleteVersions.ts +++ b/packages/db-mongodb/src/deleteVersions.ts @@ -36,8 +36,6 @@ export const deleteVersions: DeleteVersions = async function deleteVersions( throw new APIError('Either collection or globalSlug must be passed.') } - const session = await getSession(this, req) - const query = await buildQuery({ adapter: this, fields, @@ -45,5 +43,7 @@ export const deleteVersions: DeleteVersions = async function deleteVersions( where, }) + const session = await getSession(this, req) + await VersionsModel.deleteMany(query, { session }) } diff --git a/packages/db-mongodb/src/find.ts b/packages/db-mongodb/src/find.ts index 907bd744844..c3e655f9f4f 100644 --- a/packages/db-mongodb/src/find.ts +++ b/packages/db-mongodb/src/find.ts @@ -34,8 +34,6 @@ export const find: Find = async function find( ) { const { collectionConfig, Model } = getCollection({ adapter: this, collectionSlug }) - const session = await getSession(this, req) - let hasNearConstraint = false if (where) { @@ -66,6 +64,8 @@ export const find: Find = async function find( where, }) + const session = await getSession(this, req) + // useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters. const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0 const paginationOptions: PaginateOptions = { diff --git a/packages/db-mongodb/src/findDistinct.ts b/packages/db-mongodb/src/findDistinct.ts index f72977eb2a6..66943f06525 100644 --- a/packages/db-mongodb/src/findDistinct.ts +++ b/packages/db-mongodb/src/findDistinct.ts @@ -16,8 +16,6 @@ export const findDistinct: FindDistinct = async function (this: MongooseAdapter, collectionSlug: args.collection, }) - const session = await getSession(this, args.req) - const { where = {} } = args let sortAggregation: PipelineStage[] = [] @@ -202,6 +200,8 @@ export const findDistinct: FindDistinct = async function (this: MongooseAdapter, }, ] + const session = await getSession(this, args.req) + const getValues = async () => { return Model.aggregate(pipeline, { session }).then((res) => res.map((each) => ({ diff --git a/packages/db-mongodb/src/findGlobal.ts b/packages/db-mongodb/src/findGlobal.ts index 18e3ad5eb68..c87fea72da9 100644 --- a/packages/db-mongodb/src/findGlobal.ts +++ b/packages/db-mongodb/src/findGlobal.ts @@ -18,6 +18,15 @@ export const findGlobal: FindGlobal = async function findGlobal( const { globalConfig, Model } = getGlobal({ adapter: this, globalSlug }) const fields = globalConfig.flattenedFields + + const query = await buildQuery({ + adapter: this, + fields, + globalSlug, + locale, + where: combineQueries({ globalType: { equals: globalSlug } }, where), + }) + const options: QueryOptions = { lean: true, select: buildProjectionFromSelect({ @@ -28,14 +37,6 @@ export const findGlobal: FindGlobal = async function findGlobal( session: await getSession(this, req), } - const query = await buildQuery({ - adapter: this, - fields, - globalSlug, - locale, - where: combineQueries({ globalType: { equals: globalSlug } }, where), - }) - const doc: any = await Model.findOne(query, {}, options) if (!doc) { diff --git a/packages/db-mongodb/src/findGlobalVersions.ts b/packages/db-mongodb/src/findGlobalVersions.ts index 76e9bc5fbc9..f85aafa3567 100644 --- a/packages/db-mongodb/src/findGlobalVersions.ts +++ b/packages/db-mongodb/src/findGlobalVersions.ts @@ -31,13 +31,6 @@ export const findGlobalVersions: FindGlobalVersions = async function findGlobalV const versionFields = buildVersionGlobalFields(this.payload.config, globalConfig, true) - const session = await getSession(this, req) - const options: QueryOptions = { - limit, - session, - skip, - } - let hasNearConstraint = false if (where) { @@ -64,6 +57,13 @@ export const findGlobalVersions: FindGlobalVersions = async function findGlobalV where, }) + const session = await getSession(this, req) + const options: QueryOptions = { + limit, + session, + skip, + } + // useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters. const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0 const paginationOptions: PaginateOptions = { diff --git a/packages/db-mongodb/src/findOne.ts b/packages/db-mongodb/src/findOne.ts index cf6edb34f05..6abf81bdcbc 100644 --- a/packages/db-mongodb/src/findOne.ts +++ b/packages/db-mongodb/src/findOne.ts @@ -19,12 +19,6 @@ export const findOne: FindOne = async function findOne( ) { const { collectionConfig, Model } = getCollection({ adapter: this, collectionSlug }) - const session = await getSession(this, req) - const options: AggregateOptions & QueryOptions = { - lean: true, - session, - } - const query = await buildQuery({ adapter: this, collectionSlug, @@ -50,6 +44,12 @@ export const findOne: FindOne = async function findOne( query, }) + const session = await getSession(this, req) + const options: AggregateOptions & QueryOptions = { + lean: true, + session, + } + let doc if (aggregate) { const { docs } = await aggregatePaginate({ diff --git a/packages/db-mongodb/src/findVersions.ts b/packages/db-mongodb/src/findVersions.ts index 85be3661106..4b30753406b 100644 --- a/packages/db-mongodb/src/findVersions.ts +++ b/packages/db-mongodb/src/findVersions.ts @@ -33,13 +33,6 @@ export const findVersions: FindVersions = async function findVersions( versions: true, }) - const session = await getSession(this, req) - const options: QueryOptions = { - limit, - session, - skip, - } - let hasNearConstraint = false if (where) { @@ -68,6 +61,13 @@ export const findVersions: FindVersions = async function findVersions( where, }) + const session = await getSession(this, req) + const options: QueryOptions = { + limit, + session, + skip, + } + // useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters. const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0 const paginationOptions: PaginateOptions = { diff --git a/packages/db-mongodb/src/queryDrafts.ts b/packages/db-mongodb/src/queryDrafts.ts index 1dd0e84dafd..7af5faaa78a 100644 --- a/packages/db-mongodb/src/queryDrafts.ts +++ b/packages/db-mongodb/src/queryDrafts.ts @@ -36,10 +36,6 @@ export const queryDrafts: QueryDrafts = async function queryDrafts( versions: true, }) - const options: QueryOptions = { - session: await getSession(this, req), - } - let hasNearConstraint let sort @@ -78,6 +74,12 @@ export const queryDrafts: QueryDrafts = async function queryDrafts( fields, select, }) + + const session = await getSession(this, req) + const options: QueryOptions = { + session, + } + // useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters. const useEstimatedCount = hasNearConstraint || !versionQuery || Object.keys(versionQuery).length === 0 diff --git a/packages/db-mongodb/src/updateGlobal.ts b/packages/db-mongodb/src/updateGlobal.ts index 2ed7aa17069..8cdb644ac3d 100644 --- a/packages/db-mongodb/src/updateGlobal.ts +++ b/packages/db-mongodb/src/updateGlobal.ts @@ -16,6 +16,8 @@ export const updateGlobal: UpdateGlobal = async function updateGlobal( const fields = globalConfig.fields + transform({ adapter: this, data, fields, globalSlug, operation: 'write' }) + const options: MongooseUpdateQueryOptions = { ...optionsArgs, lean: true, @@ -30,8 +32,6 @@ export const updateGlobal: UpdateGlobal = async function updateGlobal( timestamps: false, } - transform({ adapter: this, data, fields, globalSlug, operation: 'write' }) - if (returning === false) { await Model.updateOne({ globalType: globalSlug }, data, options) return null diff --git a/packages/db-mongodb/src/updateGlobalVersion.ts b/packages/db-mongodb/src/updateGlobalVersion.ts index 0ec825b5a57..c924e4803bd 100644 --- a/packages/db-mongodb/src/updateGlobalVersion.ts +++ b/packages/db-mongodb/src/updateGlobalVersion.ts @@ -30,6 +30,16 @@ export async function updateGlobalVersion( const fields = buildVersionGlobalFields(this.payload.config, globalConfig) const flattenedFields = buildVersionGlobalFields(this.payload.config, globalConfig, true) + + const query = await buildQuery({ + adapter: this, + fields: flattenedFields, + locale, + where: whereToUse, + }) + + transform({ adapter: this, data: versionData, fields, operation: 'write' }) + const options: MongooseUpdateQueryOptions = { ...optionsArgs, lean: true, @@ -44,15 +54,6 @@ export async function updateGlobalVersion( timestamps: false, } - const query = await buildQuery({ - adapter: this, - fields: flattenedFields, - locale, - where: whereToUse, - }) - - transform({ adapter: this, data: versionData, fields, operation: 'write' }) - if (returning === false) { await Model.updateOne(query, versionData, options) return null diff --git a/packages/db-mongodb/src/updateJobs.ts b/packages/db-mongodb/src/updateJobs.ts index 956d32b4699..5de930263ff 100644 --- a/packages/db-mongodb/src/updateJobs.ts +++ b/packages/db-mongodb/src/updateJobs.ts @@ -36,14 +36,6 @@ export const updateJobs: UpdateJobs = async function updateMany( timestamps: true, }) - const options: MongooseUpdateQueryOptions = { - lean: true, - new: true, - session: await getSession(this, req), - // Timestamps are manually added by the write transform - timestamps: false, - } - let query = await buildQuery({ adapter: this, collectionSlug: collectionConfig.slug, @@ -88,6 +80,14 @@ export const updateJobs: UpdateJobs = async function updateMany( updateData = updateOps } + const options: MongooseUpdateQueryOptions = { + lean: true, + new: true, + session: await getSession(this, req), + // Timestamps are manually added by the write transform + timestamps: false, + } + let result: Job[] = [] try { diff --git a/packages/db-mongodb/src/updateMany.ts b/packages/db-mongodb/src/updateMany.ts index 850a99c4c60..192322a89ac 100644 --- a/packages/db-mongodb/src/updateMany.ts +++ b/packages/db-mongodb/src/updateMany.ts @@ -48,20 +48,6 @@ export const updateMany: UpdateMany = async function updateMany( }) } - const options: MongooseUpdateQueryOptions = { - ...optionsArgs, - lean: true, - new: true, - projection: buildProjectionFromSelect({ - adapter: this, - fields: collectionConfig.flattenedFields, - select, - }), - session: await getSession(this, req), - // Timestamps are manually added by the write transform - timestamps: false, - } - let query = await buildQuery({ adapter: this, collectionSlug, @@ -105,6 +91,20 @@ export const updateMany: UpdateMany = async function updateMany( data = updateOps } + const options: MongooseUpdateQueryOptions = { + ...optionsArgs, + lean: true, + new: true, + projection: buildProjectionFromSelect({ + adapter: this, + fields: collectionConfig.flattenedFields, + select, + }), + session: await getSession(this, req), + // Timestamps are manually added by the write transform + timestamps: false, + } + try { if (typeof limit === 'number' && limit > 0) { const documentsToUpdate = await Model.find( diff --git a/packages/db-mongodb/src/updateOne.ts b/packages/db-mongodb/src/updateOne.ts index 938a6eac53f..53c2ab0ee63 100644 --- a/packages/db-mongodb/src/updateOne.ts +++ b/packages/db-mongodb/src/updateOne.ts @@ -28,20 +28,6 @@ export const updateOne: UpdateOne = async function updateOne( const where = id ? { id: { equals: id } } : whereArg const fields = collectionConfig.fields - const options: MongooseUpdateQueryOptions = { - ...optionsArgs, - lean: true, - new: true, - projection: buildProjectionFromSelect({ - adapter: this, - fields: collectionConfig.flattenedFields, - select, - }), - session: await getSession(this, req), - // Timestamps are manually added by the write transform - timestamps: false, - } - const query = await buildQuery({ adapter: this, collectionSlug, @@ -89,6 +75,20 @@ export const updateOne: UpdateOne = async function updateOne( updateData = updateOps } + const options: MongooseUpdateQueryOptions = { + ...optionsArgs, + lean: true, + new: true, + projection: buildProjectionFromSelect({ + adapter: this, + fields: collectionConfig.flattenedFields, + select, + }), + session: await getSession(this, req), + // Timestamps are manually added by the write transform + timestamps: false, + } + try { if (returning === false) { await Model.updateOne(query, updateData, options) diff --git a/packages/db-mongodb/src/updateVersion.ts b/packages/db-mongodb/src/updateVersion.ts index 4b2cbc5473f..94241118604 100644 --- a/packages/db-mongodb/src/updateVersion.ts +++ b/packages/db-mongodb/src/updateVersion.ts @@ -35,6 +35,15 @@ export const updateVersion: UpdateVersion = async function updateVersion( const flattenedFields = buildVersionCollectionFields(this.payload.config, collectionConfig, true) + const query = await buildQuery({ + adapter: this, + fields: flattenedFields, + locale, + where: whereToUse, + }) + + transform({ adapter: this, data: versionData, fields, operation: 'write' }) + const options: MongooseUpdateQueryOptions = { ...optionsArgs, lean: true, @@ -49,15 +58,6 @@ export const updateVersion: UpdateVersion = async function updateVersion( timestamps: false, } - const query = await buildQuery({ - adapter: this, - fields: flattenedFields, - locale, - where: whereToUse, - }) - - transform({ adapter: this, data: versionData, fields, operation: 'write' }) - if (returning === false) { await Model.updateOne(query, versionData, options) return null diff --git a/packages/drizzle/src/count.ts b/packages/drizzle/src/count.ts index b7e415e27a1..b5ee8d15d86 100644 --- a/packages/drizzle/src/count.ts +++ b/packages/drizzle/src/count.ts @@ -15,8 +15,6 @@ export const count: Count = async function count( const tableName = this.tableNameMap.get(toSnakeCase(collectionConfig.slug)) - const db = await getTransaction(this, req) - const { joins, where } = buildQuery({ adapter: this, fields: collectionConfig.flattenedFields, @@ -25,6 +23,8 @@ export const count: Count = async function count( where: whereArg, }) + const db = await getTransaction(this, req) + const countResult = await this.countDistinct({ db, joins, diff --git a/packages/drizzle/src/countGlobalVersions.ts b/packages/drizzle/src/countGlobalVersions.ts index 4e9e7a86bb8..0552d65fe3b 100644 --- a/packages/drizzle/src/countGlobalVersions.ts +++ b/packages/drizzle/src/countGlobalVersions.ts @@ -20,8 +20,6 @@ export const countGlobalVersions: CountGlobalVersions = async function countGlob `_${toSnakeCase(globalConfig.slug)}${this.versionsSuffix}`, ) - const db = await getTransaction(this, req) - const fields = buildVersionGlobalFields(this.payload.config, globalConfig, true) const { joins, where } = buildQuery({ @@ -32,6 +30,8 @@ export const countGlobalVersions: CountGlobalVersions = async function countGlob where: whereArg, }) + const db = await getTransaction(this, req) + const countResult = await this.countDistinct({ db, joins, diff --git a/packages/drizzle/src/countVersions.ts b/packages/drizzle/src/countVersions.ts index 5ba2e2fc119..272c1fd6999 100644 --- a/packages/drizzle/src/countVersions.ts +++ b/packages/drizzle/src/countVersions.ts @@ -18,8 +18,6 @@ export const countVersions: CountVersions = async function countVersions( `_${toSnakeCase(collectionConfig.slug)}${this.versionsSuffix}`, ) - const db = await getTransaction(this, req) - const fields = buildVersionCollectionFields(this.payload.config, collectionConfig, true) const { joins, where } = buildQuery({ @@ -30,6 +28,8 @@ export const countVersions: CountVersions = async function countVersions( where: whereArg, }) + const db = await getTransaction(this, req) + const countResult = await this.countDistinct({ db, joins, diff --git a/packages/drizzle/src/create.ts b/packages/drizzle/src/create.ts index 3901cf8a10a..4ae8997e6c2 100644 --- a/packages/drizzle/src/create.ts +++ b/packages/drizzle/src/create.ts @@ -11,11 +11,12 @@ export const create: Create = async function create( this: DrizzleAdapter, { collection: collectionSlug, data, req, returning, select }, ) { - const db = await getTransaction(this, req) const collection = this.payload.collections[collectionSlug].config const tableName = this.tableNameMap.get(toSnakeCase(collection.slug)) + const db = await getTransaction(this, req) + const result = await upsertRow({ adapter: this, data, diff --git a/packages/drizzle/src/createGlobal.ts b/packages/drizzle/src/createGlobal.ts index 8d1fd0c2d69..d04188b3d53 100644 --- a/packages/drizzle/src/createGlobal.ts +++ b/packages/drizzle/src/createGlobal.ts @@ -11,13 +11,14 @@ export async function createGlobal>( this: DrizzleAdapter, { slug, data, req, returning }: CreateGlobalArgs, ): Promise { - const db = await getTransaction(this, req) const globalConfig = this.payload.globals.config.find((config) => config.slug === slug) const tableName = this.tableNameMap.get(toSnakeCase(globalConfig.slug)) data.createdAt = new Date().toISOString() + const db = await getTransaction(this, req) + const result = await upsertRow<{ globalType: string } & T>({ adapter: this, data, diff --git a/packages/drizzle/src/createGlobalVersion.ts b/packages/drizzle/src/createGlobalVersion.ts index b9382acc44d..8b7371d9ff6 100644 --- a/packages/drizzle/src/createGlobalVersion.ts +++ b/packages/drizzle/src/createGlobalVersion.ts @@ -24,11 +24,12 @@ export async function createGlobalVersion( versionData, }: CreateGlobalVersionArgs, ): Promise> { - const db = await getTransaction(this, req) const global = this.payload.globals.config.find(({ slug }) => slug === globalSlug) const tableName = this.tableNameMap.get(`_${toSnakeCase(global.slug)}${this.versionsSuffix}`) + const db = await getTransaction(this, req) + const result = await upsertRow>({ adapter: this, data: { diff --git a/packages/drizzle/src/createVersion.ts b/packages/drizzle/src/createVersion.ts index a76152c0273..a7a1dc7eeab 100644 --- a/packages/drizzle/src/createVersion.ts +++ b/packages/drizzle/src/createVersion.ts @@ -25,7 +25,6 @@ export async function createVersion( versionData, }: CreateVersionArgs, ): Promise> { - const db = await getTransaction(this, req) const collection = this.payload.collections[collectionSlug].config const defaultTableName = toSnakeCase(collection.slug) @@ -47,6 +46,8 @@ export async function createVersion( version, } + const db = await getTransaction(this, req) + const result = await upsertRow>({ adapter: this, data, diff --git a/packages/drizzle/src/deleteMany.ts b/packages/drizzle/src/deleteMany.ts index 9e10290a30b..7e191abc41b 100644 --- a/packages/drizzle/src/deleteMany.ts +++ b/packages/drizzle/src/deleteMany.ts @@ -13,7 +13,6 @@ export const deleteMany: DeleteMany = async function deleteMany( this: DrizzleAdapter, { collection, req, where: whereArg }, ) { - const db = await getTransaction(this, req) const collectionConfig = this.payload.collections[collection].config const tableName = this.tableNameMap.get(toSnakeCase(collectionConfig.slug)) @@ -55,6 +54,8 @@ export const deleteMany: DeleteMany = async function deleteMany( ) } + const db = await getTransaction(this, req) + await this.deleteWhere({ db, tableName, diff --git a/packages/drizzle/src/deleteOne.ts b/packages/drizzle/src/deleteOne.ts index ab36edfcf1d..c29170e1607 100644 --- a/packages/drizzle/src/deleteOne.ts +++ b/packages/drizzle/src/deleteOne.ts @@ -15,7 +15,6 @@ export const deleteOne: DeleteOne = async function deleteOne( this: DrizzleAdapter, { collection: collectionSlug, req, returning, select, where: whereArg }, ) { - const db = await getTransaction(this, req) const collection = this.payload.collections[collectionSlug].config const tableName = this.tableNameMap.get(toSnakeCase(collection.slug)) @@ -30,6 +29,8 @@ export const deleteOne: DeleteOne = async function deleteOne( where: whereArg, }) + const db = await getTransaction(this, req) + const selectDistinctResult = await selectDistinct({ adapter: this, db, diff --git a/packages/drizzle/src/deleteVersions.ts b/packages/drizzle/src/deleteVersions.ts index a0aba3a6970..2f0df95251a 100644 --- a/packages/drizzle/src/deleteVersions.ts +++ b/packages/drizzle/src/deleteVersions.ts @@ -13,8 +13,6 @@ export const deleteVersions: DeleteVersions = async function deleteVersion( this: DrizzleAdapter, { collection: collectionSlug, globalSlug, locale, req, where: where }, ) { - const db = await getTransaction(this, req) - let tableName: string let fields: FlattenedField[] @@ -53,6 +51,8 @@ export const deleteVersions: DeleteVersions = async function deleteVersion( }) if (ids.length > 0) { + const db = await getTransaction(this, req) + await this.deleteWhere({ db, tableName, diff --git a/packages/drizzle/src/find/findMany.ts b/packages/drizzle/src/find/findMany.ts index e991b55197a..167f96f3a76 100644 --- a/packages/drizzle/src/find/findMany.ts +++ b/packages/drizzle/src/find/findMany.ts @@ -37,7 +37,6 @@ export const findMany = async function find({ versions, where: whereArg, }: Args) { - const db = await getTransaction(adapter, req) let limit = limitArg let totalDocs: number let totalPages: number @@ -96,6 +95,8 @@ export const findMany = async function find({ } } + const db = await getTransaction(adapter, req) + const selectDistinctResult = await selectDistinct({ adapter, db, diff --git a/packages/drizzle/src/findDistinct.ts b/packages/drizzle/src/findDistinct.ts index a7876f36623..0759d801603 100644 --- a/packages/drizzle/src/findDistinct.ts +++ b/packages/drizzle/src/findDistinct.ts @@ -9,7 +9,6 @@ import { getTransaction } from './utilities/getTransaction.js' import { DistinctSymbol } from './utilities/rawConstraint.js' export const findDistinct: FindDistinct = async function (this: DrizzleAdapter, args) { - const db = await getTransaction(this, args.req) const collectionConfig: SanitizedCollectionConfig = this.payload.collections[args.collection].config const page = args.page || 1 @@ -36,6 +35,8 @@ export const findDistinct: FindDistinct = async function (this: DrizzleAdapter, orderBy.pop() + const db = await getTransaction(this, args.req) + const selectDistinctResult = await selectDistinct({ adapter: this, db, diff --git a/packages/drizzle/src/updateGlobal.ts b/packages/drizzle/src/updateGlobal.ts index e97bd15ea9c..959e99069d0 100644 --- a/packages/drizzle/src/updateGlobal.ts +++ b/packages/drizzle/src/updateGlobal.ts @@ -11,10 +11,10 @@ export async function updateGlobal>( this: DrizzleAdapter, { slug, data, req, returning, select }: UpdateGlobalArgs, ): Promise { - const db = await getTransaction(this, req) const globalConfig = this.payload.globals.config.find((config) => config.slug === slug) const tableName = this.tableNameMap.get(toSnakeCase(globalConfig.slug)) + const db = await getTransaction(this, req) const existingGlobal = await db.query[tableName].findFirst({}) const result = await upsertRow<{ globalType: string } & T>({ diff --git a/packages/drizzle/src/updateGlobalVersion.ts b/packages/drizzle/src/updateGlobalVersion.ts index 223e4096d14..1961a9822ac 100644 --- a/packages/drizzle/src/updateGlobalVersion.ts +++ b/packages/drizzle/src/updateGlobalVersion.ts @@ -27,7 +27,6 @@ export async function updateGlobalVersion( where: whereArg, }: UpdateGlobalVersionArgs, ): Promise> { - const db = await getTransaction(this, req) const globalConfig: SanitizedGlobalConfig = this.payload.globals.config.find( ({ slug }) => slug === global, ) @@ -47,6 +46,8 @@ export async function updateGlobalVersion( where: whereToUse, }) + const db = await getTransaction(this, req) + const result = await upsertRow>({ id, adapter: this, diff --git a/packages/drizzle/src/updateJobs.ts b/packages/drizzle/src/updateJobs.ts index 7463dab2480..8969cf7bd1e 100644 --- a/packages/drizzle/src/updateJobs.ts +++ b/packages/drizzle/src/updateJobs.ts @@ -23,7 +23,6 @@ export const updateJobs: UpdateJobs = async function updateMany( const whereToUse: Where = id ? { id: { equals: id } } : whereArg const limit = id ? 1 : limitArg - const db = await getTransaction(this, req) const collection = this.payload.collections['payload-jobs'].config const tableName = this.tableNameMap.get(toSnakeCase(collection.slug)) const sort = sortArg !== undefined && sortArg !== null ? sortArg : collection.defaultSort @@ -34,6 +33,8 @@ export const updateJobs: UpdateJobs = async function updateMany( }) if (useOptimizedUpsertRow && id) { + const db = await getTransaction(this, req) + const result = await upsertRow({ id, adapter: this, @@ -64,6 +65,8 @@ export const updateJobs: UpdateJobs = async function updateMany( return [] } + const db = await getTransaction(this, req) + const results = [] // TODO: We need to batch this to reduce the amount of db calls. This can get very slow if we are updating a lot of rows. diff --git a/packages/drizzle/src/updateMany.ts b/packages/drizzle/src/updateMany.ts index a0f8b773152..c4babe7f733 100644 --- a/packages/drizzle/src/updateMany.ts +++ b/packages/drizzle/src/updateMany.ts @@ -25,7 +25,6 @@ export const updateMany: UpdateMany = async function updateMany( where: whereToUse, }, ) { - const db = await getTransaction(this, req) const collection = this.payload.collections[collectionSlug].config const tableName = this.tableNameMap.get(toSnakeCase(collection.slug)) @@ -40,6 +39,8 @@ export const updateMany: UpdateMany = async function updateMany( where: whereToUse, }) + const db = await getTransaction(this, req) + let idsToUpdate: (number | string)[] = [] const selectDistinctResult = await selectDistinct({ diff --git a/packages/drizzle/src/updateOne.ts b/packages/drizzle/src/updateOne.ts index 8fddd9378fd..879d4344527 100644 --- a/packages/drizzle/src/updateOne.ts +++ b/packages/drizzle/src/updateOne.ts @@ -25,11 +25,12 @@ export const updateOne: UpdateOne = async function updateOne( where: whereArg, }, ) { - const db = await getTransaction(this, req) const collection = this.payload.collections[collectionSlug].config const tableName = this.tableNameMap.get(toSnakeCase(collection.slug)) let idToUpdate = id + const db = await getTransaction(this, req) + if (!idToUpdate) { const { joins, selectFields, where } = buildQuery({ adapter: this, diff --git a/packages/drizzle/src/updateVersion.ts b/packages/drizzle/src/updateVersion.ts index 49350b7bf8c..1572a141f13 100644 --- a/packages/drizzle/src/updateVersion.ts +++ b/packages/drizzle/src/updateVersion.ts @@ -27,7 +27,6 @@ export async function updateVersion( where: whereArg, }: UpdateVersionArgs, ): Promise> { - const db = await getTransaction(this, req) const collectionConfig: SanitizedCollectionConfig = this.payload.collections[collection].config const whereToUse = whereArg || { id: { equals: id } } const tableName = this.tableNameMap.get( @@ -44,6 +43,8 @@ export async function updateVersion( where: whereToUse, }) + const db = await getTransaction(this, req) + const result = await upsertRow>({ id, adapter: this,