3

I am trying to do a put item on a Dynamo Table using the nodejs sdk. I tried using the same document and a few other variations but nothing seems to work. Everytime I receive the same error:

"message":"Invalid attribute value type"
"code":"ValidationException"
"time":"2016-10-11T06:32:26.361Z"
"statusCode":400
"retryable":false

The following is the relevant code snippet:

var params = {
    TableName: "MY_Table_Name",
    Item: { 
        "stringAtt": "stringValue",
        "boolAtt": true,
        "numAtt": 123,
    },
};
docClient.put(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response
});

My table's indexes are as follows:

Primary: Partition Key: stringAtt, Sort Key: boolAtt
GSI: Partition Key: boolAtt, Sort Key: numAtt

I am not sure if it's my query or the index structure that is wrong.

2 Answers 2

8

The BOOL data type can't be a key attribute (i.e. Partition or Sort key). The Partition or Sort key data type can be of three types (listed below). If you have created the table with Sort key of type 'B', it means that sort key is of type Binary (i.e. not Bool).

EDIT: You can't use a BOOL attribute as the partition or sort key in a GSI as well.

AttributeType: 'S | N | B'

S - the attribute is of type String

N - the attribute is of type Number

B - the attribute is of type Binary

When the table is created with key of type BOOL, the API would throw the below exception.

Unable to create table. Error JSON: {
  "message": "Member must satisfy enum value set: [B, N, S]",
  "code": "ValidationException",
3
  • Thanks, What is the recommended way to solve this? My requirement is to get all items with the specified boolean value true/false.
    – Tanmay
    Commented Oct 11, 2016 at 10:00
  • You have to scan the table to get all items where boolAttribute = true/false. Commented Oct 11, 2016 at 11:05
  • 1
    You can also use a numeric type and just constrain yourself to only using the values 0 and 1. This will cause issues at scale but given your design I doubt that you will be in that range.
    – lod
    Commented Oct 16, 2017 at 22:06
0

You can simply use Binary:

You can set the boolean attribute type to binary B and define these values as constant.


export const BinaryBoolean = {
  True: new Uint8Array([1]),
  False: new Uint8Array([0]),
} as const;

export const boolToBinary = (value: boolean): Uint8Array => value
  ? BinaryBoolean.True
  : BinaryBoolean.False;

export const binaryToBool = (value: Uint8Array): boolean => value[0] === 1;

Now you can use this to convert boolean to binary and successfully put Items into DynamoDB. So far its not throwing errors while dbClientInstance.putItem().

Querying also works. It does return Uint8Array, that you can pass to binaryToBool.

Note: Tested using docker local : DockerHub/LocalDynamoDB

Not the answer you're looking for? Browse other questions tagged or ask your own question.