Skip to content

Commit

Permalink
feat: add count endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Barbapapazes committed Feb 24, 2023
1 parent 737d5c5 commit 413e418
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
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 @@ -46,6 +46,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 @@ -229,6 +229,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

0 comments on commit 413e418

Please sign in to comment.