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 c315ece
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 105 deletions.
204 changes: 102 additions & 102 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
// @deno-types="./crocks.d.ts"
export { default as crocks } from 'https://cdn.skypack.dev/[email protected]'
export * as R from 'https://cdn.skypack.dev/ramda@^0.29.0?dts'
export * as R from 'https://cdn.skypack.dev/[email protected]?dts'

export { EJSON } from 'npm:[email protected]'
export { type Collection, MongoClient } from 'npm:[email protected]'
Expand Down
34 changes: 34 additions & 0 deletions test/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,40 @@ async (
],
})

const withSkip = await a.queryDocuments({
db: DB,
query: {
selector: { year: { $lte: '1978' } },
fields: ['_id', 'genre', 'year'],
sort: [{ title: 'DESC' }, { year: 'DESC' }],
skip: 2,
},
})

assertObjectMatch(withSkip as any, {
ok: true,
docs: [
{ _id: '10-caddyshack', year: '1978', genre: ['comedy'] },
],
})

const withLimit = await a.queryDocuments({
db: DB,
query: {
selector: { year: { $lte: '1978' } },
fields: ['_id', 'genre', 'year'],
sort: [{ title: 'DESC' }, { year: 'DESC' }],
limit: 1,
},
})

assertObjectMatch(withLimit as any, {
ok: true,
docs: [
{ _id: '15-starwars', year: '1976', genre: ['sci-fi'] },
],
})

await a.removeDatabase(DB)
},
)
Expand Down
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 c315ece

Please sign in to comment.