forked from max-mapper/datacouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·202 lines (188 loc) · 7.31 KB
/
app.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
var couchapp = require('couchapp')
, path = require('path')
;
ddoc =
{ _id:'_design/datacouch'
, rewrites :
[ {from:"/", to:'pages/index.html'}
, {from:"/edit", to:"pages/recline.html"}
, {from:"/edit/*", to:"pages/recline.html"}
// , {from:"/fakelogin", to:"../../../_twitter/auth/fakelogin"} // only enable when testing offline
, {from:"/proxy", to:"../../../_smalldata/"}
, {from:"/proxy/*", to:"../../../_smalldata/*"}
, {from:"/socket.io", to:"../../../_smalldata/wiki/socket.io"}
, {from:"/socket.io/*", to:"../../../_smalldata/wiki/socket.io/*"}
, {from:"/login", to:"../../../_smalldata/twitter/auth/twitter"}
, {from:"/login/callback", to:"../../../_smalldata/twitter/auth/twitter/callback"}
, {from:"/logout", to:"../../../_smalldata/twitter/logout"}
, {from:"/api/token", to:"../../../_smalldata/twitter/auth/token"}
, {from:"/api/upload/*", to:"../../../_smalldata/upload/*"}
, {from:"/api/applications/:dataset", to:"_view/applications", query:{endkey:":dataset", startkey:":dataset", include_docs:"true", descending: "true"}}
, {from:"/api/applications", to:"_view/applications", query:{include_docs:"true", descending: "true"}}
, {from:"/api/applications/user/:user", to:"_view/applications_by_user", query:{endkey:":user", startkey:":user", include_docs:"true", descending: "true"}}
, {from:"/api/datasets/:user", to:"_view/by_user", query:{endkey: [":user",null], startkey:[":user",{}], include_docs:"true", descending: "true"}}
, {from:"/api/datasets", to:"_view/by_date", query:{include_docs:"true", descending: "true"}}
, {from:"/api/forks/:id", to:"_view/forks", query:{endkey:":id", startkey:":id", include_docs:"true", descending: "true"}}
, {from:"/api/forks", to:"_view/forks", query:{include_docs:"true", descending: "true"}}
, {from:"/api/profile/all", to:"../../../datacouch-users/_design/users/_list/all/users"}
, {from:"/api/trending", to:"_view/popular", query:{include_docs: "true", descending: "true", limit: "10"}}
, {from:"/api/templates", to:"_view/templates", query:{include_docs: "true"}}
, {from:"/api/users/search/:user", to:"../../../datacouch-users/_design/users/_view/users", query:{startkey:":user", endkey:":user", include_docs: "true"}}
, {from:"/api/users", to:'../../../datacouch-users/'}
, {from:"/api/users/*", to:'../../../datacouch-users/*'}
, {from:"/api/couch", to:"../../../"}
, {from:"/api/couch/*", to:"../../../*"}
, {from:"/api/epsg/:code", to:"../../../epsg/:code"}
, {from:"/api", to:"../../"}
, {from:"/api/*", to:"../../*"}
, {from:"/analytics.gif", to:"../../../_analytics/spacer.gif"}
, {from:"/db/:id/csv", to:'../../../:id/_design/recline/_list/csv/all'}
, {from:"/db/:id/json", to:'../../../:id/_design/recline/_list/bulkDocs/all'}
, {from:"/db/:id/headers", to:'../../../:id/_design/recline/_list/array/headers', query: {group: "true"}}
, {from:"/db/:id/rows", to:'../../../:id/_design/recline/_view/all'}
, {from:"/db/:id", to:"../../../:id/"}
, {from:"/db/:id/*", to:"../../../:id/*"}
, {from:"/:user", to:"pages/index.html"}
, {from:"/*", to:'*'}
]
}
;
ddoc.views = {
/**
* A simple map function mocking _all, but allows usage with lists etc.
*/
all: {
map: function(doc) {
emit(doc._id, doc);
}
},
headers: {
map: function(doc) {
var keys = [];
for (var key in doc) {
emit(key, 1);
}
},
reduce: "_sum"
},
by_user: {
map: function(doc) {
if(doc.type === "database") emit([doc.user, doc.createdAt] , doc.name);
}
},
by_date: {
map: function(doc) {
if(doc.type === "database") emit(doc.createdAt, doc.name);
}
},
popular: {
map: function(doc) {
if(doc.hits) emit(doc.hits);
}
},
templates: {
map: function(doc) {
if(doc.type === "template") emit(doc.name);
}
},
applications: {
map: function(doc) {
if(doc.type === "app" && doc.url) emit(doc.dataset);
}
},
applications_by_user: {
map: function(doc) {
if(doc.type === "app" && doc.url) emit(doc.user);
}
},
forks: {
map: function(doc) {
if(doc.forkedFrom) emit(doc.forkedFrom);
}
}
};
ddoc.filters = {
by_value: function(doc, req) {
if (!req.query.k || !req.query.v || !doc[req.query.k]) return false;
return doc[req.query.k] === req.query.v;
}
}
ddoc.lists = {
/**
* Generates a CSV from all the rows in the view.
*
* Takes in a url encoded array of headers as an argument. You can
* generate this by querying /_list/urlencode/headers. Pass it in
* as the headers get parameter, e.g.: ?headers=%5B%22_id%22%2C%22_rev%5D
*
* @author Max Ogden
*/
csv: function(head, req) {
if ('headers' in req.query) {
var headers = JSON.parse(unescape(req.query.headers));
var row, sep = '\n', headerSent = false, startedOutput = false;
start({"headers":{"Content-Type" : "text/csv; charset=utf-8"}});
send('"' + headers.join('","') + '"\n');
while (row = getRow()) {
for (var header in headers) {
if (row.value[headers[header]]) {
if (startedOutput) send(",");
var value = row.value[headers[header]];
if (typeof(value) == "object") value = JSON.stringify(value);
if (typeof(value) == "string") value = value.replace(/\"/g, '""');
send("\"" + value + "\"");
} else {
if (startedOutput) send(",");
}
startedOutput = true;
}
startedOutput = false;
send('\n');
}
} else {
send("You must pass in the urlencoded headers you wish to build the CSV from. Query /_list/urlencode/headers?group=true");
}
},
/**
* Returns an array of the view keys
*
* @author Max Ogden
*/
array: function(head, req) {
start({"headers":{"Content-Type" : "application/json; charset=utf-8"}});
if ('callback' in req.query) send(req.query['callback'] + "(");
var headers = [];
while (row = getRow()) {
headers.push(row.key);
}
send(JSON.stringify(headers));
if ('callback' in req.query) send(")");
},
/**
* A list function that outputs the same format that you use to post into the _bulk_docs API
*
* @author Max Ogden
*/
bulkDocs: function(head, req) {
var row, out, sep = '\n';
start({"headers":{"Content-Type" : "application/json"}});
if ('callback' in req.query) send(req.query['callback'] + "(");
send('{"docs":[');
while (row = getRow()) {
out = JSON.stringify(row.value);
send(sep + out);
sep = ',\n';
}
send("\n]}");
if ('callback' in req.query) send(")");
}
}
ddoc.validate_doc_update = function (newDoc, oldDoc, userCtx, securityObj) {
if (userCtx.roles.indexOf('_admin') > -1) return;
if (["app", "database", "template"].indexOf(newDoc.type) === -1) throw({forbidden : "Invalid doc type"});
if ( !userCtx.name ) throw({forbidden : "You have to sign in to do that."});
if ( (newDoc.user !== userCtx.name) ) throw({forbidden : "You can't create datasets or apps for other users."});
if( newDoc.forkedFromUser && ( newDoc.forkedFromUser === userCtx.name )) throw({forbidden : "You can't fork your own datasets."});
};
couchapp.loadAttachments(ddoc, path.join(__dirname, 'attachments'));
module.exports = ddoc;