While building my most recent web app, I was deploying the app to AWS and using DynamoDB for storage. DynamoDB is a NoSql database by AWS. I have written about how to CRUD data to it in an earlier post but this post talks about how to query DynamoDB with non-primary key column in the where clause. This post will include code samples along with step-by-step instructions on how to configure AWS DynamoDB admin to enable filtering rows by non-primary key column.

How to query DynamoDB with non-primary key column

In this tutorial we will use the table defined in this repo cptdanko/node_typescript_crud_notes (github.com) to create an index. Based on that repository, if you had to create an index for the user_id column for the Todo table, you would follow the steps below,

  1. Open DynamoDB dashboard in AWS console
  2. Click on tables
  3. Open table details in the column
  4. Click on the index tab
  5. From the Actions drop-down click on Create index
  6. Enter the following values in the fields you see
    • Partition key: user_id
    • Sort key – you can leave it blank
    • Index name – user_id-index (it can be anything you want)
  7. Click on the Create index button

Great and having created the index, you can now query the Todo table and filter the values by the user_id column.

In terms of how to retrieve it in your NodeJS code, while the code below is not efficient, it is sufficient to demonstrate the concept talked about in this post.

  /**
   * While the code below works
   * DocumentClient.scan is very ineffecient, as such
   * Improve this as part of another issue
   * @param userId 
   * @returns 
   */
  async getTodoByUser(userId: string): Promise<QueryOutput> {
    const params = {
      TableName: TODO_TABLE,
      FilterExpression: '#userid = :user',
      ExpressionAttributeNames: {
        "#userid": "user_id"
      },
      ExpressionAttributeValues: {
        ':user': userId
      },
    };
    return this.documentClient.scan(params).promise();
  }

That’s it, you can refer to the full code to query DynamoDB in this file here of this repository.

node_typescript_crud_notes/ddbTodo.ts at main · cptdanko/node_typescript_crud_notes (github.com)

Conclusion

Hope you found this post useful, and it helps you understand how to query DynamoDB.

If you find any of my posts useful and want to support me, you can buy me a coffee 🙂

https://www.buymeacoffee.com/bhumansoni

While you are here, maybe try one of my apps for the iPhone.

Products – My Day To-Do (mydaytodo.com)

Have a read of some of my other posts on AWS

Deploy NodeJS, Typescript app on AWS Elastic beanstalk – (mydaytodo.com)

How to deploy spring boot app to AWS & serve via https – My Day To-Do (mydaytodo.com)

Some of my other posts on Javascript …

What is Javascript event loop? – My Day To-Do (mydaytodo.com)

How to build a game using Vanilla Javascript – My Day To-Do (mydaytodo.com)

Vanilla Javascript: Create Radio Buttons (How-To) – Bhuman Soni (mydaytodo.com)

Java Spring Boot & Vanilla Javascript solution – My Day To-Do (mydaytodo.com)

Vanilla Javascript: Create Radio Buttons (How-To) – Bhuman Soni (mydaytodo.com)

Categories: AWSJavascript

0 Comments

Leave a Reply

Avatar placeholder
Verified by MonsterInsights