Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put item on DynamoDB table using AWS SDK for Node.js

I'm new to javascript and node.js and was wondering if someone can help me figure out the syntax of putting a new item onto an existing table on AWS Dynamodb through their node.js SDK. Here's what I have so far. Is there an example for what I'm trying to do? If anyone could point me in the right direction, it'd be much appreciated.

var AWS = require('aws-sdk');
AWS.config.loadFromPath('./config.json');
AWS.config.update({region: 'us-east-1'});
var dynamodb = new AWS.DynamoDB();

var item = {
    // I need to put the an item with a the primary key of "id", and an attribute called "item"
    // I'm new to js and node.js, so if somebody could help me understand the documentation
    // http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/frames.html#!http%3A//docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB_20120810.html
}

dynamodb.putItem({TableName: 'log_dev', Item: item}, function(err, data){
    if (err) {
    console.log(err); // an error occurred
    } else {
    console.log(data); // successful response
    }
});
like image 203
yz10 Avatar asked Jul 20 '13 23:07

yz10


People also ask

How do I access DynamoDB from AWS SDK?

To access DynamoDB, create an AWS. DynamoDB service object. Create a JSON object containing the parameters needed to add an item, which in this example includes the name of the table and a map that defines the attributes to set and the values for each attribute. Call the putItem method of the DynamoDB service object.


2 Answers

dynamoDB.putItem(
{
    "TableName": "Table1",
    "Item": {
        "Color": {"S": "white"},
        "Name": {"S": "fancy vase"},
        "Weight": {"N": "2"},
        "LastName":{"S": "Kumar"}
    }
}, function(result) {
    result.on('data', function(chunk) {
        console.log("" + chunk);
    });
});
console.log("Items are succesfully ingested in table .................."); 
like image 88
Anki007 Avatar answered Oct 13 '22 23:10

Anki007


I expect your "id" to be numeric...

var item = {
    "id": {"N": 1234},
    "title": {"S": "Foobar"}
}

Note that with DynamoDB you specify the data type (N » numeric, S » string, B » binary) at table creation, only for the primary key (HashKey or HashKey+RangeKey). All other columns are allowed to vary in their data type, and can be seen as key-value pairs. So it is essential for DynamoDB to always encode the data type with the item attributes.

like image 5
muhqu Avatar answered Oct 13 '22 23:10

muhqu