Skip to content

Commit aa10216

Browse files
authored
Merge pull request #170 from mkamioner/nodejs-queries-v3
Update Examples: Javascript Queries to use V3.
2 parents f8030db + fba1187 commit aa10216

17 files changed

+652
-711
lines changed
Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
1-
const AWS = require("aws-sdk");
1+
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
2+
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";
23

3-
const documentClient = new AWS.DynamoDB.DocumentClient({ region: "us-west-2" });
4+
const client = new DynamoDBClient({ region: "us-west-2" });
5+
const docClient = DynamoDBDocumentClient.from(client);
46

57
const query = async () => {
6-
const response = await documentClient
7-
.query({
8-
TableName: "Music",
9-
KeyConditionExpression: "#pk = :pk",
10-
ExpressionAttributeNames: {
11-
"#pk": "Artist",
12-
},
13-
ExpressionAttributeValues: {
14-
":pk": "Michael Jackson",
15-
},
16-
ConsistentRead: true,
17-
})
18-
.promise();
8+
// Define query parameters
9+
const params = {
10+
TableName: "Music",
11+
KeyConditionExpression: "#pk = :pk",
12+
ExpressionAttributeNames: {
13+
"#pk": "Artist",
14+
},
15+
ExpressionAttributeValues: {
16+
":pk": "Michael Jackson", // No need for { S: "Michael Jackson" }
17+
},
18+
ConsistentRead: true,
19+
};
1920

20-
if (response.LastEvaluatedKey) {
21-
console.log(
22-
`Not all items have been retrieved by this query. At least one another request is required to get all available items. The last evaluated key corresponds to ${JSON.stringify(
23-
response.LastEvaluatedKey
24-
)}.`
25-
);
26-
}
21+
try {
22+
// Send the query command
23+
const response = await docClient.send(new QueryCommand(params));
24+
25+
// Check if there are more results to retrieve
26+
if (response.LastEvaluatedKey) {
27+
console.log(
28+
`Not all items have been retrieved by this query. At least one another request is required to get all available items. The last evaluated key corresponds to ${JSON.stringify(response.LastEvaluatedKey)}.`
29+
);
30+
}
2731

28-
console.log(`Query response: ${JSON.stringify(response, null, 2)}`);
32+
return response;
33+
} catch (error) {
34+
console.error("Error querying DynamoDB:", error);
35+
throw error;
36+
}
2937
};
3038

31-
query().catch((error) => console.error(JSON.stringify(error, null, 2)));
39+
query()
40+
.then((response) => console.log(`Query response: ${JSON.stringify(response, null, 2)}`))
41+
.catch((error) => console.error(JSON.stringify(error, null, 2)));
Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
1-
const AWS = require("aws-sdk");
1+
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
2+
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";
23

3-
const documentClient = new AWS.DynamoDB.DocumentClient({ region: "us-west-2" });
4+
const client = new DynamoDBClient({ region: "us-west-2" });
5+
const docClient = DynamoDBDocumentClient.from(client);
46

57
const query = async () => {
6-
const response = await documentClient
7-
.query({
8-
TableName: "Music",
9-
ExpressionAttributeNames: {
10-
"#pk": "Artist",
11-
"#yr": "Year",
12-
},
13-
ExpressionAttributeValues: {
14-
":pk": "Michael Jackson",
15-
":yr": 2012,
16-
},
17-
FilterExpression: "#yr = :yr",
18-
KeyConditionExpression: "#pk = :pk",
19-
})
20-
.promise();
8+
// Define query parameters
9+
const params = {
10+
TableName: "Music",
11+
ExpressionAttributeNames: {
12+
"#pk": "Artist",
13+
"#yr": "Year",
14+
},
15+
ExpressionAttributeValues: {
16+
":pk": "Michael Jackson",
17+
":yr": 2012,
18+
},
19+
FilterExpression: "#yr = :yr",
20+
KeyConditionExpression: "#pk = :pk",
21+
};
2122

22-
console.log(`Query response: ${JSON.stringify(response, null, 2)}`);
23+
try {
24+
// Send the query command
25+
const response = await docClient.send(new QueryCommand(params));
26+
27+
// Check if there are more results to retrieve
28+
if (response.LastEvaluatedKey) {
29+
console.log(
30+
`Not all items have been retrieved by this query. At least one another request is required to get all available items. The last evaluated key corresponds to ${JSON.stringify(response.LastEvaluatedKey)}.`
31+
);
32+
}
33+
34+
return response;
35+
} catch (error) {
36+
console.error("Error querying DynamoDB:", error);
37+
throw error;
38+
}
2339
};
2440

25-
query().catch((error) => console.error(JSON.stringify(error, null, 2)));
41+
query()
42+
.then((response) => console.log(`Query response: ${JSON.stringify(response, null, 2)}`))
43+
.catch((error) => console.error(JSON.stringify(error, null, 2)));
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,46 @@
1-
const AWS = require('aws-sdk');
1+
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
2+
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";
23

3-
AWS.config.update({ region: "us-west-2" });
4-
5-
const documentClient = new AWS.DynamoDB.DocumentClient();
4+
const client = new DynamoDBClient({ region: "us-west-2" });
5+
const docClient = DynamoDBDocumentClient.from(client);
66

77
const query = async () => {
8-
try {
9-
const q = {
10-
TableName: 'ParcelTracker',
11-
"ScanIndexForward": false,
12-
"Limit": "1",
13-
KeyConditionExpression: '#id = :id and begins_with(#dt, :dt)',
14-
ExpressionAttributeNames: {
15-
'#id': 'TrackID',
16-
'#dt': 'ObjType',
17-
},
18-
ExpressionAttributeValues: {
19-
':id': "34234ZX89734292834845234",
20-
':dt': "package::tracking::status",
21-
},
22-
};
23-
24-
const response = await documentClient.query(q).promise();
25-
26-
/* In this query's case, we only want one item, but there are likely more in
27-
the table. If we care, we'd uncomment this and be able to grab the remaining
28-
items in the item collection.
29-
30-
if (response.LastEvaluatedKey) {
31-
const message = `
32-
Not all items with this TrackID have been retrieved by this query.
33-
At least one other request is required to get all available items.
34-
The last evaluated key corresponds to:
35-
${JSON.stringify(response.LastEvaluatedKey)}.
36-
`.replace(/\s+/gm, ' ');
37-
console.log(message);
38-
}*/
39-
40-
return response;
41-
} catch (error) {
42-
throw new Error(JSON.stringify(error, null, 2));
43-
}
8+
// Define query parameters
9+
const params = {
10+
TableName: "ParcelTracker",
11+
ScanIndexForward: false,
12+
Limit: 1,
13+
KeyConditionExpression: "#id = :id and begins_with(#dt, :dt)",
14+
KeyConditionExpression: "#pk = :pk",
15+
ExpressionAttributeNames: {
16+
"#id": "TrackID",
17+
"#dt": "ObjType",
18+
},
19+
ExpressionAttributeValues: {
20+
":id": "34234ZX89734292834845234",
21+
":dt": "package::tracking::status",
22+
},
23+
};
24+
25+
try {
26+
const response = await docClient.send(new QueryCommand(params));
27+
28+
// In this query's case, we only want one item, but there are likely more in
29+
// the table. If we care, we'd uncomment this and be able to grab the remaining
30+
// items in the item collection.
31+
// // if (response.LastEvaluatedKey) {
32+
// // console.log(
33+
// // `Not all items have been retrieved by this query. At least one another request is required to get all available items. The last evaluated key corresponds to ${JSON.stringify(response.LastEvaluatedKey)}.`
34+
// // );
35+
// // }
36+
37+
return response;
38+
} catch (error) {
39+
console.error("Error querying DynamoDB:", error);
40+
throw error;
41+
}
4442
};
4543

46-
(async () => {
47-
try {
48-
const data = await query();
49-
console.log(JSON.stringify(data.Items[0], null, 2));
50-
} catch (error) {
51-
console.error(error);
52-
}
53-
})();
44+
query()
45+
.then((response) => console.log(`Query response: ${JSON.stringify(response, null, 2)}`))
46+
.catch((error) => console.error(JSON.stringify(error, null, 2)));
Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,39 @@
1-
const AWS = require('aws-sdk');
1+
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
2+
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";
23

3-
AWS.config.update({ region: "us-west-2" });
4-
5-
const documentClient = new AWS.DynamoDB.DocumentClient();
4+
const client = new DynamoDBClient({ region: "us-west-2" });
5+
const docClient = DynamoDBDocumentClient.from(client);
66

77
const query = async () => {
8-
try {
9-
const q = {
10-
TableName: 'Reply',
11-
KeyConditionExpression: '#id = :id and begins_with(#dt, :dt)',
12-
ExpressionAttributeNames: {
13-
'#id': 'Id',
14-
'#dt': 'ReplyDateTime',
15-
},
16-
ExpressionAttributeValues: {
17-
':id': "Amazon DynamoDB#DynamoDB Thread 1",
18-
':dt': "2015-09",
19-
},
20-
};
21-
22-
const response = await documentClient.query(q).promise();
23-
24-
if (response.LastEvaluatedKey) {
25-
const message = `
26-
Not all items have been retrieved by this query.
27-
At least one another request is required to get all available items.
28-
The last evaluated key corresponds to:
29-
${JSON.stringify(response.LastEvaluatedKey)}.
30-
`.replace(/\s+/gm, ' ');
31-
32-
console.log(message);
33-
}
34-
35-
return response;
36-
} catch (error) {
37-
throw new Error(JSON.stringify(error, null, 2));
38-
}
8+
const params = {
9+
TableName: "Reply",
10+
KeyConditionExpression: "#id = :id and begins_with(#dt, :dt)",
11+
ExpressionAttributeNames: {
12+
"#id": "Id",
13+
"#dt": "ReplyDateTime",
14+
},
15+
ExpressionAttributeValues: {
16+
":id": "Amazon DynamoDB#DynamoDB Thread 1",
17+
":dt": "2015-09",
18+
},
19+
};
20+
21+
try {
22+
const response = await docClient.send(new QueryCommand(params));
23+
24+
// Check if there are more results to retrieve
25+
if (response.LastEvaluatedKey) {
26+
console.log(
27+
`Not all items have been retrieved by this query. At least one another request is required to get all available items. The last evaluated key corresponds to ${JSON.stringify(response.LastEvaluatedKey)}.`
28+
);
29+
}
30+
return response;
31+
} catch (error) {
32+
console.error("Error querying DynamoDB:", error);
33+
throw error;
34+
}
3935
};
4036

41-
(async () => {
42-
try {
43-
const data = await query();
44-
console.log("Query succeeded:", JSON.stringify(data, null, 2));
45-
} catch (error) {
46-
console.error(error);
47-
}
48-
})();
37+
query()
38+
.then((response) => console.log(`Query response: ${JSON.stringify(response, null, 2)}`))
39+
.catch((error) => console.error(JSON.stringify(error, null, 2)));
Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,42 @@
1-
const AWS = require('aws-sdk');
1+
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
2+
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";
23

3-
AWS.config.update({ region: "us-west-2" });
4-
5-
const documentClient = new AWS.DynamoDB.DocumentClient();
4+
const client = new DynamoDBClient({ region: "us-west-2" });
5+
const docClient = DynamoDBDocumentClient.from(client);
66

77
const query = async () => {
8-
try {
9-
const q = {
10-
TableName: 'Reply',
11-
KeyConditionExpression: '#id = :id AND #dt BETWEEN :start AND :end',
12-
ExpressionAttributeNames: {
13-
'#id': 'Id',
14-
'#dt': 'ReplyDateTime',
15-
},
16-
ExpressionAttributeValues: {
17-
':id': "Amazon DynamoDB#DynamoDB Thread 2",
18-
':start': "2015-09-29T19:58:22.947Z",
19-
':end': "2015-10-05T19:58:22.947Z",
20-
},
21-
};
22-
23-
const response = await documentClient.query(q).promise();
24-
25-
if (response.LastEvaluatedKey) {
26-
const message = `
27-
Not all items have been retrieved by this query.
28-
At least one another request is required to get all available items.
29-
The last evaluated key corresponds to:
30-
${JSON.stringify(response.LastEvaluatedKey)}.
31-
`.replace(/\s+/gm, ' ');
32-
33-
console.log(message);
34-
}
35-
36-
return response;
37-
} catch (error) {
38-
throw new Error(JSON.stringify(error, null, 2));
39-
}
8+
const params = {
9+
TableName: "Reply",
10+
KeyConditionExpression: "#id = :id AND #dt BETWEEN :start AND :end",
11+
ExpressionAttributeNames: {
12+
"#id": "Id",
13+
"#dt": "ReplyDateTime",
14+
},
15+
ExpressionAttributeValues: {
16+
":id": "Amazon DynamoDB#DynamoDB Thread 2",
17+
":start": "2015-09-29T19:58:22.947Z",
18+
":end": "2015-10-05T19:58:22.947Z",
19+
},
20+
};
21+
22+
try {
23+
// Send the query command
24+
const response = await docClient.send(new QueryCommand(params));
25+
26+
// Check if there are more results to retrieve
27+
if (response.LastEvaluatedKey) {
28+
console.log(
29+
`Not all items have been retrieved by this query. At least one another request is required to get all available items. The last evaluated key corresponds to ${JSON.stringify(response.LastEvaluatedKey)}.`
30+
);
31+
}
32+
33+
return response;
34+
} catch (error) {
35+
console.error("Error querying DynamoDB:", error);
36+
throw error;
37+
}
4038
};
4139

42-
(async () => {
43-
try {
44-
const data = await query();
45-
console.log("Query succeeded:", JSON.stringify(data, null, 2));
46-
} catch (error) {
47-
console.error(error);
48-
}
49-
})();
40+
query()
41+
.then((response) => console.log(`Query response: ${JSON.stringify(response, null, 2)}`))
42+
.catch((error) => console.error(JSON.stringify(error, null, 2)));

0 commit comments

Comments
 (0)