source: Dev/trunk/src/server/config/couchdb-design-docs.js @ 531

Last change on this file since 531 was 531, checked in by hendrikvanantwerpen, 11 years ago
  • Return to using truly ISO formatted dates, including milliseconds.
  • Also set constraint on surveyrun dates when value is initially set.
  • Separate runs & results from surveys and questions.
  • Moved date & email format to schema itself.
File size: 7.2 KB
RevLine 
[525]1var fs = require('fs')
2  , path = require('path')
3  ;
4
5function readModule(name) {
6    var filename;
7    if ( name.indexOf('.') === 0 ) {
8        filename = path.join(path.dirname(module.filename),name+'.js');
9    } else {
10        var dirname = path.join(path.dirname(module.filename),
11                                '../../node_modules',name);
12        filename = path.join(dirname,name+'.js');
13        var pkgfile = path.join(dirname,'package.json');
14        if ( fs.exists(pkgfile) ) {
15            var pkg = require(pkgfile);
16            if ( pkg.main ) {
17                filename = path.join(dirname,pkg.main);
18            }
19        }
20    }
21    var src = fs.readFileSync(filename, 'utf8');
22    src = src.replace(/require\s*\(\s*(['\"])([^'"]*)(['\"])\s*\)/g,
23                      'require($1views/lib/$2$3)');
24    return src;
25}
26
[455]27module.exports = {
[464]28
[519]29    "schemaInfo": {
30        _id: "schemaInfo",
[523]31        version: "4",
[531]32        viewsVersion: "2"
[480]33    },
34
[519]35    "_design/protectPublished": {
[455]36        __configAction: "replace",
37        language: "javascript",
38        validate_doc_update: function(newDoc, oldDoc, userCtx, secObj) {
[487]39            if ( oldDoc && oldDoc.publicationDate ) {
40                throw({forbidden:'Published documents cannot be modified.'});
41            }
[516]42        }
43    },
44
[519]45    "_design/default": {
[516]46        __configAction: "replace",
47        language: "javascript",
[455]48        views: {
49            by_type: {
50                map: function(doc){
[531]51                    emit(doc.type, 1);
52                },
53                reduce: function(keys,values){ return sum(values); }
[477]54            },
55            typeless: {
56                map: function(doc){
57                    if ( !doc.type ) {
58                        emit(doc._id, doc);
59                    }
60                }
[455]61            }
62        }
63    },
[464]64
[519]65    "_design/questions": {
[455]66        __configAction: "replace",
67        language: "javascript",
68        views: {
69            all: {
70                map: function(doc){
71                    if ( doc.type !== 'Question' ) { return; }
72                    if ( doc.categories && doc.categories.length > 0 ) {
73                        for ( var i = 0; i < doc.categories.length; i++ ) {
[487]74                            emit([doc.categories[i],doc.topic||"(default)"],1);
[455]75                        }
76                    } else {
[487]77                        emit(["(default)","(default)"],1);
[455]78                    }
79                },
80                reduce: function(keys,values){ return sum(values); }
81            },
82            published: {
83                map: function(doc){
84                    if ( doc.type!=='Question' || !doc.publicationDate ) { return; }
85                    if ( doc.categories && doc.categories.length > 0 ) {
86                        for ( var i = 0; i < doc.categories.length; i++ ) {
[487]87                            emit([doc.categories[i],doc.topic||"(default)"],1);
[455]88                        }
89                    } else {
[487]90                        emit(["(default)","(default)"],1);
[455]91                    }
92                },
93                reduce: function(keys,values){ return sum(values); }
94            },
95            all_topics: {
96                map: function(doc){
97                    if( doc.type !== 'Question' ){ return; }
[487]98                    emit(doc.topic||"(default)",1);
[455]99                },
[487]100                reduce: function(key, values, rereduce) { return sum(values); }
[455]101            },
102            published_topics: {
103                map: function(doc){
104                    if ( doc.type !== 'Question' || !doc.publicationDate ) { return; }
[487]105                    emit(doc.topic||"(default)",1);
[455]106                },
[531]107                reduce: function(key, values ) { return sum(values); }
[487]108            },
[525]109            all_variables: {
110                map: function(doc) {
111                    if ( doc.type !== 'Question' ) { return; }
112                    var _ = require('views/lib/underscore');
113                    var objF = require('views/lib/object');
114                    _.chain(objF.collectFields('subcode',doc.content))
115                    .map(function(subcode){
116                        return doc.code+subcode;
117                    })
118                    .each(function(variable){
119                        emit(variable,doc._id);
120                    });
121                }
122            },
123            published_variables: {
124                map: function(doc) {
125                    if ( doc.type !== 'Question' || !doc.publicationDate ) { return; }
126                    var _ = require('views/lib/underscore');
127                    var objF = require('views/lib/object');
128                    _.chain(objF.collectFields('subcode',doc.content))
129                    .map(function(subcode){
130                        return doc.code+subcode;
131                    })
132                    .each(function(variable){
133                        emit(variable,doc._id);
134                    });
135                }
136            },
[501]137            all_by_code: {
[487]138                map: function(doc){
139                    if ( doc.type !== 'Question' ) { return; }
[531]140                    emit(doc.code,1);
141                },
142                reduce: function(key, values) { return sum(values); }
[501]143            },
144            published_by_code: {
145                map: function(doc){
[531]146                    if ( doc.type !== 'Question' ||
147                         !doc.publicationDate ) { return; }
148                    emit(doc.code,1);
149                },
150                reduce: function(key, values) { return sum(values); }
[525]151            },
152            lib: {
153                underscore: readModule('underscore'),
154                object: readModule('../util/object')
[455]155            }
156        }
157    },
[464]158
[519]159    "_design/surveys": {
[455]160        __configAction: "replace",
161        language: "javascript",
162        views: {
163            drafts: {
164                map: function(doc){
[531]165                    if ( doc.type !== 'Survey' ||
166                         doc.publicationDate ) { return; }
[455]167                    emit(doc._id,doc);
168                }
169            },
170            published: {
171                map: function(doc){
[531]172                    if ( doc.type !== 'Survey' ||
173                         !doc.publicationDate ) { return; }
[455]174                    emit(doc._id,doc);
175                }
176            }
177        }
178    },
[464]179
[519]180    "_design/surveyRuns": {
[492]181        __configAction: "replace",
182        language: "javascript",
183        views: {
[531]184            by_start_date: {
[492]185                map: function(doc){
186                    if ( doc.type !== 'SurveyRun' ) { return; }
[531]187                    emit(doc.startDate||null,1);
188                },
189                reduce: function(keys,values){ return sum(values); }
190            },
191            by_end_date: {
192                map: function(doc){
193                    if ( doc.type !== 'SurveyRun' ) { return; }
194                    emit(doc.endDate||{},1);
195                },
196                reduce: function(keys,values){ return sum(values); }
[492]197            }
198        }
199    },
200
[519]201    "_design/responses": {
[455]202        __configAction: "replace",
203        language: "javascript",
204        views: {
205            by_surveyrun: {
206                map: function(doc){
207                    if ( doc.type !== 'Response' ) { return; }
[531]208                    emit(doc.surveyRunId, 1);
209                },
210                reduce: function(keys,values){ return sum(values); }
[455]211            }
212        }
213    }
[464]214
[455]215};
Note: See TracBrowser for help on using the repository browser.