Skip to content

Instantly share code, notes, and snippets.

@Troy-Yang
Created February 12, 2018 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Troy-Yang/07711dd95b647a8e1e0784876daecb95 to your computer and use it in GitHub Desktop.
Save Troy-Yang/07711dd95b647a8e1e0784876daecb95 to your computer and use it in GitHub Desktop.
// https://docs.aws.amazon.com/zh_cn/sdk-for-javascript/v2/developer-guide/dynamodb-example-document-client.html
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
AWS.config.loadFromPath('./config.json');
// Create DynamoDB document client
var docClient = new AWS.DynamoDB.DocumentClient({ apiVersion: '2012-08-10' });
deleteItem();
// Get item
function getItem() {
var params = {
TableName: 'Room',
Key: {
'name': 'room2'
}
};
docClient.get(params, function (err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Item);
}
});
}
// Query
function query() {
var params = {
TableName: 'Room',
ExpressionAttributeNames: {
"#na": "name"
},
ExpressionAttributeValues: {
':roomName': 'room2'
},
KeyConditionExpression: '#na = :roomName',
};
docClient.query(params, function (err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Items);
}
});
}
// Create
function create() {
var params = {
TableName: 'Room',
Item: {
'name': 'room5',
'apiKey': '46038192'
}
};
docClient.put(params, function (err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
}
// Delete
function deleteItem(){
var params = {
Key: {
'name': 'room5'
},
TableName: 'Room'
};
docClient.delete(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment