dynamodb-toolbox icon indicating copy to clipboard operation
dynamodb-toolbox copied to clipboard

Creating empty set makes you unable to add items

Open lucas-subli opened this issue 1 year ago • 1 comments

I have an entity with the following attributes configured

attributes: {
    userId: { partitionKey: true },
    sk: {
	    hidden: true,
	    sortKey: true,
	    default: 'SomeDefault',
    },
    someIds: { type: 'set', setType: 'number', required: true },
},

I initially populated the data with the following input:

{ userId: 'someId', leagueIds: [] }

The dynamo DB entry was created as follows

{
  "pk": {
    "S": "someId"
  },
  "sk": {
    "S": "SomeDefault"
  },
  "leagueIds": {
    "NULL": true
  },
  "_createdAt": {
    "S": "2023-07-24T22:49:17.173Z"
  },
  "_et": {
    "S": "SomeEntity"
  },
  "_updatedAt": {
    "S": "2023-07-24T22:49:17.173Z"
  }
}

Please note the someIds field. It will be important later I then tried to add an item to someIds using:

entity.update(
	{
		userId: 'someId',
		someIds: {
			$add: [1],
		},
	},
	{ returnValues: 'ALL_NEW' },
);

and got the following error:

ValidationException: An operand in the update expression has an incorrect data type

This only happens if I create the initial value of someIds empty (i.e. []) If instead, I create the initial data as:

{ userId: 'someId', someIds: [ 0 ] }

Then the dynamo entry will be:

{
  "pk": {
    "S": "someIds"
  },
  "sk": {
    "S": "SomeDefault"
  },
  "someIds": {
    "NS": [
      "0"
    ]
  },
  "_createdAt": {
    "S": "2023-07-24T22:39:00.104Z"
  },
  "_et": {
    "S": "SomeEntity"
  },
  "_updatedAt": {
    "S": "2023-07-24T22:42:36.338Z"
  }
}

And I can properly add new items.

If I remove all the items, the dynamo entry will become

{
  "pk": {
    "S": "someIds"
  },
  "sk": {
    "S": "SomeDefault"
  },
  "_createdAt": {
    "S": "2023-07-24T22:39:00.104Z"
  },
  "_et": {
    "S": "SomeEntity"
  },
  "_updatedAt": {
    "S": "2023-07-24T22:42:36.338Z"
  }
}

Notice the lack of someIds field In that situation, I am also able to properly add new items. Thus I suspect that having someIds as null is causing the issue.

Additional info:

It may be relevant. I have this option set

removeNullAttributes: false,

lucas-subli avatar Jul 24 '23 22:07 lucas-subli

DynamoDB does not support empty sets

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html

Sets DynamoDB supports types that represent sets of number, string, or binary values. All the elements within a set must be of the same type. For example, a Number Set can only contain numbers and a String Set can only contain strings.

There is no limit on the number of values in a set, as long as the item containing the values fits within the DynamoDB item size limit (400 KB).

Each value within a set must be unique. The order of the values within a set is not preserved. Therefore, your applications must not rely on any particular order of elements within the set. DynamoDB does not support empty sets, however, empty string and binary values are allowed within a set.

rfranco avatar Sep 13 '23 01:09 rfranco