This repository has been 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
/
Copy pathtypes.js
1281 lines (1214 loc) · 41.4 KB
/
types.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
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* MOST Web Framework
* A JavaScript Web Framework
* http://themost.io
* Created by Kyriakos Barbounakis<k.barbounakis@gmail.com> on 2014-01-25.
*
* 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.
*/
var events = require('events');
var sprintf = require('sprintf').sprintf;
var _ = require('lodash');
var util = require('util');
var async = require('async');
var types = { };
/**
* @classdesc Represents an abstract data connector to a database
* <p>
There are several data adapters for connections to common database engines:
</p>
<ul>
<li>MOST Web Framework MySQL Adapter for connecting with MySQL Database Server
<p>Install the data adapter:<p>
<pre class="prettyprint"><code>npm install most-data-mysql</code></pre>
<p>Append the adapter type in application configuration (app.json#adapterTypes):<p>
<pre class="prettyprint"><code>
...
"adapterTypes": [
...
{ "name":"MySQL Data Adapter", "invariantName": "mysql", "type":"most-data-mysql" }
...
]
</code></pre>
<p>Register an adapter in application configuration (app.json#adapters):<p>
<pre class="prettyprint"><code>
adapters: [
...
{ "name":"development", "invariantName":"mysql", "default":true,
"options": {
"host":"localhost",
"port":3306,
"user":"user",
"password":"password",
"database":"test"
}
}
...
]
</code></pre>
</li>
<li>MOST Web Framework MSSQL Adapter for connecting with Microsoft SQL Database Server
<p>Install the data adapter:<p>
<pre class="prettyprint"><code>npm install most-data-mssql</code></pre>
<p>Append the adapter type in application configuration (app.json#adapterTypes):<p>
<pre class="prettyprint"><code>
...
"adapterTypes": [
...
{ "name":"MSSQL Data Adapter", "invariantName": "mssql", "type":"most-data-mssql" }
...
]
</code></pre>
<p>Register an adapter in application configuration (app.json#adapters):<p>
<pre class="prettyprint"><code>
adapters: [
...
{ "name":"development", "invariantName":"mssql", "default":true,
"options": {
"server":"localhost",
"user":"user",
"password":"password",
"database":"test"
}
}
...
]
</code></pre>
</li>
<li>MOST Web Framework PostgreSQL Adapter for connecting with PostgreSQL Database Server
<p>Install the data adapter:<p>
<pre class="prettyprint"><code>npm install most-data-pg</code></pre>
<p>Append the adapter type in application configuration (app.json#adapterTypes):<p>
<pre class="prettyprint"><code>
...
"adapterTypes": [
...
{ "name":"PostgreSQL Data Adapter", "invariantName": "postgres", "type":"most-data-pg" }
...
]
</code></pre>
<p>Register an adapter in application configuration (app.json#adapters):<p>
<pre class="prettyprint"><code>
adapters: [
...
{ "name":"development", "invariantName":"postgres", "default":true,
"options": {
"host":"localhost",
"post":5432,
"user":"user",
"password":"password",
"database":"db"
}
}
...
]
</code></pre>
</li>
<li>MOST Web Framework Oracle Adapter for connecting with Oracle Database Server
<p>Install the data adapter:<p>
<pre class="prettyprint"><code>npm install most-data-oracle</code></pre>
<p>Append the adapter type in application configuration (app.json#adapterTypes):<p>
<pre class="prettyprint"><code>
...
"adapterTypes": [
...
{ "name":"Oracle Data Adapter", "invariantName": "oracle", "type":"most-data-oracle" }
...
]
</code></pre>
<p>Register an adapter in application configuration (app.json#adapters):<p>
<pre class="prettyprint"><code>
adapters: [
...
{ "name":"development", "invariantName":"oracle", "default":true,
"options": {
"host":"localhost",
"port":1521,
"user":"user",
"password":"password",
"service":"orcl",
"schema":"PUBLIC"
}
}
...
]
</code></pre>
</li>
<li>MOST Web Framework SQLite Adapter for connecting with Sqlite Databases
<p>Install the data adapter:<p>
<pre class="prettyprint"><code>npm install most-data-sqlite</code></pre>
<p>Append the adapter type in application configuration (app.json#adapterTypes):<p>
<pre class="prettyprint"><code>
...
"adapterTypes": [
...
{ "name":"SQLite Data Adapter", "invariantName": "sqlite", "type":"most-data-sqlite" }
...
]
</code></pre>
<p>Register an adapter in application configuration (app.json#adapters):<p>
<pre class="prettyprint"><code>
adapters: [
...
{ "name":"development", "invariantName":"sqlite", "default":true,
"options": {
database:"db/local.db"
}
}
...
]
</code></pre>
</li>
<li>MOST Web Framework Data Pool Adapter for connection pooling
<p>Install the data adapter:<p>
<pre class="prettyprint"><code>npm install most-data-pool</code></pre>
<p>Append the adapter type in application configuration (app.json#adapterTypes):<p>
<pre class="prettyprint"><code>
...
"adapterTypes": [
...
{ "name":"Pool Data Adapter", "invariantName": "pool", "type":"most-data-pool" }
{ "name":"...", "invariantName": "...", "type":"..." }
...
]
</code></pre>
<p>Register an adapter in application configuration (app.json#adapters):<p>
<pre class="prettyprint"><code>
adapters: [
{ "name":"development", "invariantName":"...", "default":false,
"options": {
"server":"localhost",
"user":"user",
"password":"password",
"database":"test"
}
},
{ "name":"development_with_pool", "invariantName":"pool", "default":true,
"options": {
"adapter":"development"
}
}
...
]
</code></pre>
</li>
</ul>
* @class
* @constructor
* @param {*} options - The database connection options
* @abstract
* @property {*} rawConnection - Gets or sets the native database connection
* @property {*} options - Gets or sets the database connection options
*/
function DataAdapter(options) {
this.rawConnection=null;
this.options = options;
}
/**
* Opens the underlying database connection
* @param {Function} callback - A callback function where the first argument will contain the Error object if an error occured, or null otherwise.
*/
DataAdapter.prototype.open = function(callback) {
//
};
/**
* Closes the underlying database connection
* @param {Function=} callback - A callback function where the first argument will contain the Error object if an error occured, or null otherwise.
*/
DataAdapter.prototype.close = function(callback) {
//
};
/**
* Executes the given query against the underlying database.
* @param {string|*} query - A string or a query expression to execute.
* @param {*} values - An object which represents the named parameters that are going to used during query parsing
* @param {Function} callback - A callback function where the first argument will contain the Error object if an error occured, or null otherwise. The second argument will contain the result.
*/
DataAdapter.prototype.execute = function(query, values, callback) {
//
};
/**
* Executes a batch query expression and returns the result.
* @param {DataModelBatch} batch - The batch query expression to execute
* @param {Function=} callback - A callback function where the first argument will contain the Error object if an error occured, or null otherwise. The second argument will contain the result.
* @deprecated This method is deprecated.
*/
DataAdapter.prototype.executeBatch = function(batch, callback) {
//
};
/**
* Produces a new identity value for the given entity and attribute.
* @param {string} entity - A string that represents the target entity name
* @param {string} attribute - A string that represents the target attribute name
* @param {Function=} callback - A callback function where the first argument will contain the Error object if an error occured, or null otherwise. The second argument will contain the result.
*/
DataAdapter.prototype.selectIdentity = function(entity, attribute , callback) {
//
};
/**
* Begins a transactional operation and executes the given function
* @param {Function} fn - The function to execute
* @param {Function=} callback - A callback function where the first argument will contain the Error object if an error occured, or null otherwise. The second argument will contain the result.
*/
DataAdapter.prototype.executeInTransaction = function(fn, callback) {
//
};
/**
* A helper method for creating a database view if the current data adapter supports views
* @param {string} name - A string that represents the name of the view to be created
* @param {QueryExpression|*} query - A query expression that represents the database view
* @param {Function=} callback - A callback function where the first argument will contain the Error object if an error occured, or null otherwise.
*/
DataAdapter.prototype.createView = function(name, query, callback) {
//
};
/**
* @classdesc EventEmitter2 class is an extension of node.js EventEmitter class where listeners are excuting in series.
* @class
* @augments EventEmitter
* @constructor
*/
function EventEmitter2() {
//
}
util.inherits(EventEmitter2, events.EventEmitter);
/**
* Raises the specified event and executes event listeners in series.
* @param {String} event - The event that is going to be raised.
* @param {*} args - An object that contains the event arguments.
* @param {Function} callback - A callback function to be invoked after the execution.
*/
EventEmitter2.prototype.emit = function(event, args, callback)
{
var self = this;
////example: call super class function
//EventEmitter2.super_.emit.call(this);
//ensure callback
callback = callback || function() {};
//get listeners
if (typeof this.listeners !== 'function') {
console.log('undefined listeners');
}
var listeners = this.listeners(event);
//validate listeners
if (listeners.length===0) {
//exit emitter
callback.call(self, null);
return;
}
/*
An EventEmitter2 listener must be a function with args and a callback e.g.
function(e, cb) {
//do some code
...
//finalize event
cb(null);
//or
cb(err)
}
*/
//get event arguments
var e = args;
//apply each series
async.applyEachSeries(listeners, e, function(err) {
callback.call(self, err);
});
};
EventEmitter2.prototype.once = function(type, listener) {
var self = this;
if (typeof listener !== 'function')
throw TypeError('listener must be a function');
var fired = false;
function g() {
self.removeListener(type, g);
if (!fired) {
fired = true;
listener.apply(this, arguments);
}
}
g.listener = listener;
this.on(type, g);
return this;
};
/**
* @classdesc Represents the event arguments of a data model listener.
* @class
* @constructor
* @property {DataModel|*} model - Represents the underlying model.
* @property {DataObject|*} target - Represents the underlying data object.
* @property {Number|*} state - Represents the operation state (Update, Insert, Delete).
* @property {DataQueryable|*} emitter - Represents the event emitter, normally a DataQueryable object instance.
* @property {*} query - Represents the underlying query expression. This property may be null.
*/
function DataEventArgs() {
//
}
/**
* @classdesc Represents the main data context.
* @class
* @augments EventEmitter2
* @constructor
*/
function DataContext() {
/**
* Gets the current database adapter
* @type {DataAdapter}
*/
this.db = undefined;
Object.defineProperty(this, 'db', {
get : function() {
return null;
},
configurable : true,
enumerable:false });
}
/**
* Gets a data model based on the given data context
* @param name {string} A string that represents the model to be loaded.
* @returns {DataModel}
*/
// eslint-disable-next-line no-unused-vars
DataContext.prototype.model = function(name) {
return null;
};
/**
* Gets an instance of DataConfiguration class which is associated with this data context
* @returns {DataConfiguration}
*/
DataContext.prototype.getConfiguration = function() {
return null;
};
/**
* @param cb {Function}
*/
DataContext.prototype.finalize = function(cb) {
//
};
//set EventEmitter2 inheritance
util.inherits(DataContext, EventEmitter2);
/**
* @classdesc Represents a data model's listener
* @class
* @constructor
* @abstract
*/
function DataEventListener() {
//
}
/**
* Occurs before executing a data operation. The event arguments contain the query that is going to be executed.
* @param {DataEventArgs} e - An object that represents the event arguments passed to this operation.
* @param {Function} cb - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
*/
DataEventListener.prototype.beforeExecute = function(e, cb) {
return this;
};
/**
* Occurs after executing a data operation. The event arguments contain the executed query.
* @param {DataEventArgs} e - An object that represents the event arguments passed to this operation.
* @param {Function} cb - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
*/
DataEventListener.prototype.afterExecute = function(e, cb) {
return this;
};
/**
* Occurs before creating or updating a data object.
* @param {DataEventArgs} e - An object that represents the event arguments passed to this operation.
* @param {Function} cb - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
*/
DataEventListener.prototype.beforeSave = function(e, cb) {
return this;
};
/**
* Occurs after creating or updating a data object.
* @param {DataEventArgs} e - An object that represents the event arguments passed to this operation.
* @param {Function} cb - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
*/
DataEventListener.prototype.afterSave = function(e, cb) {
return this;
};
/**
* Occurs before removing a data object.
* @param {DataEventArgs} e - An object that represents the event arguments passed to this operation.
* @param {Function} cb - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
* @returns {DataEventListener}
*/
DataEventListener.prototype.beforeRemove = function(e, cb) {
return this;
};
/**
* Occurs after removing a data object.
* @param {DataEventArgs} e - An object that represents the event arguments passed to this operation.
* @param {Function} cb - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
*/
DataEventListener.prototype.afterRemove = function(e, cb) {
return this;
};
/**
* Occurs after upgrading a data model.
* @param {DataEventArgs} e - An object that represents the event arguments passed to this operation.
* @param {Function} cb - A callback function that should be called at the end of this operation. The first argument may be an error if any occured.
*/
DataEventListener.prototype.afterUpgrade = function(e, cb) {
return this;
};
var DateTimeRegex = /^(\d{4})(?:-?W(\d+)(?:-?(\d+)D?)?|(?:-(\d+))?-(\d+))(?:[T ](\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?)?(?:Z(-?\d*))?$/g;
var BooleanTrueRegex = /^true$/ig;
var BooleanFalseRegex = /^false$/ig;
var NullRegex = /^null$/ig;
var UndefinedRegex = /^undefined$/ig;
var IntegerRegex =/^[-+]?\d+$/g;
var FloatRegex =/^[+-]?\d+(\.\d+)?$/g;
/*
* EXCEPTIONS
*/
/**
* @classdesc Extends Error object for throwing exceptions on data operations
* @class
* @param {string=} code - A string that represents an error code
* @param {string=} message - The error message
* @param {string=} innerMessage - The error inner message
* @param {string=} model - The target model
* @param {string=} field - The target field
* @param {*} additionalData - Additional data associated with this error
* @constructor
* @property {string} code - A string that represents an error code e.g. EDATA
* @property {string} message - The error message.
* @property {string} innerMessage - The error inner message.
* @property {number} status - A number that represents an error status. This error status may be used for throwing the approriate HTTP error.
* @property {*} additionalData - Additional data associated with this error
* @augments Error
*/
function DataException(code, message, innerMessage, model, field, additionalData) {
this.code = code || 'EDATA';
if (model)
this.model = model;
if (field)
this.field = field;
this.message = message || 'A general data error occured.';
if (innerMessage)
this.innerMessage = innerMessage;
this.additionalData = additionalData;
}
util.inherits(DataException, Error);
/**
* @classdesc Extends Error object for throwing not null exceptions.
* @class
* @param {string=} message - The error message
* @param {string=} innerMessage - The error inner message
* @constructor
* @property {string} code - A string that represents an error code. The default error code is ENULL.
* @property {string} message - The error message.
* @property {string} innerMessage - The error inner message.
* @property {number} status - A number that represents an error status. This error status may be used for throwing the approriate HTTP error. The default status is 409 (Conflict)
* @property {string} model - The target model name
* @property {string} field - The target field name
* @augments Error
*/
function NotNullException(message, innerMessage, model, field) {
NotNullException.super_.call(this, 'ENULL', message || 'A value is required', innerMessage, model, field);
this.status = 409;
}
util.inherits(NotNullException, DataException);
/**
* @classdesc Extends Error object for throwing not found exceptions.
* @class
* @param {string=} message - The error message
* @param {string=} innerMessage - The error inner message
* @constructor
* @property {string} code - A string that represents an error code. The default error code is EFOUND.
* @property {string} message - The error message.
* @property {string} innerMessage - The error inner message.
* @property {number} status - A number that represents an error status. This error status may be used for throwing the approriate HTTP error. The default status is 404 (Conflict)
* @property {string} model - The target model name
* @augments Error
*/
function DataNotFoundException(message, innerMessage, model) {
DataNotFoundException.super_.call(this, 'EFOUND', message || 'The requested data was not found.', innerMessage, model);
this.status = 404;
}
util.inherits(DataNotFoundException, DataException);
/**
* @classdesc Extends Error object for throwing unique constraint exceptions.
* @class
* @param {string=} message - The error message
* @param {string=} innerMessage - The error inner message
* @constructor
* @property {string} code - A string that represents an error code. The default error code is ENULL.
* @property {string} message - The error message.
* @property {string} innerMessage - The error inner message.
* @property {number} status - A number that represents an error status. This error status may be used for throwing the approriate HTTP error. The default status is 409 (Conflict)
* @property {string} model - The target model name
* @property {string} constraint - The target constraint name
* @augments Error
*/
function UniqueConstraintException(message, innerMessage, model, constraint) {
UniqueConstraintException.super_.call(this, 'EUNQ', message || 'A unique constraint violated', innerMessage, model);
if (constraint)
this.constraint = constraint;
this.status = 409;
}
util.inherits(UniqueConstraintException, DataException);
/**
* @classdesc Represents an access denied data exception.
* @class
*
* @param {string=} message - The error message
* @param {string=} innerMessage - The error inner message
* @property {string} code - A string that represents an error code. The error code is EACCESS.
* @property {number} status - A number that represents an error status. The error status is 401.
* @property {string} message - The error message.
* @property {string} innerMessage - The error inner message.
* @augments DataException
* @constructor
*/
function AccessDeniedException(message, innerMessage) {
AccessDeniedException.super_.call(this, 'EACCESS', ('Access Denied' || message) , innerMessage);
this.status = 401;
}
util.inherits(AccessDeniedException, DataException);
/**
* @ignore
* @class
* @param name
* @constructor
*/
function DataQueryableField(name) {
if (typeof name !== 'string') {
throw new Error('Invalid argument type. Expected string.')
}
this.name = name;
}
/**
* @returns {DataQueryableField}
*/
DataQueryableField.prototype.as = function(s) {
if (_.isNil(s)) {
delete this.$as;
return this;
}
/**
* @private
* @type {string}
*/
this.$as = s;
return this;
};
/**
* Returns the alias expression, if any.
* @returns {string}
* @private
*/
DataQueryableField.prototype._as = function() {
return (typeof this.$as !== 'undefined' && this.$as != null) ? ' as ' + this.$as : '';
};
DataQueryableField.prototype.toString = function() {
return this.name + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.max = function() {
return sprintf('max(%s)', this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.min = function() {
return sprintf('min(%s)', this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.count = function() {
return sprintf('count(%s)', this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.average = function() {
return sprintf('avg(%s)', this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.length = function() {
return sprintf('length(%s)', this.name) + this._as();
};
/**
* @param {number} pos
* @param {number} length
* @returns {string}
*/
DataQueryableField.prototype.substr = function(pos, length) {
return sprintf('substring(%s,%s,%s)',this.name, pos, length) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.floor = function() {
return sprintf('floor(%s)',this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.round = function() {
return sprintf('round(%s)',this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.getYear = function() {
return sprintf('year(%s)',this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.getDay = function() {
return sprintf('day(%s)',this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.getMonth = function() {
return sprintf('month(%s)',this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.getMinutes = function() {
return sprintf('minute(%s)',this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.getHours = function() {
return sprintf('hour(%s)',this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.getSeconds = function() {
return sprintf('second(%s)',this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.getDate = function() {
return sprintf('date(%s)',this.name) + this._as();
};
///**
// * @returns {string}
// */
//DataQueryableField.prototype.ceil = function() {
// return util('ceil(%s)',this.name);
//};
/**
* @returns {string}
*/
DataQueryableField.prototype.toLocaleLowerCase = function() {
return sprintf('tolower(%s)',this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.toLowerCase = function() {
return sprintf('tolower(%s)',this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.toLocaleUpperCase = function() {
return sprintf('toupper(%s)',this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.toUpperCase = function() {
return sprintf('toupper(%s)',this.name) + this._as();
};
/**
* @returns {string}
*/
DataQueryableField.prototype.trim = function() {
return sprintf('trim(%s)',this.name) + this._as();
};
/** native extensions **/
if (typeof String.prototype.fieldOf === 'undefined')
{
/**
* @returns {DataQueryableField}
* @private
*/
var fnFieldOf = function() {
if (this == null) {
throw new TypeError('String.prototype.fieldOf called on null or undefined');
}
return new DataQueryableField(this.toString());
};
if (!String.prototype.fieldOf) { String.prototype.fieldOf = fnFieldOf; }
}
/**
* Represents a model migration scheme against data adapters
* @class
* @constructor
* @ignore
*/
function DataModelMigration() {
/**
* Gets an array that contains the definition of fields that are going to be added
* @type {Array}
*/
this.add = [];
/**
* Gets an array that contains a collection of constraints which are going to be added
* @type {Array}
*/
this.constraints = [];
/**
* Gets an array that contains a collection of indexes which are going to be added or updated
* @type {Array}
*/
this.indexes = [];
/**
* Gets an array that contains the definition of fields that are going to be deleted
* @type {Array}
*/
this.remove = [];
/**
* Gets an array that contains the definition of fields that are going to be changed
* @type {Array}
*/
this.change = [];
/**
* Gets or sets a string that contains the internal version of this migration. This property cannot be null.
* @type {string}
*/
this.version = '0.0';
/**
* Gets or sets a string that represents a short description of this migration
* @type {string}
*/
this.description = null;
/**
* Gets or sets a string that represents the adapter that is going to be migrated through this operation.
* This property cannot be null.
*/
this.appliesTo = null;
/**
* Gets or sets a string that represents the model that is going to be migrated through this operation.
* This property may be null.
*/
this.model = null;
}
/**
* @classdesc DataAssociationMapping class describes the association between two models.
* <p>
* An association between two models is described in field attributes. For example
* model Order may have an association with model Party (Person or Organization) through the field Order.customer:
* </p>
<pre class="prettyprint"><code>
{ "name": "Order",
"fields": [
...
{
"name": "customer",
"title": "Customer",
"description": "Party placing the order.",
"type": "Party"
}
...]
}
</code></pre>
<p>
This association is equivalent with the following DataAssociationMapping instance:
</p>
<pre class="prettyprint"><code>
"mapping": {
"cascade": "null",
"associationType": "association",
"select": [],
"childField": "customer",
"childModel": "Order",
"parentField": "id",
"parentModel": "Party"
}
</code></pre>
<p>
The above association mapping was auto-generated from the field definition of Order.customer where the field type (Party)
actually defines the association between these models.
</p>
<p>
Another example of an association between two models is a many-to-many association. User model has a many-to-many association (for user groups) with Group model:
</p>
<pre class="prettyprint"><code>
{ "name": "User",
"fields": [
...
{
"name": "groups",
"title": "User Groups",
"description": "A collection of groups where user belongs.",
"type": "Group",
"expandable": true,
"mapping": {
"associationAdapter": "GroupMembers",
"parentModel": "Group",
"parentField": "id",
"childModel": "User",
"childField": "id",
"associationType": "junction",
"cascade": "delete"
}
}
...]
}
</code></pre>
<p>This association may also be defined in Group model:</p>
<pre class="prettyprint"><code>
{ "name": "Group",
"fields": [
...
{
"name": "members",
"title": "Group Members",
"description": "Contains the collection of group members (users or groups).",
"type": "Account",
"many":true
}
...]
}
</code></pre>
*
* @class
* @property {string} associationAdapter - Gets or sets the association database object
* @property {string} parentModel - Gets or sets the parent model name
* @property {string} childModel - Gets or sets the child model name
* @property {string} parentField - Gets or sets the parent field name
* @property {string} childField - Gets or sets the child field name
* @property {string} refersTo - Gets or sets the parent property where this association refers to
* @property {string} parentLabel - Gets or sets the parent field that is going to be used as label for this association
* @property {string} cascade - Gets or sets the action that occurs when parent item is going to be deleted (all|none|null|delete). The default value is 'none'.
* @property {string} associationType - Gets or sets the type of this association (junction|association). The default value is 'association'.
* @property {string[]} select - Gets or sets an array of fields to select from associated model. If this property is empty then all associated model fields will be selected.
* @property {*} options - Gets or sets a set of default options which are going to be used while expanding results based on this data association.
* @param {*=} obj - An object that contains relation mapping attributes
* @constructor
*/
function DataAssociationMapping(obj) {
this.cascade = 'none';
this.associationType = 'association';
//this.select = [];
if (typeof obj === 'object') { _.assign(this, obj); }
}
/**
* @class
* @constructor
* @property {string} name - Gets or sets the internal name of this field.
* @property {string} property - Gets or sets the property name for this field.
* @property {string} title - Gets or sets the title of this field.
* @property {boolean} nullable - Gets or sets a boolean that indicates whether field is nullable or not.
* @property {string} type - Gets or sets the type of this field.
* @property {boolean} primary - Gets or sets a boolean that indicates whether field is primary key or not.
* @property {boolean} many - Gets or sets a boolean that indicates whether field defines an one-to-many relationship between models.
* @property {boolean} model - Gets or sets the parent model of this field.
* @property {*} value - Gets or sets the default value of this field.
* @property {*} calculation - Gets or sets the calculated value of this field.
* @property {boolean} readonly - Gets or sets a boolean which indicates whether a field is readonly.
* @property {boolean} editable - Gets or sets a boolean which indicates whether a field is available for edit. The default value is true.
* @property {DataAssociationMapping} mapping - Get or sets a relation mapping for this field.
* @property {string} coltype - Gets or sets a string that indicates the data field's column type. This attribute is used in data view definition
* @property {boolean} expandable - Get or sets whether the current field defines an association mapping and the associated data object(s) must be included while getting data.
* @property {string} section - Gets or sets the section where the field belongs.
* @property {boolean} nested - Gets or sets a boolean which indicates whether this field allows object(s) to be nested and updatable during an insert or update operation
* @property {string} description - Gets or sets a short description for this field.