feat: add count operation to collections (#5930)

This commit is contained in:
Ritsu
2024-04-20 21:45:44 +03:00
committed by GitHub
parent d987e5628a
commit d5cbbc472d
25 changed files with 538 additions and 9 deletions

View File

@@ -6,6 +6,7 @@ import { TestButton } from './TestButton.js'
import {
docLevelAccessSlug,
firstArrayText,
hiddenAccessCountSlug,
hiddenAccessSlug,
hiddenFieldsSlug,
noAdminAccessEmail,
@@ -428,6 +429,32 @@ export default buildConfigWithDefaults({
},
],
},
{
slug: hiddenAccessCountSlug,
access: {
read: ({ req: { user } }) => {
if (user) return true
return {
hidden: {
not_equals: true,
},
}
},
},
fields: [
{
name: 'title',
type: 'text',
required: true,
},
{
name: 'hidden',
type: 'checkbox',
hidden: true,
},
],
},
],
onInit: async (payload) => {
await payload.create({

View File

@@ -8,6 +8,7 @@ import { initPayloadInt } from '../helpers/initPayloadInt.js'
import configPromise, { requestHeaders } from './config.js'
import {
firstArrayText,
hiddenAccessCountSlug,
hiddenAccessSlug,
hiddenFieldsSlug,
relyOnRequestHeadersSlug,
@@ -420,6 +421,30 @@ describe('Access Control', () => {
expect(docs).toHaveLength(1)
})
it('should respect query constraint using hidden field on count', async () => {
await payload.create({
collection: hiddenAccessCountSlug,
data: {
title: 'hello',
},
})
await payload.create({
collection: hiddenAccessCountSlug,
data: {
title: 'hello',
hidden: true,
},
})
const { totalDocs } = await payload.count({
collection: hiddenAccessCountSlug,
overrideAccess: false,
})
expect(totalDocs).toBe(1)
})
it('should respect query constraint using hidden field on versions', async () => {
await payload.create({
collection: restrictedVersionsSlug,

View File

@@ -12,7 +12,7 @@ export const siblingDataSlug = 'sibling-data'
export const relyOnRequestHeadersSlug = 'rely-on-request-headers'
export const docLevelAccessSlug = 'doc-level-access'
export const hiddenFieldsSlug = 'hidden-fields'
export const hiddenAccessSlug = 'hidden-access'
export const hiddenAccessCountSlug = 'hidden-access-count'
export const noAdminAccessEmail = 'no-admin-access@payloadcms.com'

View File

@@ -116,6 +116,20 @@ describe('collections-graphql', () => {
expect(docs).toContainEqual(expect.objectContaining({ id: existingDoc.id }))
})
it('should count', async () => {
const query = `query {
countPosts {
totalDocs
}
}`
const { data } = await restClient
.GRAPHQL_POST({ body: JSON.stringify({ query }) })
.then((res) => res.json())
const { totalDocs } = data.countPosts
expect(typeof totalDocs).toBe('number')
})
it('should read using multiple queries', async () => {
const query = `query {
postIDs: Posts {
@@ -848,6 +862,21 @@ describe('collections-graphql', () => {
expect(docs[0].relationToCustomID.id).toStrictEqual(1)
})
it('should query on relationships with custom IDs - count', async () => {
const query = `query {
countPosts(where: { title: { equals: "has custom ID relation" }}) {
totalDocs
}
}`
const { data } = await restClient
.GRAPHQL_POST({ body: JSON.stringify({ query }) })
.then((res) => res.json())
const { totalDocs } = data.countPosts
expect(totalDocs).toStrictEqual(1)
})
it('should query a document with a deleted relationship', async () => {
const relation = await payload.create({
collection: relationSlug,

View File

@@ -69,6 +69,16 @@ describe('collections-rest', () => {
expect(result.docs).toEqual(expect.arrayContaining(expectedDocs))
})
it('should count', async () => {
await createPost()
await createPost()
const response = await restClient.GET(`/${slug}/count`)
const result = await response.json()
expect(response.status).toEqual(200)
expect(result).toEqual({ totalDocs: 2 })
})
it('should find where id', async () => {
const post1 = await createPost()
await createPost()
@@ -510,6 +520,18 @@ describe('collections-rest', () => {
expect(result.totalDocs).toEqual(1)
})
it('should count query by property value', async () => {
const response = await restClient.GET(`/${slug}/count`, {
query: {
where: { relationField: { equals: relation.id } },
},
})
const result = await response.json()
expect(response.status).toEqual(200)
expect(result).toEqual({ totalDocs: 1 })
})
it('query by id', async () => {
const response = await restClient.GET(`/${slug}`, {
query: {