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-listeners.js
More file actions
798 lines (777 loc) · 29.5 KB
/
data-listeners.js
File metadata and controls
798 lines (777 loc) · 29.5 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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/**
* MOST Web Framework
* A JavaScript Web Framework
* http://themost.io
* Created by Kyriakos Barbounakis<k.barbounakis@gmail.com> on 2015-02-13.
*
* Copyright (c) 2014, Kyriakos Barbounakis k.barbounakis@gmail.com
Anthi Oikonomou anthioikonomou@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of MOST Web Framework nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @ignore
*/
var async = require('async'),
sprintf = require('sprintf'),
_ = require('lodash'),
dataCommon = require('./data-common'),
moment = require("moment"),
util = require('util'),
types = require('./types');
/**
* @classdesc Represents an event listener for validating not nullable fields. This listener is automatically registered in all data models.
* @class
* @constructor
*/
function NotNullConstraintListener() {
//
}
/**
* Occurs before creating or updating a data object and validates not nullable fields.
* @param {DataEventArgs|*} e - An object that represents the event arguments passed to this operation.
* @param {Function} callback - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
*/
NotNullConstraintListener.prototype.beforeSave = function(e, callback) {
//find all attributes that have not null flag
var attrs = e.model.attributes.filter(
function(x) {
return !x.primary && !(typeof x.nullable === 'undefined' ? true: x.nullable);
});
if (attrs.length===0) {
callback(null);
return 0;
}
async.eachSeries(attrs, function(attr, cb)
{
var name = attr.property || attr.name, value = e.target[name];
if ((((value === null) || (value===undefined)) && (e.state===1))
|| ((value === null) && (typeof value!=='undefined') && (e.state === 2)))
{
var er = new types.NotNullException('A value is required.', null, e.model.name, attr.name);
if (process.env.NODE_ENV==='development') { dataCommon.log(er); }
return cb(er);
}
else
cb(null);
}, function(err) {
callback(err);
});
};
/**
* @class
* @constructor
* @classdesc Represents an event listener for validating data model's unique constraints. This listener is automatically registered in all data models.
*/
function UniqueContraintListener() {
//
}
/**
* Occurs before creating or updating a data object and validates the unique constraints of data model.
* @param {DataEventArgs|*} e - An object that represents the event arguments passed to this operation.
* @param {Function} callback - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
*/
UniqueContraintListener.prototype.beforeSave = function(e, callback) {
//there are no constraints
if (e.model.constraints===null)
{
//do nothing
callback(null);
return;
}
//get unique constraints
var constraints = e.model.constraints.filter(function(x) {
return (x.type=='unique');
});
if (constraints.length===0) {
//do nothing
callback(null);
return;
}
async.eachSeries(constraints, function(constraint, cb)
{
/**
* @type {DataQueryable}
*/
var q;
//build query
for (var i = 0; i < constraint.fields.length; i++) {
var attr = constraint.fields[i];
var value = e.target[attr];
if (typeof value === 'undefined') {
cb(null);
return;
}
//check field mapping
var mapping = e.model.inferMapping(attr);
if (typeof mapping !== 'undefined' && mapping !== null) {
if (typeof e.target[attr] === 'object') {
value=e.target[attr][mapping.parentField];
}
}
if (typeof value=== 'undefined')
value = null;
if (q) {
q.and(attr).equal(value);
}
else {
q = e.model.where(attr).equal(value);
}
}
if (typeof q === 'undefined')
cb(null);
else {
q.silent().select(e.model.primaryKey).first(function(err, result) {
if (err) {
cb(err);
return;
}
if (!result) {
//object does not exist
cb(null);
}
else {
var objectExists = true;
if (e.state===2) {
//validate object id (check if target object is the same with the returned object)
objectExists = (result[e.model.primaryKey]!== e.target[e.model.primaryKey]);
}
//if object already exists
if (objectExists) {
var er;
//so throw exception
if (constraint.description) {
er = new types.UniqueConstraintException(constraint.description, null, e.model.name);
}
else {
er = new types.UniqueConstraintException("Object already exists. A unique constraint violated.", null, e.model.name);
}
if (process.env.NODE_ENV==='development') { dataCommon.log(er); }
return cb(er);
}
else {
return cb();
}
}
});
}
}, function(err) {
callback(err);
});
};
/**
* @class
* @constructor
* @classdesc Represents an event listener which calculates field values. This listener is being registered for all data models.
<p>
A data field may have a calculation attribute.
An instance of <a href="FunctionContext.html">FunctionContext</a> class will calculate this value by evaluating the expression provided.
<pre class="prettyprint"><code>
{
"name": "modifiedBy",
"title": "Modified By",
"description": "Modified by user.",
"type": "User",
"calculation":"javascript:return this.user();"
}
</code></pre>
<p>In the previous example modifiedBy field has a calculation for setting the user which performs the update operation.</p>
<p><strong>Note:</strong>FunctionContext class may be extended in order to allow applications to perform value calculations.</p>
<pre class="prettyprint"><code>
FunctionContext.prototype.myColor = function() {
var deferred = Q.defer(),
self = this;
process.nextTick(function() {
return self.context.model("UserColor")
.where("user/name").equal(self.context.user.name)
.select("color")
.value().then(function(value) {
deferred.resolve(value);
}).catch(function(err) {
deferred.reject(err);
});
});
return deferred.promise;
}
</code></pre>
<pre class="prettyprint"><code>
{
"name": "color",
"title": "Color",
"type": "Text",
"calculation":"javascript:return this.myColor();"
}
</code></pre>
<p>In this example a custom method of FunctionContext class gets the user's favourite color.</p>
<p>This calculation may also be performed by setting the following promise expression:</p>
<pre class="prettyprint"><code>
{
"name": "color",
"title": "Color",
"type": "Text",
"calculation":"javascript:return this.context.model('UserColor').where('user/name').equal(this.context.user.name).select('color').value();"
}
</code></pre>
</p>
*/
function CalculatedValueListener() {
//
}
/**
* Occurs before creating or updating a data object and calculates field values with the defined calculation expression.
* @param {DataEventArgs} e - An object that represents the event arguments passed to this operation.
* @param {Function} callback - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
*/
CalculatedValueListener.prototype.beforeSave = function(e, callback) {
//get function context
var functions = require('./functions'),
functionContext = functions.createContext();
_.assign(functionContext, e);
functionContext.context = e.model.context;
//find all attributes that have a default value
var attrs = e.model.attributes.filter(function(x) { return (x.calculation!==undefined); });
async.eachSeries(attrs, function(attr, cb) {
var expr = attr.calculation;
//validate expression
if (typeof expr !== 'string') {
e.target[attr.name] = expr;
return cb();
}
//check javascript: keyword for code evaluation
if (expr.indexOf('javascript:')===0) {
//get expression
var fnstr = expr.substring('javascript:'.length);
//if expression starts with function add parenthesis (fo evaluation)
if (fnstr.indexOf('function')===0) {
fnstr = '('.concat(fnstr,')');
}
//if expression starts with return then normalize expression (surround with function() {} keyword)
else if (fnstr.indexOf('return')===0) {
fnstr = '(function() { '.concat(fnstr,'})');
}
var value = eval(fnstr);
//if value is function
if (typeof value === 'function') {
//then call function against the target object
var value1 = value.call(functionContext);
if (typeof value1 !== 'undefined' && value1 !==null && typeof value1.then === 'function') {
//we have a promise, so we need to wait for answer
value1.then(function(result) {
//otherwise set result
e.target[attr.name] = result;
return cb();
}).catch(function(err) {
cb(err);
});
}
else {
e.target[attr.name] = value1;
return cb();
}
}
else if (typeof value !== 'undefined' && value !==null && typeof value.then === 'function') {
//we have a promise, so we need to wait for answer
value.then(function(result) {
//otherwise set result
e.target[attr.name] = result;
return cb();
}).catch(function(err) {
cb(err);
});
}
else {
//otherwise get value
e.target[attr.name] = value;
return cb();
}
}
else if (expr.indexOf('fn:')===0) {
return cb(new Error ('fn: syntax is deprecated.'));
}
else {
functionContext.eval(expr, function(err, result) {
if (err) {
cb(err);
}
else {
e.target[attr.name] = result;
cb(null);
}
});
}
}, function(err) {
callback(err);
});
};
/**
* @classdesc Represents a data caching listener which is going to be used while executing queries against
* data models where data caching is enabled. This listener is registered by default.
<p>
Data caching may be disabled when <a href="DataModel.html">DataModel</a>.caching property is set to 'none'. This is the default behaviour of a data model.
</p>
<pre class="prettyprint"><code>
{
"name": "Order", ... , "caching":"none"
...
}
</code></pre>
<p>
Data caching may be used when <a href="DataModel.html">DataModel</a>.caching property is set to 'always'.
</p>
<pre class="prettyprint"><code>
{
"name": "OrderStatus", ... , "caching":"always"
...
}
</code></pre>
<p>
Data caching may be conditionally enabled when <a href="DataModel.html">DataModel</a>.caching property is set to 'conditional'.
</p>
<pre class="prettyprint"><code>
{
"name": "Product", ... , "caching":"conditional"
...
}
</code></pre>
<p>
In this case, data caching will be used when an instance of <a href="DataQueryable.html">DataQueryable</a> class requests data with cache equal to true:
</p>
<pre class="prettyprint"><code>
context.model('Product')
.where('category').is('Laptops')
.cache(true)
.orderBy('name')
.list().then(function(result) {
done(null, result);
}).catch(function(err) {
done(err);
});
</code></pre>
* @class
* @constructor
*/
function DataCachingListener() {
//
}
/**
* Occurs before executing an query expression, validates data caching configuration and gets cached data.
* @param {DataEventArgs|*} event - An object that represents the event arguments passed to this operation.
* @param {Function} callback - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
*/
DataCachingListener.prototype.beforeExecute = function(event, callback) {
try {
var cache = require('./data-cache');
if (_.isNil(event)) {
return callback();
}
//validate caching
var caching = (event.model.caching==='always' || event.model.caching==='conditional');
if (!caching) { return callback(); }
//validate conditional caching
if (event.model.caching==='conditional') {
if (event.emitter && typeof event.emitter.data == 'function') {
if (!event.emitter.data('cache')) {
return callback();
}
}
}
if (event.query && event.query.$select) {
//create hash
var hash;
if (event.emitter && typeof event.emitter.toMD5 === 'function') {
//get hash from emitter (DataQueryable)
hash = event.emitter.toMD5();
}
else {
//else calculate hash
hash = dataCommon.md5({ query: event.query });
}
//format cache key
var key = '/' + event.model.name + '/?query=' + hash;
//calculate execution time (debug)
var logTime = new Date().getTime();
//query cache
cache.current.get(key, function(err, result) {
if (err) {
dataCommon.log('DataCacheListener: An error occured while trying to get cached data.');
dataCommon.log(err);
}
if (typeof result !== 'undefined') {
//delete expandables
if (event.emitter) {
delete event.emitter.$expand;
}
//set cached flag
event['cached'] = true;
//set execution default
event['result'] = result;
//log execution time (debug)
try {
if (process.env.NODE_ENV==='development') {
dataCommon.log(sprintf.sprintf('Cache (Execution Time:%sms):%s', (new Date()).getTime()-logTime, key));
}
}
catch(err) { }
//exit
return callback();
}
else {
//do nothing and exit
return callback();
}
});
}
else {
return callback();
}
}
catch (err) {
return callback(err);
}
};
/**
* Occurs before executing an query expression, validates data caching configuration and stores data to cache.
* @param {DataEventArgs|*} e - An object that represents the event arguments passed to this operation.
* @param {Function} callback - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
*/
DataCachingListener.prototype.afterExecute = function(event, callback) {
try {
var cache = require('./data-cache');
//validate caching
var caching = (event.model.caching==='always' || event.model.caching==='conditional');
if (!caching) { return callback(); }
//validate conditional caching
if (event.model.caching==='conditional') {
if (event.emitter && typeof event.emitter.data == 'function') {
if (!event.emitter.data('cache')) {
return callback();
}
}
}
if (event.query && event.query.$select) {
if (typeof event.result !== 'undefined' && !event.cached) {
//create hash
var hash;
if (event.emitter && typeof event.emitter.toMD5 === 'function') {
//get hash from emitter (DataQueryable)
hash = event.emitter.toMD5();
}
else {
//else calculate hash
hash = dataCommon.md5({ query: event.query });
}
var key = '/' + event.model.name + '/?query=' + hash;
if (process.env.NODE_ENV==='development') {
dataCommon.debug('DataCacheListener: Setting data to cache [' + key + ']');
}
cache.current.add(key, event.result);
return callback();
}
}
return callback();
}
catch(err) {
return callback(err);
}
};
/**
* @class
* @constructor
* @classdesc Represents an event listener for calculating default values.
* DefaultValueListener is one of the default listeners which are being registered for all data models.
<p>
A data field may have a default value attribute.
An instance of <a href="FunctionContext.html">FunctionContext</a> class will calculate this value by evaluating the expression provided.
The default value listener will process all fields of an inserted data object which have a default value expression and does not have a defined value.
<pre class="prettyprint"><code>
{
"name": "createdBy",
"title": "Created By",
"type": "User",
"value":"javascript:return this.user();",
"readonly":true
}
</code></pre>
<p></p>
<p><strong>Note:</strong> FunctionContext class may be extended in order to allow applications to perform value calculations.</p>
</p>
*/
function DefaultValueListener() {
//
}
/**
* Occurs before creating or updating a data object and calculates default values with the defined value expression.
* @param {DataEventArgs|*} e - An object that represents the event arguments passed to this operation.
* @param {Function} callback - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
*/
DefaultValueListener.prototype.beforeSave = function(e, callback) {
var state = e.state!==undefined ? e.state : 0;
if (state!=1)
{
callback(null);
}
else {
//get function context
var functions = require('./functions'), functionContext = functions.createContext();
_.assign(functionContext, e);
//find all attributes that have a default value
var attrs = e.model.attributes.filter(function(x) { return (x.value!==undefined); });
async.eachSeries(attrs, function(attr, cb) {
var expr = attr.value;
//if attribute is already defined
if (typeof e.target[attr.name] !== 'undefined') {
//do nothing
cb(null);
return;
}
//validate expression
if (typeof expr !== 'string') {
e.target[attr.name] = expr;
return cb();
}
//check javascript: keyword for code evaluation
if (expr.indexOf('javascript:')==0) {
//get expression
var fnstr = expr.substring('javascript:'.length);
//if expression starts with function add parenthesis (fo evaluation)
if (fnstr.indexOf('function')==0) {
fnstr = '('.concat(fnstr,')');
}
//if expression starts with return then normalize expression (surround with function() {} keyword)
else if (fnstr.indexOf('return')==0) {
fnstr = '(function() { '.concat(fnstr,'})');
}
var value = eval(fnstr);
//if value is function
if (typeof value === 'function') {
//then call function against the target object
var value1 = value.call(functionContext);
if (typeof value1 !== 'undefined' && value1 !=null && typeof value1.then === 'function') {
//we have a promise, so we need to wait for answer
value1.then(function(result) {
//otherwise set result
e.target[attr.name] = result;
return cb();
}).catch(function(err) {
cb(err);
});
}
else {
e.target[attr.name] = value1;
return cb();
}
}
else if (typeof value !== 'undefined' && value !=null && typeof value.then === 'function') {
//we have a promise, so we need to wait for answer
value.then(function(result) {
//otherwise set result
e.target[attr.name] = result;
return cb();
}).catch(function(err) {
cb(err);
});
}
else {
//otherwise get value
e.target[attr.name] = value;
return cb();
}
}
else if (expr.indexOf('fn:')==0) {
return cb(new Error ('fn: syntax is deprecated.'));
}
else {
functionContext.eval(expr, function(err, result) {
if (err) {
cb(err);
}
else {
e.target[attr.name] = result;
cb(null);
}
});
}
}, function(err) {
callback(err);
});
}
};
/**
* @class
* @constructor
*/
function DataModelCreateViewListener() {
//
}
/**
* Occurs after upgrading a data model.
* @param {DataEventArgs} event - An object that represents the event arguments passed to this operation.
* @param {Function} callback - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
*/
DataModelCreateViewListener.prototype.afterUpgrade = function(event, callback) {
var self = event.model,
qry = require("most-query"),
db = self.context.db;
var view = self.viewAdapter, adapter = self.sourceAdapter;
//if data model is a sealed model do nothing anb exit
if (self.sealed) { return callback(); }
//if view adapter is the same with source adapter do nothing and exit
if (view===adapter) { return callback(); }
var baseModel = self.base();
//get array of fields
var fields = self.attributes.filter(function(x) {
return (self.name== x.model) && (!x.many);
}).map(function(x) {
return qry.fields.select(x.name).from(adapter);
});
/**
* @type {QueryExpression}
*/
var q = qry.query(adapter).select(fields);
//get base adapter
var baseAdapter = (baseModel!=null) ? baseModel.name.concat('Data') : null, baseFields = [];
//enumerate columns of base model (if any)
if (dataCommon.isDefined(baseModel)) {
baseModel.attributes.forEach(function(x) {
//get all fields (except primary and one-to-many relations)
if ((!x.primary) && (!x.many))
baseFields.push(qry.fields.select(x.name).from(baseAdapter))
});
}
if (baseFields.length>0)
{
var from = qry.createField(adapter, self.key().name),
to = qry.createField(baseAdapter, self.base().key().name);
q.$expand = { $entity: { },$with:[] };
q.$expand.$entity[baseAdapter]=baseFields;
q.$expand.$with.push(from);
q.$expand.$with.push(to);
}
//execute query
return db.createView(view, q, function(err) {
callback(err);
});
};
/**
* @class
* @constructor
*/
function DataModelSeedListener() {
//
}
/**
* Occurs after upgrading a data model.
* @param {DataEventArgs} event - An object that represents the event arguments passed to this operation.
* @param {Function} callback - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
*/
DataModelSeedListener.prototype.afterUpgrade = function(event, callback) {
var self = event.model;
try {
/**
* Gets items to be seeded
* @type {Array}
*/
var items = self['seed'];
//if model has an array of items to be seeded
if (_.isArray(items)) {
if (items.length==0) {
//if seed array is empty exit
return callback();
}
//try to insert items if model does not have any record
self.asQueryable().silent().flatten().count(function(err, count) {
if (err) {
callback(err); return;
}
//if model has no data
if (count==0) {
//set items state to new
items.forEach(function(x) { x.$state=1; });
self.silent().save(items, callback);
}
else {
//model was already seeded
return callback();
}
});
}
else {
//do nothing and exit
return callback();
}
}
catch (e) {
callback(e);
}
};
/**
* @class
* @constructor
*/
function DataModelSubTypesListener() {
//
}
/**
* Occurs after upgrading a data model.
* @param {DataEventArgs} event - An object that represents the event arguments passed to this operation.
* @param {Function} callback - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
*/
DataModelSubTypesListener.prototype.afterUpgrade = function(event, callback) {
var self = event.model, context = event.model.context;
try {
self.getSubTypes().then(function(result) {
if (result.length==0) { return callback(); }
//enumerate sub types
async.eachSeries(result, function(name, cb) {
//get model
var model = context.model(name);
if (_.isNil(model)) { return cb(); }
//if model is sealed do nothing
if (model.sealed) { return cb(); }
//create event arguments
var ev = { model:model };
//execute create view listener
DataModelCreateViewListener.prototype.afterUpgrade(ev, cb);
}, function(err) {
return callback(err);
});
}).catch(function(err) {
return callback(err);
});
}
catch (e) {
callback(e);
}
};
if (typeof exports !== 'undefined')
{
module.exports = {
NotNullConstraintListener:NotNullConstraintListener,
UniqueContraintListener:UniqueContraintListener,
CalculatedValueListener:CalculatedValueListener,
DataCachingListener:DataCachingListener,
DefaultValueListener:DefaultValueListener,
DataModelCreateViewListener:DataModelCreateViewListener,
DataModelSeedListener:DataModelSeedListener,
DataModelSubTypesListener:DataModelSubTypesListener
};
}