Using Amazon DynamoDB Database With AWS Lambda In Nodejs

Posted By : Ankit Uniyal | 29-Sep-2018

The goal of this blog to understand about AWS DynamoDB database and connect the Nodejs application with AWS DynamoDB database in AWS Lambda environment.

 

 

Amazon DynamoDB is a fast high-performance cost-effective serverless NoSQL database that can scale on demand to support the virtually unlimited number of read-write operations with response times under single milliseconds.

 

With DynamoDB accelerators and tags caching services, you can bring down these response times further into microseconds.

 

DynamoDB provides seamless scaling and predictable performance is serverless cloud database meaning you don't have to specify how many servers's you need or what kind of backend infrastructure you need, all you must tell them dynamoDB is how many tables reads and writes your application perform per second and that's it and you are good to go with dynamoDB.

 


Before we get into dynamoDB integration with Nodejs, let's quickly compare SQL and NoSQL technologies.

 

In SQL's we have tables and in DynamoDB, we have tables as well. SQL tables have rows and tables and dynamoDB tables have items and attributes.

 

SQL tables have primary keys that can have several columns whereas dynamoDB tables have a primary key that can have minimum one and maximum two attributes. The mandatory attribute is called as partition key and the optional attribute is called as a sort key.

 

In SQL, we have indexes whereas in case of dynamoDB, local secondary indexes exists. In SQL we have views whereas in case of dynamoDB, global secondary indexes exists.

 

Now, what are these local and global secondary indexes as local secondary indexes are those indexes in which the partition key is same as that of the primary key whereas in case of global secondary indexes are those indexes in which partition key is different from primary key.

 

Now, we will create a dynamoDB table using Nodejs and then perform some operations i.e insert data into the dynamoDB table and get data from the table using Nodejs.

 

Create DynamoDB table: 

 

const dynamodb = new AWS.DynamoDB();

let params = {
    TableName: "SupervisorsTable",
    KeySchema: [{
        AttributeName: "name",
        KeyType: "HASH"
    }],
    AttributeDefinitions: [{
            AttributeName: "name",
            AttributeType: "S"
        },
        {
            AttributeName: "company",
            AttributeType: "S"
        },
        {
            AttributeName: "factory",
            AttributeType: "S"
        }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 5,
        WriteCapacityUnits: 5
    },
    GlobalSecondaryIndexes: [{
        IndexName: "FactoryIndex",
        KeySchema: [{
                AttributeName: "company",
                KeyType: "HASH"
            },
            {
                AttributeName: "factory",
                KeyType: "RANGE"
            }
        ],
        Projection: {
            ProjectionType: "ALL"
        },
        ProvisionedThroughput: {
            ReadCapacityUnits: 1,
            WriteCapacityUnits: 1
        }
    }]
};

dynamodb.createTable(params, callback);    
        

 

PutAttributes to DynamoDB table :

 

var params = {
    TableName: "SupervisorsTable",
    Item: {
        name: {
            S: name
        },
        company: {
            S: company
        },
        factory: {
            S: factory
        }
    }
};

dynamodb.putItem(params, callback);     
        

 

Get Attributes from DynamoDB table: 

 

const docClient = new AWS.DynamoDB.DocumentClient();

let params = {
    TableName: "SupervisorsTable",
    IndexName: "FactoryIndex",
    KeyConditionExpression: "#company = :companyValue and #factory = :factoryValue",
    ExpressionAttributeNames: {
        "#company": "company",
        "#factory": "factory"
    },
    ExpressionAttributeValues: {
        ":companyValue": company,
        ":factoryValue": factory
    }
};

docClient.query(params, callback);

 

Thanks.

About Author

Author Image
Ankit Uniyal

Ankit has knowledge in Javascript, NodeJS, AngularJS and MongoDB also have experience in using AWS Services.

Request for Proposal

Name is required

Comment is required

Sending message..