Skip to content

Commit 0ffd18f

Browse files
committed
feat(queryDocuments): add skip to query options
1 parent 32ee338 commit 0ffd18f

File tree

5 files changed

+148
-105
lines changed

5 files changed

+148
-105
lines changed

deno.lock

Lines changed: 102 additions & 102 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
// @deno-types="./crocks.d.ts"
55
export { default as crocks } from 'https://cdn.skypack.dev/[email protected]'
6-
export * as R from 'https://cdn.skypack.dev/ramda@^0.29.0?dts'
6+
export * as R from 'https://cdn.skypack.dev/[email protected]?dts'
77

88
export { EJSON } from 'npm:[email protected]'
99
export { type Collection, MongoClient } from 'npm:[email protected]'

test/suite.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,40 @@ async (
320320
],
321321
})
322322

323+
const withSkip = await a.queryDocuments({
324+
db: DB,
325+
query: {
326+
selector: { year: { $lte: '1978' } },
327+
fields: ['_id', 'genre', 'year'],
328+
sort: [{ title: 'DESC' }, { year: 'DESC' }],
329+
skip: 2,
330+
},
331+
})
332+
333+
assertObjectMatch(withSkip as any, {
334+
ok: true,
335+
docs: [
336+
{ _id: '10-caddyshack', year: '1978', genre: ['comedy'] },
337+
],
338+
})
339+
340+
const withLimit = await a.queryDocuments({
341+
db: DB,
342+
query: {
343+
selector: { year: { $lte: '1978' } },
344+
fields: ['_id', 'genre', 'year'],
345+
sort: [{ title: 'DESC' }, { year: 'DESC' }],
346+
limit: 1,
347+
},
348+
})
349+
350+
assertObjectMatch(withLimit as any, {
351+
ok: true,
352+
docs: [
353+
{ _id: '15-starwars', year: '1976', genre: ['sci-fi'] },
354+
],
355+
})
356+
323357
await a.removeDatabase(DB)
324358
},
325359
)

utils.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ Deno.test('utils', async (t) => {
5454
assertEquals(res, { limit: 25 })
5555
})
5656

57+
await t.step('should map the skip', () => {
58+
const res = queryOptions({ skip: 25 })
59+
assertObjectMatch(res, { skip: 25 })
60+
})
61+
5762
await t.step('should map fields to projection', () => {
5863
const res = queryOptions({ fields: ['_id', 'foo', 'bar'] })
5964
assertObjectMatch(res, { projection: { foo: 1, bar: 1, _id: 1 } })

utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ export const toBulkOperations = map(
3131

3232
export const queryOptions = ({
3333
limit,
34+
skip,
3435
fields,
3536
sort,
3637
}: {
3738
limit?: number | string
39+
skip?: number | string
3840
fields?: string[]
3941
sort?: string[] | { [field: string]: 'ASC' | 'DESC' }[]
4042
}) => {
@@ -46,13 +48,15 @@ export const queryOptions = ({
4648
*/
4749
const options: {
4850
limit?: number
51+
skip?: number
4952
projection?: { [field: string]: 0 | 1 }
5053
sort?: { [field: string]: 1 | -1 }
5154
} = {
5255
/**
5356
* See https://www.mongodb.com/docs/manual/reference/operator/aggregation/limit/
5457
*/
5558
...(limit ? { limit: Number(limit) } : { limit: 25 }),
59+
...(skip ? { skip: Number(skip) } : {}),
5660
...(fields
5761
? {
5862
projection: fields.reduce(
@@ -86,8 +90,8 @@ export const queryOptions = ({
8690
*/
8791
export const mapSort = (
8892
sort: string[] | { [field: string]: 'ASC' | 'DESC' }[],
89-
) => {
90-
if (!sort || !sort.length) return sort
93+
): { [field: string]: 1 | -1 } => {
94+
if (!sort || !sort.length) return {}
9195

9296
// deno-lint-ignore ban-ts-comment
9397
// @ts-ignore

0 commit comments

Comments
 (0)