-
Notifications
You must be signed in to change notification settings - Fork 5
/
curious2.js
386 lines (326 loc) · 10.4 KB
/
curious2.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Curious client-side JSON parsing
//
(function(){
var QueryTermFollow = function(term) {
this.term = term;
this.to_s = function() { return this.term; };
this.conditional = false;
this.left = false;
};
var QueryTermHaving = function(term) {
this.term = term;
this.to_s = function() { return '+('+term+')'; };
this.conditional = true;
this.left = false;
};
var QueryTermNotHaving = function(term) {
this.term = term;
this.to_s = function() { return '-('+term+')'; };
this.conditional = true;
this.left = false;
};
var QueryTermWith = function(term) {
this.term = term;
this.to_s = function() { return '?('+term+')'; };
this.conditional = false;
this.left = true;
};
function make_obj(klass) {
return function(model) {
return new klass();
}
}
var CuriousQuery = function() {
this.terms = [];
this.relationships = [];
this.objfs = [];
this.params = null;
this.existing_objects = null; // array of object arrays
};
CuriousQuery.prototype = {
query: function() {
var s = [];
for (var i=0; i<this.terms.length; i++) {
if (i > 0) {
if (this.terms[i].conditional)
s.push(' ');
else if (!this.terms[i].conditional && !this.terms[i].left && !this.terms[i-1].left)
s.push(', ');
else
s.push(' ');
}
s.push(this.terms[i].to_s());
}
return s.join('');
},
// extend this query with another query
extend: function(query) {
for (var i=0; i<query.terms.length; i++) {
this.append(query.terms[i], query.relationships[i], query.objfs[i]);
}
return this;
},
append: function(term, relationship, obj_f) {
this.terms.push(term);
if (relationship) {
this.relationships.push(relationship);
this.objfs.push(obj_f);
}
return this;
},
start: function(s, relationship) {
return this.append(new QueryTermFollow(s), relationship);
},
follow: function(s, relationship) {
return this.append(new QueryTermFollow(s), relationship);
},
having: function(s) {
return this.append(new QueryTermHaving(s));
},
not_having: function(s) {
return this.append(new QueryTermNotHaving(s));
},
with: function(s, relationship) {
return this.append(new QueryTermWith(s), relationship);
},
wrap_with: function(klass) {
if (this.objfs.length === 0)
throw("Cannot specify type of object before starting a query");
this.objfs[this.objfs.length-1] = make_obj(klass);
return this;
},
wrap_dynamically: function(f) {
if (this.objfs.length === 0)
throw("Cannot specify function for creating object before starting a query");
this.objfs[this.objfs.length-1] = f;
return this;
},
set_params: function(p) {
this.params = p;
return this;
},
set_existing_objects: function(objs) { // array of object arrays
this.existing_objects = objs;
return this;
},
perform: function(clt, objects_cb, trees_cb) {
var q = this.query()
clt.get(q, this.relationships, this.objfs,
this.params, this.existing_objects,
objects_cb, trees_cb);
},
promise: function(clt) {
var q = this.query();
var self = this;
return new Promise(function(resolve, reject) {
clt.get(q, self.relationships, self.objfs, self.params, self.existing_objects,
function(objects) { resolve(objects); });
});
}
};
var CuriousObjects = (function() {
function CuriousObject(hash_data) {
this.id = hash_data.id;
for (var k in hash_data) {
this[k] = hash_data[k];
}
this.__url = null;
this.__model = null;
}
function parse_objects(data, model, obj_f, existing_objs) {
if (data.objects === undefined) { return []; }
var objects = [];
for (var i=0; i<data.objects.length; i++) {
var obj = data.objects[i];
var url = data.urls[i];
var obj_data = {};
for (var j=0; j<data.fields.length; j++) {
obj_data[data.fields[j]] = obj[j];
}
var id = obj_data.id;
var obj;
if (id !== undefined && existing_objs && existing_objs[id] !== undefined) {
obj = existing_objs[id];
for (var k in obj_data) { obj[k] = obj_data[k]; }
}
else if (!obj_f)
obj = new CuriousObject(obj_data);
else {
obj = obj_f(obj_data);
for (var k in obj_data) { obj[k] = obj_data[k]; }
}
obj.id = obj_data.id;
obj.__url = url;
obj.__model = model;
objects.push(obj);
}
return objects;
}
function parse_results_with_trees(relationships, objfs, results, existing_object_dicts) {
// get objects associated with each subquery. for each subquery, build a
// hash of ID to object. existing_object_dicts should be an array of dicts,
// each dict is a mapping of ID to existing objects. if existing objects
// are specified, will build relationships using existing objects.
var objects = [];
var trees = [];
for (var i=0; i<results.data.length; i++) {
var obj_f = null;
if (objfs)
obj_f = objfs[i];
var model = results.results[i].model;
var existing_objs = null;
if (existing_object_dicts !== undefined && existing_object_dicts !== null &&
existing_object_dicts[i] !== undefined && existing_object_dicts[i] !== null)
existing_objs = existing_object_dicts[i];
var result_objects = parse_objects(results.data[i], model, obj_f, existing_objs);
var d = {};
for (var j=0; j<result_objects.length; j++) { d[result_objects[j].id] = result_objects[j]; }
objects.push(d);
trees.push(null);
}
// for each subquery, add a relationship to results of next subquery, and
// then a reverse relationship
for (var i=1; i<results.results.length; i++) {
var rel = relationships[i];
var res_tups = results.results[i].objects;
var join_idx = results.results[i].join_index;
var join_src = objects[join_idx];
var join_obj = objects[i];
var rev = relationships[join_idx];
trees[i] = results.results[i].tree;
// add empty replationship
for (var k in join_src) { join_src[k][rel] = []; }
for (var k in join_obj) { join_obj[k][rev] = []; }
for (var j=0; j<res_tups.length; j++) {
var obj_src = res_tups[j];
var src = join_src[obj_src[1]];
if (obj_src[0]) {
var obj = join_obj[obj_src[0]];
if (src && obj) {
// forward relationship from query to next query
src[rel].push(obj);
// reverse relationship
obj[rev].push(src);
}
}
}
}
return {objects: objects, trees: trees};
}
function dict_to_array(d) {
var r = [];
for (var k in d) { r.push(d[k]); }
return r;
}
function array_to_dict(a) {
var d = {};
for (var i=0; i<a.length; i++) {
d[a[i].id] = a[i];
}
return d;
}
function id_list(objects) {
var ids = [];
for (var i=0; i<objects.length; i++) {
if (ids.indexOf(objects[i].id) < 0) {
ids.push(objects[i].id);
}
}
return ids;
}
function id_str(objects) {
var ids = id_list(objects);
if (ids.length === 0) { return null; }
return ids.join(',');
}
return {
parse_with_trees: parse_results_with_trees,
d2a: dict_to_array,
a2d: array_to_dict,
id_list: id_list,
id_str: id_str,
}
}());
function convert_results_to_output(relationships, objects) {
var i, j;
var output = {};
for (i = 0; i < objects.length; i++) {
if (output[relationships[i]]) {
j = 2;
while (output[relationships[i]+'_'+j]) { j++; }
output[relationships[i]+'_'+j] = CuriousObjects.d2a(objects[i]);
}
else
output[relationships[i]] = CuriousObjects.d2a(objects[i]);
}
return output;
}
function get_args(query_args, app_default_args) {
var k;
var args = {x: 0, fk: 0}; // default args
var immutable_args = {d: 1}; // these are always set, no matter what
if (app_default_args) {
for (k in app_default_args) {
if (app_default_args.hasOwnProperty(k)) {
args[k] = app_default_args[k];
}
}
}
if (query_args) {
for (k in query_args) {
if (query_args.hasOwnProperty(k)) {
args[k] = query_args[k];
}
}
}
for (k in immutable_args) {
args[k] = immutable_args[k];
}
return args;
}
function convert_array_array_to_dict_array(objects_array) {
var i;
var dict_array = [];
for (i=0; i<objects_array.length; i++) {
if (objects_array[i]) {
dict_array.push(CuriousObjects.a2d(objects_array[i]));
}
else {
dict_array.push(null);
}
}
return dict_array;
}
// Helper for making a Curious query and getting back parsed objects. Use with
// angular $http compatible HTTP request facilities (e.g. jQuery?)
var CuriousQ = function(curious_url, http, app_default_params, quiet) {
function get(q, relationships, objfs, params, existing_objects, objects_cb, trees_cb) {
var args;
var post_cb;
if (quiet === undefined || quiet !== true) {
console.warn(q);
}
if (existing_objects) {
existing_objects = convert_array_array_to_dict_array(existing_objects);
}
args = get_args(params, app_default_params);
args.q = q;
post_cb = function(resp) {
var objects;
var res;
res = CuriousObjects.parse_with_trees(relationships, objfs, resp.result, existing_objects);
objects = convert_results_to_output(relationships, res.objects);
objects_cb(objects);
if (trees_cb) { trees_cb(res.trees); }
};
return http.post(curious_url, args).success(post_cb);
}
return { get: get }
};
var ex = undefined;
if (typeof window !== 'undefined') { ex = window; }
else if (typeof exports !== 'undefined' && exports) { ex = exports; }
ex.CuriousObjects = CuriousObjects;
ex.CuriousQ = CuriousQ;
ex.CuriousQuery = CuriousQuery;
})();