Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamodb node aws-sdk simple getItem() call

Folks, New to Javascript... trying to do simple dynamo queries from node:

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

var params = {
"TableName" : 'admins',
"Key" : [
      { "username" : { "S" : "foo" } },
    ],
}

 db.getItem(params, function(err, data) {
    console.log('error: '+ err);
    console.log(data);
    return next();
    res.send(data);
  });
 }

Output:

error: UnexpectedParameter: Unexpected key 'username' found in params.Key['0']

Thanks! Any help would be greatly appreciated!

like image 307
Cmag Avatar asked Sep 29 '13 03:09

Cmag


People also ask

What is GetItem in DynamoDB?

The GetItem operation returns a set of attributes for the item with the given primary key. If there is no matching item, GetItem does not return any data and there will be no Item element in the response. GetItem provides an eventually consistent read by default.

How can you get a single item in DynamoDB?

To access DynamoDB, create an AWS. DynamoDB service object. To identify the item to get, you must provide the value of the primary key for that item in the table. By default, the getItem method returns all the attribute values defined for the item.

What is the difference between GetItem and query in DynamoDB?

There is no performance difference between the two. The hash calculation in both the queries are done 1 by 1. The latter, i.e., get item is just provided as an analogy to the JPA repository/spring findOne/findById to make wiring in Spring Bean wiring/ Hibernate configs easier.


2 Answers

Must follow the SDK and Docs, its simple: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html

var params = {
    AttributesToGet: [
      "password"
    ],
    TableName : 'foo',
    Key : { 
      "username" : {
        "S" : "bar"
      }
    }
  }

  db.getItem(params, function(err, data) {
    if (err) {
      console.log(err); // an error occurred
      } 
    else {
      console.log(data); // successful response
      res.send(data);
      }
    return next();
  });
like image 198
Cmag Avatar answered Sep 21 '22 19:09

Cmag


I was trying to do it as it was suggested in the documentation, but also got errors.

At the end the following worked:

var aws = require('aws-sdk');
var db = new aws.DynamoDB({
  region: 'eu-central-1',
  maxRetries: 1
});

exports.handler = event => {
    return queryMyThings();
}

const queryMyThings = async (event) => { 
 var params = {
      Key: {
      "ColumnByWhichYouSearch": {
         S: "ValueThatYouAreQueriing"
        }
      }, 
      TableName: "YourTableName"
     };

    return await db.getItem(params).promise();

}
like image 27
Diana Avatar answered Sep 21 '22 19:09

Diana