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

Last change on this file since 531 was 525, checked in by hendrikvanantwerpen, 11 years ago
  • Allow empty subcodes.
  • Use HTTPResult exclusively on server (no more q).
  • Set readonly & disabled on ourselves as well in _ComplexValueMixin
  • Split server into several modules.
  • Check codes on the variable level, not question level.
  • We can add modules in design documents now.
File size: 2.4 KB
Line 
1var HTTPResult = require('../util/http-result')
2  , _ = require('underscore')
3  , CouchDB = require('../util/couch')
4  , util = require('util')
5  ;
6
7module.exports = function(couchServerURL, dbName, designDocs) {
8    var server = new CouchDB(couchServerURL);
9
10    if ( !designDocs ) { throw new Error("Forgot to pass design docs to checkCouch."); }
11
12    console.log("Configuring CouchDB for QED");
13    console.log("Checking CouchDB version");
14    return server.get('')
15    .then(function(info){
16        if (info.version !== "1.4.0" ) {
17            console.log("Found "+info.version+", only tested with CouchDB 1.2.0");
18        } else {
19            console.log("CouchDB 1.4.0 found");
20        }
21    }).then(function(res){
22        console.log("Checking database '"+dbName+"'");
23        return server.get(dbName)
24        .then(function(db){
25            console.log("Database found.");
26        },function(err){
27            console.log("Creating database.");
28            return server.put(dbName);
29        });
30    }).then(function(){
31        console.log("Putting documents in database.");
32        return _.reduce(designDocs, function(memo, doc, docUrl) {
33            return memo.then(function(){
34                var configAction = doc.__configAction || "replace";
35                delete doc.__configAction;
36                docUrl = dbName+'/'+docUrl;
37                switch (configAction) {
38                case "ignore":
39                    console.log(docUrl+" ignored.");
40                    return null;
41                case "update":
42                    console.log(docUrl+" updating.");
43                    return server.get(docUrl)
44                    .then(function(oldDoc){
45                        _.extend(oldDoc,doc);
46                        return server.put(docUrl,oldDoc);
47                    },function(){
48                        return server.put(docUrl,doc);
49                    });
50                case "replace":
51                    console.log(docUrl+" replacing.");
52                    return server.get(docUrl)
53                    .then(function(oldDoc){
54                        _.extend(doc,_.pick(oldDoc,'_id','_rev'));
55                        return server.put(docUrl,doc);
56                    },function(){
57                        return server.put(docUrl,doc);
58                    });
59                default:
60                    console.warn("Unknown action",configAction);
61                    return null;
62                }
63            });
64        }, new HTTPResult(200));
65    });
66};
Note: See TracBrowser for help on using the repository browser.