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(queryDocuments): add skip to query options #47

Merged
merged 1 commit into from
Oct 10, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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