In the SimpleDB database model, you have items, attributes and values. Each row in the database is an item and can be identified by a unique and assignable item name. Each item can have up to 256 pairs of attributes and values. An unexpected aspect of SimpleDB is that an attribute can have more than one pair per item. I think the best way to think about SimpleDB is to think of a spreadsheet, but instead of each column/row intersection representing a single value, it represents an array of values.
The first column is the item name—this is the only column where you can have only a single value, and you can think of it as a unique index column.
The other four columns (pets, cars, furniture, and phones) represent attributes that are currently in this domain—you aren’t limited to this, so every item can have an entirely unique set of attributes. In this data, the attribute pets on the item personInventory1 has three pairs; expressed in JSON, it’ll look something like this:
- { "Name" : "pets", "Value" : "dog" },
- { "Name" : "pets", "Value" : "cat" },
- { "Name" : "pets", "Value" : "fish" }
- { "Name" : "pets", "Value" : "cat" }
SimpleDB is distributed, which has some distinct traits that you need to understand and keep in mind as you design your app. Being a distributed database means a whole group of machines will respond to your requests and your data will be replicated throughout these servers. This distribution will be completely transparent to your program, but it does introduce the possibility of consistency issues—your data cannot be guaranteed to be present on all servers initially.
Don’t panic: it’s not as bad as it sounds for a few reasons. With SimpleDB, the consistency isn’t promised, but it is usually pretty good and quickly reaches all nodes from my experience. Designing around this also isn’t so hard—normally you try to avoid immediately reading a record you just wrote. Finally, SimpleDB has the option to perform consistent reads, but they are slower and may consume more resources. If your app requires consistent reading every time, you might want to reconsider using SimpleDB as your data store, but for many applications, this can be designed around or not even worried about.
On the upside, the distributed nature also affords SimpleDB a few advantages that mesh nicely with the Node.js environment. Since you don’t have a single server responding to your requests, you don’t need to worry about saturating the service, and you can achieve good performance by making many parallel requests to SimpleDB. Parallel and asynchronous requests are something that Node.js can handle easily.
Unlike many AWS services, there isn’t an Amazon-delivered console for management of SimpleDB. Luckily, there is a nice in-browser management console in the form of a Google Chrome plugin, SdbNavigator. In the SdbNavigator you can add or delete domains, insert, update and delete items, modify attributes, and perform queries.
AWS SDK
Now that we’ve gotten to know the SimpleDB service, let’s start writing our REST server. First, we’ll need to install the AWS SDK. This SDK handles not just SimpleDB but all the AWS services, so you may already be including it in your package.json file. To install the SDK, run the following from the command line:
- npm install aws-sdk —-save
Word of warning: As with any pay-as-you-go service, be aware that it’s possible to write code that can rack up big bills, so you’re going to want to keep an eye on your usage and keep your credentials private and safe.
Once you get the AWS SDK installed and have acquired your credentials, you’ll need to set up SimpleDB in your code. In this example, we'll use AWS credentials stored in a JSON file in your home directory. First, you’ll need to include the SDK module, create an AWS object, and finally set up your SimpleDB interface.
- var
- aws = require('aws-sdk'),
- simpledb;
- aws.config.loadFromPath(process.env['HOME'] + '/aws.credentials.json');
- //We'll use the Northern Virginia datacenter, change the region / endpoint for other datacenters http://docs.aws.amazon.com/general/latest/gr/rande.html#sdb_region
- simpledb = new aws.SimpleDB({
- region : 'US-East',
- endpoint : 'https://sdb.amazonaws.com'
- });
The SimpleDB object that you’ve created with new aws.SimpleDB is where all your methods for interacting with SimpleDB will be based. The AWS SDK for SimpleDB has only a few methods:
Batch Operations
- batchDeleteAttributes
- batchPutAttributes
- createDomain
- deleteDomain
- domainMetadata
- listDomains
- deleteAttributes
- getAttributes
- putAttributes
- select
Test Data
Using SdbNavigator, enter your access and security keys into the tool, select ‘US-East’, and click connect.
- var
- aws = require('aws-sdk'),
- simpledb;
- aws.config.loadFromPath(process.env['HOME'] + '/aws.credentials.json');
- simpledb = new aws.SimpleDB({
- region : 'US-East',
- endpoint : 'https://sdb.amazonaws.com'
- });
- simpledb.select({
- SelectExpression : 'select * from `sdb-rest-tut` limit 100'
- }, function(err,resp) {
- if (err) {
- console.error(err);
- } else {
- console.log(JSON.stringify(resp,null,' '));
- }
- });
- {
- "ResponseMetadata": {
- "RequestId": "...", //Every request made to SimpleDB has a request ID
- "BoxUsage": "0.0000228616" //This is how your account is charged, as of time of writing US-East region is 14 US cents per hour, so this request costs 0.00032 cents + the transfer cost (if you are currently outside of your free tier)
- },
- "Items": [ //For a Select, your response will be in the "Items" object property
- {
- "Name": "myfirstitem", //this is the itemName()
- "Attributes": [ //these are the attribute pairs
- {
- "Name": "colors", //attribute name
- "Value": "red" //value - note that every Value is a string, regardless of the contents
- },
- {
- "Name": "colors", //Since the attribute name is repeated, we can see that `colors` has more than one value
- "Value": "blue"
- }
- ]
- }
- ]
- }
Since we’ll be building a REST Server that stores data in SimpleDB, it’s important to understand what a REST server does. REST stands for REpresentational State Transfer. A REST server is really just a server that uses HTTP standard mechanisms as an interface for your data. Often, REST is used for server-to-server communications, but you can use REST servers with the client through JavaScript libraries such as jQuery or Angular. Generally, however, an end-user won’t interact directly with a REST server.
Interestingly, the AWS SDK actually uses the REST protocol to interact with SimpleDB, so it may seem odd to create a REST server to another REST server. You wouldn’t want to use the SimpleDB REST API directly because you need to authenticate your requests, which would risk the security of your AWS account. Also, by writing a server, you’ll be able to add a layer of both abstraction and validation to your data storage that will make the rest of your whole application much easier to deal with.
In this tutorial we will be building the basic CRUD+L functions, that is Create, Read, Update, Delete and List. If you think about it, you can break down most applications into CRUD+L. With REST, you will use a limited number of paths and several HTTP methods or verbs to create an intuitive API. Most developers are familiar with a few of the HTTP verbs, namely GET and POST, as they are used most often in web applications, but there are several others.
As our example, we’ll build a personal inventory server—a database to save whatever you own. Here is how the paths will look:
Written by Kyle Davis(continue)
If you found this post interesting, follow and support us.
Suggest for you:
How to Build a Slack Chatbot In Node.js using Botkit
Make A Real-Time Chat Room using Node Webkit, Socket.io, MEAN
Learn How To Deploy Node.Js App on Google Compute Engine
Learn and Understand NodeJS
Complete Node JS Developer Course Building 5 Real World Apps
No comments:
Post a Comment