Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
273 lines (259 loc) · 8.29 KB
/
Copy pathindex.js
File metadata and controls
273 lines (259 loc) · 8.29 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"use strict";
// Basic HTTP wrapper for CouchDB 2.0
// Nano is probably a better solution but
// does not support 2.0 yet.
const request = require('request');
// This module is initialized with:
// - url STRING Url to the DB server with port
// - config OBJECT Authentication configuration
// -- user STRING username to the DB server
// -- pass STRING password to the DB server
module.exports = function(url, config) {
// Create a new database
// If it already exist that is ok, no error returned
<
8000
div id="LC18" class="react-file-line html-div" data-testid="code-cell" data-line-number="18" style="position:relative"> // - db STRING The name of the database to create
// - next FUNCTION Callback. Format next(err, result)
function create(db, next) {
request({
method: 'PUT',
url: url + '/' + db,
'auth': {
'user': config.user,
'pass': config.password
},
json: true
// 'headers': {
// 'content-type': 'application/json'
// }
}, function(err, response) {
var msg = '';
var statusCode = 0;
if (err && err.code === 'ECONNREFUSED') {
msg = "Could not connect to CouchDB. Please check connection";
statusCode = 502;
}
else {
statusCode = response.statusCode;
// If database already exist, it's not an error
if (response.statusCode == 412) {
msg = 'Database ' + db + ' already exist';
err = null;
}
else if (response.statusCode == 201) {
msg = 'Database ' + db + ' was created';
}
}
next(err, {msg: msg, statusCode: statusCode});
});
}
// Create a Mango index (CouchDB 2.0)
// See: https://docs.cloudant.com/cloudant_query.html#creating-an-index
// - db STRING The database to contact
// - index OBJECT The CouchDB index object
// - next FUNCTION Callback. Format: next(err, result);
function createIndex(db, index, next) {
request({
method: 'POST',
url: url + '/' + db + '/_index',
'auth': {
'user': config.user,
'pass': config.password
},
'json': true,
'body': index
}, function(err, response) {
response = response || {};
var reply = {};
if (response && response.body) reply = response.body;
next(err, reply);
});
}
// Get a single document using the id
function get(db, id, next) {
request({
method: 'GET',
url: url + '/' + db + '/' + id,
'auth': {
'user': config.user,
'pass': config.password
},
'json': true
}, function(err, response) {
response = response || {};
var body = response.body || {};
next(err, body);
});
}
// Find documents using Mango queries (CouchDB 2.0)
// see: https://docs.cloudant.com/cloudant_query.html
// A query need to have a "selector"
// return: {docs: [...]}
function find(db, query, next) {
request({
method: 'POST',
url: url + '/' + db + '/_find',
'auth': {
'user': config.user,
'pass': config.password
},
'json': true,
'body': query
}, function(err, response) {
response = response || {};
var body = response.body || {};
next(err, body);
});
}
function insert(db, entry, next) {
request({
method: 'POST',
url: url + '/' + db,
'auth': {
'user': config.user,
'pass': config.password
},
'json': true,
'body': entry
}, function(err, response) {
response = response || {};
var body = response.body || {};
next(err, body);
});
}
// When updating a CouchDB document the
// entire document is replaced
// _rev id need to be part of entry
// if _rev id is not the latest, conflic error is returned
function update(db, id, entry, next) {
request({
method: 'PUT',
url: url + '/' + db + '/' + id,
'auth': {
'user': config.user,
'pass': config.password
},
'json': true,
'body': entry
}, function(err, response) {
response = response || {};
var body = response.body || {};
next(err, body);
});
}
// This is a convenience method allowing
// part of a document to be updated
// Only shallow update
// It will result in two DB requests (GET & PUT)
function edit(db, id, data, next) {
get(db, id, function(err, doc) {
if (err) {
next(err, doc);
}
else {
for (var key in data) {
if (data.hasOwnProperty(key)) {
doc[key] = data[key];
}
}
update(db, id, doc, function(err, response) {
next(err, response);
});
}
});
}
function remove(db, id, rev, next) {
request({
method: 'DELETE',
url: url + '/' + db + '/' + id + '?rev=' + rev,
'auth': {
'user': config.user,
'pass': config.password
},
'json': true
}, function(err, response) {
response = response || {};
var body = response.body || {};
if (next) next(err, body);
});
}
function destroy(db, next) {
request({
method: 'DELETE',
url: url + '/' + db + '/',
'auth': {
'user': config.user,
'pass': config.password
},
'json': true
}, function(err, response) {
response = response || {};
var body = response.body || {};
if (next) next(err, body);
});
}
function setup(next) {
var errors = [];
request({
method: 'PUT',
url: url + '/_global_changes',
'auth': {
'user': config.user,
'pass': config.password
},
json: true
}, function(err, res) {
if (err) errors.push(err);
if (res && res.error) errors.push(res.error);
request({
method: 'PUT',
url: url + '/_metadata',
'auth': {
'user': config.user,
'pass': config.password
},
json: true
}, function(err, res) {
if (err) errors.push(err);
if (res && res.error) errors.push(res.error);
request({
method: 'PUT',
url: url + '/_replicator',
'auth': {
'user': config.user,
'pass': config.password
},
json: true
}, function(err, res) {
if (err) errors.push(err);
if (res && res.error) errors.push(res.error);
request({
method: 'PUT',
url: url + '/_users',
'auth': {
'user': config.user,
'pass': config.password
},
json: true
}, function(err, res) {
if (err) errors.push(err);
if (res && res.error) errors.push(res.error);
var error = !!errors.length;
next(error, errors);
});
});
});
});
}
return {
create: create,
create_index: createIndex,
get: get,
find: find,
insert: insert,
update: update,
edit: edit,
remove: remove,
destroy: destroy,
setup: setup
};
};