Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
add table methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jed committed Jan 30, 2012
1 parent 709e75c commit ca7edcf
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 9 deletions.
50 changes: 46 additions & 4 deletions README.md
Expand Up @@ -84,7 +84,7 @@ If neither of the above are provided, an error will be thrown.

#### db\[_operationName_\](data, cb)

All of the original DynamoDB operations are provided as methods on database instances. You won't need to use them unless you want to sacrifice a clean interdace for more control, and don't mind learning their JSON format.
All of the original DynamoDB operations are provided as methods on database instances. You won't need to use them unless you want to sacrifice a clean interdace for more control, and don't mind learning Amazon's JSON format.

### TableList

Expand All @@ -101,7 +101,7 @@ Calls back with the tables in the current database, as a list of table instances

A convenience method that uses the `fetch` method to call back for each table fetched. This abstracts away batching, making it much easier to iterate over tables in a natural yet non-blocking way.

<!-- #### tables.add(_name_, _args..._)
#### tables.add(_name_, _args..._)

An alias for `tables.get(name).create(args...)`.

Expand All @@ -112,9 +112,51 @@ An alias for `tables.get(name).destroy(args...)`.
### Table

#### table = tables.get(_tableName_)
-->

### Table, Item, ItemList, etc.
Returns a table instance for the given name. Note that this only represents the logic for the table, and does not fetch any information.

#### table.create([_schema_], [_throughput_], [_cb_])

Creates a table with the optionally specified schema and throughput.

The schema is specified as an object with a `hash` key and an optional `range` key, each with a `name` property and an optional `type` property that defaults to `"String"`. If no schema is specified, `{hash: {name: "id", type: "String }}` is used.

The throughput is specified as an object with a `read` property and a `write` property, both defaulting to the minimum possible throughput (5 units).

This means that the following are identical:

```javascript
table.create()
table.create("id")
table.create({id: "String"})
table.create({id: "String"}, {read: 5, write: 5})
```

This method returns the table description.

#### table.fetch([_cb_])

Fetches details about the table, which can include its status, size, schema, and other details, such as the following:

```javascript
{
name: 'DYNAMO_TEST_TABLE_3',
createdAt: Mon, 30 Jan 2012 15:47:02 GMT,
status: 'CREATING',
schema: { hash: { name: 'id', type: 'String' } },
throughput: { read: 5, write: 5 }
}
```

#### table.destroy([_cb_])

Deletes the table.

#### table.watch([_cb_])

Polls every 5 seconds until the status of the table changes.

### Item, ItemList, etc.

Coming this week!

Expand Down
54 changes: 51 additions & 3 deletions lib/Table.js
Expand Up @@ -12,16 +12,63 @@ Table.prototype = {
return this
},

parse: function(data) {
data = data.Table || data.TableDescription

this.name = data.TableName

var createdAt = data.CreationDateTime
, size = data.TableSizeBytes
, status = data.TableStatus
, items = {length: data.ItemCount}
, schema = data.KeySchema
, throughput = data.ProvisionedThroughput

if (createdAt) this.createdAt = new Date(createdAt * 1000)
if (size > -1) this.size = size
if (status) this.status = status
if (items > -1) this.items = {length: items}

if (schema) this.schema = (new Schema).parse(schema)
if (throughput) this.throughput = (new Throughput).parse(throughput)

return this
},

request: function(target, data, cb) {
data.TableName = this.name
cb || (cb = log)

this.database.request(target, data, function(err, data, next) {
if (!err) data = (new Table).parse(data)

this.database.request(target, data, function(err, data) {
err ? cb(err) : cb(null, (new Table).parse(data))
cb(err, data, next)
})

return this
},

create: function(schema, throughput, cb) {
var data

if (schema && typeof schema == "function") {
cb = schema
schema = void 0
}

if (throughput && typeof throughput == "function") {
cb = throughput
throughput = void 0
}

data = {
KeySchema: new Schema(schema),
ProvisionedThroughput: new Throughput(throughput)
}

return this.request("CreateTable", data, cb)
},

destroy: function(cb) {
return this.request("DeleteTable", {}, cb)
},
Expand All @@ -31,7 +78,8 @@ Table.prototype = {
},

update: function(opts, cb) {
return this.request("DescribeTable", {}, cb)
return this.request("UpdateTable", {}, cb)
},
}
}

Expand Down
5 changes: 3 additions & 2 deletions lib/TableList.js
Expand Up @@ -25,8 +25,9 @@ TableList.prototype = {
return this
},

get: function(arg) {
var obj
get: function() {
var arg = shift.call(arguments)
, obj

switch (typeof arg) {
case "string":
Expand Down
68 changes: 68 additions & 0 deletions test/tests/table.js
@@ -0,0 +1,68 @@
var should = require("should")
, dynamo = require("../../")
, db = dynamo.createClient()
, table = db.tables.get("DYNAMO_TEST_TABLE_3")

describe("Tables", function() {
describe("#create()", function() {
it("should create a table", function(done) {
table.create(function(err, table) {
should.not.exist(err)
should.exist(table)

table.should.have.property("name", "DYNAMO_TEST_TABLE_3")
table.should.have.property("status", "CREATING")

done()
})
})
})

describe("#watch()", function() {
it("should notify when status changes", function(done) {
table.watch(function(err, table) {
should.not.exist(err)
should.exist(table)

done()
})
})
})

describe("#fetch()", function() {
it("should return table with details", function(done) {
table.fetch(function(err, table) {
should.not.exist(err)
should.exist(table)

table.should.have.property("name", "DYNAMO_TEST_TABLE_3")
table.should.have.property("status", "ACTIVE")

table.should.have.property("createdAt")
table.createdAt.should.be.an.instanceof(Date)

table.should.have.property("size")
table.size.should.be.a("number")

table.should.have.property("schema")
table.should.have.property("throughput")

done()
})
})
})

describe("#destroy()", function() {
it("should delete a table", function(done) {
table.destroy(function(err, table) {
should.not.exist(err)
should.exist(table)

table.should.have.property("status", "DELETING")

done()
})
})
})
})

0 comments on commit ca7edcf

Please sign in to comment.