@incdevco/framework
Version:
node.js lambda framework
265 lines (144 loc) • 4.97 kB
JavaScript
var AWS = require('aws-sdk');
function DynamoDB(config) {
'use strict';
config = config || {};
this.console = config.console || console;
this.ddb = config.ddb
|| new AWS.DynamoDB.DocumentClient();
this.debug = config.debug || false;
}
DynamoDB.prototype.delete = function (params) {
'use strict';
var self = this;
this.log('dynamodb.delete', JSON.stringify(params, null, 2));
return this.ddb.delete(params).promise()
.then(function (result) {
self.log('result', JSON.stringify(result, null, 2));
return result;
}, function (exception) {
self.log('exception', exception.message, JSON.stringify(exception.stack, null, 2));
throw exception;
});
};
DynamoDB.prototype.get = function (params) {
'use strict';
var self = this;
this.log('dynamodb.get', JSON.stringify(params, null, 2));
return this.ddb.get(params).promise()
.then(function (result) {
self.log('result', JSON.stringify(result, null, 2));
return result;
}, function (exception) {
self.log('exception', exception.message, JSON.stringify(exception.stack, null, 2));
throw exception;
});
};
DynamoDB.prototype.ignoreConditionalException = function (exception) {
'use strict';
this.log('ignoreConditionalException', exception);
if (this.isConditionalException(exception)) {
this.log('Conditional Exception Received', exception.message);
return false;
}
throw exception;
};
DynamoDB.prototype.isConditionalException = function (exception) {
'use strict';
if (exception.code === 'ConditionalCheckFailedException'
|| exception.errorType === 'ConditionalCheckFailedException') {
return true;
}
return false;
};
DynamoDB.prototype.log = function () {
'use strict';
if (this.debug) {
this.console.log.apply(this.console, arguments);
}
return true;
};
DynamoDB.prototype.put = function (params) {
'use strict';
var self = this;
this.log('dynamodb.put', JSON.stringify(params, null, 2));
return this.ddb.put(params).promise()
.then(function (result) {
self.log('result', JSON.stringify(result, null, 2));
return result;
}, function (exception) {
self.log('exception', exception.message, JSON.stringify(exception.stack, null, 2));
throw exception;
});
};
DynamoDB.prototype.query = function (params) {
'use strict';
var self = this;
this.log('dynamodb.query', JSON.stringify(params, null, 2));
return this.ddb.query(params).promise()
.then(function (result) {
self.log('result', JSON.stringify(result, null, 2));
return result;
}, function (exception) {
self.log('exception', exception.message, JSON.stringify(exception.stack, null, 2));
throw exception;
});
};
DynamoDB.prototype.queryThemAll = function (params, result) {
'use strict';
var self = this;
result = result || {};
result.Items = result.Items || [];
return this.query(params)
.then(function (current) {
result.Items = result.Items.concat(current.Items);
if (current.LastEvaluatedKey) {
params.ExclusiveStartKey = current.LastEvaluatedKey;
return self.queryThemAll(params, result);
} else {
return result;
}
});
};
DynamoDB.prototype.scan = function (params) {
'use strict';
var self = this;
this.log('dynamodb.scan', JSON.stringify(params, null, 2));
return this.ddb.scan(params).promise()
.then(function (result) {
self.log('result', JSON.stringify(result, null, 2));
return result;
}, function (exception) {
self.log('exception', exception.message, JSON.stringify(exception.stack, null, 2));
throw exception;
});
};
DynamoDB.prototype.scanThemAll = function (params, result) {
'use strict';
var self = this;
result = result || {};
result.Items = result.Items || [];
return this.scan(params)
.then(function (current) {
result.Items = result.Items.concat(current.Items);
if (current.LastEvaluatedKey) {
params.ExclusiveStartKey = current.LastEvaluatedKey;
return self.scanThemAll(params, result);
} else {
return result;
}
});
};
DynamoDB.prototype.update = function (params) {
'use strict';
var self = this;
this.log('dynamodb.update', JSON.stringify(params, null, 2));
return this.ddb.update(params).promise()
.then(function (result) {
self.log('result', JSON.stringify(result, null, 2));
return result;
}, function (exception) {
self.log('exception', exception.message, JSON.stringify(exception.stack, null, 2));
throw exception;
});
};
module.exports = DynamoDB;