I am implementing a single table design in dynamodb and overloading keys. My current design allows for an email to be subscribed to a thread.
This is a noSQL workbench screenshot:
I am using EM#\<email>
as the partition key and SB#\<thread id>
as the sort key. I am constructing a putItemCommand from nodeJS lambda env. Basically, the command works as expected.
Here is the payload:
new PutItemCommand({
TableName: 'sometable',
Item: {
pk: {
S: asEmail(email), //Resolves to `EM${email}`
},
sk: {
S: asSubscription(chain), //Resolves to `SB${chain}`
},
},
ConditionExpression: 'attribute_not_exists(pk)',
}),
Now I am just confused why this works. I am trying to ensure that the primary key (pk,sk) is unique so an email cannot be subscribed twice to a thread. But I am confused why
ConditionExpression: 'attribute_not_exists(pk)',
correctly accomplishes this. Reading this condition expression makes me believe that it is checking to make sure there is no partition key that matches. Is 'pk' an alias or does this have something to do with how dynamo retrieves data? I just need someone to spell this out for me.