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 a count method #1924

Merged
merged 3 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/runtime/query/match/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export function createPipelineFetcher<T> (getContentsList: () => Promise<T[]>) {
return filteredData[0]
}

// return count if query is for count
if (params.count) {
return (filteredData.length ?? 0) as unknown as T
}

return filteredData
}
}
4 changes: 4 additions & 0 deletions src/runtime/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export const createQuery = <T = ParsedContent>(
queryParams.surround = { query: surroundQuery, ...options }
return fetcher(query) as Promise<Array<T>>
},
count: () => {
queryParams.count = true
return fetcher(query) as Promise<number>
},
// locale
locale: (_locale: string) => query.where({ _locale })
}
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/server/api/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export default defineEventHandler(async (event) => {
return content
}

if (query.count) {
const count = await serverQueryContent(event, query).count()
return count
}

const contents = await serverQueryContent(event, query).find()

return contents
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,11 @@ export interface QueryBuilder<T = ParsedContentMeta> {
*/
findSurround(query: string | QueryBuilderWhere, options?: Partial<{ before: number; after: number }>): Promise<Array<T>>

/**
* Count matched contents
*/
count(): Promise<number>

/**
* Filter contents based on locale
*/
Expand Down
41 changes: 41 additions & 0 deletions test/features/query/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,47 @@ describe('Database Provider', () => {
assert(result[2] === null)
})

test('Count items', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[]))

const result = await createQuery(fetcher)
.count()

assert(result === 3)
})

test('Count items with where condition', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[]))

const result = await createQuery(fetcher)
.where({ _path: { $contains: 'b' } })
.count()

assert(result === 1)
})

test('Count items with where condition and without method', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[]))

const result = await createQuery(fetcher)
.where({ _path: { $contains: 'b' } })
.without('id')
.count()

assert(result === 1)
})

test('Count items with where condition and only method', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[]))

const result = await createQuery(fetcher)
.where({ _path: { $contains: 'b' } })
.only(['_path'])
.count()

assert(result === 1)
})

test('Chain multiple where conditions', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, path: '/a' }, { id: 2, path: '/b' }, { id: 3, path: '/c' }] as any[]))
const query = createQuery(fetcher).where({ id: { $in: [1, 2] } })
Expand Down