Connecting Data from DynamoDB with NodeJS

Connecting Data From DynamoDB With Node.js

Connecting Data from DynamoDB with NodeJS

DynamoDB is a unique hosted NoSQL database that Amazon Web Services (AWS) offers. This database boasts reliable performance and scalability, a managed experience, and a simple API. 

DynamoDB is very popular for its query examples; we use it in cases that demand real-time data-intensive apps like Leaderboards, Gaming, etc. 

Amazon Web Services also offers libraries for several popular programming stacks to work and connect with DynamoDB like DynamoDB with NodeJS. This article looks at connecting data between DynamoDB with NodeJS and some of the best DynamoDB query examples. 

DynamoDB with Node JS: reading, writing, examples and more

NodeJS is a cross-platform, open-source, back-end JavaScript runtime environment, and it executes JavaScript codes outside web browsers while running on the V8 engine. Also, NodeJS is great at creating, opening, writing, reading, closing, and deleting close files on servers. 

Here we find out how to connect data from DynamoDB with NodeJS. Make sure you log in to your AWS account when making a DynamoDB NodeJS connection, create a group and a user.  You should also receive an Access Key ID and a Secret access key downloadable in a CSV file.

Create a table, insert relevant records into your table, and use this code app.js with your Access key ID and Secret access key, region and table name. Follow these code when connecting data from DynamoDB with NodeJS:

let app=require(‘express’)(); let dynamoRoute= require(‘./dynamo’); app.use(‘/’,dynamoRoute); app.listen(‘4000’,(err)=>{ if(err) console.log(err) else console.log(‘server running’); }) module.exports=app;

dynamo.js

let AWS = require(‘aws-sdk’); let express = require(‘express’); let router = express.Router(); let config = require(‘./config/dev’); AWS.config.update({ “region”: “”, “accessKeyId”: “”, “secretAccessKey”: “” }); let docClient = new AWS.DynamoDB.DocumentClient(); let table = “sports”; router.get(‘/fetch’, (req, res) => { let spid = ‘101’; let params = { TableName: table, Key: { spid: spid } }; docClient.get(params, function (err, data) { if (err) { console.log(err); handleError(err, res); } else { handleSuccess(data.Item, res); } }); }); function handleError(err, res) { res.json({ ‘message’: ‘server side error’, statusCode: 500, error: err }); } function handleSuccess(data, res) { res.json({ message: ‘success’, statusCode: 200, data: data }) }

Need help connecting DynamoDB with NodeJS?

We have an experienced team ready to help you. Don’t hesitate to drop us a line and schedule a consultation.



Get in touch

Reading and Writing A Single Item in DynamoDB

While we focus on connecting data relating to DynamoDB with NodeJS, knowing and writing a single item in DynamoDB is also important. 

In our example, we use several Node.js modules to write and read one item in a DynamoDB while using some methods of AWS.DynamoDB client class, such as getItem, putItem, and deleteItem.

In setting up and running our example, there are some tasks you will need to complete. Firstly, you need to install NodeJS. Next, create a shared configurations file and do it with your user credentials. 

Lastly, create a DynamoDB table with accessible items. Once that is done, you can try writing an item, getting an item, or deleting it in DynamoDB NodeJS. Below are the examples and codes to guide you:

#1. Writing An Item

In writing an item in DynamoDB NodeJS, you must create a Node.js module. Create it using the file name ddb_putitem.js. Make sure you configure the Software Development Kit (SDK). 

Accessing DynamoDB will require you to create a service object, AWS.DynamoDB. Next, create a JavaScript Object Notation (JSON) that contains the relevant parameters you need to add an item, such as the table’s name, including a map that defines the attributes to set and values for every attribute. 

You can call the putItem method of the DynamoDB service object. And when you’re done and you need to run the example, type node ddb_putitem.js at the command line. 

// Load the AWS SDK for Node.js

var AWS = require(‘aws-sdk’);

// Set the region 

AWS.config.update({region: ‘REGION’});

// Create the DynamoDB service object

var ddb = new AWS.DynamoDB({apiVersion: ‘2012-08-10’});

var params = {

  TableName: ‘CUSTOMER_LIST’,

  Item: {

    ‘CUSTOMER_ID’ : {N: ‘001’},

    ‘CUSTOMER_NAME’ : {S: ‘Richard Roe’}

  }

};

// Call DynamoDB to add the item to the table

ddb.putItem(params, function(err, data) {

  if (err) {

    console.log(“Error”, err);

  } else {

    console.log(“Success”, data);

  }

});

#2. Getting An Item

In getting an item in DynamoDB NodeJS, create a Node.js module with ddb_getitems.js as its file name. Configure the SDK like in the previous step and access DynamoDB by creating an AWS.DynamoDB service object.

To identify the item you’re getting, you need to provide the primary key’s value for the said item in the table. What’s more, the getItem method will return all the attribute values defined for the item by default.

Specify a projection expression so you can get only a subset of every possible attribute value. In writing an item, you also need to create a JSON object containing the necessary parameters required to get an item. 

Call the getItem method of the DynamoDB service object and when you’re done and you need to run the example, type node ddb_getitem.js at the command line.

// Load the AWS SDK for Node.js

var AWS = require(‘aws-sdk’);

// Set the region 

AWS.config.update({region: ‘REGION’});

// Create the DynamoDB service object

var ddb = new AWS.DynamoDB({apiVersion: ‘2012-08-10’});

var params = {

  TableName: ‘TABLE’,

  Key: {

    ‘KEY_NAME’: {N: ‘001’}

  },

  ProjectionExpression: ‘ATTRIBUTE_NAME’

};

// Call DynamoDB to read the item from the table

ddb.getItem(params, function(err, data) {

  if (err) {

    console.log(“Error”, err);

  } else {

    console.log(“Success”, data.Item);

  }

});

#3. Deleting An Item

In deleting an item, create a Node.js module and use ddb_deleteitem.js as the file name, making sure to configure the SDK. Accessing DynamoDB means creating an AWS.DynamoDB service object.

You also need to create a JSON object here, and it should contain the parameters you need to delete an item, such as the table’s name, value, and key name for the item you’re deleting. Next, call the deleteItem method of your DynamoDB service object. 

When you’re done, you can type node ddb_deleteitem.js at the command line to run the example.

// Load the AWS SDK for Node.js

var AWS = require(‘aws-sdk’);

// Set the region 

AWS.config.update({region: ‘REGION’});

// Create the DynamoDB service object

var ddb = new AWS.DynamoDB({apiVersion: ‘2012-08-10’});

var params = {

  TableName: ‘TABLE’,

  Key: {

    ‘KEY_NAME’: {N: ‘VALUE’}

  }

};

// Call DynamoDB to delete the item from the table

ddb.deleteItem(params, function(err, data) {

  if (err) {

    console.log(“Error”, err);

  } else {

    console.log(“Success”, data);

  }

});

The Best 5 DynamoDB Query Examples

In DynamoDB, queries help locate secondary indices or items via primary keys. Therefore, a query needs a specific value and partition key with an option for filtering with comparisons. The query returns every attribute for items associated with the primary key by default. 

Besides, users can provide a sort key attribute while using a comparison operator to refine search results. This means that the Query operation in DynamoDB finds items based on some relevant primary key values. 

One thing to look out for and pay keen attention to in these examples is the content of the params that are passed to the different methods of our DynamoDB Document Client interface or  DynamoDB service interface.

To make it easier to use nodes to see the results of your queries, write them as async functions with a console.log() of the operation’s result, although you can choose not to do this. That being said, here are some of the best 5 DynamoDB query examples:

#1. Getting a Single Item with the DynamoDB Service Interface

One of the top DynamoDB query examples is getting a single item with the DynamoDB service interface. We use the getItem operation async function to get a single item with the DynamoDB service interface.

Once you’ve applied the code below, the result of your request should be an object with an item attribute that contains data for a single item. More so, within the said item, there are several unique attributes that a data type descriptor of “N” (numbers) or “S” (strings) describes.

// Get a single item with the getItem operation

async function logSingleItem(){

    try {

        var params = {

            Key: {

             “artist”: {“S”: “Arturus Ardvarkian”}, 

             “song”: {“S”: “Carrot Eton”}

            }, 

            TableName: tableName

        };

        var result = await dynamodb.getItem(params).promise()

        console.log(JSON.stringify(result))

    } catch (error) {

        console.error(error);

    }

}

logSingleItem()

// { 

//    “Item”:{ 

//       “priceUsdCents”:{ 

//          “N”:”161″

//       },

//       “artist”:{ 

//          “S”:”Arturus Ardvarkian”

//       },

//       “song”:{ 

//          “S”:”Carrot Eton”

//       },

//       “publisher”:{ 

//          “S”:”MUSICMAN INC”

//       },

//       “id”:{ 

//          “S”:”dbea9bd8-fe1f-478a-a98a-5b46d481cf57″

//       }

//    }

// }

#2. Using the DynamoDB Service Interface to Scan the DynamoDB Table

Another top DynamoDB query example is using the DynamoDB service interface to scan the DynamoDB table. Here, we use the DynamoDB client scan operation to get or retrieve all items of the table and scanForResults async function

However, DynamoDB table scans are typically not efficient operations. That being said, the scan operation comes in a clutch when you don’t need to get all the data out of the table while deciding against using other options or when you don’t care what items you get back. Here’s an example:

// Use the DynamoDB client scan operation to retrieve all items of the table

async function scanForResults(){

    try {

        var params = {

            TableName: tableName

        };

        var result = await dynamodb.scan(params).promise()

        console.log(JSON.stringify(result))

    } catch (error) {

        console.error(error);

    }

}

scanForResults()

// { 

//    “Items”:[ 

//       { 

//          “priceUsdCents”:{ 

//             “N”:”207″

//          },

//          “artist”:{ 

//             “S”:”Marguerite Mcclure”

//          },

//          “song”:{ 

//             “S”:”Cedar Columbia”

//          },

//          “publisher”:{ 

//             “S”:”TELEQUIET”

//          },

//          “id”:{ 

//             “S”:”4e01c867-3084-4ae4-9c8a-5b0750465037″

//          }

//       },

//       …

//    ],

//    “Count”:70,

//    “ScannedCount”:70

// }

Note that our table is small enough to return all the items in the table, but if we had a bigger DynamoDB table or bigger items, we might reach a data limit on the data we can get back in one call. What’s more, we would be getting a back value for continuing our scan operation; suppose we were iterating over all the table data.

#3. Using the Document Client to Get a Single Item from DynamoDB

Our third instance of the top 5 DynamoDB query examples is using the Document Client to get a single item from DynamoDB. Here, we use the getItem operation and Document Client async function logSingleItemDdbDc.

We begin by following a slightly similar approach as with our first example while getting a single item from DynamoDB.

// Get a single item with the getItem operation and Document Client

async function logSingleItemDdbDc(){

    try {

        var params = {

            Key: {

             “artist”: “Arturus Ardvarkian”, 

             “song”: “Carrot Eton”

            }, 

            TableName: tableName

        };

        var result = await ddbDocumentClient.get(params).promise()

        console.log(JSON.stringify(result))

    } catch (error) {

        console.error(error);

    }

}

logSingleItemDdbDc()

// { 

//    “Item”:{ 

//       “priceUsdCents”:161,

//       “artist”:”Arturus Ardvarkian”,

//       “song”:”Carrot Eton”,

//       “publisher”:”MUSICMAN INC”,

//       “id”:”dbea9bd8-fe1f-478a-a98a-5b46d481cf57″

//    }

// }

Know that the result doesn’t contain any of the Data Type Descriptors such as “S” and “N.” The document client automatically converts data into native JavaScript types to make things easier. Besides, unlike getting a single item with the DynamoDB service interface, we do not include Data Type descriptors to params.

#4. Querying for All Songs by Artist with the DynamoDB Service Interface

The next DynamoDB query example on our list is querying artists’ songs with the DynamoDB service interface. Here, we use the query operation to get all song by artist Arturus Ardvarkian.

// Use the query operation to get all song by artist Arturus Ardvarkian

async function logSongsByArtist(){

    try {

        var params = {

            KeyConditionExpression: ‘artist = :artist’,

            ExpressionAttributeValues: {

                ‘:artist’: {‘S’: ‘Arturus Ardvarkian’}

            },

            TableName: tableName

        };

        var result = await dynamodb.query(params).promise()

        console.log(JSON.stringify(result))

    } catch (error) {

        console.error(error);

    }

}

logSongsByArtist()

// { 

//    “Items”:[ 

//       { 

//          “priceUsdCents”:{ 

//             “N”:”312″

//          },

//          “artist”:{ 

//             “S”:”Arturus Ardvarkian”

//          },

//          “song”:{ 

//             “S”:”Baker Firebrick”

//          },

//          “publisher”:{ 

//             “S”:”MUSICMAN INC”

//          },

//          “id”:{ 

//             “S”:”1a4e5bc5-4fa3-4b37-9d36-e15dc9ab6b21″

//          }

//       },

//       …

//    ],

//    “Count”:9,

//    “ScannedCount”:9

// }

With querying for all songs by artists with the DynamoDB service interface, we return objects with multiple items. The params used in the query contain a KeyConditionExpression that helps look for the artist property of “artist” to match the value of :artist we store in the ExpressionAttributeValues.

Besides, in this case, the value is an object containing the attribute type descriptor and, of course, the value: {‘:artist’: {‘S’: ‘Arturus Ardvarkian’}. When we work with the service interface, we have to make sure to specify the value in this manner correctly.

#5. Using the DynamoDB Service Interface and “begins_with” to Find an Artist’s Songs that Start With “C”

Another DynamoDB query example is using the DynamoDB service interface and begins_with to find an artist’s song that begins with “C”. To add another condition to your query, you can use the sort key operators, one of which is begins_with.

You extend the KeyConditionExpression from a simple equality operator you used on the artist partition key and add the new begins_with operator. The song attribute starts with a “C” for every item. What’s more, we can create the value that we need the sort key, to begin with in our ExpressionAttributeValues:

// Query songs by artist “Arturus Ardvarkian” that start with “C”

async function logArtistSongsStartingWithC(){

    try {

        var params = {

            KeyConditionExpression: ‘artist = :artist AND begins_with ( song , :song )’,

            ExpressionAttributeValues: {

                ‘:artist’: {‘S’: ‘Arturus Ardvarkian’},

                ‘:song’: {‘S’: ‘C’}

            },

            TableName: tableName

        };

        var result = await dynamodb.query(params).promise()

        console.log(JSON.stringify(result))

    } catch (error) {

        console.error(error);

    }

}

logArtistSongsStartingWithC()

// { 

//    “Items”:[ 

//       { 

//          “priceUsdCents”:{ 

//             “N”:”142″

//          },

//          “artist”:{ 

//             “S”:”Arturus Ardvarkian”

//          },

//          “song”:{ 

//             “S”:”Cadet Celadon”

//          },

//          “publisher”:{ 

//             “S”:”MUSICMAN INC”

//          },

//          “id”:{ 

//             “S”:”fd7667cb-3a41-4777-93bb-ed2d0d8d7458″

//          }

//       },

//       …

//    ],

//    “Count”:4,

//    “ScannedCount”:4

// }

Do you want an Experienced team to help you with your DynamoDB Query?

Feel free to drop us a line to receive a relevant consultation.



Get in touch

Conclusion

DynamoDB, a NoSQL database, is a great fit for applications with strict latency requirements and big amounts of data. Users who need data sets with simple and known access patterns and require serverless apps using AWS Lambda will find DynamoDB resourceful. 

With a cross-platform, open-source, back-end JavaScript runtime environment like NodeJS capable of generating dynamic page content, adding, modifying, deleting data in your database, or collecting form data, DynamoDB NodeJS is very resourceful.

Leave a Reply

Your email address will not be published. Required fields are marked *