Skip to content

Instantly share code, notes, and snippets.

@id0Sch
Last active August 29, 2015 14:19
Show Gist options
  • Save id0Sch/ab66b6af429ce49439bf to your computer and use it in GitHub Desktop.
Save id0Sch/ab66b6af429ce49439bf to your computer and use it in GitHub Desktop.
dynamodb-doc wrapper functions
/**
* Created by zeev manilovich and ido schachter on 4/16/15.
*/
var aws = require('aws-sdk'),
Promise = require('bluebird'),
logger = require("./logger"),
config = require('../../config'),
doc = require('dynamodb-doc');
aws.config.update({
region : config.region,
endpoint: new aws.Endpoint(process.env.ENDPOINT)
});
var docClient = new doc.DynamoDB();
Promise.promisifyAll(Object.getPrototypeOf(docClient));
function deleteTable(tableName) {
return Promise.resolve(docClient.describeTableAsync({TableName: tableName})
.then(function (data) {
logger.debug('Attempting to delete table %s', tableName);
return docClient.deleteTableAsync({TableName: tableName})
})
.then(function () {
logger.debug("Deleted table %s", tableName);
})
.catch(function (err) {
logger.debug("Table doesn't exist");
})
)
}
function createTable(tableName) {
return deleteTable(tableName)
.then(function () {
logger.debug('Creating table %s', tableName);
return docClient.createTableAsync({
TableName : tableName,
ProvisionedThroughput: {
ReadCapacityUnits : 1,
WriteCapacityUnits: 1
},
KeySchema : [
{AttributeName: 'id', KeyType: 'HASH'}
],
AttributeDefinitions : [
{AttributeName: 'id', AttributeType: 'S'}
]
})
}).catch(function (err) {
logger.error(err);
});
}
function populateTable(tableName, data) {
logger.debug('populating table %s', tableName);
return Promise.map(data, function (item) {
return docClient.putItemAsync({
TableName: tableName,
Item : item
}).catch(function(err){
logger.debug(tableName, item);
logger.error(err);
});
})
}
module.exports = {
delete : deleteTable,
create : createTable,
populate: populateTable
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment