0

Apologies if this is a basic question; I'm new to web development. I've been trying to set up a web server using NodeJS and AWS DynamoDB.

I noticed in many tutorials, the command new AWS.DynamoDB(); and new AWS.DynamoDB.DocumentClient(); are in two different .js files (for example, the tutorial here).

Are these files meant to be run simultaneously? Obviously the database should be set up before the call, but all tutorials just say "run this file" then "run this file", which I'm not sure how it applies in a production setting.

I didn't see any require or import statements.

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
Victor M
  • 603
  • 4
  • 22
  • you can check the difference between them in this question https://stackoverflow.com/questions/57804745/difference-between-aws-sdk-dynamodb-client-and-documentclient – Sankalp Chari Nov 19 '21 at 05:00

1 Answers1

1

When you use AWS.DynamoDB.DocumentClient() it simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values. This abstraction annotates native JavaScript types supplied as input parameters, as well as converts annotated response data to native JavaScript types.

A Simple example is :

var params = {
    Item: {
        "Item1": "Value 1",
        "Item2": "Value 1",
    },
    TableName: "Example"
};

var documentClient = new AWS.DynamoDB.DocumentClient();

documentClient.put(params, function (err, data) {
    if (err) console.log(err);
    else console.log(data);
});

However, when you use dynamodb client using new AWS.DynamoDB(), you need to pass types and other notions. A simple example is:

var params = {
    Item: {
        "Item1": { 
             "S" : "Value 1",
         },
        "Item2": {
             "N": 1000,
         }
    },
    TableName: "Example"
};

var documentClient = new AWS.DynamoDB.DocumentClient();

documentClient.put(params, function (err, data) {
    if (err) console.log(err);
    else console.log(data);
});

As per the announcement of the DocumentClient:

The document client abstraction makes it easier to read and write data to Amazon DynamoDB with the AWS SDK for JavaScript. Now you can use native JavaScript objects without annotating them as AttributeValue types.

Does DynamoDB require multiple JS files to be run simultaneously?

No, you don't need to.

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
  • I'm still a bit confused. If we don't need to run multiple JS files simultaneously, how does one set up the database before the actual client makes a call? – Victor M Nov 19 '21 at 20:36
  • Creating multiple JS file gives you modular architecture. You can write it in single file too. The client you choose from the dynamodb instance is of your choice to interact with it. Set up the db- is just creating a connection with to perform CRUD. – Apoorva Chikara Nov 20 '21 at 04:26