Skip to content

Commit df29170

Browse files
authored
Merge pull request aws-samples#85 from zachjonesnoel/master
Added PartiQL with Pagination sample NodeJS
2 parents 7d140de + bc855c1 commit df29170

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const REGION = "us-west-2";
2+
// I am using the DynamoDB low level because the DocClient does not support executeStatement used with PartiQL
3+
const { DynamoDB } = require("@aws-sdk/client-dynamodb");
4+
const { unmarshall } = require("@aws-sdk/util-dynamodb");
5+
6+
// Create low level client
7+
const dbclient = new DynamoDB({ region: REGION });
8+
9+
let isDone = false
10+
// In PartiQL, we can execute statement with execute-statement which performs one action similar to Scan operation and returns NextToken which could be used to retrieve the next page/set of records.
11+
let params = {
12+
Statement: `SELECT * from "cars-demo"`,
13+
NextToken: null
14+
}
15+
16+
while (!isDone) {
17+
try {
18+
let response = await dbclient.executeStatement(params);
19+
/* Returns response as Items and NextToken if the statement execution has more items.
20+
{
21+
"Items":[],
22+
"NextToken":""
23+
}
24+
*/
25+
//Iterate through the Items array returned in the data variable and then unmarshall the DynamoDB JSON format to "regular" json format.
26+
data.Items.forEach(function (item) {
27+
console.log(JSON.stringify(unmarshall(item), null, 2));
28+
});
29+
if (data.NextToken != undefined) {
30+
params.NextToken = data.NextToken
31+
} else {
32+
isDone = true;
33+
}
34+
} catch (error) {
35+
console.error(JSON.stringify(error, null, 2));
36+
}
37+
}
38+

0 commit comments

Comments
 (0)