hyprstack
hyprstack

Reputation: 4228

DynamoDb Nodejs - Put and Update table

I have the following table in dynamoDB that I want to write value1 to on a first instance and leave value2 blank. I then run extra code and only then do I want to update that item with value2. I am new to dynamodb so am having some trouble with this.

How would I set-up my table so that the first write can contain only value1 and the second write update the item with value2?

As it stands, I get the following error when trying to update: ValidationException: The provided key element does not match the schema

I run the following script to create my Id-map-table:

var params3 = {
    TableName : "my-Id-map",
    KeySchema: [
        { AttributeName: "value1", KeyType: "HASH" },
        { AttributeName: "value2", KeyType: "RANGE"}
    ],
    AttributeDefinitions: [
        { AttributeName: "value1", AttributeType: "S" },
        { AttributeName: "value2", AttributeType: "S" }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 10,
        WriteCapacityUnits: 10
    }
};

dynamodb.describeTable({TableName:"my-Id-map"}, function(err,result) {
     if (err) {
        createIdMapTable();
     }
   else {
        console.log("Table my-Id-map already exists");
    }
});

function createIdMapTable()
{
    dynamodb.createTable(params3, function(err, data) {
        if (err) {
            console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
        } else {
            console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
        }
    });
}

I perform the first write in the following way:

itemManager.saveItemsId = function (value2, value1, callback) {

  value2 = value2.toString();
  value1 = value1.toString();

  var params = {
    TableName: 'my-Id-map',
    Item:{
      "value1": value1,
      "value2": value2
    }
  };

  dynamodb.put(params, callback);
};

I would like to update my item with something like:

    itemManager.updateItemId = function (value2, value1, callback) {
  value2 = value2.toString();
  value1 = value1.toString();

  var params = {
    TableName: 'my-Id-map',
    Key : {
      "value1" : {
        "S" : value1
      }
    },
    UpdateExpression : "SET #attrName =:attrValue",
    ExpressionAttributeNames : {
      "#attrName" : "value2"
    },
    ExpressionAttributeValues : {
      ":attrValue" : {
        "S" : value2
      }
    }
  };

  dynamodb.update(params, callback);
};

Upvotes: 2

Views: 5365

Answers (2)

hyprstack
hyprstack

Reputation: 4228

I have decided to answer my question.

It turns out that because I am creating a compound key (HASH + RANGE), I am trying to update the key itself and dynamodb complains about that. I need to change the way I create the table to have only a HASH key in order for the rest to work.

Creating the table should be:

var params3 = {
    TableName : "my-Id-map",
    KeySchema: [
        { AttributeName: "value1", KeyType: "HASH" }
    ],
    AttributeDefinitions: [
        { AttributeName: "value1", AttributeType: "S" }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 10,
        WriteCapacityUnits: 10
    }
};

So that writing an item:

itemManager.saveItemsId = function (value1, callback) {
  value1 = value1.toString();

  var params = {
    TableName: 'my-Id-map',
    Item:{
      "value1": value1
    }
  };

  dynamodb.put(params, callback);
};

and updating:

itemManager.updateItemId = function (value2, value1, callback) {
  value2 = value2.toString();
  value1 = value1.toString();

  var params = {
    TableName: 'my-Id-map',
    Key : {
      "value1" : value1
    },
    UpdateExpression : "SET #attrName =:attrValue",
    ExpressionAttributeNames : {
      "#attrName" : "value2"
    },
    ExpressionAttributeValues : {
      ":attrValue" : value2
    }
  };

  dynamodb.update(params, callback);
};

Upvotes: 3

arjun kori
arjun kori

Reputation: 1120

As i can see that your createTable() and describeTable() Syntax are right but update and insert commands are wrong.

In your insert command there should be putItems() and in update there should be updateItem.

Here i m giving you syntax. This is Syntax for putItem i.e insert command

itemManager.saveItemsId = function(value2, value1, callback) {

value2 = value2.toString();
value1 = value1.toString();

var params = {
    TableName: 'my-Id-map',
    Item: {
        'value1': {
            'S': value1
        },
        'vallue2': {
            'S': value2
        }
    }
};
dynamodb.putItem(params, function(err, result) {
    if (err) {
        console.log("Error")
    } else {
        console.log("Data saved Success")
    }
})

};

For update the table :

itemManager.updateItemId = function(value2, value1, callback) {

value2 = value2.toString();
value1 = value1.toString();

var params = {
    TableName: 'my-Id-map',
    Key: {
        id: {
            'S': id
        }
    },
    UpdateExpression: 'SET #attrName =:attrValue',
    ExpressionAttributeNames: {
        "#attrName": "value2"
    },
    ExpressionAttributeValues: {
        ':attrValue': {
            'S': value2
        },
    }
};
dynamodb.updateItem(params, function(err, data) {
    if (err) {
        console.log("Error while update")

    } else {
        console.log("Data updated Successfully")
    }
})

};

Also you can refer this link dynamodbSyntax

Upvotes: 5

Related Questions