-
Notifications
You must be signed in to change notification settings - Fork 6
/
init.js
50 lines (46 loc) · 962 Bytes
/
init.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
// the purpose of this script
// is to create a couch db design doc
// and view for the model list functionality
var nano = require('nano');
var all = function(doc) {
emit(doc.type, doc);
};
var get = function (doc) {
if (/_/.test(doc.type)) {
var parentDoc = doc.type.split('_')[0];
emit([
parentDoc,
doc[parentDoc + '_id']
], doc);
}
emit([doc.type, doc._id], doc);
};
module.exports = init = function(couch, cb) {
var db = nano(couch);
db.insert({
type: 'javascript',
version: "0.0.2",
views: {
all: {
map: all.toString()
},
get: {
map: get.toString()
}
}},'_design/model',
function(err, body) {
if(!module.parent) {
console.log(body);
}
if (cb) {
cb(err);
}
});
}
if (!module.parent) {
if (process.argv[2]) {
init(process.argv[2]);
} else {
console.log('couchdb url required');
}
}