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
51 changes: 50 additions & 1 deletion packages/db-mongodb/src/findVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import type { MongooseAdapter } from './index.js'

import { buildQuery } from './queries/buildQuery.js'
import { buildSortParam } from './queries/buildSortParam.js'
import { aggregatePaginate } from './utilities/aggregatePaginate.js'
import { buildJoinAggregation } from './utilities/buildJoinAggregation.js'
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'
import { getCollection } from './utilities/getEntity.js'
import { getSession } from './utilities/getSession.js'
import { resolveJoins } from './utilities/resolveJoins.js'
import { transform } from './utilities/transform.js'

export const findVersions: FindVersions = async function findVersions(
this: MongooseAdapter,
{
collection: collectionSlug,
joins = false,
limit = 0,
locale,
page,
Expand Down Expand Up @@ -121,7 +125,52 @@ export const findVersions: FindVersions = async function findVersions(
}
}

const result = await Model.paginate(query, paginationOptions)
let result

// result = await Model.paginate(query, paginationOptions)

// if (false) {
const aggregate = await buildJoinAggregation({
adapter: this,
collection: collectionSlug,
collectionConfig,
draftsEnabled: true,
joins,
locale,
query,
versions: true,
})

if (aggregate) {
result = await aggregatePaginate({
adapter: this,
collation: paginationOptions.collation,
joinAggregation: aggregate,
limit: paginationOptions.limit,
Model,
page: paginationOptions.page,
pagination: paginationOptions.pagination,
projection: paginationOptions.projection,
query,
session,
sort: paginationOptions.sort as object,

useEstimatedCount: paginationOptions.useEstimatedCount,
})
} else {
result = await Model.paginate(query, paginationOptions)
}

if (!this.useJoinAggregations) {
await resolveJoins({
adapter: this,
collectionSlug,
docs: result.docs as Record<string, unknown>[],
joins,
locale,
})
}
// }

transform({
adapter: this,
Expand Down
10 changes: 8 additions & 2 deletions packages/db-mongodb/src/utilities/aggregatePaginate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,27 @@ export const aggregatePaginate = async ({

let countPromise: Promise<null | number> = Promise.resolve(null)

const sessionParams: { session?: ClientSession } = {}

if (session) {
sessionParams.session = session
}

if (pagination !== false && limit) {
if (useEstimatedCount) {
countPromise = Model.estimatedDocumentCount(query)
} else {
const hint = adapter.disableIndexHints !== true ? { _id: 1 } : undefined
countPromise = Model.countDocuments(query, {
collation,
session,
...(hint ? { hint } : {}),
...sessionParams,
})
}
}

const [docs, countResult] = await Promise.all([
Model.aggregate(aggregation, { collation, session }),
Model.aggregate(aggregation, { collation, ...sessionParams }),
countPromise,
])

Expand Down
18 changes: 16 additions & 2 deletions packages/drizzle/src/findVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@ import { findMany } from './find/findMany.js'

export const findVersions: FindVersions = async function findVersions(
this: DrizzleAdapter,
{ collection, limit, locale, page, pagination, req, select, skip, sort: sortArg, where },
{
collection,
joins = false,
limit,
locale,
page,
pagination,
req,
select,
skip,
sort: sortArg,
where,
},
) {
const collectionConfig: SanitizedCollectionConfig = this.payload.collections[collection].config
const sort = sortArg !== undefined && sortArg !== null ? sortArg : collectionConfig.defaultSort
Expand All @@ -22,8 +34,9 @@ export const findVersions: FindVersions = async function findVersions(

return findMany({
adapter: this,
draftsEnabled: true,
fields,
joins: false,
joins,
limit,
locale,
page,
Expand All @@ -33,6 +46,7 @@ export const findVersions: FindVersions = async function findVersions(
skip,
sort,
tableName,
versions: true,
where,
})
}
1 change: 1 addition & 0 deletions packages/payload/src/collections/operations/findByID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ export const findByIDOperation = async <
doc: result,
entity: collectionConfig,
entityType: 'collection',
joins,
overrideAccess,
req,
select,
Expand Down
1 change: 1 addition & 0 deletions packages/payload/src/database/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ type BaseVersionArgs = {

export type FindVersionsArgs = {
collection: CollectionSlug
joins?: JoinQuery
} & BaseVersionArgs

export type FindVersions = <T = JsonObject>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { SanitizedCollectionConfig, TypeWithID } from '../../collections/co
import type { AccessResult } from '../../config/types.js'
import type { FindGlobalVersionsArgs, FindVersionsArgs } from '../../database/types.js'
import type { SanitizedGlobalConfig } from '../../globals/config/types.js'
import type { PayloadRequest, SelectType, Where } from '../../types/index.js'
import type { JoinQuery, PayloadRequest, SelectType, Where } from '../../types/index.js'

import { hasWhereAccessResult } from '../../auth/index.js'
import { combineQueries } from '../../database/combineQueries.js'
Expand All @@ -17,6 +17,7 @@ type Arguments<T> = {
doc: T
entity: SanitizedCollectionConfig | SanitizedGlobalConfig
entityType: 'collection' | 'global'
joins?: JoinQuery
overrideAccess: boolean
req: PayloadRequest
select?: SelectType
Expand All @@ -27,6 +28,7 @@ export const replaceWithDraftIfAvailable = async <T extends TypeWithID>({
doc,
entity,
entityType,
joins,
req,
select,
}: Arguments<T>): Promise<T> => {
Expand Down Expand Up @@ -76,6 +78,7 @@ export const replaceWithDraftIfAvailable = async <T extends TypeWithID>({
const findVersionsArgs: FindGlobalVersionsArgs & FindVersionsArgs = {
collection: entity.slug,
global: entity.slug,
joins,
limit: 1,
locale: locale!,
pagination: false,
Expand Down
20 changes: 20 additions & 0 deletions test/joins/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,26 @@ describe('Joins Field', () => {

expect(res.docs[0].relatedVersionsMany.docs[0].id).toBe(version.id)
})

it('should populate the join field if the version is unpublished and draft: true', async () => {
const category = await payload.create({
collection: 'categories-versions',
data: { title: 'category', _status: 'draft' },
draft: true,
})
const post = await payload.create({
collection: 'versions',
data: { title: 'post', categoryVersion: category.id },
draft: true,
})
const res = await payload.find({
collection: 'categories-versions',
// id: category.id,
draft: true,
})
expect(res.docs[0].relatedVersions?.docs[0].id).toBe(post.id)
expect(res.docs[0].relatedVersions?.docs[0].title).toBe(post.title)
})
})

describe('REST', () => {
Expand Down
Loading