DynamoDB:UpdateItem,忽略 ExpressionAttributeValues 中的 Null 值 [英] DynamoDB: UpdateItem, Ignore Null values in ExpressionAttributeValues
问题描述
我正在使用 DynamoDB UpdateItem 更新记录我的数据库.像这样的基本功能对我有用.
I'm using DynamoDB UpdateItem to update records in my DB. A basic function like this is working for me.
var user = {
userID: '123213',
name: 'John Doe',
age: 12,
type: 'creator'
};
var params = {
TableName:table,
Key:{
"UserID": user.userID
},
UpdateExpression: "set Name = :r, Age=:p, Type=:a",
ExpressionAttributeValues:{
":r":user.name,
":p":user.age,
":a":user.type
},
ReturnValues:"UPDATED_NEW"
};
docClient.update(params, function(err, data) {
if (err) {
console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
}
});
但是...
如果我只想更新一个属性,名称,如下所示:
If I'd like to only update one attribute, the name, like this:
var user = {
userID: '123213',
name: 'John Smith'
};
var params = {
TableName:table,
Key:{
"UserID": user.userID
},
UpdateExpression: "set Name = :r, Age=:p, Type=:a",
ExpressionAttributeValues:{
":r":user.name,
":p":user.age,
":a":user.type
},
ReturnValues:"UPDATED_NEW"
};
它给了我一个错误
ExpressionAttributeValues 不能为 NULL.
我知道我可以通过检查用户中的值来动态生成 UpdateExpression
字符串,如下所示:
I know that I could dynamically produce the UpdateExpression
String by checking for values in user, like this:
for (var key in user) {
if (user.hasOwnProperty(key)) {
...add to DynamicUpdateExpression..
}
}
但是有没有办法让 updateItem 忽略空值而只更新 name
?
but is there a way that I can tell updateItem to ignore the null values and only update the name
?
推荐答案
我也在问同样的问题...在 Java 中有 SaveBehavior.UPDATE_SKIP_NULL_ATTRIBUTES 但我在 aws-sdk for nodejs 中找不到类似的东西.
I was asking the same question...In Java there's the SaveBehavior.UPDATE_SKIP_NULL_ATTRIBUTES but I couldn't find anything like that in aws-sdk for nodejs.
您可以使用 AttributeUpdates 而不是 UpdateExpression 来做出更简洁的解决方法:
You could use AttributeUpdates instead of UpdateExpression to make a cleaner workaround:
const AWS = require(aws-sdk);
const bluebird = require('bluebird');
const _ = require('lodash');
AWS.config.setPromisesDependency(bluebird);
const dynamodb = new AWS.DynamoDB.DocumentClient();
var skipNullAttributes = (attributes) => {
return _.omitBy(attributes, (attr) => {
return _.isNil(attr.Value);
});
}
var update = (id, attributes) => {
var params = {
TableName : 'MyTableName',
Key : { id: id },
AttributeUpdates: skipNullAttributes(attributes)
};
return dynamodb.update(params).promise();
}
exports.handler = (event, context, callback) => {
var body = JSON.parse(event.body);
var userId = event.pathParameters.id;
var attributes = {
firstName: { Action: 'PUT', Value: body.firstName },
lastName : { Action: 'PUT', Value: body.lastName }
};
update(userId, attributes)
.then((result) => console.log(result) )
.catch((error) => console.error(error) );
callback(null, {statusCode: 200, body: JSON.stringify({message: 'done!'})});
}
这篇关于DynamoDB:UpdateItem,忽略 ExpressionAttributeValues 中的 Null 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!