Skip to content

Commit

Permalink
Merge pull request awslabs#58 from tiratatp/bug/better_query_pagination
Browse files Browse the repository at this point in the history
Simplify query pagination
  • Loading branch information
tiratatp authored Feb 11, 2021
2 parents 38ead3b + f9e0209 commit 12b976f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 47 deletions.
14 changes: 7 additions & 7 deletions sample_apps/js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sample_apps/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"homepage": "https://github.com/awslabs/amazon-timestream-tools/tree/master/sample_apps/js#readme",
"dependencies": {
"aws-sdk": "^2.798.0",
"aws-sdk": "^2.842.0",
"minimist": "^1.2.5"
}
}
69 changes: 30 additions & 39 deletions sample_apps/js/query-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,32 +232,28 @@ async function runAllQueries() {
}
}

async function getAllRows(query, nextToken) {
const params = {
QueryString: query
};

if (nextToken) {
params.NextToken = nextToken;
async function getAllRows(query, nextToken = undefined) {
let response;
try {
response = await queryClient.query(params = {
QueryString: query,
NextToken: nextToken,
}).promise();
} catch (err) {
console.error("Error while querying:", err);
throw err;
}

await queryClient.query(params).promise()
.then(
(response) => {
parseQueryResult(response);
if (response.NextToken) {
getAllRows(query, response.NextToken);
}
},
(err) => {
console.error("Error while querying:", err);
});
parseQueryResult(response);
if (response.NextToken) {
await getAllRows(query, response.NextToken);
}
}

async function tryQueryWithMultiplePages(limit) {
const queryWithLimits = SELECT_ALL_QUERY + " LIMIT " + limit;
console.log(`Running query with multiple pages: ${queryWithLimits}`);
await getAllRows(queryWithLimits, null)
await getAllRows(queryWithLimits)
}

async function tryCancelQuery() {
Expand All @@ -266,29 +262,24 @@ async function tryCancelQuery() {
};
console.log(`Running query: ${SELECT_ALL_QUERY}`);

await queryClient.query(params).promise()
.then(
async (response) => {
await cancelQuery(response.QueryId);
},
(err) => {
console.error("Error while executing select all query:", err);
});
const response = await queryClient.query(params).promise();

console.log(`Sending cancellation for query: ${SELECT_ALL_QUERY}`);

await cancelQuery(response.QueryId);
}

async function cancelQuery(queryId) {
const cancelParams = {
QueryId: queryId
};
console.log(`Sending cancellation for query: ${SELECT_ALL_QUERY}`);
await queryClient.cancelQuery(cancelParams).promise()
.then(
(response) => {
console.log("Query has been cancelled successfully");
},
(err) => {
console.error("Error while cancelling select all:", err);
});
try {
await queryClient.cancelQuery({
QueryId: queryId
}).promise();
} catch (err) {
console.error("Error while cancelling query:", err);
throw err;
}

console.log("Query has been cancelled successfully");
}

function parseQueryResult(response) {
Expand Down

0 comments on commit 12b976f

Please sign in to comment.