Reputation: 433
I'm currently trying to return all the attributes of an entry based off the primary key in Lambda, but I don't want to type out every single attribute to get. Is there an optimized way to do this instead of typing out every single one?
Here is my lambda function:
exports.handler = async function(event, ctx, callback) {
var data;
var params = {
Key: {
key: event.key
},
TableName : 'app',
AttributesToGet: [
'email',
'lastName',
'firstName',
'...nextAttribute',
'...nextAttribute'
],
};
try {
data = await dynamoDb.get(params).promise()
console.log(data);
}
catch (err) {
console.log(err);
}
return data.Item;
}
So in this example, instead of return ...nextAttribute
is there a way to return all the attributes?
Upvotes: 0
Views: 979
Reputation: 33779
You do not have to specify AttributsToGet
. If you omit it, you will get all attributes by default, e.g.
exports.handler = async function(event, ctx, callback) {
const params = {
Key: {
key: event.key
},
TableName : 'app',
};
try {
const data = await dynamoDb.get(params).promise();
console.log(data);
return data.Item;
}
catch (err) {
console.log(err);
return undefined;
}
}
See details in the AWS JavaScript API, specifically the getItem() function of DynamoDB.
Alternatively, you may find the DynamoDB DocumentClient interesting. Copied from the get() documentation:
var params = {
TableName : 'Table',
Key: {
HashKey: 'hashkey'
}
};
var documentClient = new AWS.DynamoDB.DocumentClient();
documentClient.get(params, function(err, data) {
if (err) console.log(err);
else console.log(data);
});
Side note, AttributesToGet
is a legacy parameter according to the documentation. Consider using ProjectionExpression
instead if you change your mind and would like to be explicit about which attributes to fetch.
Upvotes: 5