I want to create a table and want to create 6-7 columns/attributes using Dynamodb(NodeJs). I have created a table but I am not able to add more than 2 attributes.I am new to this platform, can anyone help me to create multiple attributes in one table.
On DynamoDB, you must define only the Hash Key and optionally a Sort Key for your table. The rest of the attributes don't have to be defined! You can push whatever data you want.
Check out the example below, based on the official docs.
I'm creating a table Movies with Hash: Year and Sort: Title.
Then I'm creating a movie with more attributes:
var AWS = require("aws-sdk");
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:8000"
});
var client = new AWS.DynamoDB();
var documentClient = new AWS.DynamoDB.DocumentClient();
var tableName = "Movies";
var params = {
TableName: tableName,
KeySchema: [
{ AttributeName: "year", KeyType: "HASH"}, //Partition key
{ AttributeName: "title", KeyType: "RANGE" } //Sort key
],
AttributeDefinitions: [
{ AttributeName: "year", AttributeType: "N" },
{ AttributeName: "title", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
};
client.createTable(params, function(tableErr, tableData) {
if (tableErr) {
console.error("Error JSON:", JSON.stringify(tableErr, null, 2));
} else {
console.log("Created table successfully!");
}
// Adding Batman movie to our collection
var params = {
TableName: tableName,
Item: {
"year": 2005,
"title": "Batman Begins",
"info": {
"plot": "A young Bruce Wayne (Christian Bale) travels to the Far East.",
"rating": 0
}
}
};
console.log("Adding a new item...");
documentClient.put(params, function(err, data) {
if (err) {
console.error("Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Added item successfully!");
}
});
});
In dynamoDB, attributes will created automatically when you add an Item to the db.
While creating a table, we only specify the primary key and an optional sort key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With