Skip to content

Instantly share code, notes, and snippets.

@setou
Created September 13, 2017 07:22
Show Gist options
  • Save setou/9b8bb60e87d8ec65c8fc3cf201f81b3b to your computer and use it in GitHub Desktop.
Save setou/9b8bb60e87d8ec65c8fc3cf201f81b3b to your computer and use it in GitHub Desktop.
/*
* This is a wrapper for AWS Dynamo DB Document Client.
* License: MIT License
*
* required AWS SDK And jQuery.
* However, it is a good idea to replace Deferred with Promise.
*
* How to use.
*
* 1, Insert HTML tag.
* <script src="https://sdk.amazonaws.com/js/aws-sdk-2.95.0.min.js"></script>
* <script src="dist/dynamodb.js"></script>
*
* 2, Set _accessKeyId and _secretAccessKey as necessary, or set AWS.config.credentials using AWS Cognito Identity.
*
* Example of use.
* var dynamodb = AWSDyDBDC.getInstance();
* dynamodb.get(__PARAMS__).then(__CODE__).fail(__CODE__).always(__CODE__);
*/
var AWSDyDBDC = (function(){
var _region = 'ap-northeast-1';
var _accessKeyId = '';
var _secretAccessKey = '';
var _docClient = null;
var _getDocClient = function(){
if (_docClient === null) {
_docClient = new AWS.DynamoDB.DocumentClient();
console.log('AWS.DynamoDB.DocumentClient Ready.');
}
return _docClient;
};
var _constructor = function(){
if (typeof AWS !== 'object') {
throw new Error('RequiredObjectDoesNotExist');
}
AWS.config.region = _region;
if (_accessKeyId && _secretAccessKey) {
AWS.config.update({
accessKeyId: _accessKeyId,
secretAccessKey: _secretAccessKey
});
}
};
_constructor.prototype.getInstance = function(){
return this;
};
_constructor.prototype.promiseCallback = function(callback){
var deferred = $.Deferred();
if (typeof callback === 'function') {
return callback(deferred);
}
return deferred.reject();
};
_constructor.prototype.put = function(params){
var _params = params || {
TableName: '',
Item: {},
};
return this.promiseCallback(function(deferred){
_getDocClient().put(_params, function(err, data){
return ((err) ? deferred.reject(err) : deferred.resolve(data));
});
return deferred.promise();
});
};
_constructor.prototype.get = function(params){
var _params = params || {
TableName: '',
Key: {},
};
return this.promiseCallback(function(deferred){
_getDocClient().get(_params, function(err, data){
return ((err) ? deferred.reject(err) : deferred.resolve(data));
});
return deferred.promise();
});
};
_constructor.prototype.update = function(params){
var _params = params || {
TableName: '',
Key: {},
ExpressionAttributeNames: {},
ExpressionAttributeValues: {},
UpdateExpression: '',
};
return this.promiseCallback(function(deferred){
_getDocClient().update(_params, function(err, data){
return ((err) ? deferred.reject(err) : deferred.resolve(data));
});
return deferred.promise();
});
};
_constructor.prototype.delete = function(params){
var _params = params || {
TableName: '',
Key: {},
};
return this.promiseCallback(function(deferred){
_getDocClient().delete(_params, function(err, data){
return ((err) ? deferred.reject(err) : deferred.resolve(data));
});
return deferred.promise();
});
};
_constructor.prototype.scan = function(tableName, useSort, sortKey){
var params = {
TableName: tableName || '',
};
useSort = useSort || false;
sortKey = sortKey || 'id';
return this.promiseCallback(function(deferred){
_getDocClient().scan(params, function(err, data){
if (err) { return deferred.reject(err); }
var items = (useSort) ? data.Items.sort(function(a, b){
if (a[sortKey] < b[sortKey]) return -1;
if (a[sortKey] > b[sortKey]) return 1;
return 0;
}) : data.Items ;
return deferred.resolve(items);
});
return deferred.promise();
});
};
return new _constructor();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment