This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
76 lines (65 loc) · 1.84 KB
/
index.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
var cradle = require('cradle')
function Setup (name, config){
if(!(this instanceof Setup)) return new Setup(name,config)
if('object' === typeof name){
config = name
name = config.name
}
config = config || {cache:true}
config.host = config.host || 'http://localhost'
config.port = config.port || 5984
var db = new(cradle.Connection)(config.host,config.port,config).database(name)
, self = this
this.views = {}
this.view = function (name,map,reduce){
name = name.split('/')
var id = '_design/' + name[0]
, view = this.views[id] || {_id: id, views: {}}
view.views[name[1]] = {
map: map,
reduce: reduce
}
this.views[id] = view
return this
}
function updateViews (exists,callback){
var ids = Object.keys(self.views)
db.get(ids,function (err,data){
(data.rows || data).forEach(function (e){
var doc
Setup.log(e._id, ':',e.rev, e)
if ((doc = e.doc || e)) //self.views[e._id])
self.views[doc._id]._rev = doc._rev
})
db.save(ids.map(function (e){
console.log(self.views[e])
return self.views[e]
}),function (err){ callback(err,db) })
})
}
this.ready = function(callback){
db.exists(function (err,exists){
Setup.log('does couchdb "' + name + '" exist?', exists)
if(err)
callback(err)//probably that there is no couchdb running at host:port
if(exists){
//check to update views.
updateViews(exists,callback)
} else {
db.create(function(err,ok){
Setup.log('couchdb "' + name + '" created')
//create views
if(err)
return callback(err)
updateViews(exists,callback)
})
}
})
return db
}
}
Setup.log = console.log
module.exports = Setup
if(!module.parent){
Setup('test').ready(console.log)
}