Node.js

Query DynamoDB Items with Node.js Part 2

On a previous post we had the chance to issue some basic DynamoDB query actions. However apart from the basic actions the DynamoDB api provides us with some extra functionality.

Projections is a feature that has a select-like functionality. You choose which attributes from a DynamoDB Item shall be fetched. Keep in mind that using projection will not have any impact on your query billing.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
var getRegisterDate = function(email,callback) {
     
    var docClient = new AWS.DynamoDB.DocumentClient();
     
    var params = {
            TableName: "Users",
            KeyConditionExpression: "#email = :email",
            ExpressionAttributeNames:{
                "#email": "email"
            },
            ExpressionAttributeValues: {
                ":email":email
            },
            ProjectionExpression: 'registerDate'
        };
     
    docClient.query(params,callback);
}

Apart from selecting the attributes we can also specify the order according to our range key. We shall query the logins Table in a Descending order using scanIndexForward.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
var fetchLoginsDesc = function(email,callback) {
 
    var docClient = new AWS.DynamoDB.DocumentClient();
 
    var params = {
        TableName:"Logins",
        KeyConditionExpression:"#email = :emailValue",
        ExpressionAttributeNames: {
            "#email":"email"
        },
        ExpressionAttributeValues: {
            ":emailValue":email
        },
        ScanIndexForward: false
    };
     
    docClient.query(params,callback);
}

A common functionality of databases is counting the items persisted in a collection. In our case we want to count the login occurrences of a specific user. However pay extra attention since the count functionality does nothing more than counting the total items fetched, therefore it will cost you as if you fetched the items.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
var countLogins = function(email,callback) {
 
    var docClient = new AWS.DynamoDB.DocumentClient();
 
    var params = {
        TableName:"Logins",
        KeyConditionExpression:"#email = :emailValue",
        ExpressionAttributeNames: {
            "#email":"email"
        },
        ExpressionAttributeValues: {
            ":emailValue":email
        },
        Select:'COUNT'
    };
     
    docClient.query(params,callback);
}

Another feature of DynamoDB is getting items in batches even if they belong on different tables. This is really helpful in cases where data that belong on a specific context are spread through different tables. Every get item is handled and charged as a DynamoDB read action. In case of batch get item all table keys should be specified since every query’s purpose on BatchGetItem is to fetch a single Item.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
var getMultipleInformation = function(email,name,callback) {
     
    var params = {
            "RequestItems" : {
                "Users": {
                  "Keys" : [
                    {"email" : { "S" : email }}
                  ]
                },
                "Supervisors": {
                   "Keys" : [
                    {"name" : { "S" : name }}
                  ]
                }
              }
            };
     
    dynamodb.batchGetItem(params,callback);
};

You can find the sourcecode on github

Reference: Query DynamoDB Items with Node.js Part 2 from our WCG partner Emmanouil Gkatziouras at the gkatzioura blog.
Do you want to know how to develop your skillset to become a Web Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Building web apps with Node.js
2. HTML5 Programming Cookbook
3. CSS Programming Cookbook
4. AngularJS Programming Cookbook
5. jQuery Programming Cookbook
6. Bootstrap Programming Cookbook
and many more ....
I agree to the Terms and Privacy Policy

Emmanouil Gkatziouras

He is a versatile software engineer with experience in a wide variety of applications/services.He is enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button