Created
February 12, 2018 01:55
-
-
Save Troy-Yang/07711dd95b647a8e1e0784876daecb95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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