I am a newbie to this technology . So bear with my below explanation.
My usecase :
I am updating an item in Dynamodb based on primary key(ID) . Basically my record has 4 columns ( ID, description, name , price ) .
The problem here is suppose when i have an item with key 123
ID description name price
123 Model nokia 10
when i use above lamda function to update all the columns it is working fine. But when i try to update only the description
column it is throwing error that 'ExpressionAttributeValues can't be empty` . So for that i had did some thing like this
ExpressionAttributeValues:{
":d":event.description ? event.description : null ,
":n":event.name ? event.name : null,
":p":event.price ? event.price : null
}
But the problem with this is it will update the only value that i had given . Like if i had given only description
to modal1
then it will change that and give the result like this
ID description name price
123 Model1 null null
Which is right because that above snippet says to do that.
What i want to achieve ?
How can i change that behaviour to acheive like this
ID description name price
123 Model1 nokia 10
Any help is appreciated
Updatedcode
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)
}
When i ran the code i got output like this :
D:\\Dynamo>node FullUpdate.js
Update succeeded.
first...
second...
third...
'set description = :d, #na = :n, price = :p',
{ '#na': 'name' }
{ ':d': 'Iphone', ':n': 'xx', ':p': 50 }
You have to fabric your query depending on the variables you have, so you have to do something like this
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
}
be carful of the comma, and log your query to be sure it fits your expectations. you can check a function i use for this purpose,
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.