Skip to content

Commit 7e3013a

Browse files
committed
feat(dynamodb): add batch get item paginators for low level and document client
1 parent 954d411 commit 7e3013a

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddDocumentClientPlugin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ private void writeAdditionalFiles(
105105
writerFactory.accept(String.format("%s%s/index.ts", DocumentClientUtils.DOC_CLIENT_PREFIX,
106106
DocumentClientPaginationGenerator.PAGINATION_FOLDER), writer -> {
107107
writer.write("export * from './Interfaces';");
108+
writer.write("export * from './BatchGetPaginator';");
108109
for (OperationShape operation : overridenOperationsList) {
109110
if (operation.hasTrait(PaginatedTrait.ID)) {
110111
String paginationFileName =
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { type DynamoDBClient } from "@aws-sdk/client-dynamodb";
2+
3+
import { BatchGetCommand, BatchGetCommandInput, BatchGetCommandOutput } from "../commands/BatchGetCommand";
4+
import { DynamoDBDocumentClient } from "../DynamoDBDocumentClient";
5+
/**
6+
* @public
7+
*
8+
* Async generator that issues {@link BatchGetCommand}s repeatedly until all keys are processed or an error response is received.
9+
*
10+
* @see {@link paginateBatchGetItem} for a variant that uses the {@link DynamoDBClient | low level DynamoDB client}.
11+
*
12+
* @example
13+
*
14+
* ```
15+
* const client = new DynamoDBClient();
16+
* const docClient = DynamoDBDocumentClient.from(client);
17+
* const input: BatchGetCommandInput = {
18+
* RequestItems: {
19+
* table1: Keys: [...],
20+
* table2: Keys: [...],
21+
* }
22+
* };
23+
*
24+
* let pageNumber = 1;
25+
* for await (const page of paginateBatchGet({ client: docClient }, input)) {
26+
* console.log("page:", pageNumber++);
27+
* console.log("items:", page.Responses);
28+
* console.log("unprocessed:", page.UnprocessedKeys); // will be returned in the next page(s)
29+
* }
30+
* ```
31+
*/
32+
export async function* paginateBatchGet(
33+
config: {
34+
client: DynamoDBDocumentClient;
35+
},
36+
input: BatchGetCommandInput
37+
): AsyncGenerator<BatchGetCommandOutput> {
38+
let RequestItems = input.RequestItems;
39+
40+
while (RequestItems && Object.keys(RequestItems).length > 0) {
41+
const cmd = new BatchGetCommand({ ...input, RequestItems });
42+
const response = await config.client.send(cmd);
43+
RequestItems = { ...response.UnprocessedKeys };
44+
45+
yield response;
46+
}
47+
}

lib/lib-dynamodb/src/pagination/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from "./BatchGetPaginator";
12
// smithy-typescript generated code
23
export * from "./Interfaces";
34
export * from "./QueryPaginator";

packages/util-dynamodb/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export * from "./convertToAttr";
33
export * from "./convertToNative";
44
export * from "./marshall";
55
export * from "./models";
6+
export * from "./paginateBatchGetItem";
67
export * from "./unmarshall";
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { BatchGetItemCommand, BatchGetItemCommandInput, DynamoDBClient } from "@aws-sdk/client-dynamodb";
2+
import { type DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
3+
4+
/**
5+
* @public
6+
*
7+
* Async generator that issues {@link BatchGetItemCommand}s repeatedly until all keys are processed or an error response is received.
8+
*
9+
* @see {@link paginateBatchGetItem} for a variant that uses the {@link DynamoDBDocumentClient | DynamoDB document client}.
10+
*
11+
* @example
12+
*
13+
* ```
14+
* const client = new DynamoDBClient();
15+
* const input: BatchGetCommandInput = {
16+
* RequestItems: {
17+
* table1: Keys: [...],
18+
* table2: Keys: [...],
19+
* }
20+
* };
21+
*
22+
* let pageNumber = 1;
23+
* for await (const page of paginateBatchGetItem({ client }, input)) {
24+
* console.log("page:", pageNumber++);
25+
* console.log("items:", page.Responses);
26+
* console.log("unprocessed:", page.UnprocessedKeys); // will be returned in the next page(s)
27+
* }
28+
* ```
29+
*/
30+
export async function* paginateBatchGetItem(client: DynamoDBClient, input: BatchGetItemCommandInput) {
31+
let RequestItems = input.RequestItems;
32+
33+
while (RequestItems && Object.keys(RequestItems).length > 0) {
34+
const cmd = new BatchGetItemCommand({ ...input, RequestItems });
35+
const response = await client.send(cmd);
36+
RequestItems = { ...response.UnprocessedKeys };
37+
38+
yield response;
39+
}
40+
}

0 commit comments

Comments
 (0)