2
votes

am trying to do a ConditionExpression in a DynamoDB put to check whether a stored boolean is true (in this example, whether the user is already verified don't run the put), i'm using the javascript DocumentClient SDK (thanks to @shimon-tolts), the code looks like:

var query = {
    TableName: tableName,
    Item: {
        email: email,
        verified: false,
        verifyToken: token
    },
    ConditionExpression: 'attribute_exists(email) AND verified = :bool',
    ExpressionAttributeValues: {
        ":bool":"false"
    }
};

dynamodb.put(query, function(err, data){
    if (err) return fn(err)
    fn(null, data);
});

Which doesn't work, it fails the condition check no matter what the call.

Pretty much what I need (in pseudocode):

IF email already exists AND verified equals false 
   THEN allow PUT
IF email already exists AND verified equals true
   THEN don't allow PUT
IF email does not exist
   THEN allow PUT

Any ideas?

3

3 Answers

4
votes

I suggest using DocumentClient as it works with javascript objects. To do a condition expression you have to specify the ExpressionAttributeNames and ExpressionAttributeValues for example::

ConditionExpression: "#yr <> :yyyy and title <> :t",
ExpressionAttributeNames:{"#yr":"year"},
ExpressionAttributeValues:{
    ":yyyy":year,
    ":t":title
}

You can see more examples here and read more here

2
votes

I finally got this to work after figuring out the right ExpressionAttributeValues:

dclient.scan(TableName='bix-workflow-images', 
FilterExpression="wgs = :t or attribute_not_exists(wgs)", 
ExpressionAttributeValues={':t':{'BOOL':True}})
0
votes

I am stumbling about this because I have a similar problem and although this is a year old, for me it looks like your boolean shouldn't be a string:

ExpressionAttributeValues: {
    ":bool": "false"
}

Instead:

ExpressionAttributeValues: {
    ":bool": false
}

Have you tried it that way?