From c315eceb103d60176668868474c702ee9e800544 Mon Sep 17 00:00:00 2001 From: Tyler Hall Date: Tue, 10 Oct 2023 18:01:13 -0400 Subject: [PATCH] feat(queryDocuments): add skip to query options --- deno.lock | 204 +++++++++++++++++++++++++------------------------- deps.ts | 2 +- test/suite.ts | 34 +++++++++ utils.test.ts | 5 ++ utils.ts | 8 +- 5 files changed, 148 insertions(+), 105 deletions(-) diff --git a/deno.lock b/deno.lock index 123a23e..19c17ae 100644 --- a/deno.lock +++ b/deno.lock @@ -1,5 +1,105 @@ { - "version": "2", + "version": "3", + "packages": { + "specifiers": { + "npm:bson@5.4.0": "npm:bson@5.4.0", + "npm:cuid@3.0.0": "npm:cuid@3.0.0", + "npm:mongodb@5.7.0": "npm:mongodb@5.7.0" + }, + "npm": { + "@types/node@18.16.19": { + "integrity": "sha512-IXl7o+R9iti9eBW4Wg2hx1xQDig183jj7YLn8F7udNceyfkbn1ZxmzZXuak20gR40D7pIkIY1kYGx5VIGbaHKA==", + "dependencies": {} + }, + "@types/webidl-conversions@7.0.1": { + "integrity": "sha512-8hKOnOan+Uu+NgMaCouhg3cT9x5fFZ92Jwf+uDLXLu/MFRbXxlWwGeQY7KVHkeSft6RvY+tdxklUBuyY9eIEKg==", + "dependencies": {} + }, + "@types/whatwg-url@8.2.2": { + "integrity": "sha512-FtQu10RWgn3D9U4aazdwIE2yzphmTJREDqNdODHrbrZmmMqI0vMheC/6NE/J1Yveaj8H+ela+YwWTjq5PGmuhA==", + "dependencies": { + "@types/node": "@types/node@18.16.19", + "@types/webidl-conversions": "@types/webidl-conversions@7.0.1" + } + }, + "bson@5.4.0": { + "integrity": "sha512-WRZ5SQI5GfUuKnPTNmAYPiKIof3ORXAF4IRU5UcgmivNIon01rWQlw5RUH954dpu8yGL8T59YShVddIPaU/gFA==", + "dependencies": {} + }, + "cuid@3.0.0": { + "integrity": "sha512-WZYYkHdIDnaxdeP8Misq3Lah5vFjJwGuItJuV+tvMafosMzw0nF297T7mrm8IOWiPJkV6gc7sa8pzx27+w25Zg==", + "dependencies": {} + }, + "ip@2.0.0": { + "integrity": "sha512-WKa+XuLG1A1R0UWhl2+1XQSi+fZWMsYKffMZTTYsiZaUD8k2yDAj5atimTUD2TZkyCkNEeYE5NhFZmupOGtjYQ==", + "dependencies": {} + }, + "memory-pager@1.5.0": { + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", + "dependencies": {} + }, + "mongodb-connection-string-url@2.6.0": { + "integrity": "sha512-WvTZlI9ab0QYtTYnuMLgobULWhokRjtC7db9LtcVfJ+Hsnyr5eo6ZtNAt3Ly24XZScGMelOcGtm7lSn0332tPQ==", + "dependencies": { + "@types/whatwg-url": "@types/whatwg-url@8.2.2", + "whatwg-url": "whatwg-url@11.0.0" + } + }, + "mongodb@5.7.0": { + "integrity": "sha512-zm82Bq33QbqtxDf58fLWBwTjARK3NSvKYjyz997KSy6hpat0prjeX/kxjbPVyZY60XYPDNETaHkHJI2UCzSLuw==", + "dependencies": { + "bson": "bson@5.4.0", + "mongodb-connection-string-url": "mongodb-connection-string-url@2.6.0", + "saslprep": "saslprep@1.0.3", + "socks": "socks@2.7.1" + } + }, + "punycode@2.3.0": { + "integrity": "sha512-rRV+zQD8tVFys26lAGR9WUuS4iUAngJScM+ZRSKtvl5tKeZ2t5bvdNFdNHBW9FWR4guGHlgmsZ1G7BSm2wTbuA==", + "dependencies": {} + }, + "saslprep@1.0.3": { + "integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==", + "dependencies": { + "sparse-bitfield": "sparse-bitfield@3.0.3" + } + }, + "smart-buffer@4.2.0": { + "integrity": "sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==", + "dependencies": {} + }, + "socks@2.7.1": { + "integrity": "sha512-7maUZy1N7uo6+WVEX6psASxtNlKaNVMlGQKkG/63nEDdLOWNbiUMoLK7X4uYoLhQstau72mLgfEWcXcwsaHbYQ==", + "dependencies": { + "ip": "ip@2.0.0", + "smart-buffer": "smart-buffer@4.2.0" + } + }, + "sparse-bitfield@3.0.3": { + "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==", + "dependencies": { + "memory-pager": "memory-pager@1.5.0" + } + }, + "tr46@3.0.0": { + "integrity": "sha512-l7FvfAHlcmulp8kr+flpQZmVwtu7nfRV7NZujtN0OqES8EL4O4e0qqzL0DC5gAvx/ZC/9lk6rhcUwYvkBnBnYA==", + "dependencies": { + "punycode": "punycode@2.3.0" + } + }, + "webidl-conversions@7.0.0": { + "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==", + "dependencies": {} + }, + "whatwg-url@11.0.0": { + "integrity": "sha512-RKT8HExMpoYx4igMiVMY83lN6UeITKJlBQ+vR/8ZJ8OCdSiN3RwCq+9gH0+Xzj0+5IrM6i4j/6LuvzbZIQgEcQ==", + "dependencies": { + "tr46": "tr46@3.0.0", + "webidl-conversions": "webidl-conversions@7.0.0" + } + } + } + }, "remote": { "https://cdn.skypack.dev/-/crocks@v0.12.4-Mje8nEhNx2rmIpwz3ROp/dist=es2019,mode=imports/optimized/crocks.js": "93d587d18dc5f124f30e5b38de37a6471eb65309c94ef2ffc7a36dc40ab394da", "https://cdn.skypack.dev/-/ramda@v0.28.0-vDL6KbwpPkq7DJAM324O/dist=es2019,mode=imports/optimized/common/_Set-d915dfa1.js": "85edd0c60ae74c28bfd7a6d831d1274b2299de6c46cc19ddfed24f593365f0c8", @@ -615,7 +715,7 @@ "https://cdn.skypack.dev/ramda@0.28.0": "ac1e3c64a2a3491971bee55f88faeb0923023e903d3ad125f1bd8044b4cad03e", "https://cdn.skypack.dev/ramda@0.28.0?dts": "ac1e3c64a2a3491971bee55f88faeb0923023e903d3ad125f1bd8044b4cad03e", "https://cdn.skypack.dev/ramda@0.29.0": "58d3aec92683f33d6d780ff5619ec2fe5cfafd016bf9b6d8190be173b0d9426a", - "https://cdn.skypack.dev/ramda@^0.29.0?dts": "58d3aec92683f33d6d780ff5619ec2fe5cfafd016bf9b6d8190be173b0d9426a", + "https://cdn.skypack.dev/ramda@0.29.0?dts": "58d3aec92683f33d6d780ff5619ec2fe5cfafd016bf9b6d8190be173b0d9426a", "https://deno.land/std@0.182.0/_util/asserts.ts": "178dfc49a464aee693a7e285567b3d0b555dc805ff490505a8aae34f9cfb1462", "https://deno.land/std@0.182.0/_util/os.ts": "d932f56d41e4f6a6093d56044e29ce637f8dcc43c5a90af43504a889cf1775e3", "https://deno.land/std@0.182.0/fs/_util.ts": "65381f341af1ff7f40198cee15c20f59951ac26e51ddc651c5293e24f9ce6f32", @@ -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": { - "bson@5.4.0": "bson@5.4.0", - "cuid@3.0.0": "cuid@3.0.0", - "mongodb@5.7.0": "mongodb@5.7.0" - }, - "packages": { - "@types/node@18.16.19": { - "integrity": "sha512-IXl7o+R9iti9eBW4Wg2hx1xQDig183jj7YLn8F7udNceyfkbn1ZxmzZXuak20gR40D7pIkIY1kYGx5VIGbaHKA==", - "dependencies": {} - }, - "@types/webidl-conversions@7.0.0": { - "integrity": "sha512-xTE1E+YF4aWPJJeUzaZI5DRntlkY3+BCVJi0axFptnjGmAoWxkyREIh/XMrfxVLejwQxMCfDXdICo0VLxThrog==", - "dependencies": {} - }, - "@types/whatwg-url@8.2.2": { - "integrity": "sha512-FtQu10RWgn3D9U4aazdwIE2yzphmTJREDqNdODHrbrZmmMqI0vMheC/6NE/J1Yveaj8H+ela+YwWTjq5PGmuhA==", - "dependencies": { - "@types/node": "@types/node@18.16.19", - "@types/webidl-conversions": "@types/webidl-conversions@7.0.0" - } - }, - "bson@5.4.0": { - "integrity": "sha512-WRZ5SQI5GfUuKnPTNmAYPiKIof3ORXAF4IRU5UcgmivNIon01rWQlw5RUH954dpu8yGL8T59YShVddIPaU/gFA==", - "dependencies": {} - }, - "cuid@3.0.0": { - "integrity": "sha512-WZYYkHdIDnaxdeP8Misq3Lah5vFjJwGuItJuV+tvMafosMzw0nF297T7mrm8IOWiPJkV6gc7sa8pzx27+w25Zg==", - "dependencies": {} - }, - "ip@2.0.0": { - "integrity": "sha512-WKa+XuLG1A1R0UWhl2+1XQSi+fZWMsYKffMZTTYsiZaUD8k2yDAj5atimTUD2TZkyCkNEeYE5NhFZmupOGtjYQ==", - "dependencies": {} - }, - "memory-pager@1.5.0": { - "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", - "dependencies": {} - }, - "mongodb-connection-string-url@2.6.0": { - "integrity": "sha512-WvTZlI9ab0QYtTYnuMLgobULWhokRjtC7db9LtcVfJ+Hsnyr5eo6ZtNAt3Ly24XZScGMelOcGtm7lSn0332tPQ==", - "dependencies": { - "@types/whatwg-url": "@types/whatwg-url@8.2.2", - "whatwg-url": "whatwg-url@11.0.0" - } - }, - "mongodb@5.7.0": { - "integrity": "sha512-zm82Bq33QbqtxDf58fLWBwTjARK3NSvKYjyz997KSy6hpat0prjeX/kxjbPVyZY60XYPDNETaHkHJI2UCzSLuw==", - "dependencies": { - "bson": "bson@5.4.0", - "mongodb-connection-string-url": "mongodb-connection-string-url@2.6.0", - "saslprep": "saslprep@1.0.3", - "socks": "socks@2.7.1" - } - }, - "punycode@2.3.0": { - "integrity": "sha512-rRV+zQD8tVFys26lAGR9WUuS4iUAngJScM+ZRSKtvl5tKeZ2t5bvdNFdNHBW9FWR4guGHlgmsZ1G7BSm2wTbuA==", - "dependencies": {} - }, - "saslprep@1.0.3": { - "integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==", - "dependencies": { - "sparse-bitfield": "sparse-bitfield@3.0.3" - } - }, - "smart-buffer@4.2.0": { - "integrity": "sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==", - "dependencies": {} - }, - "socks@2.7.1": { - "integrity": "sha512-7maUZy1N7uo6+WVEX6psASxtNlKaNVMlGQKkG/63nEDdLOWNbiUMoLK7X4uYoLhQstau72mLgfEWcXcwsaHbYQ==", - "dependencies": { - "ip": "ip@2.0.0", - "smart-buffer": "smart-buffer@4.2.0" - } - }, - "sparse-bitfield@3.0.3": { - "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==", - "dependencies": { - "memory-pager": "memory-pager@1.5.0" - } - }, - "tr46@3.0.0": { - "integrity": "sha512-l7FvfAHlcmulp8kr+flpQZmVwtu7nfRV7NZujtN0OqES8EL4O4e0qqzL0DC5gAvx/ZC/9lk6rhcUwYvkBnBnYA==", - "dependencies": { - "punycode": "punycode@2.3.0" - } - }, - "webidl-conversions@7.0.0": { - "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==", - "dependencies": {} - }, - "whatwg-url@11.0.0": { - "integrity": "sha512-RKT8HExMpoYx4igMiVMY83lN6UeITKJlBQ+vR/8ZJ8OCdSiN3RwCq+9gH0+Xzj0+5IrM6i4j/6LuvzbZIQgEcQ==", - "dependencies": { - "tr46": "tr46@3.0.0", - "webidl-conversions": "webidl-conversions@7.0.0" - } - } - } } } diff --git a/deps.ts b/deps.ts index 123bf32..50a5475 100644 --- a/deps.ts +++ b/deps.ts @@ -3,7 +3,7 @@ */ // @deno-types="./crocks.d.ts" export { default as crocks } from 'https://cdn.skypack.dev/crocks@0.12.4' -export * as R from 'https://cdn.skypack.dev/ramda@^0.29.0?dts' +export * as R from 'https://cdn.skypack.dev/ramda@0.29.0?dts' export { EJSON } from 'npm:bson@5.4.0' export { type Collection, MongoClient } from 'npm:mongodb@5.7.0' diff --git a/test/suite.ts b/test/suite.ts index 757e09c..a4f56a5 100644 --- a/test/suite.ts +++ b/test/suite.ts @@ -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) }, ) diff --git a/utils.test.ts b/utils.test.ts index 5a43e0e..68d8fe1 100644 --- a/utils.test.ts +++ b/utils.test.ts @@ -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 } }) diff --git a/utils.ts b/utils.ts index f56c445..e880b3f 100644 --- a/utils.ts +++ b/utils.ts @@ -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' }[] }) => { @@ -46,6 +48,7 @@ export const queryOptions = ({ */ const options: { limit?: number + skip?: number projection?: { [field: string]: 0 | 1 } sort?: { [field: string]: 1 | -1 } } = { @@ -53,6 +56,7 @@ export const queryOptions = ({ * 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( @@ -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