This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcouchapp.js
122 lines (105 loc) · 2.67 KB
/
couchapp.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
var couchapp = require('couchapp')
, path = require('path');
ddoc = {
_id: '_design/antelope'
, views: {
lib: {
referenceMap: "module.exports = " + JSON.stringify({
"invoice" : ["recipient"]
})
}
}
, lists: {}
, shows: {}
, updates: {}
}
module.exports = ddoc;
ddoc.views.byType = {
map: function(doc) {
emit(doc.type, null);
}
}
ddoc.views.countByType = {
map: function(doc) {
emit(doc.type, null);
},
reduce: '_count'
}
ddoc.views.contacts = {
map: function(doc) {=
if(doc.type === 'contact') {
emit(doc._id, null);
}
}
}
ddoc.views.contactsByEmail = {
map: function(doc) {
if(doc.type === 'contact' && doc.emails && doc.emails.length) {
for (var i=0; i < doc.emails.length; i++){
emit(doc.emails[i].email, doc.emails[i]);
}
}
}
}
ddoc.views.inbox = {
map: function(doc){
if(doc.type === "x-email-inbound" || doc.type === 'inbox') {
emit(doc._id, doc._attachments.length);
}
}
}
ddoc.views.attachments = {
map: function(doc) {
if(doc._attachments && doc._id[0] != "_") {
emit(doc._id, null);
}
}
}
ddoc.views['with-references'] = {
map: function(doc){
var rMap = require("views/lib/referenceMap");
emit([doc._id, 0, doc.type, null, doc.updated_at, doc.created_at], {_id: doc._id});
if (doc.type && rMap[doc.type]){
var attrs = rMap[doc.type];
for (var i=0; i < attrs.length ; i++){
var key = attrs[i],
refId = doc[key];
if (refId) {
emit([refId, 1, doc.type, key, doc.updated_at, doc.created_at], {_id: doc._id});
}
}
}
}
}
// sequence generator
ddoc.updates.nextSequence = function(doc, req) {
if (!doc) {
doc = {
_id: req.id,
type: 'sequence',
version: 1,
count: 0
};
}
doc.count++;
var count = "" + doc.count;
if (doc.padLeft){
count = doc.padLeft.substring(count.length) + count;
}
return [doc, toJSON((doc.prefix || '') + count)];
}
ddoc.validate_doc_update = function (newDoc, oldDoc, userCtx) {
function require(field, message) {
message = message || "Document must have a " + field;
if (!newDoc[field]) throw({forbidden : message});
};
if (newDoc._deleted && userCtx.roles.indexOf('_admin') === -1) {
throw({
forbidden: 'Only admins may delete docs.'
})
}
if (newDoc.type == "x-email-inbound") {
require("msg");
}
}
couchapp.loadAttachments(ddoc, path.join(__dirname, 'assets'));