DynamoDB ConditionalExpression not recognizing Attribute Names or Values - node.js

I'm using NodeJS's aws-sdk and am trying to do an update such that if the item doesn't exist it will throw an error. I'm using the Expression API instead of the legacy one. Here is my contrived example that isn't working for me.
client.update({
TableName: 'User',
Key: {'_id': '10'},
UpdateExpression: 'SET username = :user, password = :pword',
ConditionalExpression: 'attribute_exists(#idKey) AND #idKey = :idVal',
ExpressionAttributeNames: {
'#idKey': '_id'
},
ExpressionAttributeValues: {
':idVal': '10',
':user': 'user10',
':pword': 'password10'
}}, function(err, data){
if(err) console.log(err);
else console.log(data);
});
ValidationException: Value provided in ExpressionAttributeNames unused in expressions: keys: {#idKey}
I've tried various other ConditionalExpressions both using attribute names and inserting the actual value into the expression. I'm beginning to think this is a bug. Using the legacy Expected->Exists with the legacy AttributeUpdate works but I am unable to demonstrate this feature with Expressions.

You are already narrowing down to the specific item where _id=10 with the Key parameter of your UpdateItemRequest. If an item does not exist, you cannot condition the UpdateItem call on a specific value of a key. Therefore, only the attribute_exists(#idKey) in the ConditionExpression is necessary.
The following code elicits the behavior you desire (I had to change table name to Images and primary key to Id to match the contents of the DynamoDB Local Shell tutorial.
var params = {
TableName: 'Image',
Key: { // The primary key of the item (a map of attribute name to AttributeValue)
'_id': 'dynamodb.png'
},
UpdateExpression: 'SET username = :user, password = :pword',
ConditionExpression: 'attribute_exists(#id)',
ExpressionAttributeValues: {
':user': 'user10',
':pword': 'password10'
},
ExpressionAttributeNames: {
'#id': '_id'
},
ReturnValues: 'ALL_NEW'
};
docClient.update(params, function(err, data) {
if (err) ppJson(err); // an error occurred
else ppJson(data); // successful response
});
As a reminder, please do not post any real password data here :)

Related

How to execute multiple operations while updating an item in Amazon DynamoDB?

I'm trying to update an item in Amazon DynamoDB. I want to execute an operation that consists of one sum and one subtraction, but I'm not able to accomplish my goal.
Here is the function, ddb is an instance of the class AWS.DynamoDB.DocumentClient:
function updateLecture(event){
const params = {
TableName: 'lecture',
Key: {
'lecture_id': Number.parseInt(event.lecture_id)
},
UpdateExpression: 'SET #free = :total - (#total + #free)',
ExpressionAttributeValues: {
':total': event.total
},
ExpressionAttributeNames: {
"#total" : "total",
"#free" : "free"
},
ReturnValues : 'UPDATED_NEW'
}
return ddb.update(params).promise();
}
When I try to run the function, I get the following error:
"errorType": "ValidationException",
"errorMessage": "Invalid UpdateExpression: Syntax error; token: \"+\", near: \"#total + #free\""
I've tried to work with parenthesis, but I always receive the same error.
I'm afraid I can't use more than one operation, but I couldn't find any trace of that in the docs. Anyway, is there a way to use multiple operations?
Just tried the following PartiQL statement in the DynamoDB console, and it worked fine:
// Works just fine
UPDATE "ddb-playground"
SET myNumA = 10 - (myNumB + myNumA)
WHERE PK = 'foo' AND SK = 'bar'
It runs just fine, so at least there's no fundamental limitation here.
However, just like you, I'm not able to get the JS SDK to do this with an UpdateExpression, I get syntax error no matter what I try. So it seems like the JS SDK is limited to a single arithmetic expression on numeric values.
// Fails with:
//
// ValidationException: Invalid UpdateExpression: Syntax error; token: "+", near: "myNumB + myNumA"
import AWS from "aws-sdk"
const ddb = new AWS.DynamoDB()
await ddb.putItem({
TableName: "ddb-playground",
Item: {
PK: {S: "foo"},
SK: {S: "bar"},
myNumA: {N: "10"},
myNumB: {N: "20"}
}
}).promise()
await ddb.updateItem({
TableName: "ddb-playground",
Key: {
PK: {S: "foo"},
SK: {S: "bar"}
},
UpdateExpression: "SET myNumA = :newVal - (myNumB + myNumA)",
ExpressionAttributeValues: {
":newVal": {N: "30"}
}
}).promise()
So perhaps your only option here is to actually use PariQL for this one?
await ddb.executeStatement({
Statement: 'UPDATE "ddb-playground" SET myNumA = ? - (myNumB + myNumA) WHERE PK = ? AND SK = ?',
Parameters: [{N: "30"}, {S: "foo"}, {S: "bar"}]
}).promise()

AWS DynamoDB Transact Write using IN operator on Array

I am working on one query where I need to use TrasactWrite of dynamoDB and perform update query on one of my table.
Scenario is I am having array of unique IDs and I need to perform same update operation on each ID in array and change one flag in table without using Loop or map.
But I am having difficulty in finding example or docs related to how do I use IN operator on IDs which are primary keys in a Key parameter of transactWrite.
Here is my sample code:
let IDs = await find('table', { type: 'some_type' }, 'type-index}
let params = {
TransactItems: [{
Update: {
TableName: 'table',
Key: '#_id IN :IDs,
UpdateExpression: 'set #flag = :flag',
ExpressionAttributeNames: {
'#flag': 'flag',
'#_id': '_id'
},
ExpressionAttributeValues: {
':flag': false,
":IDs": IDs
}
}
}]
}
Already tried: Answer Its not same question as mine
I will answer if anyone has any question. Any help will be really helpful. Thank you
You can't use IN operator in this case - Update. With update operator you have to put key value to Update object. A key includes partitionKey and sortKey, I think in your case, you just set partitionKey.
To update more than one item by ID, you can put many Update object to TransactItems.
let IDs = await find('table', { type: 'some_type' }, 'type-index');
let params = {
TransactItems: [], // init empty array
}
IDs.forEach((ID) => {// loop though IDs array
params.TransactItems.push({
Update: {
TableName: 'table',
Key: {
'_id': ID, // ID value
},
UpdateExpression: 'set #flag = :flag',
ExpressionAttributeNames: {
'#flag': 'flag',
// '#_id': '_id' // remove this line
},
ExpressionAttributeValues: {
':flag': false,
// ":IDs": IDs // remove this line
}
}
})
});
// await client.transactWrite(params).promise()

How to check and then update AWS dynamoDB item in node.js?

Here is an example. I am using AWS Lambda function in Node.JS. This is the object in DynamoDB:
{
email: "johndoe#gmail.com",
name: "John Doe",
score: "12"
}
Now I want to first check what his saved score is. If his current score is more than the score already present in DB then update the DB else leave it as it is. Currently, my code is updating it every time. I'm not sure how to proceed and which function to use.
If his score is 15, then after checking the DB it should update it to:
{
email: "johndoe#gmail.com",
name: "John Doe",
score: "15"
}
If his score is 7, it should leave it as it is.
{
email: "johndoe#gmail.com",
name: "John Doe",
score: "12"
}
Edit - The following issue has also been solved. Found a workaround. Still looking for answers. So leaving it open.
Now my issue is that I am using the same function for both updating and creating records. Is that possible?
If a new user plays the game his email, name, and score are saved to the DB. And if an existing user plays, only his score is updated if it is greater than the one in DB.
This is where I'm currently stuck. Getting these 2 issues,
1. name is a reserved keyword. I wonder how was it allowed me to use name as an attribute when i was just using put.
2. email cannot be updated since it is part of key.
Here is my current code,
function addToLeaderBoard(currentScore, email, name){
var params = {
TableName: tablename,
Key: {
"email": email
},
UpdateExpression: "set score = :num, name = :player, email = :mail",
ConditionExpression: "score > :num",
ExpressionAttributeValues: {
":num": currentScore,
":player": name,
":mail": email
},
ReturnValues: "UPDATED_NEW"
};
docClient.update(params, function(err, data) {
if (err) console.log(err);
else console.log(data);
});
}
What you want to do is a conditional update. This allows you to only the update the item in DynamoDB if a condition is met. In your case that condition would be, that the new score has to be higher than the existing score.
Such a conditional update would look like the following example. Note the ConditionExpression in there, which is responsible for only updating the item when the condition is met.
'use strict';
const aws = require('aws-sdk');
var newScore = 123;
var docClient = new AWS.DynamoDB.DocumentClient()
var params = {
TableName: "players",
Key: {
"email": "johndoe#gmail.com"
},
UpdateExpression: "set score = :num",
ConditionExpression: "score > :num",
ExpressionAttributeValues: {
":num": newScore
},
ReturnValues: "UPDATED_NEW"
};
docClient.update(params, function(err, data) {
if (err) console.log(err);
else console.log(data);
}
});

DynamoDB scanning using filter

Im new to DynamoDB and have a table which is "feeds" and partition key is "id" and i have 3 other attributes which are "category", "description", "pubDate".
I want to query the "category" attribute. But it doesn't work, because i can only query the partition key (hashkey), if im right.
Now my query is that which doesnt work;
let category = event.category;
const params = {
Key: {
"category": {
S: category
}
},
TableName: "feeds"
};
dynamodb.getItem(params, function (err, data) {
if (err) {
console.log(err);
callback(err);
}
else {
console.log(data);
callback(null, data);
}
});
How can i make it work? I tried to write a scan query but i couldn't understand the documentation of AWS good.
Edit: I did make it work with the help of Dunedan. Here is the working code,
var params = {
TableName: 'feeds',
IndexName: 'category-index',
KeyConditionExpression: 'category = :category',
ExpressionAttributeValues: {
':category': 'backup',
}
};
var docClient = new AWS.DynamoDB.DocumentClient();
docClient.query(params, function(err, data) {
if (err) callback(err);
else callback(null, data);
});
If your application will regularly query for the category, you should check out Global Secondary Indexes (GSI), which allow you to generate a projection of your data with another key than the original hash key as the key you can use to query.
Scanning and filtering as you suggested doesn't scale very well, as it fetches all data in the table and just filters the results.

Cant update Dynamo Db table , getting ValidationException

I need to update my dynamo db table by using only partition key. But i got validation exeption.
I have created a table with 3 fields.
id (Partition Key)
name (Sort Key)
age
Then i have triyed to update age field using only id.(tryied to modify age 30 to 40) this is my code
var AWS = require("aws-sdk");
AWS.config.update({
region: "us-east-1",
});
var params = {
TableName: 'test',
Key: { id: '100' },
UpdateExpression: 'set #age = :age ',
ConditionExpression: '#age = :testAge',
ExpressionAttributeNames: { '#age': 'age' },
ExpressionAttributeValues: { ':age': '40', ':testAge': '30' }
};
var docClient = new AWS.DynamoDB.DocumentClient();
docClient.update(params, function (err, data) {
if (err) {
console.log(err);
}
else {
console.log(data);
}
});
But i got error like this.
{ [ValidationException: The provided key element does not match the schema]
message: 'The provided key element does not match the schema',
code: 'ValidationException',
time: Thu Nov 17 2016 22:38:01 GMT+0530 (IST),
requestId: '34PNMFM6CEACQIRHTSV77OI0JRVV4KQNSO5AEMVJF66Q9ASUAAJG',
statusCode: 400,
retryable: false,
retryDelay: 0 }
After getting error, i modified my params variable like this
var params = {
TableName: 'test',
Key: { id: '100',name: 'manaf' },
UpdateExpression: 'set #age = :age ',
ConditionExpression: '#age = :testAge',
ExpressionAttributeNames: { '#age': 'age' },
ExpressionAttributeValues: { ':age': '40', ':testAge': '30' }
};
Using this, updation is successfully completed. How to update table using without sort key?
Currently, the DynamoDB update API doesn't have an option to update the item by partition key only. There is no batchUpdateItem API similar to batchWriteItem as well.
So, if the sort key is not available, get all the sort keys of partition key and update each item for the partition and sort key combination.
For the primary key, you must provide all of the attributes. For
example, with a simple primary key, you only need to provide a value
for the partition key. For a composite primary key, you must provide
values for both the partition key and the sort key.
Sample code:-
You may need to change it for your table. The below code uses "Movies" table which has "yearkey" as partition key and "title" as sort key.
The below code updates the "createdate" attribute for the given hash key "2012".
The variable paramsUpdate is formed based on the query operation. Please update it accordingly for your requirement (i.e. table structure). Logic remains same, you just need to change the table name and key values accordingly.
var AWS = require("aws-sdk");
var creds = new AWS.Credentials('akid', 'secret', 'session');
AWS.config.update({
region : "us-west-2",
endpoint : "http://localhost:8000",
credentials : creds
});
var docClient = new AWS.DynamoDB.DocumentClient();
var hashKey = 2012;
var paramsQuery = {
TableName : "Movies",
KeyConditionExpression : 'yearkey = :hkey',
ExpressionAttributeValues : {
':hkey' : hashKey
}
};
function updateItem(paramsUpdate) {
console.log("Updating the item...");
docClient.update(paramsUpdate, 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));
}
});
}
docClient.query(paramsQuery, function(err, data) {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err,
null, 2));
} else {
console.log(data.Count);
var itemIndex = 0;
while (itemIndex < data.Count) {
console.log('Hashkey to be updated ======>',
data.Items[itemIndex].yearkey,
';Title to be updated ========>',
data.Items[itemIndex].title);
var paramsUpdate = {
TableName : "Movies",
Key : {
"yearkey" : data.Items[itemIndex].yearkey,
"title" : data.Items[itemIndex].title
},
UpdateExpression : "set #createdate = :createdate",
ExpressionAttributeNames : {
'#createdate' : 'createdate'
},
ExpressionAttributeValues : {
':createdate' : '2016-11-17'
},
ReturnValues : 'UPDATED_NEW'
};
updateItem(paramsUpdate);
itemIndex++;
}
}
});
In DynamoDB, partition key + sort key is treated as a "composite primary key", which uniquely identifies an item (on the contrary, Dynamo also supports simple primary key, which only contains partition key). So you need both to update an item. This is the reason that you can have two items with the same partition key but different sort key. So if you only provide the partition keys, Dynamo will get confused with which item to update.
For your current table configuration, the only way to update an item given a partition key is to make a query with only partition key to get all the items, and filter out the one with the intended sort key. Then use the combination of partition key and sort key to update this item.

Resources