-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcouch.js
142 lines (129 loc) · 4.08 KB
/
couch.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
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
var _ = require('underscore'),
fs = require('fs'),
request = require('request');
var Couch = module.exports = function(config) {
var host = config.host || '127.0.0.1';
var port = config.port || (config.secure ? '6984' : '5984');
if (!config.name) throw 'Database name is required';
var credentials = config.auth ? config.auth.username + ":" + config.auth.password + "@" : '';
this.uri = (config.secure ? 'https://' : 'http://') +
credentials +
host + ':' +
port + '/' +
config.name;
this.name = config.name;
};
// General response parser
// -----------------------
Couch.prototype.parse = function(callback) {
var that = this;
return function(err, res, body) {
body = (typeof body === 'string' ? JSON.parse(body) : body) || {};
if (!err) {
if (body.error) {
err = new Error(body.reason);
err.error = body.error;
err.reason = body.reason;
err.statusCode = res.statusCode;
} else if (res.headers['etag']) {
body._rev = res.headers['etag'].slice(1, -1);
}
}
callback && callback(err, body);
}
};
// PUT a document to Couch
// -----------------------
Couch.prototype.put = function(doc, callback) {
request.put({
uri: this.uri + '/' + encodeURIComponent(doc._id),
json: doc
}, this.parse(callback));
};
// POST a document to Couch
// ------------------------
Couch.prototype.post = function(doc, callback) {
request.post({
uri: this.uri,
json: doc
}, this.parse(callback));
};
// DELETE a document from Couch
// ----------------------------
Couch.prototype.del = function(doc, callback) {
request.del({
uri: this.uri + '/' + encodeURIComponent(doc._id) + '?rev=' + doc._rev
}, this.parse(callback));
};
// GET a document from Couch
// -------------------------
Couch.prototype.get = function(id, callback) {
request.get({
uri: this.uri + '/' + encodeURIComponent(id)
}, this.parse(callback));
};
// HEAD request to retrieve document _rev.
// ---------------------------------------
Couch.prototype.head = function(id, callback) {
request.head({
uri: this.uri + '/' + encodeURIComponent(id),
json: true
}, this.parse(function(err, body) {
body && !err && (body._id = id);
callback(err, body);
}));
};
// GET documents via view from Couch
// ---------------------------------
Couch.prototype.view = function(view, options, callback) {
var opts = [];
_.each(options, function(v, k) {
opts.push(encodeURIComponent(k) + '=' + encodeURIComponent(v));
});
request.get({
uri: this.uri + '/' + view + '/?' + opts.join('&')
}, this.parse(callback));
};
// Create database
// ---------------
Couch.prototype.dbPut = function(callback) {
request.put({
uri: this.uri
}, this.parse(callback));
};
// Delete database
// ---------------
Couch.prototype.dbDel = function(callback) {
request.del({
uri: this.uri
}, this.parse(callback));
};
// PUT design docs from files or JSON objects
// ------------------------------------------
Couch.prototype.putDesignDocs = function(files, callback) {
callback = callback || function() {};
var remaining = files.length;
var put = function(id, doc) {
if (!id) return callback(new Error('Document _id required.'));
doc = _(doc).isString() ? doc : JSON.stringify(doc);
request.put({
uri: this.uri + '/' + id,
body: doc
}, function(err, res) {
remaining--;
if (err) return callback(err);
if (!remaining) return callback(err, res);
});
}.bind(this);
files.forEach(function(file) {
if (_(file).isString()) {
fs.readFile(file, 'utf8', function(err, data) {
if (err) return callback(err);
var id = data.match(/.*"_id".*?:.*?"(.*?)".*/)[1];
put(id, data);
});
} else {
put(file._id, file);
}
}.bind(this));
};