DynamoDB using AWS SDK for Javascript/Node.js
Tags: dynamodb, aws, javascript, node
Published: 2023-03-07 05:28:47


**What is DynamoDB?**

Amazon DynamoDB is a fully managed NoSQL database service. It allows you to work with Big Data. It provides features like:

- Seamless on-demand scaling.
- Unlimited concurrent read/write operations.
- Single-digit millisecond latency.
- Microsecond latency with DAX ( caching service).
- Fault tolerant, supports cross-region replication and highly scalable.
- Can process largely unstructured data with high volume and frequency.
- Pricing based on capacity you provision for each table.

_DynamoDB must have a primary key._

**Data Types in DynamoDB**

1. Scalar Types : _Represents exactly one value which can be string, numbers, binary, boolean or null._
2. Set Types : _Represents multiple scalar values which can se string set, number set or binary set._
3. Document Types : _It has complex structure with nested attributes which ca be map or list._

**Pre-Requisites for using DynamoDB with AWS SDK in Node.js/ Javascript**

Firstly make sure the aws-cli is installed in your system along with the aws environment variable and configurations set up. You can check this by simply running command: aws --version.

Go to terminal and run the instructions:

1. npm init
2. npm install aws-sdk — -save

---

> **_Basic Table operations in DynamoDB using AWS SDK_**


- **Listing DynamoDB tables**

```
//referencing sdk in js file
const AWS = require("aws-sdk");
//specifying aws region where dynamodb table will be created
AWS.config.update({ region: 'us-east-1' });
//instantiate dynamodb class
const dynamodb = new AWS.DynamoDB();
//listing tables
dynamodb.listTables({}, (err, data)=>{
    if(err) {
        console.log(err);
    } else {
        console.log(data);
    }
});
```

- **Describe Tables**

```
dynamodb.describeTable({
     TableName: "demo_sdk"
 }, (err, data)=>{
     if(err) {
         console.log(err);
     } else {
         console.log(JSON.stringify(data, null, 2));// '2' is to beautify the output 
     }
 });
```

- **Create Table**

Provisioned throughput here allows for predictable performance at scale. It is a major factor in pricing and is defined in read capacity units(RCUs) and write capacity units(WCUs).

1 capacity unit= 1 request per second

```
dynamodb.createTable({
    TableName: "demo_sdk",
    AttributeDefinitions: [
        {
            AttributeName: "id",
            AttributeType: "S" //string
        },
        {
            AttributeName: "timestamp",
            AttributeType: "N" //number
        }
    ],
    KeySchema: [
        {
            AttributeName: "id",
            KeyType: "HASH"
        },
        {
            AttributeName: "timestamp",
            KeyType: "RANGE"
        }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 1,
        WriteCapacityUnits: 1
    }
}, (err, data)=>{
    if(err) {
        console.log(err);
    } else {
        console.log(JSON.stringify(data, null, 2));
    }
});
```

- **Update Table**

```
//Changing read capacity units from 1 to 2 for the table
dynamodb.updateTable({
    TableName: "demo_sdk",
    ProvisionedThroughput: {
        ReadCapacityUnits: 2,
        WriteCapacityUnits: 1
    }
}, (err, data) => {
    if(err) {
        console.log(err);
    } else {
        console.log(JSON.stringify(data, null, 2));
    }
});

```
- **Delete table**

```
dynamodb.deleteTable({
    TableName: "demo_sdk"
}, (err, data) => {
    if(err) {
        console.log(err);
    } else {
        console.log(JSON.stringify(data, null, 2));
    }
});
```

You can view the changes after each operations by using the describeTable function of dynamodb. All the changes will be reflected in the AWS Management console by writing just a few lines of code and without manually clicking and doing the tedious tasks.

---
If you go through the syntax for the code snippets carefully it’s quite simple isn’t it? So assuming that now you have got a bit of hold of using AWS SDK for dynamodb we’ll jump to a bit more of detailed table operations.

Earlier we used AWS.DynamoDB class which uses low-level programming approach. Now we’ll use another class of dynamodb which is AWS.DynamoDB.DocumentClient , provides us with high level access while working with items. We say it provides high level access because it abstracts all the unnecessary details from the user like mapping of the internal DynamoDB data types to the JavaScript data types.

> _**High Level Access Table Operations**_

- **Put Item**

The instantiation of the aws sdk and the aws region will remain the same as previous codes.

```
const docClient = new AWS.DynamoDB.DocumentClient();
docClient.put({
    TableName: 'demo_sdk',
    Item: {
// specify attributes as key value pairs
        user_id: 'first',
  //timestamp is the primary key
        timestamp: 3,
        title: 'The Secret',
        content: 'Book'
    }
}, (err, data)=>{
    if(err) {
        console.log(err);
    } else {
        console.log(data);
    }
});
```

- **Update Items**

Want to update the item entry but don’t want to waste time searching and modifying it’s value? DynamoDB provides a solution for that too, which is the docClient.update function.

```
const docClient = new AWS.DynamoDB.DocumentClient();
docClient.update({
    TableName: 'demo_sdk',
    Key: {
        user_id: 'first',
        timestamp: 3
    },
    UpdateExpression: 'set #old = :new',
    ExpressionAttributeNames: {
        '#old': 'The Secret' 
    },
    ExpressionAttributeValues: {
       //Title of item is updated
        ':new': "Hall of Games"
    }
}, (err, data)=>{
    if(err) {
        console.log(err);
    } else {
        console.log(data);
    }
});
```

- **Deleting Items**

Once you start seeing the pattern in the syntax of the code it becomes fairly easy to write it on your own so try this one by yourself and then compare your code.

```
const docClient = new AWS.DynamoDB.DocumentClient();
docClient.delete({
    TableName: 'demo_sdk',
    Key: {
        user_id: 'first',
        timestamp: 3
    }
}, (err, data)=>{
    if(err) {
        console.log(err);
    } else {
        console.log(data);
    }
});
```

- **Batch Write Items**

This function helps us put or delete multiple items at once making our job much simpler when working with large set of table entries.

```
const docClient = new AWS.DynamoDB.DocumentClient();
docClient.batchWrite({
    RequestItems: {
        'demo_sdk': [
            {
              // deletes item , here, only one
                DeleteRequest: {
                    Key: {
                        user_id: 'first',
                        timestamp: 3
                    }
                }
            },
            {
              // inserting two items in the table
                PutRequest: {
                    Item: {
                        user_id: 'new',
                        timestamp: 1,
                        title: 'To Kill a MockingBird',
                        content: 'Harper Lee'
                    }
                }
            },
            {
                PutRequest: {
                    Item: {
                        user_id: 'two',
                        timestamp: 2,
                        title: 'Harry Potter',
                        content: 'J.K Rowling'
                    }
                }
            }
        ]
    }
}, (err, data)=>{
    if(err) {
        console.log(err);
    } else {
        console.log(data);
    }
});
```

- **Get an item**

```
const docClient = new AWS.DynamoDB.DocumentClient();
// to get an item with the corresponding key parameters
docClient.get({
    TableName: 'demo_sdk',
    Key: {
        user_id: 'two',
        timestamp: 2
    }
}, (err, data)=>{
    if(err) {
        console.log(err);
    } else {
        console.log(data);
    }
});
```

- **Query an item**

```
const docClient = new AWS.DynamoDB.DocumentClient();
//here we specify th query condition or the where clause, for instance if we
// have multiple table entries for a user_id and want to get all those items at
//once (in this case we don't, but for the sake of learning XD)
docClient.query({
    TableName: 'demo_sdk',
// condition is: user_id must be equal to the value of expression attribute id
    KeyConditionExpression: "user_id = :id",
    ExpressionAttributeValues: {
        ":id": "new"
    }
}, (err, data)=>{
    if(err) {
        console.log(err);
    } else {
        console.log(data);
    }
});
```

> _**Summary:**_

You have familiarized yourself with a basic understanding of what AWS DynamoDB is and then further learned how to work with dynamodb using simple javascript codes with AWS SDK. Further learned about the classes of dynamodb and the low level and high level table operations like: Create Table, Delete Table, Describe Table, Put item, Get item, Delete item, Batch Write item, Query an item and update the item.

> _**References:**_

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html

---
**Thank you for reading!**
Github Link: https://github.com/rashi2911/dynamo-db-aws-sdk