Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How store an object in Dynamodb?

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"
 };
like image 279
Berry Blue Avatar asked Sep 20 '19 22:09

Berry Blue


1 Answers

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/

like image 165
ug_ Avatar answered Oct 01 '22 16:10

ug_