Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
const AWS = require("aws-sdk");
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";

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

const query = async () => {
const response = await documentClient
.query({
TableName: "Music",
KeyConditionExpression: "#pk = :pk",
ExpressionAttributeNames: {
"#pk": "Artist",
},
ExpressionAttributeValues: {
":pk": "Michael Jackson",
},
ConsistentRead: true,
})
.promise();
// Define query parameters
const params = {
TableName: "Music",
KeyConditionExpression: "#pk = :pk",
ExpressionAttributeNames: {
"#pk": "Artist",
},
ExpressionAttributeValues: {
":pk": "Michael Jackson", // No need for { S: "Michael Jackson" }
},
ConsistentRead: true,
};

if (response.LastEvaluatedKey) {
console.log(
`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
)}.`
);
}
try {
// Send the query command
const response = await docClient.send(new QueryCommand(params));

// Check if there are more results to retrieve
if (response.LastEvaluatedKey) {
console.log(
`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)}.`
);
}

console.log(`Query response: ${JSON.stringify(response, null, 2)}`);
return response;
} catch (error) {
console.error("Error querying DynamoDB:", error);
throw error;
}
};

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

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

const query = async () => {
const response = await documentClient
.query({
TableName: "Music",
ExpressionAttributeNames: {
"#pk": "Artist",
"#yr": "Year",
},
ExpressionAttributeValues: {
":pk": "Michael Jackson",
":yr": 2012,
},
FilterExpression: "#yr = :yr",
KeyConditionExpression: "#pk = :pk",
})
.promise();
// Define query parameters
const params = {
TableName: "Music",
ExpressionAttributeNames: {
"#pk": "Artist",
"#yr": "Year",
},
ExpressionAttributeValues: {
":pk": "Michael Jackson",
":yr": 2012,
},
FilterExpression: "#yr = :yr",
KeyConditionExpression: "#pk = :pk",
};

console.log(`Query response: ${JSON.stringify(response, null, 2)}`);
try {
// Send the query command
const response = await docClient.send(new QueryCommand(params));

// Check if there are more results to retrieve
if (response.LastEvaluatedKey) {
console.log(
`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)}.`
);
}

return response;
} catch (error) {
console.error("Error querying DynamoDB:", error);
throw error;
}
};

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

AWS.config.update({ region: "us-west-2" });

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

const query = async () => {
try {
const q = {
TableName: 'ParcelTracker',
"ScanIndexForward": false,
"Limit": "1",
KeyConditionExpression: '#id = :id and begins_with(#dt, :dt)',
ExpressionAttributeNames: {
'#id': 'TrackID',
'#dt': 'ObjType',
},
ExpressionAttributeValues: {
':id': "34234ZX89734292834845234",
':dt': "package::tracking::status",
},
};

const response = await documentClient.query(q).promise();

/* In this query's case, we only want one item, but there are likely more in
the table. If we care, we'd uncomment this and be able to grab the remaining
items in the item collection.

if (response.LastEvaluatedKey) {
const message = `
Not all items with this TrackID have been retrieved by this query.
At least one other request is required to get all available items.
The last evaluated key corresponds to:
${JSON.stringify(response.LastEvaluatedKey)}.
`.replace(/\s+/gm, ' ');
console.log(message);
}*/

return response;
} catch (error) {
throw new Error(JSON.stringify(error, null, 2));
}
// Define query parameters
const params = {
TableName: "ParcelTracker",
ScanIndexForward: false,
Limit: 1,
KeyConditionExpression: "#id = :id and begins_with(#dt, :dt)",
KeyConditionExpression: "#pk = :pk",
ExpressionAttributeNames: {
"#id": "TrackID",
"#dt": "ObjType",
},
ExpressionAttributeValues: {
":id": "34234ZX89734292834845234",
":dt": "package::tracking::status",
},
};

try {
const response = await docClient.send(new QueryCommand(params));

// In this query's case, we only want one item, but there are likely more in
// the table. If we care, we'd uncomment this and be able to grab the remaining
// items in the item collection.
// // if (response.LastEvaluatedKey) {
// // console.log(
// // `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)}.`
// // );
// // }

return response;
} catch (error) {
console.error("Error querying DynamoDB:", error);
throw error;
}
};

(async () => {
try {
const data = await query();
console.log(JSON.stringify(data.Items[0], null, 2));
} catch (error) {
console.error(error);
}
})();
query()
.then((response) => console.log(`Query response: ${JSON.stringify(response, null, 2)}`))
.catch((error) => console.error(JSON.stringify(error, null, 2)));
Original file line number Diff line number Diff line change
@@ -1,48 +1,39 @@
const AWS = require('aws-sdk');
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";

AWS.config.update({ region: "us-west-2" });

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

const query = async () => {
try {
const q = {
TableName: 'Reply',
KeyConditionExpression: '#id = :id and begins_with(#dt, :dt)',
ExpressionAttributeNames: {
'#id': 'Id',
'#dt': 'ReplyDateTime',
},
ExpressionAttributeValues: {
':id': "Amazon DynamoDB#DynamoDB Thread 1",
':dt': "2015-09",
},
};

const response = await documentClient.query(q).promise();

if (response.LastEvaluatedKey) {
const message = `
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)}.
`.replace(/\s+/gm, ' ');

console.log(message);
}

return response;
} catch (error) {
throw new Error(JSON.stringify(error, null, 2));
}
const params = {
TableName: "Reply",
KeyConditionExpression: "#id = :id and begins_with(#dt, :dt)",
ExpressionAttributeNames: {
"#id": "Id",
"#dt": "ReplyDateTime",
},
ExpressionAttributeValues: {
":id": "Amazon DynamoDB#DynamoDB Thread 1",
":dt": "2015-09",
},
};

try {
const response = await docClient.send(new QueryCommand(params));

// Check if there are more results to retrieve
if (response.LastEvaluatedKey) {
console.log(
`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)}.`
);
}
return response;
} catch (error) {
console.error("Error querying DynamoDB:", error);
throw error;
}
};

(async () => {
try {
const data = await query();
console.log("Query succeeded:", JSON.stringify(data, null, 2));
} catch (error) {
console.error(error);
}
})();
query()
.then((response) => console.log(`Query response: ${JSON.stringify(response, null, 2)}`))
.catch((error) => console.error(JSON.stringify(error, null, 2)));
Original file line number Diff line number Diff line change
@@ -1,49 +1,42 @@
const AWS = require('aws-sdk');
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";

AWS.config.update({ region: "us-west-2" });

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

const query = async () => {
try {
const q = {
TableName: 'Reply',
KeyConditionExpression: '#id = :id AND #dt BETWEEN :start AND :end',
ExpressionAttributeNames: {
'#id': 'Id',
'#dt': 'ReplyDateTime',
},
ExpressionAttributeValues: {
':id': "Amazon DynamoDB#DynamoDB Thread 2",
':start': "2015-09-29T19:58:22.947Z",
':end': "2015-10-05T19:58:22.947Z",
},
};

const response = await documentClient.query(q).promise();

if (response.LastEvaluatedKey) {
const message = `
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)}.
`.replace(/\s+/gm, ' ');

console.log(message);
}

return response;
} catch (error) {
throw new Error(JSON.stringify(error, null, 2));
}
const params = {
TableName: "Reply",
KeyConditionExpression: "#id = :id AND #dt BETWEEN :start AND :end",
ExpressionAttributeNames: {
"#id": "Id",
"#dt": "ReplyDateTime",
},
ExpressionAttributeValues: {
":id": "Amazon DynamoDB#DynamoDB Thread 2",
":start": "2015-09-29T19:58:22.947Z",
":end": "2015-10-05T19:58:22.947Z",
},
};

try {
// Send the query command
const response = await docClient.send(new QueryCommand(params));

// Check if there are more results to retrieve
if (response.LastEvaluatedKey) {
console.log(
`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)}.`
);
}

return response;
} catch (error) {
console.error("Error querying DynamoDB:", error);
throw error;
}
};

(async () => {
try {
const data = await query();
console.log("Query succeeded:", JSON.stringify(data, null, 2));
} catch (error) {
console.error(error);
}
})();
query()
.then((response) => console.log(`Query response: ${JSON.stringify(response, null, 2)}`))
.catch((error) => console.error(JSON.stringify(error, null, 2)));
Loading