Amazon DynamoDB and AngularJS

So, I created an AWAM dynamoDB table (database), and I'm ready to get this data using AngularJS. How to do it using AngularJS? Do I need to configure another service using Amazon? Or can I directly access my database?

I could not find anything related to DynamoDB and AngularJS directly. Any help would be greatly appreciated!

+5
source share
2 answers

While the Mars JSON demo is excellent, here is a very simple example to get started using the AWS SDK for JavaScript v2.1.33. Turn off the keys yourself. This is just a demo, not secret keys of hard code, you can use AWS Cognito instead. View screenshots for several AWS gotcha.

https://github.com/mayosmith/HelloDynamoDB

/* ----------------------------------------------------------------- AWS configure Note: this is a simple experiement for demonstration purposes only. Replace the keys below with your own. Do not include the secret key in an actual production environment, because, then, it wont be secret anymore... ----------------------------------------------------------------- */ AWS.config.update({accessKeyId: 'AKIAJUPWRIYYQGDB6AFA', secretAccessKey: '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'}); // Configure the region AWS.config.region = 'us-west-2'; //us-west-2 is Oregon //create the ddb object var ddb = new AWS.DynamoDB(); /* ----------------------------------------------------------------- Update the Table ----------------------------------------------------------------- */ //update the table with this data var params = { Key: { name: {S: 'John Mayo-Smith'}, city: {S: 'New York'} }, AttributeUpdates: { food: { Action: 'PUT', Value: {S: 'chocolate'} } }, TableName: 'sampletable', ReturnValues: 'ALL_NEW' }; //update the table update(); /* ----------------------------------------------------------------- Get Item from the Table ----------------------------------------------------------------- */ //attribute to read var readparams = { Key: { name: {S: 'John Mayo-Smith'}, city: {S: 'New York'} }, AttributesToGet: ['food'], TableName: 'sampletable' }; //get the item read(); /* ----------------------------------------------------------------- function update() Description: Calls updateItem which is part of the AWS Javascript SDK. Returns: JSON object (the object is stringifyed so we can see what going on in the javascript console) ----------------------------------------------------------------- */ function update(){ ddb.updateItem(params, function(err, data) { if (err) { return console.log(err); } console.log("We updated the table with this: " + JSON.stringify(data)); }); } /* ----------------------------------------------------------------- function read() Description: Calls getItem which is part of the AWS Javascript SDK. Returns: JSON object (the object is stringifyed so we can see what going on in the javascript console) ----------------------------------------------------------------- */ function read(){ ddb.getItem(readparams, function(err, data) { if (err) { return console.log(err); } console.log(": " + data); console.log("John favorite food is: "+ JSON.stringify(data.Item.food.S)); // print the item data }); } 
+8
source

Yes, you can access Amazon DynamoDB directly from your AngularJS application using the AWS JavaScript SDK for your browser. The same code snippet should work for NodeJS as well.

It should be noted that your application must securely authenticate with AWS without embedding security credentials in the code. Amazon Cognito simplifies and automatically authenticates if you create an identity pool and configure your app accordingly. Here are some links to get you started. We created a demo application from AngularJS on Amazon DynamoDB. It uses Amazon Cognito for authentication and SDK documents to store and retrieve JSON objects from DynamoDB directly, without the need to serialize and deserialize them in client code in a browser.

Source code: https://github.com/awslabs/aws-dynamodb-mars-json-demo

Live demo: http://dynamodb-msl-image-explorer.s3-website-us-east-1.amazonaws.com/

AWS Library Usage Examples: https://github.com/awslabs/aws-dynamodb-mars-json-demo/blob/master/viewer/app/scripts/services/mars-photos.js

examples of using the service using the AWS library: https://github.com/awslabs/aws-dynamodb-mars-json-demo/blob/master/viewer/app/scripts/controllers/favorites.js

https://github.com/awslabs/aws-dynamodb-mars-json-demo/blob/master/viewer/app/scripts/controllers/top-voted.js

SDK Document: https://github.com/awslabs/dynamodb-document-js-sdk

+5
source

Source: https://habr.com/ru/post/1211722/


All Articles