source: Dev/branches/rest-dojo-ui/client/config/db.js @ 361

Last change on this file since 361 was 361, checked in by hendrikvanantwerpen, 13 years ago

Easier to change DB config, all docs in one file now.

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