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忽略空值,而只更新名称
?
but is there a way that I can tell updateItem to ignore the null values and only update the name
?
推荐答案
我在问同样的问题...在Java中, SaveBehavior.UPDATE_SKTES_NULL我找不到那样的东西
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 来进行更清洁的解决: / p>
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屋!