Changeset 475 for Dev/trunk/src/server/config/config-couchdb.js
- Timestamp:
- 07/28/13 17:46:12 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/src/server/config/config-couchdb.js
r470 r475 1 var q = require('q'), 2 request = require('../util/q-request'), 3 _ = require('underscore'), 4 util = require('util'); 1 var Q = require('q') 2 , request = require('../util/q-request') 3 , _ = require('underscore') 4 , CouchDB = require('../util/couch').CouchDB 5 , util = require('util'); 5 6 6 7 var designDocs = require('./couchdb-design-docs'); 7 8 8 9 module.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 } 26 } 27 28 function dbRequest(method,path,content) { 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 }, 36 body: JSON.stringify(content) 37 }; 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 }); 44 } 10 var server = new CouchDB(couchDbURL); 45 11 46 12 console.log("Configuring CouchDB for QED"); 47 13 console.log("Checking CouchDB version"); 48 return dbRequest('GET','')49 .then(function( res){50 if ( res.version !== "1.2.0" ) {51 console.log("Found "+ res.version+", only tested with CouchDB 1.2.0");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"); 52 18 } else { 53 19 console.log("CouchDB 1.2.0 found"); … … 55 21 }).then(function(res){ 56 22 console.log("Checking database 'qed'"); 57 return dbRequest('GET','qed')58 .then(function( res){23 return server.get('qed') 24 .then(function(db){ 59 25 console.log("Database 'qed' found."); 60 26 },function(err){ 61 27 console.log("Creating database 'qed'"); 62 return dbRequest('PUT','qed');28 return server.put('qed'); 63 29 }); 64 30 }).then(function(){ 65 31 console.log("Putting documents in database."); 66 designDocs = stringifyFunctions(designDocs); 67 return q.all(_.map(designDocs, function(doc,docUrl){ 32 return _.reduce(designDocs, function(memo, doc, docUrl) { 68 33 var configAction = doc.__configAction || "replace"; 69 34 delete doc.__configAction; … … 71 36 case "ignore": 72 37 console.log(docUrl+" ignored."); 73 break;38 return memo; 74 39 case "update": 75 40 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); 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 }); 82 49 }); 83 50 case "replace": 84 51 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); 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 }); 91 60 }); 92 61 default: 93 62 console.warn("Unknown action",configAction); 94 break;63 return memo; 95 64 } 96 } ));65 }, Q.resolve()); 97 66 }).then(function(results){ 98 console.log("Done!" );67 console.log("Done!", results); 99 68 },function(err){ 100 69 console.error("ERROR",err,err.stack);
Note: See TracChangeset
for help on using the changeset viewer.