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

Last change on this file since 470 was 470, checked in by hendrikvanantwerpen, 12 years ago

Reorganized app and fixed some style issues.

Was looking for a bug where the proxy would not forward requests to the
database. This is somehow magically resolved now.

File size: 3.4 KB
RevLine 
[468]1var q = require('q'),
2    request = require('../util/q-request'),
3    _ = require('underscore'),
4    util = require('util');
[455]5
6var designDocs = require('./couchdb-design-docs');
7
[464]8module.exports = function(couchDbURL) {
9
10    function stringifyFunctions(value) {
11        if ( value === null ) {
12            return null;
13        } else if ( _.isArray(value) ) {
14            return value;
15        } else if ( _.isFunction(value) ) {
16            return value.toString();
17        } else if ( _.isObject(value) ) {
18            value = _.clone(value);
19            _.each(value, function(propValue, propName){
20                value[propName] = stringifyFunctions(propValue);
21            });
22            return value;
23        } else {
24            return value;
25        }
[455]26    }
27
[466]28    function dbRequest(method,path,content) {
[464]29        var url = couchDbURL+path;
30        var options = {
31            method: method,
32            headers: {
33                'content-type': 'application/json; charset=utf-8',
34                'accept': 'application/json'
35            },
[468]36            body: JSON.stringify(content)
[464]37        };
[468]38        //console.log('req',url,options);
39        return request(url,options).then(function(res){
40            return JSON.parse(res);
41        }, function(err){
42            return JSON.parse(err);
43        });
[464]44    }
[455]45
[464]46    console.log("Configuring CouchDB for QED");
47    console.log("Checking CouchDB version");
[466]48    return dbRequest('GET','')
[455]49    .then(function(res){
[464]50        if (res.version !== "1.2.0" ) {
51            console.log("Found "+res.version+", only tested with CouchDB 1.2.0");
52        } else {
53            console.log("CouchDB 1.2.0 found");
54        }
55    }).then(function(res){
56        console.log("Checking database 'qed'");
[466]57        return dbRequest('GET','qed')
[464]58        .then(function(res){
59            console.log("Database 'qed' found.");
60        },function(err){
61            console.log("Creating database 'qed'");
[466]62            return dbRequest('PUT','qed');
[464]63        });
64    }).then(function(){
65        console.log("Putting documents in database.");
66        designDocs = stringifyFunctions(designDocs);
67        return q.all(_.map(designDocs, function(doc,docUrl){
[470]68            var configAction = doc.__configAction || "replace";
[464]69            delete doc.__configAction;
70            switch (configAction) {
[470]71            case "ignore":
72                console.log(docUrl+" ignored.");
[464]73                break;
[470]74            case "update":
75                console.log(docUrl+" updating.");
76                return dbRequest('GET',docUrl)
77                .then(function(oldDoc){
78                    _.extend(oldDoc,doc);
79                    return dbRequest('PUT',docUrl,oldDoc);
80                },function(){
81                    return dbRequest('PUT',docUrl,doc);
82                });
83            case "replace":
84                console.log(docUrl+" replacing.");
85                return dbRequest('GET',docUrl)
86                .then(function(oldDoc){
87                    _.extend(doc,_.pick(oldDoc,'_id','_rev'));
88                    return dbRequest('PUT',docUrl,doc);
89                },function(){
90                    return dbRequest('PUT',docUrl,doc);
91                });
92            default:
93                console.warn("Unknown action",configAction);
[464]94                break;
95            }
96        }));
[468]97    }).then(function(results){
[464]98        console.log("Done!");
[455]99    },function(err){
[464]100        console.error("ERROR",err,err.stack);
[455]101    });
[464]102
103};
Note: See TracBrowser for help on using the repository browser.