[455] | 1 | var q = require('q'); |
---|
[466] | 2 | var request = require('../util/request'); |
---|
[455] | 3 | var _ = require('underscore'); |
---|
[464] | 4 | var util = require('util'); |
---|
[455] | 5 | |
---|
| 6 | var designDocs = require('./couchdb-design-docs'); |
---|
| 7 | |
---|
[464] | 8 | 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 | } |
---|
[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 | }, |
---|
[466] | 36 | body: content |
---|
[464] | 37 | }; |
---|
[466] | 38 | return request(url,options); |
---|
[464] | 39 | } |
---|
[455] | 40 | |
---|
[464] | 41 | console.log("Configuring CouchDB for QED"); |
---|
| 42 | console.log("Checking CouchDB version"); |
---|
[466] | 43 | return dbRequest('GET','') |
---|
[455] | 44 | .then(function(res){ |
---|
[464] | 45 | if (res.version !== "1.2.0" ) { |
---|
| 46 | console.log("Found "+res.version+", only tested with CouchDB 1.2.0"); |
---|
| 47 | } else { |
---|
| 48 | console.log("CouchDB 1.2.0 found"); |
---|
| 49 | } |
---|
| 50 | }).then(function(res){ |
---|
| 51 | console.log("Checking database 'qed'"); |
---|
[466] | 52 | return dbRequest('GET','qed') |
---|
[464] | 53 | .then(function(res){ |
---|
| 54 | console.log("Database 'qed' found."); |
---|
| 55 | },function(err){ |
---|
| 56 | console.log("Creating database 'qed'"); |
---|
[466] | 57 | return dbRequest('PUT','qed'); |
---|
[464] | 58 | }); |
---|
| 59 | }).then(function(){ |
---|
| 60 | console.log("Putting documents in database."); |
---|
| 61 | designDocs = stringifyFunctions(designDocs); |
---|
| 62 | return q.all(_.map(designDocs, function(doc,docUrl){ |
---|
| 63 | var configAction = doc.__configAction; |
---|
| 64 | delete doc.__configAction; |
---|
| 65 | switch (configAction) { |
---|
| 66 | case "ignore": |
---|
| 67 | console.log(docUrl+" ignored."); |
---|
| 68 | break; |
---|
| 69 | case "update": |
---|
| 70 | console.log(docUrl+" updating."); |
---|
[466] | 71 | return dbRequest('GET',docUrl) |
---|
[464] | 72 | .then(function(oldDoc){ |
---|
| 73 | _.extend(oldDoc,doc); |
---|
[466] | 74 | return dbRequest('PUT',docUrl,oldDoc); |
---|
[464] | 75 | },function(){ |
---|
[466] | 76 | return dbRequest('PUT',docUrl,doc); |
---|
[464] | 77 | }); |
---|
| 78 | break; |
---|
| 79 | case "replace": |
---|
| 80 | default: |
---|
| 81 | console.log(docUrl+" replacing."); |
---|
[466] | 82 | return dbRequest('GET',docUrl) |
---|
[464] | 83 | .then(function(oldDoc){ |
---|
| 84 | _.extend(doc,_.pick(oldDoc,'_id','_rev')); |
---|
[466] | 85 | return dbRequest('PUT',docUrl,doc); |
---|
[464] | 86 | },function(){ |
---|
[466] | 87 | return dbRequest('PUT',docUrl,doc); |
---|
[464] | 88 | }); |
---|
| 89 | break; |
---|
| 90 | } |
---|
| 91 | })); |
---|
| 92 | }).then(function(){ |
---|
| 93 | console.log("Done!"); |
---|
[455] | 94 | },function(err){ |
---|
[464] | 95 | console.error("ERROR",err,err.stack); |
---|
[455] | 96 | }); |
---|
[464] | 97 | |
---|
| 98 | }; |
---|