node.js: How to add non-key attribute in DynamoDB while table creation?

Viewed 2281

I am using dynamoDB local. I want to create a table with 6 attributes, only one of them is key. How do I do that? Specify the key attribute in keySchema and all attributes in AttributeDefinitions?

var params = {
    TableName : "Movies",
    KeySchema: [
        { AttributeName: "year", KeyType: "HASH"},  //Partition key
    ],
    AttributeDefinitions: [
        { AttributeName: "year", AttributeType: "N" },
        { AttributeName: "title", AttributeType: "S" }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 10,
        WriteCapacityUnits: 10
    }
};

dynamodb.createTable(params, 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));
    }
});
2 Answers

Short answer: You can't.

You can't add non-key attributes while creating the table. You have to create the table first, then add non-key attributes using dynamodb.putItem() or dynamodb.batchWriteItem().

Example:

var params = {
  TableName: 'Movies',
  Item: {
    'year': {N: '1994'},
    'title': {S: 'Pulp Fiction'},
    'description': {S: 'Two hitmen with a penchant for philosophical discussions.'},
  }
}

dynamodb.putItem(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data);
  }
});
Related