Skip to content

Commit

Permalink
view method works
Browse files Browse the repository at this point in the history
  • Loading branch information
rockymeza committed May 14, 2011
1 parent 7302e2d commit 6de4901
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -45,7 +45,17 @@ db.view 'myapp/things', (err, res) ->
`db.view(path:string, options:object, [callback])`

```coffee-script
db.view 'myapp/thingsByName', {key: 'Foo'}, (err, res) ->
db.view 'myapp/thingsByName', {key: 'Foo', limit: 3}, (err, res) ->
res.forEach (thing) ->
console.log thing
```

To post with keys, use this syntax:

`db.view(path:string, options:array, [callback])`

```coffee-script
db.view 'myapp/thingsByName', ['myThing'], (err, res) ->
res.forEach (thing) ->
console.log thing
```
Expand Down
40 changes: 28 additions & 12 deletions lib/couchy.coffee
Expand Up @@ -3,6 +3,7 @@ Proxy = require 'node-proxy'
request = require 'request'
url = require 'url'
sys = require 'sys'
qs = require 'querystring'

# a default callback
noop = ->
Expand All @@ -12,15 +13,6 @@ class CouchyError extends Error
name: 'CouchyError'
class RequestError extends CouchyError

requestOrError = (data, cb) ->
request data, (err, res, body) ->
throw err if err?

body = JSON.parse(body) if body
error = new RequestError(body.error, body, data) if body.error?

cb(error, res, body)

class Seed
number: (max = 100, precision = 0) ->
Math.round(Math.random() * max, precision)
Expand Down Expand Up @@ -102,18 +94,24 @@ class Database
if data? and typeof data != 'object'
cb = data

requestOrError {uri: uri, method: method, json: data}, cb
options = {uri: uri, method: method, json: data}

request options, (err, res, body) =>
throw err if err?
body = JSON.parse(body) if body
error = new RequestError(body.error, body, options) if body.error?
cb(error, res, body)

# setup methods
exists: (cb) ->
@query 'head', (err, res) =>
@query 'head', (err, res) ->
cb(err, res.statusCode == 200)
undefined

create: (cb) ->
@exists (err, bool) =>
if not bool
@query 'put', (err, res, body) =>
@query 'put', (err, res, body) ->
cb(err, res.statusCode == 201)
else
cb(null, true)
Expand All @@ -122,9 +120,27 @@ class Database
destroy: (cb) ->
@query 'delete', cb

# design document stuff
app: (name) ->
new App(this, name)

# sugar for views
viewPath: (path) ->
[doc, view] = path.split('/')
"_design/#{doc}/_view/" + view

view: (path, options, cb) ->
path = @viewPath(path)
switch typeof options
when 'function'
@query 'get', path, options
when 'object'
if options.length # array
@query 'post', path, {keys: options}, cb
else
path += '?' + qs.stringify(options)
@query 'get', path, cb

# seeding
seed: (times, doc_cb, cb) ->
if typeof(times) == 'function'
Expand Down
36 changes: 33 additions & 3 deletions test/couchy_test.coffee
Expand Up @@ -129,8 +129,9 @@ vows.describe('couchy')
topic: ->
cb = this.callback
app_db.create ->
app_db.seed 5, (-> type: 'thing', name: @string()), ->
app_db.seed 3, (-> type: 'notThings', number: @number()), cb
app_db.seed (->type: 'thing', name: 'myThing', foo: 'bar'), ->
app_db.seed 5, (-> type: 'thing', name: @string()), ->
app_db.seed 3, (-> type: 'notThings', number: @number()), cb
undefined
'noninterference': ->
app1 = app_db.app('app1')
Expand Down Expand Up @@ -176,7 +177,7 @@ vows.describe('couchy')
undefined
'there is something': (err, res, body) ->
assert.isNull err
assert.equal body.total_rows, 5
assert.equal body.total_rows, 6
'retrieving an app':
topic: ->
app = app_db.app('retrievedApp')
Expand All @@ -201,4 +202,33 @@ vows.describe('couchy')
undefined
'no error': (err, res, body) ->
assert.isNull err
'views':
topic: ->
app = app_db.app('viewsApp')
app.views.thingsByName =
map: (doc) ->
emit(doc.name, doc) if doc.type == 'thing'
app.push this.callback
undefined
'get':
topic: ->
app_db.view 'viewsApp/thingsByName', this.callback
undefined
'has the correct count': (err, res, body) ->
assert.equal body.total_rows, 6
'get with options':
topic: ->
app_db.view 'viewsApp/thingsByName', {limit: 3}, this.callback
undefined
'no error': (err, res, body) ->
assert.isNull err
'has only three': (err, res, body) ->
assert.equal body.rows.length, 3
'post with keys':
topic: ->
app_db.view 'viewsApp/thingsByName', ['myThing'], this.callback
undefined
'is mine': (err, res, body) ->
assert.equal body.rows[0].value.name, 'myThing'
assert.equal body.rows[0].value.foo, 'bar'
.export(module)

0 comments on commit 6de4901

Please sign in to comment.