This repository was archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-mapping-extensions.js
More file actions
638 lines (632 loc) · 33.6 KB
/
data-mapping-extensions.js
File metadata and controls
638 lines (632 loc) · 33.6 KB
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/**
* @ignore
*/
var _ = require('lodash'),
qry = require('most-query'),
Q = require('q');
var mappingExtensions = {
/**
* @param {DataAssociationMapping|*} mapping
* @returns {*}
*/
extend: function(mapping) {
var thisQueryable, childModel_, parentModel_;
return {
/**
* @param {DataQueryable} dataQueryable
*/
for: function(dataQueryable) {
thisQueryable = dataQueryable;
return this;
},
getChildModel: function() {
if (_.isNil(thisQueryable)) {
return;
}
if (_.isObject(childModel_)) {
return childModel_;
}
childModel_ = thisQueryable.model.context.model(mapping.childModel);
return childModel_;
},
getParentModel: function() {
if (_.isNil(thisQueryable)) {
return;
}
if (_.isObject(parentModel_)) {
return parentModel_;
}
parentModel_ = thisQueryable.model.context.model(mapping.parentModel);
return parentModel_;
},
/**
* @param {*} items
* @returns {Promise|*}
*/
getParents_v1: function(items) {
var thisArg = this;
var deferred = Q.defer();
process.nextTick(function() {
if (_.isNil(items)) {
return deferred.resolve();
}
var arr = _.isArray(items) ? items : [items];
if (arr.length == 0) {
return deferred.resolve();
}
if (_.isNil(thisQueryable)) {
return deferred.reject('The underlying data queryable cannot be empty at this context.');
}
if ((mapping.childModel !== thisQueryable.model.name) || (mapping.associationType!=='junction')) {
return deferred.resolve();
}
//get array of key values (for childs)
var values = arr.filter(function(x) {
return (typeof x[mapping.childField]!=='undefined')
&& (x[mapping.childField]!=null); })
.map(function(x) { return x[mapping.childField]
});
//query junction model
var HasParentJunction = require('./has-parent-junction').HasParentJunction;
var junction = new HasParentJunction(thisQueryable.model.convert({ }), mapping);
junction.getBaseModel().where('valueId').in(values).flatten().silent().all(function(err, junctions) {
if (err) { return deferred.reject(err); }
//get array of parent key values
values = _.intersection(junctions.map(function(x) { return x['parentId'] }));
//get parent model
var parentModel = thisArg.getParentModel();
//query parent with parent key values
parentModel.filter(mapping.options, function(err, q) {
if (err) {
return deferred.reject(err);
}
q.prepare();
//Important Backward compatibility issue (<1.8.0)
//Description: if $levels parameter is not defined then set the default value to 0.
if (typeof q.$levels === 'undefined') {
q.$levels = 0;
}
q.where(mapping.parentField).in(values);
//inherit silent mode
if (thisQueryable.$silent) { q.silent(); }
//and finally query parent
q.getItems().then(function(parents){
//if result contains only one item
if (arr.length == 1) {
arr[0][mapping.refersTo] = parents;
return deferred.resolve();
}
//otherwise loop result array
arr.forEach(function(x) {
//get child (key value)
var valueId = x[mapping.childField];
//get parent(s)
var p = junctions.filter(function(y) { return (y.valueId==valueId); }).map(function(r) { return r['parentId']; });
//filter data and set property value (a filtered array of parent objects)
x[mapping.refersTo] = parents.filter(function(z) { return p.indexOf(z[mapping.parentField])>=0; });
});
return deferred.resolve();
}).catch(function(err) {
return deferred.reject(err);
});
});
});
});
return deferred.promise;
},
/**
* @param {*} items
* @returns {Promise|*}
*/
getParents : function(items) {
var thisArg = this;
var deferred = Q.defer();
process.nextTick(function() {
if (_.isNil(items)) {
return deferred.resolve();
}
var arr = _.isArray(items) ? items : [items];
if (arr.length == 0) {
return deferred.resolve();
}
if (_.isNil(thisQueryable)) {
return deferred.reject('The underlying data queryable cannot be empty at this context.');
}
if ((mapping.childModel !== thisQueryable.model.name) || (mapping.associationType!=='junction')) {
return deferred.resolve();
}
var HasParentJunction = require('./has-parent-junction').HasParentJunction;
var junction = new HasParentJunction(thisQueryable.model.convert({ }), mapping);
return junction.migrate(function(err) {
if (err) { return deferred.reject(err); }
var parentModel = thisArg.getParentModel();
parentModel.filter(mapping.options, function(err, q) {
if (err) { return deferred.reject(err); }
//get junction sub-query
var junctionQuery = qry.query(junction.getBaseModel().name).select(["parentId", "valueId"])
.join(thisQueryable.query.as("j0"))
.with(qry.where(qry.entity(junction.getBaseModel().name).select("valueId"))
.equal(qry.entity("j0").select(mapping.childField)));
//append join statement with sub-query
q.query.join(junctionQuery.as("g0"))
.with(qry.where(qry.entity(parentModel.viewAdapter).select(mapping.parentField))
.equal(qry.entity("g0").select("parentId")));
if (!q.query.hasFields()) {
q.select();
}
//inherit silent mode
if (thisQueryable.$silent) { q.silent(); }
//append child key field
q.alsoSelect(qry.fields.select("valueId").from("g0").as("ref__"));
return q.getItems().then(function (parents) {
_.forEach(arr, function(x) {
x[mapping.refersTo] = _.filter(parents, function(y) {
if (y['ref__'] === x[mapping.childField]) {
delete y['ref__'];
return true;
}
return false;
});
});
return deferred.resolve();
}).catch(function (err) {
return deferred.reject(err);
});
});
});
});
return deferred.promise;
},
/**
* @param {*} items
* @returns {Promise|*}
*/
getChilds_v1: function(items) {
var thisArg = this;
var deferred = Q.defer();
process.nextTick(function() {
if (_.isNil(items)) {
return deferred.resolve();
}
var arr = _.isArray(items) ? items : [items];
if (arr.length == 0) {
return deferred.resolve();
}
if (_.isNil(thisQueryable)) {
return deferred.reject('The underlying data queryable cannot be empty at this context.');
}
if ((mapping.parentModel !== thisQueryable.model.name) || (mapping.associationType!=='junction')) {
return deferred.resolve();
}
var values = arr.filter(function(x) {
return (typeof x[mapping.parentField]!=='undefined') && (x[mapping.parentField]!=null);
}).map(function(x) {
return x[mapping.parentField];
});
if (_.isNil(mapping.childModel)) {
var DataObjectTag = require('./data-object-tag').DataObjectTag;
junction = new DataObjectTag(thisQueryable.model.convert({ }), mapping);
return junction.getBaseModel().where("object").in(values).flatten().silent().select("object", "value").all().then(function(items) {
arr.forEach(function(x) {
x[mapping.refersTo] = items.filter(function(y) {
return y["object"]===x[mapping.parentField];
}).map(function (y) {
return y.value;
});
});
return deferred.resolve();
}).catch(function (err) {
return deferred.reject(err);
});
}
//create a dummy object
var DataObjectJunction = require('./data-object-junction').DataObjectJunction;
var junction = new DataObjectJunction(thisQueryable.model.convert({ }), mapping);
//query junction model
return junction.getBaseModel().where('parentId').in(values).silent().flatten().getItems().then(function(junctions) {
//get array of child key values
var values = junctions.map(function(x) { return x['valueId'] });
//get child model
var childModel = thisArg.getChildModel();
childModel.filter(mapping.options, function(err, q) {
if (err) {
return deferred.reject(err);
}
q.prepare();
//Important Backward compatibility issue (<1.8.0)
//Description: if $levels parameter is not defined then set the default value to 0.
if (typeof q.$levels === 'undefined') {
q.$levels = 0;
}
//inherit silent mode
if (thisQueryable.$silent) { q.silent(); }
//append where statement for this operation
if (values.length==1) {
q.where(mapping.childField).equal(values[0]);
}
else {
q.where(mapping.childField).in(values);
}
//and finally query childs
q.getItems().then(function(childs) {
//if result contains only one item
if (arr.length == 1) {
arr[0][mapping.refersTo] = childs;
return deferred.resolve();
}
//otherwise loop result array
arr.forEach(function(x) {
//get parent (key value)
var parentId = x[mapping.parentField];
//get parent(s)
var p = junctions.filter(function(y) { return (y.parentId==parentId); }).map(function(r) { return r['valueId']; });
//filter data and set property value (a filtered array of parent objects)
x[mapping.refersTo] = childs.filter(function(z) { return p.indexOf(z[mapping.childField])>=0; });
});
return deferred.resolve();
}).catch(function(err) {
return deferred.reject(err);
});
});
}).catch(function (err) {
return deferred.reject(err);
});
});
return deferred.promise;
},
/**
* @param {*} items
* @returns {Promise|*}
*/
getChilds: function(items) {
var thisArg = this;
var deferred = Q.defer();
process.nextTick(function() {
if (_.isNil(items)) {
return deferred.resolve();
}
var arr = _.isArray(items) ? items : [items];
if (arr.length == 0) {
return deferred.resolve();
}
if (_.isNil(thisQueryable)) {
return deferred.reject('The underlying data queryable cannot be empty at this context.');
}
if ((mapping.parentModel !== thisQueryable.model.name) || (mapping.associationType!=='junction')) {
return deferred.resolve();
}
var DataObjectJunction = require('./data-object-junction').DataObjectJunction;
var junction = new DataObjectJunction(thisQueryable.model.convert({ }), mapping);
return junction.migrate(function(err) {
if (err) { return deferred.reject(err); }
var childModel = thisArg.getChildModel();
childModel.filter(mapping.options, function(err, q) {
if (err) { return deferred.reject(err); }
if (!q.query.hasFields()) {
q.select();
}
//get junction sub-query
var junctionQuery = qry.query(junction.getBaseModel().name).select(["parentId", "valueId"])
.join(thisQueryable.query.as("j0"))
.with(qry.where(qry.entity(junction.getBaseModel().name).select("parentId"))
.equal(qry.entity("j0").select(mapping.parentField)));
//append join statement with sub-query
q.query.join(junctionQuery.as("g0"))
.with(qry.where(qry.entity(childModel.viewAdapter).select(mapping.childField))
.equal(qry.entity("g0").select("valueId")));
//inherit silent mode
if (thisQueryable.$silent) { q.silent(); }
//append item reference
q.alsoSelect(qry.fields.select("parentId").from("g0").as("ref__"));
return q.getItems().then(function (childs) {
_.forEach(arr, function(x) {
x[mapping.refersTo] = _.filter(childs, function(y) {
if (y['ref__'] === x[mapping.parentField]) {
delete y['ref__'];
return true;
}
return false;
});
});
return deferred.resolve();
}).catch(function (err) {
return deferred.reject(err);
});
});
});
});
return deferred.promise;
},
/**
* @param {*} items
* @returns {Promise|*}
*/
getAssociatedParents: function(items) {
var thisArg = this;
var deferred = Q.defer();
process.nextTick(function() {
if (_.isNil(items)) {
return deferred.resolve();
}
var arr = _.isArray(items) ? items : [items];
if (arr.length === 0) {
return deferred.resolve();
}
if (_.isNil(thisQueryable)) {
return deferred.reject('The underlying data queryable cannot be empty at this context.');
}
if ((mapping.childModel !== thisQueryable.model.name) || (mapping.associationType!=='association')) {
return deferred.resolve();
}
thisArg.getParentModel().migrate(function(err) {
if (err) { return deferred.reject(err); }
thisArg.getParentModel().filter(mapping.options, function(err, q) {
if (err) { return deferred.reject(err); }
//Important Backward compatibility issue (<1.8.0)
//Description: if $levels parameter is not defined then set the default value to 0.
if (typeof q.$levels === 'undefined') {
q.$levels = 0;
}
if (typeof q.query.$select === 'undefined') {
q.select();
}
q.query
.distinct()
.join(thisQueryable.query.as('j0'))
.with(qry.where(qry.entity(thisArg.getParentModel().viewAdapter).select(mapping.parentField))
.equal(qry.entity("j0").select(mapping.childField)));
//inherit silent mode
if (thisQueryable.$silent) { q.silent(); }
q.silent().getAllItems().then(function(parents) {
var childField = thisQueryable.model.field(mapping.childField);
var keyField = childField.property || childField.name;
var iterator = function(x) {
var key = x[keyField];
x[keyField] = _.find(parents, function(x) {
return x[mapping.parentField] === key;
});
};
_.forEach(arr, iterator);
return deferred.resolve();
}).catch(function (err) {
return deferred.reject(err);
});
});
});
});
return deferred.promise;
},
/**
* @param {*} items
* @returns {Promise|*}
*/
getAssociatedParents_v1: function(items) {
var thisArg = this;
var deferred = Q.defer();
process.nextTick(function() {
if (_.isNil(items)) {
return deferred.resolve();
}
var arr = _.isArray(items) ? items : [items];
if (arr.length == 0) {
return deferred.resolve();
}
if (_.isNil(thisQueryable)) {
return deferred.reject('The underlying data queryable cannot be empty at this context.');
}
if ((mapping.childModel !== thisQueryable.model.name) || (mapping.associationType!=='association')) {
return deferred.resolve();
}
thisArg.getParentModel().migrate(function(err) {
if (err) { return deferred.reject(err); }
var childField = thisQueryable.model.field(mapping.childField);
var keyField = childField.property || childField.name;
if (_.isNil(childField)) {
return deferred.reject("The specified field cannot be found on child model");
}
var values = _.intersection(_.map(_.filter(arr, function(x) {
return x.hasOwnProperty(keyField);
}), function (x) { return x[keyField];}));
if (values.length==0) {
return deferred.resolve();
}
thisArg.getParentModel().filter(mapping.options, function(err, q) {
if (err) {
return deferred.reject(err);
}
q.prepare();
//Important Backward compatibility issue (<1.8.0)
//Description: if $levels parameter is not defined then set the default value to 0.
if (typeof q.$levels === 'undefined') {
q.$levels = 0;
}
//inherit silent mode
if (thisQueryable.$silent) { q.silent(); }
//append where statement for this operation
q.where(mapping.parentField).in(values);
//set silent (?)
q.silent().getAllItems().then(function(parents) {
var key=null,
selector = function(x) {
return x[mapping.parentField]==key;
},
iterator = function(x) {
key = x[keyField];
if (childField.property && childField.property!==childField.name) {
x[childField.property] = parents.filter(selector)[0];
delete x[childField.name];
}
else
x[childField.name] = parents.filter(selector)[0];
};
if (_.isArray(arr)) {
arr.forEach(iterator);
}
return deferred.resolve();
}).catch(function(err) {
return deferred.reject(err);
});
});
});
});
return deferred.promise;
},
/**
* @param {*} items
* @returns {Promise|*}
*/
getAssociatedChilds_v1: function(items) {
var thisArg = this;
var deferred = Q.defer();
process.nextTick(function() {
if (_.isNil(items)) {
return deferred.resolve();
}
var arr = _.isArray(items) ? items : [items];
if (arr.length == 0) {
return deferred.resolve();
}
if (_.isNil(thisQueryable)) {
return deferred.reject('The underlying data queryable cannot be empty at this context.');
}
if ((mapping.parentModel !== thisQueryable.model.name) || (mapping.associationType!=='association')) {
return deferred.resolve();
}
thisArg.getChildModel().migrate(function(err) {
if (err) { return deferred.reject(err); }
var parentField = thisQueryable.model.field(mapping.parentField);
if (_.isNil(parentField)) {
return deferred.reject("The specified field cannot be found on parent model");
}
var keyField = parentField.property || parentField.name;
var values = _.intersection(_.map(_.filter(arr, function(x) {
return x.hasOwnProperty(keyField);
}), function (x) { return x[keyField];}));
if (values.length==0) {
return deferred.resolve();
}
//search for view named summary
thisArg.getChildModel().filter(mapping.options, function(err, q) {
if (err) {
return deferred.reject(err);
}
var childField = thisArg.getChildModel().field(mapping.childField);
if (_.isNil(childField)) {
return deferred.reject("The specified field cannot be found on child model");
}
var foreignKeyField = childField.property || childField.name;
//Important Backward compatibility issue (<1.8.0)
//Description: if $levels parameter is not defined then set the default value to 0.
if (typeof q.$levels === 'undefined') {
q.$levels = 0;
}
q.prepare();
if (values.length==1) {
q.where(mapping.childField).equal(values[0]);
}
else {
q.where(mapping.childField).in(values);
}
//inherit silent mode
if (thisQueryable.$silent) { q.silent(); }
//final execute query
return q.getItems().then(function(childs) {
_.forEach(arr, function(x) {
x[mapping.refersTo] = _.filter(childs, function(y) {
if (!_.isNil(y[foreignKeyField]) && y[foreignKeyField].hasOwnProperty(keyField)) {
return y[foreignKeyField][keyField] === x[keyField];
}
return y[foreignKeyField] === x[keyField];
});
});
return deferred.resolve();
}).catch(function(err) {
return deferred.resolve(err);
});
});
});
});
return deferred.promise;
},
/**
* @param {*} items
* @returns {Promise|*}
*/
getAssociatedChilds: function(items) {
var thisArg = this;
var deferred = Q.defer();
process.nextTick(function() {
if (_.isNil(items)) {
return deferred.resolve();
}
var arr = _.isArray(items) ? items : [items];
if (arr.length == 0) {
return deferred.resolve();
}
if (_.isNil(thisQueryable)) {
return deferred.reject('The underlying data queryable cannot be empty at this context.');
}
if ((mapping.parentModel !== thisQueryable.model.name) || (mapping.associationType!=='association')) {
return deferred.resolve();
}
thisArg.getChildModel().migrate(function(err) {
if (err) { return deferred.reject(err); }
var parentField = thisArg.getParentModel().field(mapping.parentField);
if (_.isNil(parentField)) {
return deferred.reject("The specified field cannot be found on parent model");
}
var keyField = parentField.property || parentField.name;
var values = _.intersection(_.map(_.filter(arr, function(x) {
return x.hasOwnProperty(keyField);
}), function (x) { return x[keyField];}));
if (values.length==0) {
return deferred.resolve();
}
//search for view named summary
thisArg.getChildModel().filter(mapping.options, function(err, q) {
if (err) {
return deferred.reject(err);
}
var childField = thisArg.getChildModel().field(mapping.childField);
if (_.isNil(childField)) {
return deferred.reject("The specified field cannot be found on child model");
}
var foreignKeyField = childField.property || childField.name;
//Important Backward compatibility issue (<1.8.0)
//Description: if $levels parameter is not defined then set the default value to 0.
if (typeof q.$levels === 'undefined') {
q.$levels = 0;
}
if (!q.query.hasFields()) {
q.select();
}
//inherit silent mode
if (thisQueryable.$silent) { q.silent(); }
//join parents
q.query.join(thisQueryable.query.as("j0"))
.with(qry.where(qry.entity(thisArg.getChildModel().viewAdapter).select(mapping.childField))
.equal(qry.entity("j0").select(mapping.parentField)));
q.prepare();
//final execute query
return q.getItems().then(function(childs) {
_.forEach(arr, function(x) {
x[mapping.refersTo] = _.filter(childs, function(y) {
if (!_.isNil(y[foreignKeyField]) && y[foreignKeyField].hasOwnProperty(keyField)) {
return y[foreignKeyField][keyField] === x[keyField];
}
return y[foreignKeyField] === x[keyField];
});
});
return deferred.resolve();
}).catch(function(err) {
return deferred.resolve(err);
});
});
});
});
return deferred.promise;
}
};
}
};
module.exports = mappingExtensions;