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: add draft argument to count operation #9483

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

r1tsuu
Copy link
Member

@r1tsuu r1tsuu commented Nov 24, 2024

Allows to count drafts in count operation (both Local and REST) via draft: true.
Additionally, passes the locale argument to db.count / db.countVersions / db.countGlobalVersions as the database adapter may use it for querying.

Copy link
Contributor

@DanRibbens DanRibbens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is right. The draft arg in other operations only controls whether or not the latest version fetch should include drafts or not. With that, the number of docs should not change based on this flag because even a document that only had _status: 'draft' is still returned in a find.

I think the purpose of what you're trying to do here is to expose db.countVersions through the count operation. Instead this should be done using:

        ;({ totalDocs: unpublishedVersionCount } = await payload.countVersions({
          collection: collectionConfig.slug,
          user,
          where: {
            and: [
              {
                parent: {
                  equals: id,
                },
              },
              {
                'version._status': {
                  equals: 'draft',
                },
              },
            ],
          },
        }))

@DanRibbens
Copy link
Contributor

I guess the args could matter if you need them for querying. I can see how the locale would change how the where query gets docs back and that would be true for draft: true also, but I don't think we would use db.countVersions since that will count drafts multiple times for the same doc.

@r1tsuu
Copy link
Member Author

r1tsuu commented Nov 25, 2024

@DanRibbens the logic for draft: true is based on how we do db.queryDrafts, e.g:

Payload side:

if (collectionConfig.versions?.drafts && draftsEnabled) {
fullWhere = appendVersionToQueryKey(fullWhere)
await validateQueryPaths({
collectionConfig: collection.config,
overrideAccess,
req,
versionFields: buildVersionCollectionFields(payload.config, collection.config, true),
where: fullWhere,
})
result = await payload.db.queryDrafts<DataFromCollectionSlug<TSlug>>({
collection: collectionConfig.slug,
joins: req.payloadAPI === 'GraphQL' ? false : sanitizedJoins,
limit: sanitizedLimit,
locale,
page: sanitizedPage,
pagination: usePagination,
req,
select: getQueryDraftsSelect({ select }),
sort: getQueryDraftsSort({ collectionConfig, sort }),
where: fullWhere,
})

Database adapter:

export const queryDrafts: QueryDrafts = async function queryDrafts(
this: DrizzleAdapter,
{
collection,
joins,
limit,
locale,
page = 1,
pagination,
req = {} as PayloadRequest,
select,
sort,
where,
},
) {
const collectionConfig: SanitizedCollectionConfig = this.payload.collections[collection].config
const tableName = this.tableNameMap.get(
`_${toSnakeCase(collectionConfig.slug)}${this.versionsSuffix}`,
)
const fields = buildVersionCollectionFields(this.payload.config, collectionConfig, true)
const combinedWhere = combineQueries({ latest: { equals: true } }, where)

So, to transition from draft: false to draft: true we need to:

  • Append the version key to each where constraint
  • Merge {latest: { equals: true }} with the original where.
  • Instead of querying the original table, query the versions table

I don't think draft: false or draft: true affects whether we query _status: 'published' or _status: 'draft', but it definitely can add some drafts to the result, since we're querying the versions table.

So really we just instead of the original table, query latest documents from the versions table which may include drafts.
Did I miss something here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants