Skip to content
Open
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
8 changes: 4 additions & 4 deletions packages/db-mongodb/src/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions packages/db-mongodb/src/countGlobalVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions packages/db-mongodb/src/countVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions packages/db-mongodb/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions packages/db-mongodb/src/createGlobalVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down
12 changes: 6 additions & 6 deletions packages/db-mongodb/src/createVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = {
Expand Down
8 changes: 4 additions & 4 deletions packages/db-mongodb/src/deleteMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ 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,
fields: collectionConfig.flattenedFields,
where,
})

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

await Model.deleteMany(query, options)
}
14 changes: 7 additions & 7 deletions packages/db-mongodb/src/deleteOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/db-mongodb/src/deleteVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ 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,
locale,
where,
})

const session = await getSession(this, req)

await VersionsModel.deleteMany(query, { session })
}
4 changes: 2 additions & 2 deletions packages/db-mongodb/src/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions packages/db-mongodb/src/findDistinct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = []
Expand Down Expand Up @@ -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) => ({
Expand Down
17 changes: 9 additions & 8 deletions packages/db-mongodb/src/findGlobal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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) {
Expand Down
14 changes: 7 additions & 7 deletions packages/db-mongodb/src/findGlobalVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 = {
Expand Down
12 changes: 6 additions & 6 deletions packages/db-mongodb/src/findOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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({
Expand Down
14 changes: 7 additions & 7 deletions packages/db-mongodb/src/findVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {
Expand Down
10 changes: 6 additions & 4 deletions packages/db-mongodb/src/queryDrafts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Loading
Loading