I understand that to put an item into a Dynamodb table it has to be in a structure like this but is there an option to be able to input a standard Javascript object without this special structure? If not, is there an existing function in the AWS SDK that would convert my Javascript object into one with this special structure for Dynamodb?
var params = {
Item: {
"AlbumTitle": {
S: "Somewhat Famous"
},
"Artist": {
S: "No One You Know"
},
"SongTitle": {
S: "Call Me Today"
}
},
ReturnConsumedCapacity: "TOTAL",
TableName: "Music"
};
The AWS SDK does provide a few ways to do this.
The one I am familiar with is the AWS.DynamoDB.Converter
. It can be used like so
const AWS = require("aws-sdk");
let record = {
"AlbumTitle": "Somewhat Famous",
"Artist": "No One You Know",
"SongTitle": "Call Me Today"
}
// Your DynamoDB representation
let ddbRecord = AWS.DynamoDB.Converter.marshall(record)
/* ddbRecord is now
{
AlbumTitle: { S: 'Somewhat Famous' },
Artist: { S: 'No One You Know' },
SongTitle: { S: 'Call Me Today' }
}
*/
To inverse the operation you would use the unmarshall
function. See https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/Converter.html
Alternatively there is a DocumentClient
available but I havent used it. The documentation on it is quite good https://aws.amazon.com/blogs/developer/announcing-the-amazon-dynamodb-document-client-in-the-aws-sdk-for-javascript/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With