Skip to content

Commit

Permalink
feat(queryDocuments): add skip to query options
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Oct 10, 2023
1 parent 32ee338 commit 9f4237d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ Deno.test('utils', async (t) => {
assertEquals(res, { limit: 25 })
})

await t.step('should map the skip', () => {
const res = queryOptions({ skip: 25 })
assertObjectMatch(res, { skip: 25 })
})

await t.step('should map fields to projection', () => {
const res = queryOptions({ fields: ['_id', 'foo', 'bar'] })
assertObjectMatch(res, { projection: { foo: 1, bar: 1, _id: 1 } })
Expand Down
8 changes: 6 additions & 2 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ export const toBulkOperations = map(

export const queryOptions = ({
limit,
skip,
fields,
sort,
}: {
limit?: number | string
skip?: number | string
fields?: string[]
sort?: string[] | { [field: string]: 'ASC' | 'DESC' }[]
}) => {
Expand All @@ -46,13 +48,15 @@ export const queryOptions = ({
*/
const options: {
limit?: number
skip?: number
projection?: { [field: string]: 0 | 1 }
sort?: { [field: string]: 1 | -1 }
} = {
/**
* See https://www.mongodb.com/docs/manual/reference/operator/aggregation/limit/
*/
...(limit ? { limit: Number(limit) } : { limit: 25 }),
...(skip ? { skip: Number(skip) } : {}),
...(fields
? {
projection: fields.reduce(
Expand Down Expand Up @@ -86,8 +90,8 @@ export const queryOptions = ({
*/
export const mapSort = (
sort: string[] | { [field: string]: 'ASC' | 'DESC' }[],
) => {
if (!sort || !sort.length) return sort
): { [field: string]: 1 | -1 } => {
if (!sort || !sort.length) return {}

// deno-lint-ignore ban-ts-comment
// @ts-ignore
Expand Down

0 comments on commit 9f4237d

Please sign in to comment.