I've got an object like this:
{
"id": (UUID generated at call time)
"referenceId": (Foreign key for another system)
"referenceType": (enum)
}
The primary index is just
- Primary key: id
And I've got a secondary index like
- Primary key: referenceId
- Secondary key: referenceType
So what I want to do, is query the secondary index with the referenceId and referenceType, and then if that's empty, write the record to the table.
The problem is, Conditional Expressions can only check same index you're querying. And I've looked into DynamoDb Transactions, but they say you can't have two transactions target the same item.
Is there a way to make this work?
If I understand your question correctly, your item has three attributes:
id,referenceIdandreferenceType. You've also defined a global secondary index with a composite primary key ofreferenceIdandreferenceType.Assuming all these attributes are part of the same item, you shouldn't need to read the secondary index before deciding to write to the table. Rather, you could perform a
PUToperation on the condition thatreferenceIdandreference typedon't yet exist on that item.You may also want to check out this fantastic article on DynamoDB condition expressions.