[410] | 1 | define([ |
---|
| 2 | 'dojo/_base/json', |
---|
| 3 | 'dojo/_base/lang', |
---|
| 4 | 'dojo/Deferred', |
---|
| 5 | 'dojo/request', |
---|
| 6 | 'dojox/lang/functional', |
---|
| 7 | './docs' |
---|
| 8 | ],function(json,lang,Deferred,request,func,docs){ |
---|
| 9 | var dbUrl = "http://localhost:5984/"; |
---|
[361] | 10 | |
---|
[410] | 11 | function serializeFunctions(value) { |
---|
| 12 | if ( value === null ) { |
---|
| 13 | return null; |
---|
| 14 | } else if ( lang.isArray(value) ) { |
---|
| 15 | return value; |
---|
| 16 | } else if ( lang.isFunction(value) ) { |
---|
| 17 | return value.toString(); |
---|
| 18 | } else if ( lang.isObject(value) ) { |
---|
| 19 | return func.mapIn(value,serializeFunctions); |
---|
| 20 | } else { |
---|
| 21 | return value; |
---|
| 22 | } |
---|
| 23 | } |
---|
[328] | 24 | |
---|
[410] | 25 | function req(method,path,body) { |
---|
| 26 | var args = { |
---|
| 27 | contentType: 'application/json', |
---|
[362] | 28 | handleAs: 'json', |
---|
[410] | 29 | data: json.toJson(body || {}) |
---|
| 30 | }; |
---|
| 31 | return request[method](dbUrl+path,args); |
---|
| 32 | } |
---|
[361] | 33 | |
---|
[410] | 34 | function asyncSeq(functions) { |
---|
| 35 | var d = new Deferred(); |
---|
| 36 | (function stepper(fs,arg) { |
---|
| 37 | if ( fs.length > 0 ) { |
---|
| 38 | try { |
---|
| 39 | var f = fs.shift(); |
---|
| 40 | var ret = f(arg); |
---|
| 41 | if ( ret && ret.then ) { |
---|
| 42 | ret.then(function(ret){ |
---|
| 43 | stepper(fs,ret); |
---|
| 44 | }); |
---|
| 45 | } else { |
---|
| 46 | stepper(fs,ret); |
---|
| 47 | } |
---|
| 48 | } catch(err) { |
---|
| 49 | d.reject(err); |
---|
| 50 | } |
---|
[328] | 51 | } else { |
---|
[410] | 52 | d.resolve(); |
---|
[328] | 53 | } |
---|
[410] | 54 | })(functions); |
---|
| 55 | return d.promise; |
---|
| 56 | } |
---|
[328] | 57 | |
---|
[410] | 58 | function asyncFor(list,callback,ctx) { |
---|
| 59 | var d = new Deferred(); |
---|
| 60 | (function stepper(list,idx){ |
---|
| 61 | if ( list.length > 0 ) { |
---|
| 62 | var el = list.shift(); |
---|
| 63 | var ret = callback.call(ctx,el,idx); |
---|
| 64 | if ( ret && ret.then ) { |
---|
| 65 | ret.then(function(){ |
---|
| 66 | stepper(list,idx+1); |
---|
[361] | 67 | }); |
---|
[410] | 68 | } else { |
---|
| 69 | stepper(list,idx+1); |
---|
| 70 | } |
---|
| 71 | } else { |
---|
| 72 | d.resolve(); |
---|
[361] | 73 | } |
---|
[410] | 74 | })(list,0); |
---|
| 75 | return d.promise; |
---|
| 76 | } |
---|
[328] | 77 | |
---|
[410] | 78 | asyncSeq([ |
---|
| 79 | function(){ |
---|
| 80 | console.log("Configuring CouchDB for RFT:"); |
---|
| 81 | }, |
---|
| 82 | function(){ |
---|
| 83 | console.log("Checking CouchDB version"); |
---|
| 84 | return req('get','') |
---|
| 85 | .then(function(res){ |
---|
| 86 | if (res.version !== "1.2.0" ) { |
---|
| 87 | console.log("Found "+res.version+", only tested with CouchDB 1.2.0") |
---|
| 88 | } else { |
---|
| 89 | console.log("CouchDB 1.2.0 found"); |
---|
| 90 | } |
---|
| 91 | }); |
---|
| 92 | },function(){ |
---|
| 93 | console.log("Checking database 'rft'"); |
---|
| 94 | return req('get','rft') |
---|
| 95 | .then(function(){ |
---|
| 96 | console.log("Database 'rft' found."); |
---|
| 97 | },function(){ |
---|
| 98 | console.log("Creating database 'rft'"); |
---|
| 99 | return req('put','/rft'); |
---|
| 100 | }); |
---|
| 101 | },function(){ |
---|
| 102 | return serializeFunctions(docs); |
---|
| 103 | },function(docs){ |
---|
| 104 | console.log("Putting documents in database."); |
---|
| 105 | return asyncFor(func.keys(docs),function(docUrl){ |
---|
| 106 | var doc = docs[docUrl]; |
---|
| 107 | var configAction = doc.__configAction; |
---|
| 108 | delete doc.__configAction; |
---|
| 109 | switch (configAction) { |
---|
| 110 | case "ignore": |
---|
| 111 | console.log(docUrl+" ignored."); |
---|
| 112 | case "update": |
---|
| 113 | console.log(docUrl+" updating."); |
---|
| 114 | return req('get',docUrl) |
---|
| 115 | .then(function(oldDoc){ |
---|
| 116 | lang.mixin(oldDoc,doc); |
---|
| 117 | return req('put',docUrl,oldDoc); |
---|
| 118 | },function(){ |
---|
| 119 | return req('put',docUrl,doc); |
---|
| 120 | }); |
---|
| 121 | case "replace": |
---|
| 122 | default: |
---|
| 123 | console.log(docUrl+" replacing."); |
---|
| 124 | return req('get',docUrl) |
---|
| 125 | .then(function(oldDoc){ |
---|
| 126 | doc['_rev'] = oldDoc['_rev']; |
---|
| 127 | return req('put',docUrl,doc); |
---|
| 128 | },function(){ |
---|
| 129 | return req('put',docUrl,doc); |
---|
| 130 | }); |
---|
| 131 | } |
---|
| 132 | }); |
---|
| 133 | },function(){ |
---|
| 134 | console.log("Done!"); |
---|
[361] | 135 | } |
---|
[410] | 136 | ]); |
---|
[360] | 137 | |
---|
[328] | 138 | |
---|
[408] | 139 | }); |
---|