0

So I have this notifications table with the following columns:

  • PK: (which stores the userId)
  • sentAt: (which stores the date the notifications was sent)
  • data: (which stores the data of the notification)
  • Read: (a boolean value which tells if the user has read the specific notification)

I wanted to create a GSI to get all the notification from a specific user that are not read (Read: False) So the partition key would be userId and the sort key would be Read but the issue here is that I cannot give a boolean value to the sort key to be able to query the users that have not read the notifications. This works with scan but that is not the result I am trying to achieve. Can anyone help me on this? Thanks

const params ={
      TableName: await this.configService.get('NOTIFICATION_TABLE'),
      FilterExpression: '#PK = :PK AND #Read = :Read',
      ExpressionAttributeNames: {
        '#PK': 'PK',
        '#Read': 'Read',
      },
      ExpressionAttributeValues: {
        ':PK': 'NOTIFICATION#a8a8e4c7-cab0-431e-8e08-1bcf962358b8',
        ':Read': true, *//this is causing the error*
      },
    };
    const response = await this.dynamoDB.scan(params).promise();
    

1 Answers1

0

Yes, we cannot have bool type value to be used as DynamoDB Partition Key or Sort Key.

Some alternatives you could actually consider:

  1. Create a GSI with only Partition Key, gsi-userId. When you do the query, you can query with userId and filter by Read. This will at least help you in saving some costs as you do not need to scan the whole table. However, be aware of Hot Partitions. Link

  2. Consider changing the Read data type to string instead. E.g. It could be values such as Y or N only. As such, you will be able to create a GSI with gsi-userId-Read and this would fulfill what you need.

ShengHow95
  • 236
  • 1
  • 5