npm install aws-sdk
si lancia poi DynamoDB in locale come visto qui
per fare il deploy sui server Amazon del DB si deve decommentare la riga evidenziata in giallo
=========================================================
var AWS = require("aws-sdk");
AWS.config.update({region: "us-west-1",endpoint: "http://localhost:8000"});
// per fare il deploy su DynamoDB sui server Amazon
//AWS.config.update({endpoint: "https://dynamodb.us-west-2.amazonaws.com"});
var dynamodb = new AWS.DynamoDB();
var params = {
TableName : "Informazioni",
KeySchema: [
{ AttributeName: "indice", KeyType: "HASH"}, //Partition key
{ AttributeName: "title", KeyType: "RANGE" } //Sort key
],
AttributeDefinitions: [
{ AttributeName: "indice", 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));
}
});
//////////// INSERT ////////////////////////
var docClient = new AWS.DynamoDB.DocumentClient();
var table = "Informazioni";
var indice = 2015;
var title = "Il nome della rosa";
var params = {
TableName:table,
Item:{
"indice": indice,
"title": title,
}
};
console.log("Adding a new item...");
docClient.put(params, function(err, data) {
if (err) {
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Added item:", JSON.stringify(data, null, 2));
}
});
//////////// QUERY ////////////////////////
var params = {
TableName : table,
KeyConditionExpression: "#yr = :yyyy",
ExpressionAttributeNames:{
"#yr": "indice"
},
ExpressionAttributeValues: {
":yyyy": 2015
}
};
docClient.query(params, function(err, data) {
if (err) {
console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
} else {
console.log("Query succeeded.");
data.Items.forEach(function(item) {
console.log(" -", item.indice + ": " + item.title);
});
}
});
//////////// DELETE ITEM ////////////////////////
var params = {
TableName:table,
Key:{
"indice": indice,
"title": title
},
ConditionExpression:"indice <= :val",
ExpressionAttributeValues: {
":val": 2016
}
};
console.log("Attempting a conditional delete...");
docClient.delete(params, function(err, data) {
if (err) {
console.error("Unable to delete item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("DeleteItem succeeded:", JSON.stringify(data, null, 2));
}
});
var params = {
TableName : table
};
//////////// DELETE TABLE////////////////////////
dynamodb.deleteTable(params, function(err, data) {
if (err) {
console.error("Unable to delete table. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Deleted table. Table description JSON:", JSON.stringify(data, null, 2));
}
});
=========================================================
var AWS = require("aws-sdk");
AWS.config.update({region: "us-west-1",endpoint: "http://localhost:8000"});
// per fare il deploy su DynamoDB sui server Amazon
//AWS.config.update({endpoint: "https://dynamodb.us-west-2.amazonaws.com"});
var dynamodb = new AWS.DynamoDB();
var params = {
TableName : "Informazioni",
KeySchema: [
{ AttributeName: "indice", KeyType: "HASH"}, //Partition key
{ AttributeName: "title", KeyType: "RANGE" } //Sort key
],
AttributeDefinitions: [
{ AttributeName: "indice", 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));
}
});
//////////// INSERT ////////////////////////
var docClient = new AWS.DynamoDB.DocumentClient();
var table = "Informazioni";
var indice = 2015;
var title = "Il nome della rosa";
var params = {
TableName:table,
Item:{
"indice": indice,
"title": title,
}
};
console.log("Adding a new item...");
docClient.put(params, function(err, data) {
if (err) {
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Added item:", JSON.stringify(data, null, 2));
}
});
//////////// QUERY ////////////////////////
var params = {
TableName : table,
KeyConditionExpression: "#yr = :yyyy",
ExpressionAttributeNames:{
"#yr": "indice"
},
ExpressionAttributeValues: {
":yyyy": 2015
}
};
docClient.query(params, function(err, data) {
if (err) {
console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
} else {
console.log("Query succeeded.");
data.Items.forEach(function(item) {
console.log(" -", item.indice + ": " + item.title);
});
}
});
//////////// DELETE ITEM ////////////////////////
var params = {
TableName:table,
Key:{
"indice": indice,
"title": title
},
ConditionExpression:"indice <= :val",
ExpressionAttributeValues: {
":val": 2016
}
};
console.log("Attempting a conditional delete...");
docClient.delete(params, function(err, data) {
if (err) {
console.error("Unable to delete item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("DeleteItem succeeded:", JSON.stringify(data, null, 2));
}
});
var params = {
TableName : table
};
//////////// DELETE TABLE////////////////////////
dynamodb.deleteTable(params, function(err, data) {
if (err) {
console.error("Unable to delete table. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Deleted table. Table description JSON:", JSON.stringify(data, null, 2));
}
});
Nessun commento:
Posta un commento