javascript - DynamoDB - Update or Create if not found method -
in dynamodb, how can make update or create if not found method if have hash key , range key in table. below works if have table hash key, not tables hash , range key:
var save = function(user_id, store_id, callback){ var doc = require('dynamodb-doc'), docclient = new doc.dynamodb(); var params = { tablename: 'stores', returnvalues: 'none', key: { 'user_id': user_id }, conditionexpression: "#a = :store_id_val", updateexpression: 'set #a = :store_id_val', expressionattributenames: { '#a': 'store_id' }, expressionattributevalues: { ':store_id_val': store_id } }; // save dynamodb document docclient.updateitem(params, function(error, response) { return callback(error, response); }); }
it simple. need specify both hash , range key in key
parameter. can away this:
var save = function(user_id, store_id, callback) { var doc = require('dynamodb-doc'), docclient = new doc.dynamodb(); var params = { tablename: 'stores', returnvalues: 'none', key: { 'user_id': user_id, 'store_id': store_id } }; // save dynamodb document docclient.updateitem(params, function(error, response) { return callback(error, response); }); }
Comments
Post a Comment