Index: Dev/trunk/src/server/config/config-couchdb.js
===================================================================
--- Dev/trunk/src/server/config/config-couchdb.js	(revision 470)
+++ Dev/trunk/src/server/config/config-couchdb.js	(revision 475)
@@ -1,53 +1,19 @@
-var q = require('q'),
-    request = require('../util/q-request'),
-    _ = require('underscore'),
-    util = require('util');
+var Q = require('q')
+  , request = require('../util/q-request')
+  , _ = require('underscore')
+  , CouchDB = require('../util/couch').CouchDB
+  , util = require('util');
 
 var designDocs = require('./couchdb-design-docs');
 
 module.exports = function(couchDbURL) {
-
-    function stringifyFunctions(value) {
-        if ( value === null ) {
-            return null;
-        } else if ( _.isArray(value) ) {
-            return value;
-        } else if ( _.isFunction(value) ) {
-            return value.toString();
-        } else if ( _.isObject(value) ) {
-            value = _.clone(value);
-            _.each(value, function(propValue, propName){
-                value[propName] = stringifyFunctions(propValue);
-            });
-            return value;
-        } else {
-            return value;
-        }
-    }
-
-    function dbRequest(method,path,content) {
-        var url = couchDbURL+path;
-        var options = {
-            method: method,
-            headers: {
-                'content-type': 'application/json; charset=utf-8',
-                'accept': 'application/json'
-            },
-            body: JSON.stringify(content)
-        };
-        //console.log('req',url,options);
-        return request(url,options).then(function(res){
-            return JSON.parse(res);
-        }, function(err){
-            return JSON.parse(err);
-        });
-    }
+    var server = new CouchDB(couchDbURL);
 
     console.log("Configuring CouchDB for QED");
     console.log("Checking CouchDB version");
-    return dbRequest('GET','')
-    .then(function(res){
-        if (res.version !== "1.2.0" ) {
-            console.log("Found "+res.version+", only tested with CouchDB 1.2.0");
+    return server.get('')
+    .then(function(info){
+        if (info.version !== "1.2.0" ) {
+            console.log("Found "+info.version+", only tested with CouchDB 1.2.0");
         } else {
             console.log("CouchDB 1.2.0 found");
@@ -55,15 +21,14 @@
     }).then(function(res){
         console.log("Checking database 'qed'");
-        return dbRequest('GET','qed')
-        .then(function(res){
+        return server.get('qed')
+        .then(function(db){
             console.log("Database 'qed' found.");
         },function(err){
             console.log("Creating database 'qed'");
-            return dbRequest('PUT','qed');
+            return server.put('qed');
         });
     }).then(function(){
         console.log("Putting documents in database.");
-        designDocs = stringifyFunctions(designDocs);
-        return q.all(_.map(designDocs, function(doc,docUrl){
+        return _.reduce(designDocs, function(memo, doc, docUrl) {
             var configAction = doc.__configAction || "replace";
             delete doc.__configAction;
@@ -71,30 +36,34 @@
             case "ignore":
                 console.log(docUrl+" ignored.");
-                break;
+                return memo;
             case "update":
                 console.log(docUrl+" updating.");
-                return dbRequest('GET',docUrl)
-                .then(function(oldDoc){
-                    _.extend(oldDoc,doc);
-                    return dbRequest('PUT',docUrl,oldDoc);
-                },function(){
-                    return dbRequest('PUT',docUrl,doc);
+                return memo.then(function(){
+                    server.get(docUrl)
+                    .then(function(oldDoc){
+                        _.extend(oldDoc,doc);
+                        return server.put(docUrl,oldDoc);
+                    },function(){
+                        return server.put(docUrl,doc);
+                    });
                 });
             case "replace":
                 console.log(docUrl+" replacing.");
-                return dbRequest('GET',docUrl)
-                .then(function(oldDoc){
-                    _.extend(doc,_.pick(oldDoc,'_id','_rev'));
-                    return dbRequest('PUT',docUrl,doc);
-                },function(){
-                    return dbRequest('PUT',docUrl,doc);
+                return memo.then(function(){
+                    server.get(docUrl)
+                    .then(function(oldDoc){
+                        _.extend(doc,_.pick(oldDoc,'_id','_rev'));
+                        return server.put(docUrl,doc);
+                    },function(){
+                        return server.put(docUrl,doc);
+                    });
                 });
             default:
                 console.warn("Unknown action",configAction);
-                break;
+                return memo;
             }
-        }));
+        }, Q.resolve());
     }).then(function(results){
-        console.log("Done!");
+        console.log("Done!", results);
     },function(err){
         console.error("ERROR",err,err.stack);
