Skip to content

Instantly share code, notes, and snippets.

@christopheretcheverry
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christopheretcheverry/bff4f26ea365c7487f33 to your computer and use it in GitHub Desktop.
Save christopheretcheverry/bff4f26ea365c7487f33 to your computer and use it in GitHub Desktop.
var filter = { indexName: 'user_id-item_id-index', indexHashKey: 'user_id'};
var hash = 'secondary-index-hash-value';
ddb.query('table', hash, filter, function(err, res, cap) {
console.log(res);
});
//DynamoDB ddb.js Update
//Remove Old HashKey
// data.HashKeyValue = scToDDB(hash)
if(options.indexHashKey){
data.KeyConditionExpression = options.indexHashKey + " = :secondary_key";
data.ExpressionAttributeValues = {
":secondary_key": scToDDB(hash)
};
}else{
//ToDo: Change to Key
data.HashKeyValue = scToDDB(hash)
}
//You have to upgrade to a newer version of Dynamo
//I'm using DynamoDB_20120810 for this example
//Upgrading Dynamo will break the HashKeyElement of this driver
//Around line 874 in ddb.js
'X-amz-target': 'DynamoDB_20120810.' + op,
'x-amz-target' + ':' + 'DynamoDB_20120810.' + op + '\n') + '\n' +
// You have to explicitly define your hash and range properties for new versions of Dyanmo
// http://stackoverflow.com/a/16618235/1748015
var key = {_id: id, user_id: userId};
ddb.get('purchase', key, function(err, res, cap) { });
getItem = function(table, key, options, cb) {
var data = {};
try {
data.TableName = table;
// Remove: HashKeyElement Deprecated
// var key = { "HashKeyElement": hash };
// if(typeof range !== 'undefined' &&
// range !== null) {
// key.RangeKeyElement = range;
// }
data.Key = objToDDB(key);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment