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);
};