Skip to content

Commit

Permalink
Merge pull request #1086 from dynamoose/batchGetAttributes
Browse files Browse the repository at this point in the history
Model.batchGet Attributes
  • Loading branch information
fishcharlie committed Dec 31, 2020
2 parents 96d4d35 + 60918cb commit d1accec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/docs/guide/Model.md
Expand Up @@ -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});
Expand All @@ -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});

Expand Down
4 changes: 4 additions & 0 deletions lib/Model/index.ts
Expand Up @@ -245,6 +245,7 @@ interface ModelBatchGetDocumentsResponse<T> extends DocumentArray<T> {
}
interface ModelBatchGetSettings {
return?: "documents" | "request";
attributes?: string[];
}
interface ModelBatchDeleteSettings {
return?: "response" | "request";
Expand Down Expand Up @@ -511,6 +512,9 @@ export class Model<T extends DocumentCarrier = AnyDocument> {
}
}
};
if (settings.attributes) {
params.RequestItems[this.name].AttributesToGet = settings.attributes;
}
if (settings.return === "request") {
if (callback) {
const localCallback: CallbackType<DynamoDB.BatchGetItemInput, AWSError> = callback as CallbackType<DynamoDB.BatchGetItemInput, AWSError>;
Expand Down
16 changes: 16 additions & 0 deletions test/unit/Model.js
Expand Up @@ -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"});
Expand Down

1 comment on commit d1accec

@vercel
Copy link

@vercel vercel bot commented on d1accec Dec 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.