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

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

Config script now independent of Dojo.

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