# @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:
```javascript
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:
```javascript
const db = new ddb();
```
For a profile other than default:
```javascript
const db = new ddb({profile: "work"});
```
Or can you include your Access Key ID and Secret Access Key:
```javascript
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.
```javascript
var scan = db.scan("TableName");
/*
scan = { 
    params: { 
        TableName: 'TableName', 
        FilterExpression: '' 
    },
    _db: [Object] 
}
*/
```
This returns a scanBuilder the the following methods:
<br>
**addFilter(name, value, op)**
```javascript
scan = scan.addFilter("id", 123, "=");
/*
scan = {
    params: {
        TableName:"TableName",
        FilterExpression:"id = :id",
        ExpressionAttributeValues:{
            ":id":{
                "N":"123"
            }
        }
    },
    _db: [Object] 
}
*/
```
For more granular controls:
<br>
**addName(alias,name)**
<br>
**addValue(alias,value)**
<br>
**setFilter(filter)**
```javascript
scan.addName("u", "uuid");
scan.addValue("u", "a7d6as-528ad-1471");
scan.setFilter("#u = :u");
```
### Get The Results
Calling the execute() method on the scanBuilder will return a Promise containing the items matching the given filters.
```javascript
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.
```javascript
var update = db.update("TableName");
```
Set the key name and its value of the item that need to be updated.
```javascript
update.setKey("uuid", "a7d6as-528ad-1471");
```
Add the key and their values that need to be updated.
```javascript
update.updateItem("email", "connor@dotconnor.com");
```
Update the selected item(s)
```javascript
update
.execute()
.then(function(data) {
    console.log(data); //Array of items updated. [] if none were updated.
}).catch(function(err){
    console.log(err):
});
```