1 | define([ |
---|
2 | 'dojo/_base/json', |
---|
3 | 'dojo/_base/lang', |
---|
4 | 'dojox/lang/functional', |
---|
5 | './util/async', |
---|
6 | './util/db', |
---|
7 | './data/design-docs' |
---|
8 | ],function(json,lang,func,async,db,docs){ |
---|
9 | |
---|
10 | function serializeFunctions(value) { |
---|
11 | if ( value === null ) { |
---|
12 | return null; |
---|
13 | } else if ( lang.isArray(value) ) { |
---|
14 | return value; |
---|
15 | } else if ( lang.isFunction(value) ) { |
---|
16 | return value.toString(); |
---|
17 | } else if ( lang.isObject(value) ) { |
---|
18 | return func.mapIn(value,serializeFunctions); |
---|
19 | } else { |
---|
20 | return value; |
---|
21 | } |
---|
22 | } |
---|
23 | |
---|
24 | async.seq([ |
---|
25 | function(){ |
---|
26 | console.log("Configuring CouchDB for QED:"); |
---|
27 | }, |
---|
28 | function(){ |
---|
29 | console.log("Checking CouchDB version"); |
---|
30 | return db.req('get','') |
---|
31 | .then(function(res){ |
---|
32 | if (res.version !== "1.2.0" ) { |
---|
33 | console.log("Found "+res.version+", only tested with CouchDB 1.2.0") |
---|
34 | } else { |
---|
35 | console.log("CouchDB 1.2.0 found"); |
---|
36 | } |
---|
37 | }); |
---|
38 | },function(){ |
---|
39 | console.log("Checking database 'qed'"); |
---|
40 | return db.req('get','qed') |
---|
41 | .then(function(res){ |
---|
42 | if (res.error) { |
---|
43 | console.log("Creating database 'qed'"); |
---|
44 | return db.req('put','/qed'); |
---|
45 | } else { |
---|
46 | console.log("Database 'qed' found."); |
---|
47 | } |
---|
48 | },function(res){ |
---|
49 | console.log("Creating database 'qed'"); |
---|
50 | return db.req('put','/qed'); |
---|
51 | }); |
---|
52 | },function(){ |
---|
53 | return serializeFunctions(docs); |
---|
54 | },function(docs){ |
---|
55 | console.log("Putting documents in database."); |
---|
56 | return async.forEach(func.keys(docs),function(docUrl){ |
---|
57 | var doc = docs[docUrl]; |
---|
58 | var configAction = doc.__configAction; |
---|
59 | delete doc.__configAction; |
---|
60 | switch (configAction) { |
---|
61 | case "ignore": |
---|
62 | console.log(docUrl+" ignored."); |
---|
63 | case "update": |
---|
64 | console.log(docUrl+" updating."); |
---|
65 | return db.req('get',docUrl) |
---|
66 | .then(function(oldDoc){ |
---|
67 | lang.mixin(oldDoc,doc); |
---|
68 | return db.req('put',docUrl,oldDoc); |
---|
69 | },function(){ |
---|
70 | return db.req('put',docUrl,doc); |
---|
71 | }); |
---|
72 | case "replace": |
---|
73 | default: |
---|
74 | console.log(docUrl+" replacing."); |
---|
75 | return db.req('get',docUrl) |
---|
76 | .then(function(oldDoc){ |
---|
77 | doc['_rev'] = oldDoc['_rev']; |
---|
78 | return db.req('put',docUrl,doc); |
---|
79 | },function(){ |
---|
80 | return db.req('put',docUrl,doc); |
---|
81 | }); |
---|
82 | } |
---|
83 | }); |
---|
84 | },function(){ |
---|
85 | console.log("Done!"); |
---|
86 | } |
---|
87 | ]); |
---|
88 | |
---|
89 | |
---|
90 | }); |
---|