-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbExplorer.js
137 lines (127 loc) · 4.38 KB
/
dbExplorer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
dojo.provide('unc.dbExporer');
dojo.require('dojox.data.JsonRestStore');
dojo.require('unc.GenericEditor');
var limit = 100;
dojo.declare('unc.dbExplorer', [], {
constructor: function() {
dojo.connect(dijit.byId('db'), 'onBlur', this, 'listCollection')
dojo.connect(dijit.byId('openButton'), 'onClick', this, 'open')
console.log('constructed');
},
listCollection: function() {
console.log('listCollection');
var db = dijit.byId('db').value;
// using store to get list of collections
uow.getDatabase({ database: db, collection: '*', mode: 'L' }).addCallback(function(db) {
var select = dijit.byId('co');
select.store = db;
select.searchAttr = '_id';
}).addErrback(function(err) {
console.log('list failed', err);
});
},
open: function() {
var db = dijit.byId('db').value;
var co = dijit.byId('co').value;
var sh = dijit.byId('schemaName').value;
console.log(db, co, sh);
this.schema = null;
this.store = null;
dojo.empty('myeditor');
if (sh) {
dojo.xhrGet({
url: sh,
handleAs: 'json',
load: dojo.hitch(this, function(data) {
this.schema = data;
this.initEditor();
}),
error: function(err) {
console.log(err);
}
});
} else {
this.schema = false;
}
uow.getDatabase({ database: db, collection: co, mode: 'crud' }).addCallback(
dojo.hitch(this, function(db) {
this.store = db;
this.initEditor();
})).addErrback(function() {
console.log('error opening db');
});
console.log('open done');
},
initEditor: function() {
console.log('init', this.schema, this.store);
if (this.schema === null) {
console.log('no schema yet');
return;
} else if (this.schema === false && this.store !== null) {
this.store.fetch({
start: 0,
count: 1,
onComplete: dojo.hitch(this, function(i) {
console.log('fetch returns', i);
this.schema = this.guessSchema(i[0]);
console.log('guessed schema', this.schema);
this.initEditor();
}),
});
return;
}
if (this.store === null) {
console.log('no db yet');
return;
}
// got both
dojo.byId('schema').innerHTML = dojo.toJson(this.schema, true);
var gridLayout = [
{ name: 'Id', field: '_id', width: "100%" } ];
console.log('creating generic editor');
var editor = new unc.GenericEditor({ store: this.store, schema: this.schema,
gridLayout: gridLayout });
dojo.place(editor.domNode, 'myeditor', 'only');
console.log('done creating');
editor.startup();
console.log('done startup');
},
guessSchema: function(a) {
if (--limit < 0) return;
console.log('guess', a);
if (typeof(a) === 'string') {
console.log('string');
return { type: 'string' };
} else if (dojo.isArray(a)) {
console.log('array');
return { type: 'array',
items: this.guessSchema(a[0]) };
} else if (typeof(a) === 'number') {
console.log('number');
if (Math.floor(a) === a) {
return { type: 'integer' };
} else {
return { type: 'number' };
}
} else if (typeof(a) === 'object') {
console.log('object');
var r = { type: 'object',
properties: {} };
for (var p in a) {
if (!p.match(/_/)) {
console.log('property', p);
r.properties[p] = this.guessSchema(a[p]);
console.log(r.properties);
}
}
return r;
} else {
return { type: 'unknown' };
}
}
});
function main() {
console.log('main');
var dbe = new unc.dbExplorer();
}
dojo.ready(main);