Dynamo DB - JS Sample
HTML
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.1.26.min.js"></script>
<div class="container theme-showcase" role="main">
<div class="col-sm-8">
<div class="form-group">
<label class="col-sm-2 control-label">access key</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="accessKeyId" value="" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">secret</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="secretAccessKey" />
<label class="checkbox-inline">
<input type="checkbox" id="startPolling" />start polling</label>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">table content</label>
<div class="col-sm-10">
<textarea class="form-control" id="friends">Bob Alice Sydney
Alice Bob Seattles
Alice Carol London
Alice Dan London</textarea>
</div>
</div>
<div>
<button class="btn btn-default" id="createTable">Create Table</button> <span class="glyphicon" aria-hidden="true" id="createGlyph"></span>
</div>
<div>
<button class="btn btn-default" id="populateTable">Populate Table</button> <span class="glyphicon" aria-hidden="true" id='populateGlyph'></span>
</div>
<button class="btn btn-default" id="queryTable">Query Table</button>
</div>
</div>
JavaScript
var intervalHandle;
$('#startPolling').click(function () {
var polling = $(this).is(':checked');
if (polling) intervalHandle = window.setInterval(pollDynamo, 3000);
else window.clearInterval(intervalHandle);
});
function pollDynamo() {
AWS.config.update({
accessKeyId: $('#accessKeyId').val(),
secretAccessKey: $('#secretAccessKey').val(),
region: 'us-east-2'
});
var dynamodb = new AWS.DynamoDB();
var params = {
TableName: 'Friends' /* required */
};
dynamodb.describeTable(params, function (err, data) {
if (err && err.code === 'ResourceNotFoundException') {
$('#createGlyph').removeClass('glyphicon-ok-sign').addClass('glyphicon-remove-sign');
} else if (err) {
console.log(err, err.stack); // an error occurred
} else {
$('#createGlyph').removeClass('glyphicon-remove-sign').addClass('glyphicon-ok-sign');
}
});
}
$('#createTable').click(function () {
var params = {
AttributeDefinitions: [{
AttributeName: 'User',
AttributeType: 'S'
}, {
AttributeName: 'City',
AttributeType: 'S'
}],
KeySchema: [ /* required */ {
AttributeName: 'User',
/* required */
KeyType: 'HASH' /* required */
}, {
AttributeName: 'City',
/* required */
KeyType: 'RANGE' /* required */
}],
ProvisionedThroughput: { /* required */
ReadCapacityUnits: 1,
/* required */
WriteCapacityUnits: 1 /* required */
},
TableName: 'Friends' /* required */
}
var dynamodb = new AWS.DynamoDB();
dynamodb.createTable(params, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
});
$('#populateTable').click(function () {
var table = new...