I'm using the javascript/nodejs AWS SDK DynamoDB Document client. My condition expression isn't working properly. I'm wanting to prevent adding emails to the "emails" list if that email is listed in the "blockedEmails" list. This isn't working however, and DynamoDB just allows me to add the email even with the condition expression.
Here's the code:
var index = {
TableName: getTableName(event),
Key: {
accountId: accountId
},
UpdateExpression: "SET #emails = list_append(if_not_exists(#emails, :empty_list), :email)",
ConditionExpression: "(attribute_not_exists(#emails) or not(contains(:emailIndividual, #emails))) and (attribute_not_exists(#blockedEmails) or not(contains(:emailIndividual, #blockedEmails)))",
ExpressionAttributeNames: {
"#emails": "emails",
"#blockedEmails": "blockedEmails"
},
ExpressionAttributeValues: {
":email": [email],
":emailIndividual": email,
":empty_list": []
}
};
console.log(JSON.stringify(index));
dynamodb.update(index, function(err) {
if(err) {
reject(err);
} else {
resolve();
}
});
Here's what the object looks like with values:
{
"TableName": "dev-netcountable-accounts",
"Key": {
"accountId": "ABCD-1234"
},
"UpdateExpression": "SET #emails = list_append(if_not_exists(#emails, :empty_list), :email)",
"ConditionExpression": "(attribute_not_exists(#emails) or not(contains(:emailIndividual, #emails))) and (attribute_not_exists(#blockedEmails) or not(contains(:emailIndividual, #blockedEmails)))",
"ExpressionAttributeNames": {
"#emails": "emails",
"#blockedEmails": "blockedEmails"
},
"ExpressionAttributeValues": {
":email": [
"bob@nowhere.com"
],
":emailIndividual": "bob@nowhere.com",
":empty_list": []
}
}
Here's what the record currently looks like in DynamoDB
{
"accountId": "ABCD-1234",
"blockedEmails": [
"bob@nowhere.com"
],
"emails": [
"success@simulator.amazonses.com"
]
}
via CamHart
No comments:
Post a Comment