更新dynamodb中的项目?

时间:2018-08-01 10:29:39

标签: aws-lambda amazon-dynamodb

我是这项技术的新手。所以请忍受我下面的解释。

我的用例:

我正在基于主键(ID)更新Dynamodb中的项目。基本上我的记录有4列(ID,描述,名称,价格)。

这里的问题是假设我有一个钥匙为123的物品

ID description name price

123 Model      nokia  10

当我使用上述lamda函数更新所有列时,它工作正常。 但是,当我尝试仅更新description列时,会抛出“ ExpressionAttributeValues不能为空”的错误。因此,我做了这样的事情

ExpressionAttributeValues:{
     ":d":event.description ? event.description : null ,
     ":n":event.name ? event.name : null,
     ":p":event.price ? event.price : null
     }

但是问题是它将更新我给定的唯一值。 就像如果我只给description赋予modal1一样,它将改变它并给出这样的结果

ID description name price

123 Model1  null  null

这是正确的,因为上面的代码片段表示可以这样做。

我想实现什么?

我如何将这种行为更改为达到这种目的

ID description name price

123 Model1     nokia  10
  1. 有没有办法     仅更新传递的参数,并将其他参数保留为其先前的参数     值?

感谢您的帮助

更新的代码

var AWS = require("aws-sdk");
AWS.config.update({
    region: "regionname",
    endpoint: "endpointurl"
});
var docClient = new AWS.DynamoDB.DocumentClient()

var tablename = "Example"; 


var UpdateExpression = "'set";
var ExpressionAttributeValues= {};
var ExpressionAttributeNames= {};
var ID = "xx";
var description = "Iphone";
var name = "xx";
var price = 50;
 var params = { 
    TableName: tablename, 
    Key:{ 
    "ID": ID 
    }
    };
docClient.update(params,onUpdate);
 function onUpdate(err) {
    if (err) {
        console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Update succeeded.");
    }
    if(description) { 
        UpdateExpression = UpdateExpression + " description = :d,",
        ExpressionAttributeValues [":d"] =  description ,
        console.log("first...")

        }
        if(name){ 
        UpdateExpression = UpdateExpression + " #na = :n,",
        ExpressionAttributeNames['#na'] = 'name' ,
        ExpressionAttributeValues[":n"] = name ,
        console.log("second...")
        }
        if(price){ 
        UpdateExpression = UpdateExpression + " price = :p',",
        ExpressionAttributeValues[":p"] = price ,
        console.log("third...")
        }        
        console.log(UpdateExpression)
        console.log(ExpressionAttributeNames)
        console.log(ExpressionAttributeValues)
        return (UpdateExpression,ExpressionAttributeNames,ExpressionAttributeValues)
    }

运行代码时,我得到这样的输出:

D:\ Dynamo>节点FullUpdate.js

Update succeeded.
first...
second...
third...
'set description = :d, #na = :n, price = :p',
{ '#na': 'name' }
{ ':d': 'Iphone', ':n': 'xx', ':p': 50 }

1 个答案:

答案 0 :(得分:1)

您必须根据所拥有的变量来构造查询,因此您必须执行类似的操作

UpdateExpression = "set";
ExpressionAttributeValues: {};
ExpressionAttributeNames: {};

if(event.description){
 UpdateExpression = UpdateExpression + "description = :d";
 ExpressionAttributeValues[":d"] = event.description
}
if(event.name){
 UpdateExpression = UpdateExpression + "#na = :n";
 ExpressionAttributeNames['#na'] = 'name'
 ExpressionAttributeValues[":d"] = event.name
}
if(event.price){
 UpdateExpression = UpdateExpression + "price = :p";
 ExpressionAttributeValues[":d"] = event.pice
}

请小心逗号,并记录查询以确保它符合您的期望。 您可以检查我为此目的使用的功能

update(_id, store_id, opts?) {

    var Key = {
      _id: _id,
      store_id: store_id
    };

    if (Object.keys(opts).length > 0) {
      let UpdateExpression =
        "SET #date = :date, #author = :author";

      let ExpressionAttributeNames = {
        "#date": "date",
        "#author": "author"
      }

      let ExpressionAttributeValues = {
        ":date": new Date().getTime(),
        ":author": localStorage.getItem('user_id')
      }

      if ((opts && opts.title)) {
        UpdateExpression = UpdateExpression + "#title = :title,";
        ExpressionAttributeNames["#title"] = "title";
        ExpressionAttributeValues[":title"] = opts.title
      }

      if ((opts && opts.category)) {
        UpdateExpression = UpdateExpression + "#category = :category,";
        ExpressionAttributeNames["#category"] = "category";
        ExpressionAttributeValues[":category"] = opts.category
      }

      if ((opts && opts.category)) {
        UpdateExpression = UpdateExpression + "#dimension = :dimension,";
        ExpressionAttributeNames["#dimension"] = "dimension";
        ExpressionAttributeValues[":dimension"] = opts.dimension
      }

      if ((opts && opts.category)) {
        UpdateExpression = UpdateExpression + "#destination = :destination,";
        ExpressionAttributeNames["#destination"] = "destination";
        ExpressionAttributeValues[":destination"] = opts.destination
      }

      if ((opts && opts.category)) {
        UpdateExpression = UpdateExpression + "#timeStart = :timeStart,";
        ExpressionAttributeNames["#timeStart"] = "timeStart";
        ExpressionAttributeValues[":timeStart"] = opts.timeStart
      }

      if ((opts && opts.category)) {
        UpdateExpression = UpdateExpression + "#timeEnd = :timeEnd,";
        ExpressionAttributeNames["#timeEnd"] = "timeEnd";
        ExpressionAttributeValues[":timeEnd"] = opts.timeEnd
      }

      if ((opts && opts.publish)) {
        UpdateExpression = UpdateExpression + "#publish = :publish,"
        ExpressionAttributeNames["#publish"] = "publish"
        ExpressionAttributeValues[":publish"] = "x";
      }


      UpdateExpression = UpdateExpression.replace(new RegExp(',$'), '');

      if ((opts && opts.nbviews)) {
        UpdateExpression = UpdateExpression + "ADD #nbviews = :nbviews"
        ExpressionAttributeNames["#nbviews"] = "nbviews"
        ExpressionAttributeValues[":nbviews"] = 1;
      }

      if ((opts && !opts.publish)) {
        UpdateExpression = UpdateExpression + " REMOVE #publish"
        ExpressionAttributeNames["#publish"] = "publish"
      }

      return this.dataService.update(TABLENAME, Key, UpdateExpression, null, ExpressionAttributeNames, ExpressionAttributeValues);