Skip to content

Commit

Permalink
Switching DynamoDB scan to FilterExpression (#55)
Browse files Browse the repository at this point in the history
* Switching DynamoDB scan to FilterExpression

* Fixing node 0.12 compatibility
  • Loading branch information
Glockenbeat authored and adrai committed Sep 27, 2017
1 parent 24bea37 commit 38bd1f2
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions lib/databases/dynamodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,22 +295,57 @@ _.extend(DynamoDB.prototype, {
}
});

function createDynamoParams(obj, objPrefix) {
objPrefix = objPrefix ? objPrefix + '.#' : '#';

return _.reduce(obj, function (result, value, key) {
var valueSearchedFor = obj[key];
var dotNotatedTargetAttribute = objPrefix + key;

if(_.isPlainObject(valueSearchedFor)) {
result = _.merge(result, createDynamoParams(valueSearchedFor, dotNotatedTargetAttribute));
result.expressionAttributeNames['#' + key] = key;
} else {
var attributeVariable = ':' + dotNotatedTargetAttribute.replace(/(\.|#)/g, '');
var attributeValueKey = '#' + key;

if (result.filterExpression !== '') {
result.filterExpression = result.filterExpression + ' AND ';
}

result.filterExpression = result.filterExpression + dotNotatedTargetAttribute + ' = ' + attributeVariable;
result.expressionAttributeValues[attributeVariable] = valueSearchedFor;
result.expressionAttributeNames['#' + key] = key;
}

return result;
},
{
expressionAttributeNames: {},
filterExpression: '',
expressionAttributeValues: {}
})
};

function queryToScanParams(tableName, query, queryOptions){
var limit = queryOptions.skip + queryOptions.limit;
var scanFilter = _.reduce(query, function(result, value, key) {
result[key] = {
ComparisonOperator: 'EQ',
AttributeValueList: [ value]
};
return result;
}, {});
var params = {
TableName: tableName,
ScanFilter: scanFilter
};
if(limit > 0){
return _.set(params, 'Limit', limit);
TableName: tableName,
}

if (limit > 0) {
params.Limit = limit;
}

if (_.isEmpty(query)) {
return params;
}

var dynamoParams = createDynamoParams(query);
params.FilterExpression = dynamoParams.filterExpression;
params.ExpressionAttributeNames = dynamoParams.expressionAttributeNames;
params.ExpressionAttributeValues = dynamoParams.expressionAttributeValues;

return params;
}

Expand Down

0 comments on commit 38bd1f2

Please sign in to comment.