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

Last change on this file since 475 was 475, checked in by hendrikvanantwerpen, 12 years ago
  • Added CouchDB script to make interacting with couch easier.
  • Use this script in the database configuration.
  • Get CSV export to work.
File size: 2.5 KB
RevLine 
[475]1var Q = require('q')
2  , request = require('../util/q-request')
3  , _ = require('underscore')
4  , CouchDB = require('../util/couch').CouchDB
5  , util = require('util');
[455]6
7var designDocs = require('./couchdb-design-docs');
8
[464]9module.exports = function(couchDbURL) {
[475]10    var server = new CouchDB(couchDbURL);
[464]11
12    console.log("Configuring CouchDB for QED");
13    console.log("Checking CouchDB version");
[475]14    return server.get('')
15    .then(function(info){
16        if (info.version !== "1.2.0" ) {
17            console.log("Found "+info.version+", only tested with CouchDB 1.2.0");
[464]18        } else {
19            console.log("CouchDB 1.2.0 found");
20        }
21    }).then(function(res){
22        console.log("Checking database 'qed'");
[475]23        return server.get('qed')
24        .then(function(db){
[464]25            console.log("Database 'qed' found.");
26        },function(err){
27            console.log("Creating database 'qed'");
[475]28            return server.put('qed');
[464]29        });
30    }).then(function(){
31        console.log("Putting documents in database.");
[475]32        return _.reduce(designDocs, function(memo, doc, docUrl) {
[470]33            var configAction = doc.__configAction || "replace";
[464]34            delete doc.__configAction;
35            switch (configAction) {
[470]36            case "ignore":
37                console.log(docUrl+" ignored.");
[475]38                return memo;
[470]39            case "update":
40                console.log(docUrl+" updating.");
[475]41                return memo.then(function(){
42                    server.get(docUrl)
43                    .then(function(oldDoc){
44                        _.extend(oldDoc,doc);
45                        return server.put(docUrl,oldDoc);
46                    },function(){
47                        return server.put(docUrl,doc);
48                    });
[470]49                });
50            case "replace":
51                console.log(docUrl+" replacing.");
[475]52                return memo.then(function(){
53                    server.get(docUrl)
54                    .then(function(oldDoc){
55                        _.extend(doc,_.pick(oldDoc,'_id','_rev'));
56                        return server.put(docUrl,doc);
57                    },function(){
58                        return server.put(docUrl,doc);
59                    });
[470]60                });
61            default:
62                console.warn("Unknown action",configAction);
[475]63                return memo;
[464]64            }
[475]65        }, Q.resolve());
[468]66    }).then(function(results){
[475]67        console.log("Done!", results);
[455]68    },function(err){
[464]69        console.error("ERROR",err,err.stack);
[455]70    });
[464]71
72};
Note: See TracBrowser for help on using the repository browser.