1.0.11 • Published 8 years ago
dynamodb-helper v1.0.11
dynamodb-helper
Installation
npm install dynamodb-helperAPI
var dbHelper = require('dynamodb-helper')dbHelper(docClient,tableName)
Create a new dbHelper object for the given docClient and tableName.
.find(queryHashKey, callback)
Get items by queryHashKey from the particular table and process callback function.
.putItem(item)
put item on the particular table
.list(callback)
Get all items from the particular table and process callback function.
Examples
Simple type negotiation
This simple example shows how to use dynamodb-helper to get items from dynamodb and upload items to dynamodb.
First we have to require aws-sdk and then configure it by config.json like
{
"accessKeyId": "AKIAJVsdfDSDSsA",
"secretAccessKey": "Dsy3GADSASDDFDFzwIx+HJY2jqvjq",
"region": "us-east-1"
}After that, we create the object of the DocumentClient of Dynamodb and inject it into dbHelper.
/* Initialize */
var AWS = require("aws-sdk");
AWS.config.loadFromPath('./config.json');
var docClient = new AWS.DynamoDB.DocumentClient({ apiVersion: '2012-08-10' });
var dbHelper = require('dynamodb-helper');
var Users = new dbHelper(docClient, 'Users');
/* Find Data */
Users.find(req.user.id, function (err,data) {
console.log(data);
if(err){
console.log("Error occured");
return new Error(err);
}
//Do something
})
/* Upload Data */
Users.putItem( {
id: req.user.id,
devs:[
{
sn: req.body.sn,
name: req.body.name
}
]
});
/* Find all Data */
Users.list(function(err,data){
if (err){
console.log(err);
}
console.log(data);
});