yz10
yz10

Reputation: 731

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
    }
});

Upvotes: 25

Views: 58616

Answers (5)

Nate-Wilkins
Nate-Wilkins

Reputation: 5492

Using the aws-sdk you would use something like the following:

import { DynamoDB } from 'aws-sdk';

const dynamo = new DynamoDB.DocumentClient();

const main = async () => {
    const results = await dynamo
        .put({
            TableName: 'people',
            Item: {
              dateCreated: { S: new Date().toUTCString() },
              name: { S: name },
            },
        })
        .promise();

    console.log(JSON.stringify(results));
};

main();

Upvotes: 1

maithri vemula
maithri vemula

Reputation: 33

I would suggest using documentClient as it makes it easier to read and write data in dynamoDb. Also using conditional putItem will make sure the item is unique and doesn't overwrite existing item. "attribute_not_exists" checks if the userId in the example doesn't exist. If userId exists, it throws an error. Hope it's not too late :P

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

var item = {
"userId" : {"N":"12345678"},
"name":{"S":"Bob"}
}

var dbParam= {
TableName: tableName,
Item:item,
ConditionExpression: 'attribute_not_exists(#u) or #u = :userId',
ExpressionAttributeNames: { "#u" : "userId"}
}

dynamodb.putItem(dbParam, function(err,data) {
if(err){
console.log("err",err);
}
else{
console.log("data",data)
}
});

Upvotes: 2

Anki007
Anki007

Reputation: 411

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 .................."); 

Upvotes: 36

Ryan Zhou
Ryan Zhou

Reputation: 126

I don't think muhqu's answer works, I believe the value of the attribute has to be a string.

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

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#putItem-property

Upvotes: 3

muhqu
muhqu

Reputation: 12809

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.

Upvotes: 5

Related Questions