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 0ffd18f
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 105 deletions.
204 changes: 102 additions & 102 deletions deno.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,105 @@
{
"version": "2",
"version": "3",
"packages": {
"specifiers": {
"npm:[email protected]": "npm:[email protected]",
"npm:[email protected]": "npm:[email protected]",
"npm:[email protected]": "npm:[email protected]"
},
"npm": {
"@types/[email protected]": {
"integrity": "sha512-IXl7o+R9iti9eBW4Wg2hx1xQDig183jj7YLn8F7udNceyfkbn1ZxmzZXuak20gR40D7pIkIY1kYGx5VIGbaHKA==",
"dependencies": {}
},
"@types/[email protected]": {
"integrity": "sha512-8hKOnOan+Uu+NgMaCouhg3cT9x5fFZ92Jwf+uDLXLu/MFRbXxlWwGeQY7KVHkeSft6RvY+tdxklUBuyY9eIEKg==",
"dependencies": {}
},
"@types/[email protected]": {
"integrity": "sha512-FtQu10RWgn3D9U4aazdwIE2yzphmTJREDqNdODHrbrZmmMqI0vMheC/6NE/J1Yveaj8H+ela+YwWTjq5PGmuhA==",
"dependencies": {
"@types/node": "@types/[email protected]",
"@types/webidl-conversions": "@types/[email protected]"
}
},
"[email protected]": {
"integrity": "sha512-WRZ5SQI5GfUuKnPTNmAYPiKIof3ORXAF4IRU5UcgmivNIon01rWQlw5RUH954dpu8yGL8T59YShVddIPaU/gFA==",
"dependencies": {}
},
"[email protected]": {
"integrity": "sha512-WZYYkHdIDnaxdeP8Misq3Lah5vFjJwGuItJuV+tvMafosMzw0nF297T7mrm8IOWiPJkV6gc7sa8pzx27+w25Zg==",
"dependencies": {}
},
"[email protected]": {
"integrity": "sha512-WKa+XuLG1A1R0UWhl2+1XQSi+fZWMsYKffMZTTYsiZaUD8k2yDAj5atimTUD2TZkyCkNEeYE5NhFZmupOGtjYQ==",
"dependencies": {}
},
"[email protected]": {
"integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==",
"dependencies": {}
},
"[email protected]": {
"integrity": "sha512-WvTZlI9ab0QYtTYnuMLgobULWhokRjtC7db9LtcVfJ+Hsnyr5eo6ZtNAt3Ly24XZScGMelOcGtm7lSn0332tPQ==",
"dependencies": {
"@types/whatwg-url": "@types/[email protected]",
"whatwg-url": "[email protected]"
}
},
"[email protected]": {
"integrity": "sha512-zm82Bq33QbqtxDf58fLWBwTjARK3NSvKYjyz997KSy6hpat0prjeX/kxjbPVyZY60XYPDNETaHkHJI2UCzSLuw==",
"dependencies": {
"bson": "[email protected]",
"mongodb-connection-string-url": "[email protected]",
"saslprep": "[email protected]",
"socks": "[email protected]"
}
},
"[email protected]": {
"integrity": "sha512-rRV+zQD8tVFys26lAGR9WUuS4iUAngJScM+ZRSKtvl5tKeZ2t5bvdNFdNHBW9FWR4guGHlgmsZ1G7BSm2wTbuA==",
"dependencies": {}
},
"[email protected]": {
"integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==",
"dependencies": {
"sparse-bitfield": "[email protected]"
}
},
"[email protected]": {
"integrity": "sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==",
"dependencies": {}
},
"[email protected]": {
"integrity": "sha512-7maUZy1N7uo6+WVEX6psASxtNlKaNVMlGQKkG/63nEDdLOWNbiUMoLK7X4uYoLhQstau72mLgfEWcXcwsaHbYQ==",
"dependencies": {
"ip": "[email protected]",
"smart-buffer": "[email protected]"
}
},
"[email protected]": {
"integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==",
"dependencies": {
"memory-pager": "[email protected]"
}
},
"[email protected]": {
"integrity": "sha512-l7FvfAHlcmulp8kr+flpQZmVwtu7nfRV7NZujtN0OqES8EL4O4e0qqzL0DC5gAvx/ZC/9lk6rhcUwYvkBnBnYA==",
"dependencies": {
"punycode": "[email protected]"
}
},
"[email protected]": {
"integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==",
"dependencies": {}
},
"[email protected]": {
"integrity": "sha512-RKT8HExMpoYx4igMiVMY83lN6UeITKJlBQ+vR/8ZJ8OCdSiN3RwCq+9gH0+Xzj0+5IrM6i4j/6LuvzbZIQgEcQ==",
"dependencies": {
"tr46": "[email protected]",
"webidl-conversions": "[email protected]"
}
}
}
},
"remote": {
"https://cdn.skypack.dev/-/[email protected]/dist=es2019,mode=imports/optimized/crocks.js": "93d587d18dc5f124f30e5b38de37a6471eb65309c94ef2ffc7a36dc40ab394da",
"https://cdn.skypack.dev/-/[email protected]/dist=es2019,mode=imports/optimized/common/_Set-d915dfa1.js": "85edd0c60ae74c28bfd7a6d831d1274b2299de6c46cc19ddfed24f593365f0c8",
Expand Down Expand Up @@ -615,7 +715,7 @@
"https://cdn.skypack.dev/[email protected]": "ac1e3c64a2a3491971bee55f88faeb0923023e903d3ad125f1bd8044b4cad03e",
"https://cdn.skypack.dev/[email protected]?dts": "ac1e3c64a2a3491971bee55f88faeb0923023e903d3ad125f1bd8044b4cad03e",
"https://cdn.skypack.dev/[email protected]": "58d3aec92683f33d6d780ff5619ec2fe5cfafd016bf9b6d8190be173b0d9426a",
"https://cdn.skypack.dev/ramda@^0.29.0?dts": "58d3aec92683f33d6d780ff5619ec2fe5cfafd016bf9b6d8190be173b0d9426a",
"https://cdn.skypack.dev/[email protected]?dts": "58d3aec92683f33d6d780ff5619ec2fe5cfafd016bf9b6d8190be173b0d9426a",
"https://deno.land/[email protected]/_util/asserts.ts": "178dfc49a464aee693a7e285567b3d0b555dc805ff490505a8aae34f9cfb1462",
"https://deno.land/[email protected]/_util/os.ts": "d932f56d41e4f6a6093d56044e29ce637f8dcc43c5a90af43504a889cf1775e3",
"https://deno.land/[email protected]/fs/_util.ts": "65381f341af1ff7f40198cee15c20f59951ac26e51ddc651c5293e24f9ce6f32",
Expand Down Expand Up @@ -689,105 +789,5 @@
"https://raw.githubusercontent.com/hyper63/hyper/hyper-port-storage%40v2.0.1/packages/port-storage/port.ts": "030875a500e069f0c71409c021dca62f2cb8f5ffd710573351ae2727d749cd2c",
"https://raw.githubusercontent.com/hyper63/hyper/hyper-utils%40v0.1.0/packages/utils/hyper-err.js": "434ce4bd1cc58e220aad95148bfb04ad7c43738cb5f771a02a45ea8608ac71cc",
"https://raw.githubusercontent.com/hyper63/hyper/hyper-utils%40v0.1.1/packages/utils/hyper-err.js": "19d894e770cc1bc1fbe3e2e31ab0186c2440a84b302dd57c1f2bc9a4dbbe7502"
},
"npm": {
"specifiers": {
"[email protected]": "[email protected]",
"[email protected]": "[email protected]",
"[email protected]": "[email protected]"
},
"packages": {
"@types/[email protected]": {
"integrity": "sha512-IXl7o+R9iti9eBW4Wg2hx1xQDig183jj7YLn8F7udNceyfkbn1ZxmzZXuak20gR40D7pIkIY1kYGx5VIGbaHKA==",
"dependencies": {}
},
"@types/[email protected]": {
"integrity": "sha512-xTE1E+YF4aWPJJeUzaZI5DRntlkY3+BCVJi0axFptnjGmAoWxkyREIh/XMrfxVLejwQxMCfDXdICo0VLxThrog==",
"dependencies": {}
},
"@types/[email protected]": {
"integrity": "sha512-FtQu10RWgn3D9U4aazdwIE2yzphmTJREDqNdODHrbrZmmMqI0vMheC/6NE/J1Yveaj8H+ela+YwWTjq5PGmuhA==",
"dependencies": {
"@types/node": "@types/[email protected]",
"@types/webidl-conversions": "@types/[email protected]"
}
},
"[email protected]": {
"integrity": "sha512-WRZ5SQI5GfUuKnPTNmAYPiKIof3ORXAF4IRU5UcgmivNIon01rWQlw5RUH954dpu8yGL8T59YShVddIPaU/gFA==",
"dependencies": {}
},
"[email protected]": {
"integrity": "sha512-WZYYkHdIDnaxdeP8Misq3Lah5vFjJwGuItJuV+tvMafosMzw0nF297T7mrm8IOWiPJkV6gc7sa8pzx27+w25Zg==",
"dependencies": {}
},
"[email protected]": {
"integrity": "sha512-WKa+XuLG1A1R0UWhl2+1XQSi+fZWMsYKffMZTTYsiZaUD8k2yDAj5atimTUD2TZkyCkNEeYE5NhFZmupOGtjYQ==",
"dependencies": {}
},
"[email protected]": {
"integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==",
"dependencies": {}
},
"[email protected]": {
"integrity": "sha512-WvTZlI9ab0QYtTYnuMLgobULWhokRjtC7db9LtcVfJ+Hsnyr5eo6ZtNAt3Ly24XZScGMelOcGtm7lSn0332tPQ==",
"dependencies": {
"@types/whatwg-url": "@types/[email protected]",
"whatwg-url": "[email protected]"
}
},
"[email protected]": {
"integrity": "sha512-zm82Bq33QbqtxDf58fLWBwTjARK3NSvKYjyz997KSy6hpat0prjeX/kxjbPVyZY60XYPDNETaHkHJI2UCzSLuw==",
"dependencies": {
"bson": "[email protected]",
"mongodb-connection-string-url": "[email protected]",
"saslprep": "[email protected]",
"socks": "[email protected]"
}
},
"[email protected]": {
"integrity": "sha512-rRV+zQD8tVFys26lAGR9WUuS4iUAngJScM+ZRSKtvl5tKeZ2t5bvdNFdNHBW9FWR4guGHlgmsZ1G7BSm2wTbuA==",
"dependencies": {}
},
"[email protected]": {
"integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==",
"dependencies": {
"sparse-bitfield": "[email protected]"
}
},
"[email protected]": {
"integrity": "sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==",
"dependencies": {}
},
"[email protected]": {
"integrity": "sha512-7maUZy1N7uo6+WVEX6psASxtNlKaNVMlGQKkG/63nEDdLOWNbiUMoLK7X4uYoLhQstau72mLgfEWcXcwsaHbYQ==",
"dependencies": {
"ip": "[email protected]",
"smart-buffer": "[email protected]"
}
},
"[email protected]": {
"integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==",
"dependencies": {
"memory-pager": "[email protected]"
}
},
"[email protected]": {
"integrity": "sha512-l7FvfAHlcmulp8kr+flpQZmVwtu7nfRV7NZujtN0OqES8EL4O4e0qqzL0DC5gAvx/ZC/9lk6rhcUwYvkBnBnYA==",
"dependencies": {
"punycode": "[email protected]"
}
},
"[email protected]": {
"integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==",
"dependencies": {}
},
"[email protected]": {
"integrity": "sha512-RKT8HExMpoYx4igMiVMY83lN6UeITKJlBQ+vR/8ZJ8OCdSiN3RwCq+9gH0+Xzj0+5IrM6i4j/6LuvzbZIQgEcQ==",
"dependencies": {
"tr46": "[email protected]",
"webidl-conversions": "[email protected]"
}
}
}
}
}
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 0ffd18f

Please sign in to comment.