0

I have started working on dynamodb and nodeJs. I want to create an admin panel for adding new item in which primary and sort keys will be same.

For example: ADD URL is abc.com/admin/add/xyz/cdef/123

I have created an api for the same, also I am using 'put' for creating new item however it is replacing the existing one instead of creating new.

Model Code:

abc.addData= function(newData, callback){  

var params = {
    TableName: tableconcept,
    Item: newData
}

docClient.put(params, function(err, data) {
    if (err) {
       console.error("Unable to create post", ". Error JSON:", 
        JSON.stringify(err, null, 2));
   } else {
    // console.log("Added item:", JSON.stringify(data, null, 2));
       callback(null, data.Items);

        return true;
   }
  });
  }

Routes Code:

router.post('/', function(req, res){
var data = req.body;

var newData = new Object()
newData.data = data ;

abc.addData(newData, function(err, data){

    if (err){
        return res.status(401).json({
            status:'failed',
            message: "unable to create post"
        });
    }


    return res.status(200).json({
        status:'success',
        message: 'Successfully Created post.'          
    });      

})
});

1 Answer 1

2

You can't.

The combination partition and sort keys are the primary key. By definition, you can't have a duplicate.

You could concatenate a timestamp onto the end of the sort key in order to make each unique.

Then when retrieving, a query using a "Sort key starts with" would return all the records.

Sign up to request clarification or add additional context in comments.

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.