-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.js
203 lines (189 loc) · 5.3 KB
/
views.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
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
var cradle = require('cradle');
cradle.setup({ host: 'localhost',
port: 5984,
auth: { username: 'admin', password: '1234' }
});
var cclient = new (cradle.Connection)
function _createdb(dbname) {
var db = cclient.database(dbname);
db.exists(function(err, exists) {
if (!exists) {
db.create()
}
});
return db;
}
var DB_TEST= _createdb('test')
var DB = _createdb('telerama')
/*
*var DB_DATA= _createdb('data')
*/
function cradle_error(err, res) {
if (err) console.log(err)
}
/*
*if doc doesn't have type - split id by /, take first and remove 's' and set as type
*/
function update_views(db, docpath, code) {
function save_doc() {
db.save(docpath, code, cradle_error);
return true;
}
// compare function definitions in document and in code
function compare_def(docdef, codedef) {
var i = 0;
if (!codedef && !docdef) {
return false;
}
if ((!docdef && codedef) || (!codedef && docdef)) {
console.log('new definitions - updating "' + docpath +'"')
return true;
}
for (var u in docdef) {
i++;
if (!codedef[u] || docdef[u] != codedef[u].toString()) {
console.log('definition of "' + u + '" changed - updating "' + docpath +'"')
return true;
}
}
// check that both doc and code have same number of functions
for (var u in codedef) {
i--;
if (i < 0) {
console.log('new definitions - updating "' + docpath +'"')
return true;
}
}
return false;
}
db.get(docpath, function(err, doc) {
if (!doc) {
console.log('no design doc found updating "' + docpath +'"')
return save_doc();
}
if (compare_def(doc.updates, code.updates) || compare_def(doc.views, code.views)) {
return save_doc();
}
console.log('"' + docpath +'" up to date')
});
}
var VENUES_DDOC = {
language: 'javascript',
views: {
/*
*byDate
*byPublished
*/
active: {
map: function (doc) {
if (doc.lastsession) {
emit(parseInt(doc.lastsession / 1000), 1)
}
},
reduce: function(keys, counts, rereduce) {
return sum(counts)
}
},
users: function(doc) {
if (doc.created) {
emit(parseInt(doc.created / 1000), 1)
}
}
}
}
var EVENTS_DDOC = {
language: 'javascript',
views: {
byDate: {
map: function(doc) {
if (doc.type == 'event' && doc.date.from && doc.date.to) {
emit([doc.date.from, doc.date.to, doc.id], null);
}
}
},
byVenue: {
map: function(doc) {
if (doc.type == 'event' && doc.venue) {
emit([doc.venue, doc.id], null);
}
}
},
countByVenue: {
map: function(doc) {
if (doc.type == 'event' && doc.venue) {
emit(doc.venue, 1);
}
},
reduce: function(keys, values, rereduce) {
if (rereduce) {
return sum(values);
}
else {
return values.length;
}
}
}
}
}
var TAGS_DDOC = {
language: 'javascript',
views: {
list: {
map: function (doc) {
if (doc.type == 'tags') {
emit([doc.original, doc._id], null)
}
}
}
}
}
var CATEGORIES_DDOC = {
language: 'javascript',
views: {
list: {
map: function (doc) {
if (doc.type == 'categories') {
emit([doc.original, doc._id], null)
}
}
}
}
}
function update_type(db) {
db.all(function(err, doc) {
/* Loop through all documents. */
for(var i = 0; i < doc.length; i++) {
/* Don't delete design documents. */
if(doc[i].id.indexOf("_design") == -1) {
if (doc[i].type == undefined) {
var id = doc[i].id
var newid = id.replace(/\//g, '%2F');
if (id.split('/')[1] != null) {
var type = id.split('/')[1].slice(0, -1);
db.merge(newid, {type: type}, function(err, d) {
console.log(d)
})
}
}
}
}
});
}
/*
*reduce: function(keys, values, rereduce) {
* return sum(values);
*}
*/
/*
*update_type(DB_TEST)
*/
/*
*update_views(DB_TEST, '_design/venues', VENUES_DDOC)
*update_views(DB_TEST, '_design/events', EVENTS_DDOC)
*update_views(DB_TEST, '_design/categories', CATEGORIES_DDOC)
*update_views(DB_TEST, '_design/tags', TAGS_DDOC)
*/
update_views(DB, '_design/venues', VENUES_DDOC)
update_views(DB, '_design/events', EVENTS_DDOC)
update_views(DB, '_design/categories', CATEGORIES_DDOC)
update_views(DB, '_design/tags', TAGS_DDOC)