I summarized how to get DynamoDB data with node.js

Posted in aws, blog on November 4, 2020 by Henk Verlinde ‐ 3 min read

I summarized how to get DynamoDB data with node.js

Introduction

I personally had the opportunity to use DynamoDB, and it was difficult to understand how to operate with node from the AWS documentation, so I modified and coded it myself.

There are various ways to retrieve data stored in DynamoDB on aws, so I will summarize the ones that I personally use frequently below.

How to get them individually using getItem()

This is the simplest method of data acquisition.

By specifying the partition key, you can get only one data. I think that it should be used when acquiring login user information etc.

However, since only the partition key can be specified, it is not very convenient to use.

The following examples use id as the partition key.

const dynamodb = new AWS.DynamoDB({ region: "ap-northeast-1" });

router.get("/get", (req, res, next) => {
  const params = {
    TableName: "user",
    Key: {
      id: { S: 1 }
    }
  };
  dynamodb.getItem(params, function(err, user) {
    const user = {
      id: "",
      name: "",
    };
    user.id = user.Item.id.S;
    user.name = user.Item.name.S;
    res.json(user);
  });
});

How to get summary data with batchGetItem()

Since there is getItem, I thought there should be getItems, but there is none.

As an alternative, we had batchGetItem, so let’s use this.

This also specifies the partition key, but unlike getItem, it returns the data corresponding to the value sent in the array.

Below is an example. As before, id is the partition key.

const dynamodb = new AWS.DynamoDB({ region: "ap-northeast-1" });

router.get("/batchGet", (req, res, next) => {
  const keys = [{id:{S:'1'}},{id:{S:'2'}},{id:{S:'3'}}];
  const params = {
    RequestItems: {
      user: {
        Keys: keys
      }
    }
  };
  dynamodb.batchGetItem(params, function(err, data) {
    data.Responses.user.forEach(function(element, index, array) {
      questions. push({
        id: element.id.S,
        name: element.name.S
      });
    });
    res.send(questions);
  });
});

How to perform detailed search extraction with Query()

This is my personal favorite function.

The problem with getItem and batchGetItem introduced in the previous two is that you can only search by the partition key, and it was difficult to search by another key in the table even if you wanted to. .

It seems that you will be happy if you use Query to search by the partition key of the secondary index.

Below is an example.

const dynamodb = new AWS.DynamoDB({ region: "ap-northeast-1" });

router.get("/query", (req, res, next) => {
  params = {
    TableName: "user",
    IndexName: "name-index",
      KeyConditionExpression: "#attrName = :attrValue",
      ExpressionAttributeNames: {
        "#attrName": "name"
      },
      ExpressionAttributeValues: {
        ":attrValue": { S: "Sakuraba" }
      }
    };
  }
  dynamodb.query(params, function(err, user) {
    return res.send(user.Items);
  });
})

at the end

Looking at the AWS documentation, there are other ways to extract, but I personally use the above three.

It doesn’t look that complicated, so I think it’s easy for beginners.

DynamoDB is easy to maintain and the advantages outweigh the disadvantages.

We want you to be able to use it.

that’s all.