Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add region support. closes #17.

  • Loading branch information...
commit ef78ee40aa4563314e4a35344ec613ca7498d74f 1 parent e85dfba
@jed authored
View
1  .gitignore
@@ -1,2 +1,3 @@
node_modules
dynamo.wiki
+*.log
View
23 README.md
@@ -13,6 +13,9 @@ Example
```javascript
// High-level API
+var dynamo = require("dynamo")
+ , client = dynamo.createClient()
+ , db = client.get("us-east-1")
db.get("myTable")
.query({id: "123", date: {">=": new Date - 6000 }})
@@ -21,6 +24,9 @@ db.get("myTable")
.fetch(function(err, data){ ... })
// Same call, using low-level API
+var dynamo = require("dynamo")
+ , client = dynamo.createClient()
+ , db = client.get("us-east-1")
db.query({
TableName: "myTable",
@@ -48,12 +54,12 @@ API
This module exposes the `createClient` method, which is the preferred way to interact with dynamo.
-### db = dynamo.createClient([_credentials_])
+### client = dynamo.createClient([_credentials_])
-Returns a database instance attached to the account specified by the given credentials. The credentials can be specified as an object with `accessKeyId` and `secretAccessKey` members such as the following:
+Returns a client instance attached to the account specified by the given credentials. The credentials can be specified as an object with `accessKeyId` and `secretAccessKey` members such as the following:
```javascript
-db = dynamo.createClient({
+client = dynamo.createClient({
accessKeyId: "...", // your access key id
secretAccessKey: "..." // your secret access key
})
@@ -63,6 +69,17 @@ You can also omit these credentials by storing them in the environment under whi
If neither of the above are provided, an error will be thrown.
+### db = client.get(_regionName_)
+
+Returns a database in the selected region. Currently, DynamoDB supports the following regions:
+
+- `us-east-1`
+- `us-west-1`
+- `us-west-2`
+- `ap-northeast-1`
+- `ap-southeast-1`
+- `eu-west-1`
+
Once you have a database instance, you can use either of the provided APIs:
### [High-level API][high-api] (blue pill)
View
28 lib/Account.js
@@ -2,9 +2,35 @@ var crypto = require("crypto")
, Database = require("./Database")
, Session = require("./Session")
+ , regions = {
+ "us-east-1": true,
+ "us-west-1": true,
+ "us-west-2": true,
+ "ap-northeast-1": true,
+ "ap-southeast-1": true,
+ "eu-west-1": true
+ }
+
function Account(credentials) {
this.session = new Session(credentials)
- this.database = new Database(this)
+}
+
+Account.prototype.get = function(host) {
+ var database = new Database(this)
+ , badHost = !(host in regions)
+
+ if (badHost) {
+ console.error(
+ "WARN: Assuming 'us-east-1' for backward compatibility.\n" +
+ "Please use client.get(region).get(table) instead, as this will soon be deprecated."
+ )
+
+ database.host = "dynamodb.us-east-1.amazonaws.com"
+ return database.get(host)
+ }
+
+ database.host = "dynamodb." + host + ".amazonaws.com"
+ return database
}
Account.prototype.sign = function sign(request, cb) {
View
2  lib/Database.js
@@ -58,7 +58,7 @@ Database.prototype = {
request: function request(target, data, cb) {
var self = this
- , req = new Request(target, data)
+ , req = new Request(this.host, target, data)
this.account.sign(req, function(err, req) {
if (err) cb(err)
View
13 lib/Request.js
@@ -1,19 +1,18 @@
var http = require("http")
, crypto = require("crypto")
-function Request(target, data) {
+function Request(host, target, data) {
var headers = this.headers = new Headers
this.json = JSON.stringify(data)
headers["x-amz-target"] = Request.prototype.target + target
- headers["Host"] = this.host
+ headers["Host"] = this.host = host
headers["Content-Length"] = Buffer.byteLength(this.json)
}
Request.prototype = {
method: "POST",
- host: "dynamodb.us-east-1.amazonaws.com",
pathname: "/",
target: "DynamoDB_20111205.",
data: {},
@@ -21,7 +20,7 @@ Request.prototype = {
toString: function() {
return this.method +
"\n" + this.pathname +
- "\n" +
+ "\n" +
"\n" + this.headers +
"\n" +
"\n" + this.json
@@ -30,7 +29,7 @@ Request.prototype = {
send: function(cb) {
var request = http.request(this, function(res) {
var json = ""
-
+
res.on("data", function(chunk){ json += chunk })
res.on("end", function() {
var error, response = JSON.parse(json)
@@ -41,7 +40,7 @@ Request.prototype = {
error.name = response.__type
error.message = response.message
error.statusCode = res.statusCode
-
+
cb(error)
})
})
@@ -70,4 +69,4 @@ Headers.prototype = {
}
Request.Headers = Headers
-module.exports = Request
+module.exports = Request
View
4 lib/index.js
@@ -13,5 +13,5 @@ exports.createClient = function(credentials) {
}
}
- return new Account(credentials).database
-}
+ return new Account(credentials)
+}
View
3  test/mocha.opts
@@ -1,3 +1,2 @@
--bail
---reporter dot
---timeout 100s
+--timeout 100s
View
3  test/setup.js
@@ -1,6 +1,7 @@
var should = require("should")
, dynamo = require("../")
- , db = dynamo.createClient()
+ , client = dynamo.createClient()
+ , db = client.get("us-east-1")
describe("setup -", function() {
it("delete existing test tables", function(done) {
View
7 test/teardown.js
@@ -1,12 +1,13 @@
var should = require("should")
, dynamo = require("../")
- , db = dynamo.createClient()
+ , client = dynamo.createClient()
+ , db = client.get("us-east-1")
describe("teardown -", function() {
it("delete existing test tables", function(done) {
db.remove("DYNAMO_TEST_TABLE_1", function() {
db.remove("DYNAMO_TEST_TABLE_2", function() {
- done()
+ done()
})
})
})
@@ -14,7 +15,7 @@ describe("teardown -", function() {
it("make sure no test tables exist", function(done) {
db.get("DYNAMO_TEST_TABLE_1").watch(function() {
db.get("DYNAMO_TEST_TABLE_2").watch(function() {
- done()
+ done()
})
})
})
View
3  test/tests/Database.js
@@ -1,6 +1,7 @@
var should = require("should")
, dynamo = require("../../")
- , db = dynamo.createClient()
+ , client = dynamo.createClient()
+ , db = client.get("us-east-1")
describe("Database", function() {
describe("#get()", function() {
View
3  test/tests/Item.js
@@ -1,6 +1,7 @@
var should = require("should")
, dynamo = require("../../")
- , db = dynamo.createClient()
+ , client = dynamo.createClient()
+ , db = client.get("us-east-1")
describe("Item", function() {
describe("#fetch", function() {
View
3  test/tests/Table.js
@@ -1,6 +1,7 @@
var should = require("should")
, dynamo = require("../../")
- , db = dynamo.createClient()
+ , client = dynamo.createClient()
+ , db = client.get("us-east-1")
describe("Table", function() {
describe("#get", function() {
Please sign in to comment.
Something went wrong with that request. Please try again.