DynamoDB Conditional Expression Nested Loop

Viewed 25
{
  "id": {
    "S": "12345678"
  },
  "legMap": {
    "M": {
      "12345678-ARCH-1": {
        "M": {
          "ata": {
            "NULL": true
          },
          "atd": {
            "NULL": true
          }
        }
      },
      "12345678-ARCH-2": {
        "M": {
          "ata": {
            "NULL": true
          },
          "atd": {
            "NULL": true
          }
        }
      }
    }
  }
}

The tree "12345678-ARCH-1" alternatively can have values like this:

"12345678-LEG-1": {
  "M": {
    "ata": {
      "S": "2022-04-11T10:38:00.000Z"
      },
    "atd": {
      "S": "2022-06-11T03:49:00.000Z"
      }
  }
}

The ConditionExpression should only allow an item to be updated if the tree '{id}-ARCH-{archNumber}' does not have any keys defined. i.e. all keys under "legMap" -> "M" should have value like

"M": { "ata": { "NULL": true }, "atd": { "NULL": true } }

Does PutCommand from '@aws-sdk/lib-dynamodb' allow for nested loops in ConditionExpression parameter? What would the expression look like?

1 Answers

Unfortunately, the answer is no. The DynamoDB condition expression syntax (see the details here) is a fairly weak language, and among other things does not give you any mechanism to loop over the content of some map and check them. You can check with the "=" operator whether the map 12345678-LEG-1 is equal something, but not whether it has any number of elements and all of them have are themselves maps with just one key called "NULL".

To fix this, you will probably need to change your data model. Perhaps try to have a single nested attribute which says whether this element is "empty" in your sense or not, and update this new attribute when you update the other pieces of the tree. Or try to keep the map actually empty instead of full of "NULL" elements - because the expression syntax does allow you to compare the map with an empty map.

Related