-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathquery-with-pagination-all-data.js
73 lines (63 loc) · 1.81 KB
/
query-with-pagination-all-data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* This example uses an extended version of the ProductCatalog data set
* https://gist.github.com/jprivillaso/7063b5e082ed2f553b697d40c60c473e
*/
const AWS = require("aws-sdk");
AWS.config.update({ region: "us-west-2" });
const documentClient = new AWS.DynamoDB.DocumentClient();
async function* getDataIterator() {
let done = false;
/**
* In this example, the pageSize is not needed. If you have so much data,
* DynamoDB will return the LastEvaluatedKey and then you should continue
* query again until the LastEvaluatedKey returned is null
*/
const params = {
TableName: "Thread",
KeyConditionExpression: "#forumName = :forumName",
ExpressionAttributeNames: {
"#forumName": "ForumName",
},
ExpressionAttributeValues: {
":forumName": "Amazon DynamoDB",
},
ScanIndexForward: true, // Default value is true
};
while (!done) {
const { Items, LastEvaluatedKey } = await documentClient
.query(params)
.promise();
if (!LastEvaluatedKey) {
done = true;
} else {
params.ExclusiveStartKey = LastEvaluatedKey;
}
if (Items && Items.length > 0) {
yield Items;
}
}
}
const queryAllData = async () => {
try {
const allData = [];
for await (const dataBatch of getDataIterator()) {
allData.push(...dataBatch);
}
return allData;
} catch (error) {
throw new Error(JSON.stringify(error, null, 2));
}
};
(async () => {
try {
/**
* If you need to query all the data for any reason, you will need to continue
* querying until the LastEvaluatedKey returned is null
*/
const allData = await queryAllData();
console.log("Should contain all the data ---");
console.log("Query succeeded:", JSON.stringify(allData, null, 2));
} catch (error) {
console.error(error);
}
})();