9

I have two tables in my dynamo db one is candidate table and the other one is user table I want to use batchWriteItem in dynamo db in order to add the data in the table.

The query which I have formatted is as follows

var user = {
        userid: usrid,
        role: 'candidate',
        password: vucrypt.encryptpass(pass)
      };

      var canduser = {
        fname: req.body.fname,
        lname: req.body.lname,
        location: req.body.location,
        phone: req.body.phone,
        ccode: req.body.ccode,
        grad: req.body.grad,
        pgrad: req.body.pgrad,
        ograd: req.body.ograd,
        experience: exp,
        linkedin: req.body.linkedin,
        terms: tandc
      };
      canduser = vutools.fixcanduser(canduser);
      canduser.userid = usrid;

      var writes = {
        'users': [{put: user}],
        'candidate': [{put: canduser}],
      };

But if i use
dynamodb.batchWriteItem(writes, function(err, regdata) { }

Its ending up as error. How can I write the right query? The error I am getting is this.

MultipleValidationErrors: There were 3 validation errors:
* MissingRequiredParameter: Missing required key 'RequestItems' in params
* UnexpectedParameter: Unexpected key 'users' found in params
* UnexpectedParameter: Unexpected key 'candidate' found in params

2 Answers 2

13

To batchwrite in DynamoDB, the data must be formated in the dynamodb way. if you want do it in standard json, go for the documentclient. you have an example below, have in mind that dynamobb batchwrite only accept mawimum of 25 element by request.

so according to the doc you must have :

1. Attributes

"ATTRIBUTE_1": { "S": "ATTRIBUTE_1_VALUE" }

According to your example :

"role": {"S":"candidate"}

2. Items

Each item must have this format

      PutRequest: {
        Item: {
            ...,
            "ATTRIBUTE_1": { "S": "ATTRIBUTE_1_VALUE" },
            ...
        }
      }

3. Array of items to add

Create an array of items, which doesn't exceed 25 elements, (it's a dynamodb limit for batchwrite)

4. Your request params

put it together

var params = {
  RequestItems: {
    "TABLE_NAME": [
        //the array you just created in step 3
     ]
   }
}

5. The request

ddb.batchWriteItem(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data);
  }
});

UPDATE

Your example will be something like this :

var params = {
  "RequestItems": {
    "TABLE_NAME": [
      {
        "PutRequest": {
          Item: {
            "userid": { "N": "usrid" },
            "role": { "S": 'candidate' },
            "password": { "S": vucrypt.encryptpass(pass) }
          }
        }
      }
    ],
    "TABLE_NAME2": [
      {
        "PutRequest": {
          Item: {
            "fname": {
              "S": req.body.fname
            },
            "lname": {
              "S": req.body.lname
            },
            "location": {
              "S": req.body.location
            },
            "phone": {
              "S": req.body.phone
            },
            "ccode": {
              "S": req.body.ccode
            },
            "grad": {
              "S": req.body.grad
            },
            "pgrad": {
              "S": req.body.pgrad
            },
            "ograd": {
              "S": req.body.ograd
            },
            "experience": {
              "S": exp
            },
            "linkedin": {
              "S": req.body.linkedin
            },
            "terms": {
              "S": tandc
            }
          }
        }
      }
    ]
  }
}
Sign up to request clarification or add additional context in comments.

12 Comments

The above query is right (Which I wrote) There is table name and all missing I don't know how to put those in the query above can u help me with that?
This is somthing which i am looking for markomedia.com.au/dynamodb-for-javascript-cheatsheet
can you show me an example based on the query which i have wrote?
@ArunVM i added an update, but check the types according to your needs
Thease are two tables 'users': [{put: user}], 'candidate': [{put: canduser}], The answer is only having one table this is where i am getting stuck.
|
4

This is the right answer there are some type problems.

  var createuser = {
    "RequestItems": {
      "users": [{
           "PutRequest": {
               Item: {
                    "userid": {
                        "S": usrid +""
                    },
                    "password": {
                        "S": vucrypt.encryptpass(pass) +""
                    },
                    "role": {
                      "S": 'candidate' +""
                    }
                }
             }
        }],
      "candidate": [{
           "PutRequest": {
             Item: {
                  "ccode": {
                      "S": req.body.ccode +""
                  },
                  "fname": {
                      "S": req.body.fname +""
                  },
                  "lname": {
                      "S": req.body.lname +""
                  },
                  "pgrad": {
                      "S": req.body.pgrad +""
                  },
                  "videoresumeurl": {
                      "S": "-"
                  },
                  "phone": {
                      "S": req.body.phone +""
                  },
                  "terms": {
                      "S": tandc +""
                  },
                  "location": {
                      "S": req.body.location +""
                  },
                  "experience": {
                      "N": req.body.experience +""
                  },
                  "userid": {
                      "S": usrid +""
                  },
                  "grad": {
                      "S": req.body.grad +""
                  }
               }
             }
        }]
    }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.