This repository has been archived by the owner on Oct 27, 2019. It is now read-only.
forked from airportyh/Tutti
/
couchdb.js
102 lines (98 loc) · 3.16 KB
/
couchdb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var http = require('http'),
sys = require('sys')
function keys(obj){
var ret = []
for (var key in obj) ret.push(key)
return ret
}
function expandViewPath(viewPath, params){
var parts = viewPath.split('/')
var viewPath = '_design/' + parts[0] + '/_view/' + parts[1]
var qs = params ? keys(params).map(function(key){return key + '=' + encodeURI(JSON.stringify(params[key]))}).join('&') : null
if (qs) viewPath += '?' + qs
//sys.debug('viewPath: ' + viewPath)
return viewPath
}
var Couch = function Couch(name, host, port){
this.name = name
this.host = host || 'localhost'
this.port = port || 5984
this.baseUrl = '/' + name + '/'
}
Couch.prototype = {
get: function(id, callback, context){
this.request('GET', this.baseUrl + id, null, callback, context)
},
post: function(doc, callback, context){
this.request('POST', this.baseUrl, doc, callback, context)
},
put: function(doc, callback, context){
this.request('PUT', this.baseUrl + doc._id, doc, callback, context)
},
del: function(doc, callback, context){
this.request('DELETE', this.baseUrl + doc._id + '?rev=' + doc._rev, null, callback, context)
},
forceDelete: function(id, callback, context){
this.get(id, function(doc){
this.del(doc, callback, context)
}, this)
},
view: function(viewPath, params, callback, context){
this.get(expandViewPath(viewPath, params), callback, context)
},
drop: function(callback, context){
this.request('DELETE', '/' + this.name, null, callback, context)
},
create: function(callback, context){
this.request('PUT', '/' + this.name, null, callback, context)
},
request: function request(verb, uri, data, callback, context){
var client = http.createClient(this.port, this.host)
var request = client.request(verb, uri, {})
if (verb != 'GET')
request.write(JSON.stringify(data), "utf8");
request.addListener('response', function(response) {
var responseBody = ""
response.setBodyEncoding("utf8")
response.addListener("data", function(chunk) {
responseBody += chunk
})
response.addListener("end", function() {
//sys.puts(responseBody)
var result = JSON.parse(responseBody);
if (callback)
callback.call(context, result, response.statusCode)
})
})
request.end()
},
updateView: function(id, _design){
var self = this
this.get(id, function(design){
if (design.error == 'not_found'){
design = {
_id: id,
language: "javascript",
views: {}
}
}
for (var view in _design){
design.views[view] = {}
if (_design[view].map)
design.views[view].map = _design[view].map.toString()
if (_design[view].reduce)
design.views[view].reduce = _design[view].reduce.toString()
}
sys.puts(JSON.stringify(design))
self.put(design)
})
},
updateViews: function(designs){
for (var key in designs){
var _design = designs[key]
var id = '_design/' + key
this.updateView(id, _design)
}
}
}
exports.Couch = Couch