@dotconnor/ddb
A DynamoDB Wrapper
Install
To install first run this command in your project's main folder:
npm install @dotconnor/ddb --save
Then in your project include this at the top:
const ddb = require("@dotconnor/ddb");
Initialization
Initializing ddb is very easy. If AWS Credentials are already on file then all you need to do is:
const db = new ddb();
For a profile other than default:
const db = new ddb({profile: "work"});
Or can you include your Access Key ID and Secret Access Key:
const db = new ddb({
accessKeyId: "...",
secretAccessKey: "...",
region: "us-west-2" //Defaults to us-east-1
});
Scanning A Table
ddb provides the scanBuilder class to help scan a table.
var scan = db.scan("TableName");
/*
scan = {
params: {
TableName: 'TableName',
FilterExpression: ''
},
_db: [Object]
}
*/
This returns a scanBuilder the the following methods:
addFilter(name, value, op)
scan = scan.addFilter("id", 123, "=");
/*
scan = {
params: {
TableName:"TableName",
FilterExpression:"id = :id",
ExpressionAttributeValues:{
":id":{
"N":"123"
}
}
},
_db: [Object]
}
*/
Get The Results
Calling the execute() method on the scanBuilder will return a Promise containing the items matching the given filters.
scan
.execute()
.then(function(data) {
console.log(data); //Array of items found. [] If none were found.
}).catch(function(err){
console.log(err):
});
Updating An Item
(WIP)
ddb provides the updateBuilder class to help update an item.
var update = db.update("TableName");
Set the key name and its value of the item that need to be updated.
update.setKey("uuid", "a7d6as-528ad-1471");
Add the key and their values that need to be updated.
update.updateItem("email", "connor@dotconnor.com");
Increment an item:
update.incrementItem("karma", 1);
Update the selected item(s)
update.execute()
.then(function(data) {
console.log(data); // data should be = []
}).catch(function(err){
console.log(err):
});
Put Item
putItem(TableName, Item)
db.putItem("Users", {
id: "1452a",
username: "dotconnor",
}).then(function(){
console.log("User Created");
});
Generate An ID
generateId(TableName, ItemName, Type, opts)
db.generateId("Users", "uuid", "uuid")
.then(function(id){
console.log(id);
});