Skip to content

Instantly share code, notes, and snippets.

@jordicenzano
Created April 12, 2018 00:17
Show Gist options
  • Save jordicenzano/f7c4b4d64ef8952c6dfd9c38b1575be7 to your computer and use it in GitHub Desktop.
Save jordicenzano/f7c4b4d64ef8952c6dfd9c38b1575be7 to your computer and use it in GitHub Desktop.
DynamoDB Javascript pagination test
#!/usr/bin/env node
const AWS = require('aws-sdk');
AWS.config.update({region: 'us-west-2'});
const dynamodb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });
let dbparams_q1 = {
"TableName": "alive-qa-brain-ddb-tbllivejobs",
"IndexName": "account_id-job_created_at-index",
"ProjectionExpression": "job_id",
"KeyConditionExpression": "account_id = :tag_account_id",
"Limit": 5,
"ExpressionAttributeValues": {
":tag_account_id": {"S": "6f0a8c2e6d514f3e8dce9520d23a75fb"}
},
"ReturnConsumedCapacity": "TOTAL"
};
//Here we are starting from the last reported item
let dbparams_q2 = {
"TableName": "alive-qa-brain-ddb-tbllivejobs",
"IndexName": "account_id-job_created_at-index",
"ProjectionExpression": "job_id",
"KeyConditionExpression": "account_id = :tag_account_id",
"ExclusiveStartKey": {"account_id":{"S":"6f0a8c2e6d514f3e8dce9520d23a75fb"},"job_id":{"S":"b6408a16663f46f594b4ea8291b4d115"},"job_created_at":{"N":"1520959910535"}},
"Limit": 5,
"ExpressionAttributeValues": {
":tag_account_id": {"S": "6f0a8c2e6d514f3e8dce9520d23a75fb"}
},
"ReturnConsumedCapacity": "TOTAL"
};
//Here we are starting from an arbitrary item (2nd reported from Q1), creating ExclusiveStartKey ourselfves
let dbparams_q3 = {
"TableName": "alive-qa-brain-ddb-tbllivejobs",
"IndexName": "account_id-job_created_at-index",
"ProjectionExpression": "job_id",
"KeyConditionExpression": "account_id = :tag_account_id",
"ExclusiveStartKey": {"account_id":{"S":"6f0a8c2e6d514f3e8dce9520d23a75fb"},"job_id":{"S":"0204b0cbd25a4dea8fbf1967cdc7d6eb"},"job_created_at":{"N":"1520959245487"}},
"Limit": 5,
"ExpressionAttributeValues": {
":tag_account_id": {"S": "6f0a8c2e6d514f3e8dce9520d23a75fb"}
},
"ReturnConsumedCapacity": "TOTAL"
};
dynamodb.query(dbparams_q1, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
}
else {
console.log("Q1: START BEGINNING, 5 items");
console.log(JSON.stringify(data));
dynamodb.query(dbparams_q2, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
}
else {
console.log("Q2: START END PAGE 1, 5 items");
console.log(JSON.stringify(data));
dynamodb.query(dbparams_q3, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
}
else {
console.log("Q3: START 2nd item page 1, 5 items");
console.log(JSON.stringify(data));
}
});
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment