From 539fd3d85c8541f064ec7a4afdfb050ba3d7b42d Mon Sep 17 00:00:00 2001 From: Charlie Fish Date: Thu, 24 Dec 2020 10:33:45 -0700 Subject: [PATCH 1/3] Adding support for batchGet attributes --- lib/Model/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/Model/index.ts b/lib/Model/index.ts index 163d9dfb9..9f2d9c8f8 100644 --- a/lib/Model/index.ts +++ b/lib/Model/index.ts @@ -245,6 +245,7 @@ interface ModelBatchGetDocumentsResponse extends DocumentArray { } interface ModelBatchGetSettings { return?: "documents" | "request"; + attributes?: string[]; } interface ModelBatchDeleteSettings { return?: "response" | "request"; @@ -511,6 +512,9 @@ export class Model { } } }; + if (settings.attributes) { + params.RequestItems[this.name].AttributesToGet = settings.attributes; + } if (settings.return === "request") { if (callback) { const localCallback: CallbackType = callback as CallbackType; From ff8e3ef2c20bbf5fd678147792c6261e5c0f74bd Mon Sep 17 00:00:00 2001 From: Charlie Fish Date: Thu, 24 Dec 2020 10:33:51 -0700 Subject: [PATCH 2/3] Adding documentation for batchGet attributes --- docs/docs/guide/Model.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/docs/guide/Model.md b/docs/docs/guide/Model.md index 97222df88..2b06c7397 100644 --- a/docs/docs/guide/Model.md +++ b/docs/docs/guide/Model.md @@ -274,6 +274,7 @@ You can also pass in an object for the optional `settings` parameter that is an | Name | Description | Type | Default | |------|-------------|------|---------| | return | What the function should return. Can be `documents`, or `request`. In the event this is set to `request` the request Dynamoose will make to DynamoDB will be returned, and no request to DynamoDB will be made. If this is `request`, the function will not be async anymore. | String | `documents` | +| attributes | What document attributes should be retrieved & returned. This will use the underlying `AttributesToGet` DynamoDB option to ensure only the attributes you request will be sent over the wire. If this value is `undefined`, then all attributes will be returned. | [String] | undefined | ```js const User = dynamoose.model("User", {"id": Number, "name": String}); @@ -296,6 +297,17 @@ User.batchGet([1, 2], (error, myUsers) => { }); ``` +```js +const User = dynamoose.model("User", {"id": Number, "name": String, "data": String}); + +try { + const myUsers = await User.batchGet([1, 2], {"attributes": ["id", "data"]}); + console.log(myUsers); // Only `id` and `data` will exist on each object (`name` will not be returned) +} catch (error) { + console.error(error); +} +``` + ```js const User = dynamoose.model("User", {"id": Number, "name": String}); From 60918cb8527e87c369b3f9623c48c289334b736b Mon Sep 17 00:00:00 2001 From: Charlie Fish Date: Thu, 24 Dec 2020 10:33:55 -0700 Subject: [PATCH 3/3] Adding tests for batchGet attributes --- test/unit/Model.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/unit/Model.js b/test/unit/Model.js index 329228814..fcb21e149 100644 --- a/test/unit/Model.js +++ b/test/unit/Model.js @@ -1620,6 +1620,22 @@ describe("Model", () => { }); }); + it("Should send correct params to batchGetItem with attributes", async () => { + promiseFunction = () => Promise.resolve({"Responses": {"User": [{"id": {"N": "1"}, "name": {"S": "Charlie"}}]}, "UnprocessedKeys": {}}); + await callType.func(User).bind(User)([1], {"attributes": ["id", "data"]}); + expect(params).to.be.an("object"); + expect(params).to.eql({ + "RequestItems": { + "User": { + "Keys": [ + {"id": {"N": "1"}} + ], + "AttributesToGet": ["id", "data"] + } + } + }); + }); + it("Should return correct request if setting option return to request", async () => { promiseFunction = () => Promise.resolve({"Responses": {"User": [{"id": {"N": "1"}, "name": {"S": "Charlie"}}]}, "UnprocessedKeys": {}}); const paramsB = await callType.func(User).bind(User)([1], {"return": "request"});