-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcouchdb.js
More file actions
186 lines (165 loc) · 3.82 KB
/
couchdb.js
File metadata and controls
186 lines (165 loc) · 3.82 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var http = require('http');
var https = require('https');
var qs = require('querystring');
var url = require('url');
function createRequest(callback, method, uri, params, headers)
{
uri = url.parse(uri);
if(!headers)
{
headers = {};
}
if(!headers.Host)
{
headers.Host = uri.hostname;
}
uri.query = qs.parse(uri.query);
if(!uri.query)
{
uri.query = {};
}
if(params)
{
for(var key in params)
{
if(params.hasOwnProperty(key))
{
uri.query[key] = params[key];
}
}
}
var queryString = qs.stringify(uri.query);
var path = uri.pathname;
if(queryString)
{
path += '?' + queryString;
}
var web = (uri.protocol == 'https:' ? https : http);
var req = web.request(
{
method: method,
host: uri.hostname,
port: uri.port ? uri.port : (uri.protocol == 'https:' ? 443 : 80),
headers: headers,
path: path
}, function(res)
{
var data = '';
res.on('data', function(chunk)
{
data += chunk;
});
res.on('end', function()
{
callback(null, res.statusCode, res.headers, data);
});
})
.on('error', callback)
.on('close', callback);
return req;
}
function get(uri, params, headers, callback)
{
var req = createRequest(callback, 'GET', uri, params, headers);
req.end();
}
function put(uri, params, headers, body, callback)
{
var req = createRequest(callback, 'PUT', uri, params, headers);
req.end(body);
}
function del(uri, params, headers, callback)
{
var req = createRequest(callback, 'DELETE', uri, params, headers);
req.end();
}
var CouchDbRepository = exports.CouchDbRepository = function(options)
{
this.database = options.database;
if(this.database.charAt(this.database.length -1) != '/')
{
this.database += '/';
}
if(options.username && options.password)
{
this.authHeaderValue = new Buffer(options.username + ':' + options.password).toString('base64');
}
};
CouchDbRepository.prototype.get = function(id, callback)
{
get(
this.database + '/' + encodeURIComponent(id), // URL
{}, // Query string parameters
{ 'Accept' : 'application/json',
'Authorization' : 'Basic ' + this.authHeaderValue }, // Headers
function(err, statusCode, headers, data)
{
if(err)
{
callback(null);
}
else if(statusCode != 200)
{
callback(null);
}
else
{
callback(JSON.parse(data));
}
});
};
CouchDbRepository.prototype.set = function(obj, callback)
{
var db = this;
put(
this.database + encodeURIComponent(obj._id),
{}, // Query string parameters
{ 'Authorization' : 'Basic ' + this.authHeaderValue }, // Headers
JSON.stringify(obj),
function(err, statusCode, headers, data)
{
if(err)
{
callback(false);
}
else if(statusCode == 201 || statusCode == 202)
{
obj._rev = data.rev;
callback(true);
}
else if(statusCode == 409)
{
db.get(obj._id, function(doc)
{
obj._rev = doc._rev;
db.del(obj, function(ok)
{
if(ok)
{
delete obj._rev;
db.set(obj, callback);
}
else
{
callback(false);
}
});
});
}
else
{
callback(false);
}
});
};
CouchDbRepository.prototype.del = function(obj, callback)
{
del(
this.database + encodeURIComponent(obj._id),
{ rev : obj._rev }, // Query string parameters
{ 'Authorization' : 'Basic ' + this.authHeaderValue }, // Headers
function(err, statusCode, headers, data)
{
callback(statusCode == 200 && !err);
});
};