1 | require([ |
---|
2 | 'dojo/_base/json', |
---|
3 | 'dojo/_base/lang', |
---|
4 | 'dojo/_base/xhr', |
---|
5 | 'dojo/parser', |
---|
6 | 'dojo/query', |
---|
7 | 'dijit/registry', |
---|
8 | 'dojo/text!./docs.json', |
---|
9 | 'dojo/domReady!', |
---|
10 | 'dijit/form/TextBox', |
---|
11 | 'dijit/form/Button', |
---|
12 | 'dijit/form/CheckBox', |
---|
13 | ], |
---|
14 | function(json,lang,xhr,parser,query,registry,docsJson){ |
---|
15 | var logNode; |
---|
16 | var usernameInput, passwordInput, resetInput, configButton; |
---|
17 | var defaultUsername = "", defaultPassword = ""; |
---|
18 | var dbUrl = "../data/"; |
---|
19 | |
---|
20 | parser.parse(); |
---|
21 | |
---|
22 | query("#log").forEach(function(n){logNode = n;}); |
---|
23 | usernameInput = registry.byId('username'); |
---|
24 | passwordInput = registry.byId('password'); |
---|
25 | resetInput = registry.byId('reset'); |
---|
26 | configButton = registry.byId('configure'); |
---|
27 | |
---|
28 | usernameInput.set('value',defaultUsername); |
---|
29 | passwordInput.set('value',defaultPassword); |
---|
30 | configButton.on('click',configure); |
---|
31 | |
---|
32 | log("Give CouchDB admin username & password and click 'Configure' to start.\nIf the database already exists, rft_admin password will suffice.",true); |
---|
33 | |
---|
34 | function configure(){ |
---|
35 | var docs; |
---|
36 | |
---|
37 | log("Configuring CouchDB for RFT:",true); |
---|
38 | |
---|
39 | var username = usernameInput.get('value'); |
---|
40 | var password = passwordInput.get('value'); |
---|
41 | var reset = resetInput.get('value'); |
---|
42 | |
---|
43 | log("Downloading most recent configuration."); |
---|
44 | xhr('GET',{ |
---|
45 | url: 'docs.json', |
---|
46 | handleAs: 'json', |
---|
47 | sync: true |
---|
48 | },true) |
---|
49 | .then(function(result){ |
---|
50 | docs = result; |
---|
51 | }); |
---|
52 | |
---|
53 | function req(method,url,body) { |
---|
54 | var args = { |
---|
55 | url: dbUrl+url, |
---|
56 | contentType: 'application/json', |
---|
57 | handleAs: 'json', |
---|
58 | sync: true |
---|
59 | }; |
---|
60 | if ( !body || lang.isObject(body) ) { |
---|
61 | body = json.toJson(body || {}); |
---|
62 | } |
---|
63 | args.rawBody = body; |
---|
64 | if ( username ) { |
---|
65 | args.user = username; |
---|
66 | args.password = password; |
---|
67 | } |
---|
68 | return xhr(method,args,true); |
---|
69 | } |
---|
70 | |
---|
71 | log("Checking CouchDB version"); |
---|
72 | req('GET','/') |
---|
73 | .then(function(res){ |
---|
74 | if (res.version !== "1.2.0" ) { |
---|
75 | log("Found "+res.version+", only tested with CouchDB 1.2.0") |
---|
76 | } else { |
---|
77 | log("CouchDB 1.2.0 found") |
---|
78 | } |
---|
79 | }); |
---|
80 | |
---|
81 | var exists = false; |
---|
82 | log("Checking database 'rft'"); |
---|
83 | req('GET','/rft') |
---|
84 | .then(function(db){ |
---|
85 | log("Database 'rft' found.") |
---|
86 | exists = true; |
---|
87 | }); |
---|
88 | if ( exists && reset ) { |
---|
89 | req('DELETE','/rft'); |
---|
90 | exists = false; |
---|
91 | } |
---|
92 | if ( !exists ) { |
---|
93 | log("Creating database 'rft'") |
---|
94 | req('PUT','/rft') |
---|
95 | }; |
---|
96 | |
---|
97 | function processDoc(docUrl,doc){ |
---|
98 | var configAction = doc.__configAction; |
---|
99 | delete doc.__configAction; |
---|
100 | switch (configAction) { |
---|
101 | case "ignore": |
---|
102 | log(docUrl+" ignored."); |
---|
103 | return false; |
---|
104 | case "update": |
---|
105 | log(docUrl+" updating."); |
---|
106 | return req('GET',docUrl) |
---|
107 | .then(function(oldDoc){ |
---|
108 | lang.mixin(oldDoc,doc); |
---|
109 | return req('PUT',docUrl,oldDoc); |
---|
110 | },function(){ |
---|
111 | return req('PUT',docUrl,doc); |
---|
112 | }); |
---|
113 | case "replace": |
---|
114 | default: |
---|
115 | log(docUrl+" replacing."); |
---|
116 | return req('GET',docUrl) |
---|
117 | .then(function(oldDoc){ |
---|
118 | doc['_rev'] = oldDoc['_rev']; |
---|
119 | return req('PUT',docUrl,doc); |
---|
120 | },function(){ |
---|
121 | return req('PUT',docUrl,doc); |
---|
122 | }); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | for (var docUrl in docs) { |
---|
127 | processDoc(docUrl,docs[docUrl]); |
---|
128 | } |
---|
129 | |
---|
130 | log("Done!"); |
---|
131 | } |
---|
132 | |
---|
133 | function log(text,overwrite) { |
---|
134 | if ( overwrite ) logNode.innerHTML = text |
---|
135 | else logNode.innerHTML = logNode.innerHTML + '\n' + text; |
---|
136 | } |
---|
137 | |
---|
138 | }); |
---|