mythix-orm 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/.eslintrc.js +94 -0
  2. package/.vscode/launch.json +34 -0
  3. package/.vscode/settings.json +10 -0
  4. package/LICENSE +21 -0
  5. package/README.md +15 -0
  6. package/lib/connection/connection-base.js +877 -0
  7. package/lib/connection/index.js +11 -0
  8. package/lib/connection/literals/average-literal.js +14 -0
  9. package/lib/connection/literals/count-literal.js +18 -0
  10. package/lib/connection/literals/distinct-literal.js +14 -0
  11. package/lib/connection/literals/index.js +23 -0
  12. package/lib/connection/literals/literal-base.js +62 -0
  13. package/lib/connection/literals/literal-field-base.js +45 -0
  14. package/lib/connection/literals/literal.js +11 -0
  15. package/lib/connection/literals/max-literal.js +14 -0
  16. package/lib/connection/literals/min-literal.js +14 -0
  17. package/lib/connection/literals/sum-literal.js +14 -0
  18. package/lib/connection/query-generator-base.js +864 -0
  19. package/lib/errors/base-error.js +14 -0
  20. package/lib/errors/connection/access-denied-error.js +13 -0
  21. package/lib/errors/connection/connection-acquire-timeout-error.js +13 -0
  22. package/lib/errors/connection/connection-refused-error.js +13 -0
  23. package/lib/errors/connection/connection-timed-out-error.js +13 -0
  24. package/lib/errors/connection/host-not-found-error.js +13 -0
  25. package/lib/errors/connection/host-not-reachable-error.js +13 -0
  26. package/lib/errors/connection/index.js +19 -0
  27. package/lib/errors/connection/invalid-connection-error.js +13 -0
  28. package/lib/errors/connection-base-error.js +13 -0
  29. package/lib/errors/database/exclusion-constraint-error.js +13 -0
  30. package/lib/errors/database/foreign-key-constraint-error.js +13 -0
  31. package/lib/errors/database/index.js +13 -0
  32. package/lib/errors/database/timeout-error.js +13 -0
  33. package/lib/errors/database/unknown-constraint-error.js +13 -0
  34. package/lib/errors/database-base-error.js +17 -0
  35. package/lib/errors/index.js +44 -0
  36. package/lib/field.js +18 -0
  37. package/lib/index.js +43 -0
  38. package/lib/model.js +1374 -0
  39. package/lib/proxy-class/index.js +3 -0
  40. package/lib/proxy-class/proxy-class.js +269 -0
  41. package/lib/query-engine/field-scope.js +99 -0
  42. package/lib/query-engine/index.js +13 -0
  43. package/lib/query-engine/model-scope.js +198 -0
  44. package/lib/query-engine/query-engine-base.js +325 -0
  45. package/lib/query-engine/query-engine.js +212 -0
  46. package/lib/types/concrete/bigint-type.js +62 -0
  47. package/lib/types/concrete/blob-type.js +46 -0
  48. package/lib/types/concrete/boolean-type.js +41 -0
  49. package/lib/types/concrete/char-type.js +39 -0
  50. package/lib/types/concrete/date-type.js +87 -0
  51. package/lib/types/concrete/datetime-type.js +92 -0
  52. package/lib/types/concrete/float-type.js +47 -0
  53. package/lib/types/concrete/foreign-key-type.js +208 -0
  54. package/lib/types/concrete/index.js +53 -0
  55. package/lib/types/concrete/integer-type.js +51 -0
  56. package/lib/types/concrete/string-type.js +44 -0
  57. package/lib/types/concrete/text-type.js +44 -0
  58. package/lib/types/concrete/uuid-base.js +77 -0
  59. package/lib/types/concrete/uuid-v1-type.js +99 -0
  60. package/lib/types/concrete/uuid-v3-type.js +108 -0
  61. package/lib/types/concrete/uuid-v4-type.js +90 -0
  62. package/lib/types/concrete/uuid-v5-type.js +108 -0
  63. package/lib/types/concrete/xid-type.js +58 -0
  64. package/lib/types/helpers/default-helpers.js +127 -0
  65. package/lib/types/helpers/index.js +35 -0
  66. package/lib/types/index.js +94 -0
  67. package/lib/types/type.js +244 -0
  68. package/lib/types/virtual/index.js +14 -0
  69. package/lib/types/virtual/model-type.js +141 -0
  70. package/lib/types/virtual/models-type.js +264 -0
  71. package/lib/types/virtual/relational-type-base.js +323 -0
  72. package/lib/utils/index.js +65 -0
  73. package/lib/utils/misc-utils.js +73 -0
  74. package/lib/utils/model-utils.js +704 -0
  75. package/lib/utils/query-utils.js +186 -0
  76. package/package.json +63 -0
  77. package/playground/test01.js +15 -0
  78. package/spec/connection/connection-base-spec.js +126 -0
  79. package/spec/connection/literals/average-literal-spec.js +45 -0
  80. package/spec/connection/literals/count-literal-spec.js +42 -0
  81. package/spec/connection/literals/distinct-literal-spec.js +42 -0
  82. package/spec/connection/literals/literal-spec.js +26 -0
  83. package/spec/connection/literals/max-literal-spec.js +42 -0
  84. package/spec/connection/literals/min-literal-spec.js +42 -0
  85. package/spec/connection/literals/sum-literal-spec.js +42 -0
  86. package/spec/helpers/default-helpers-spec.js +108 -0
  87. package/spec/model-spec.js +736 -0
  88. package/spec/proxy-class/proxy-class-spec.js +173 -0
  89. package/spec/query-engine/__snapshots__/QueryEngine-operations-query_operations_and_chaining-can_chain_query_conditions-001.snapshot +94 -0
  90. package/spec/query-engine/__snapshots__/QueryEngine-operations-query_operations_and_chaining-can_construct_a_query_context_with_a_model_call-001.snapshot +35 -0
  91. package/spec/query-engine/__snapshots__/QueryEngine-operations-query_operations_and_chaining-can_construct_a_query_context_with_a_model_sub-001.snapshot +35 -0
  92. package/spec/query-engine/__snapshots__/QueryEngine-operations-query_operations_and_chaining-can_set_a_default_scope_on_a_model-001.snapshot +57 -0
  93. package/spec/query-engine/__snapshots__/QueryEngine-operations-query_operations_and_chaining-can_unscope_default_scope_on_a_model-001.snapshot +35 -0
  94. package/spec/query-engine/query-engine-spec.js +99 -0
  95. package/spec/support/jasmine.json +13 -0
  96. package/spec/support/models/blob-test-model.js +19 -0
  97. package/spec/support/models/extended-user-model.js +38 -0
  98. package/spec/support/models/index.js +27 -0
  99. package/spec/support/models/number-model.js +24 -0
  100. package/spec/support/models/role-model.js +26 -0
  101. package/spec/support/models/role-thing-model.js +41 -0
  102. package/spec/support/models/scoped-user-model.js +13 -0
  103. package/spec/support/models/time-model.js +36 -0
  104. package/spec/support/models/user-model.js +70 -0
  105. package/spec/support/models/user-role-model.js +36 -0
  106. package/spec/support/models/user-thing-model.js +46 -0
  107. package/spec/support/models/validation-test-model.js +40 -0
  108. package/spec/support/snapshots.js +293 -0
  109. package/spec/support/test-helpers.js +13 -0
  110. package/spec/types/concrete/bigint-type-spec.js +84 -0
  111. package/spec/types/concrete/boolean-type-spec.js +83 -0
  112. package/spec/types/concrete/date-type-spec.js +85 -0
  113. package/spec/types/concrete/datetime-type-spec.js +87 -0
  114. package/spec/types/concrete/float-type-spec.js +71 -0
  115. package/spec/types/concrete/foreign-key-type-spec.js +64 -0
  116. package/spec/types/concrete/integer-type-spec.js +71 -0
  117. package/spec/types/concrete/string-type-spec.js +91 -0
  118. package/spec/types/concrete/uuid-v1-type-spec.js +73 -0
  119. package/spec/types/concrete/uuid-v4-type-spec.js +65 -0
  120. package/spec/types/type-spec.js +101 -0
  121. package/spec/types/virtual/model-types-spec.js +401 -0
  122. package/spec/utils/misc-utils-spec.js +61 -0
  123. package/spec/utils/model-utils-spec.js +55 -0
  124. package/spec/utils/query-utils-spec.js +105 -0
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ const Nife = require('nife');
4
+ const Type = require('../type');
5
+
6
+ class CharType extends Type {
7
+ static getDisplayName() {
8
+ return 'CHAR';
9
+ }
10
+
11
+ getDisplayName() {
12
+ return this.constructor.getDisplayName();
13
+ }
14
+
15
+ castToType({ value }) {
16
+ if (value == null)
17
+ return value;
18
+
19
+ if (!Nife.instanceOf('string') || value.length !== 1)
20
+ throw new TypeError(`CharType::castToType: Value provided ("${value}") can not be cast into a char. Please provide a string that is one character wide.`);
21
+
22
+ return value.charAt(0);
23
+ }
24
+
25
+ isValidValue(value) {
26
+ return (Nife.instanceOf(value, 'string') && value.length === 1);
27
+ }
28
+
29
+ toString(connection) {
30
+ return (connection)
31
+ ? this.toConnectionType(connection)
32
+ : 'CHAR';
33
+ }
34
+ }
35
+
36
+ module.exports = {
37
+ CHAR: Type.wrapConstructor(CharType),
38
+ CharType,
39
+ };
@@ -0,0 +1,87 @@
1
+ 'use strict';
2
+
3
+ const Nife = require('nife');
4
+ const moment = require('moment');
5
+ const Type = require('../type');
6
+ const { DATE_NOW } = require('../helpers/default-helpers');
7
+
8
+ moment.suppressDeprecationWarnings = true;
9
+
10
+ class DateType extends Type {
11
+ static Default = {
12
+ NOW: DATE_NOW,
13
+ };
14
+
15
+ static getDisplayName() {
16
+ return 'DATE';
17
+ }
18
+
19
+ getDisplayName() {
20
+ return this.constructor.getDisplayName();
21
+ }
22
+
23
+ constructor(format) {
24
+ super(format);
25
+
26
+ this.format = format || 'YYYY-MM-DD';
27
+ }
28
+
29
+ castToType({ value }) {
30
+ if (value == null)
31
+ return value;
32
+
33
+ let dateTime = this.deserialize(value);
34
+ if (!dateTime.isValid())
35
+ throw new TypeError(`DateType::castToType: Value provided ("${value}") can not be cast into a date.`);
36
+
37
+ return dateTime.startOf('day');
38
+ }
39
+
40
+ isValidValue(value) {
41
+ return moment(value).isValid();
42
+ }
43
+
44
+ toString(connection) {
45
+ return (connection)
46
+ ? this.toConnectionType(connection)
47
+ : 'DATE';
48
+ }
49
+
50
+ serialize(value, connection) {
51
+ if (value == null)
52
+ return (connection) ? null : value;
53
+
54
+ if (connection)
55
+ return connection.convertDateToDBTime(value);
56
+
57
+ if (this.format)
58
+ return value.format(this.format);
59
+
60
+ return value.toISOString();
61
+ }
62
+
63
+ deserialize(_value) {
64
+ let value = _value;
65
+ if (value == null)
66
+ return value;
67
+
68
+ if (Nife.instanceOf(value, 'string') && this.format) {
69
+ value = moment(_value, this.format);
70
+
71
+ if (!value.isValid())
72
+ value = moment(_value);
73
+ } else {
74
+ value = moment(value);
75
+ }
76
+
77
+ if (!value.isValid())
78
+ throw new TypeError(`DateType::deserialize: Value provided ("${_value}") can not be cast into a date.`);
79
+
80
+ return value;
81
+ }
82
+ }
83
+
84
+ module.exports = {
85
+ DATE: Type.wrapConstructor(DateType),
86
+ DateType,
87
+ };
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+
3
+ const Nife = require('nife');
4
+ const moment = require('moment');
5
+ const Type = require('../type');
6
+ const { DATETIME_NOW } = require('../helpers/default-helpers');
7
+
8
+ moment.suppressDeprecationWarnings = true;
9
+
10
+ class DateTimeType extends Type {
11
+ static Default = {
12
+ NOW: DATETIME_NOW,
13
+ };
14
+
15
+ static getDisplayName() {
16
+ return 'DATETIME';
17
+ }
18
+
19
+ getDisplayName() {
20
+ return this.constructor.getDisplayName();
21
+ }
22
+
23
+ constructor(length, format) {
24
+ super(length, format);
25
+
26
+ this.length = length || null;
27
+ this.format = format;
28
+ }
29
+
30
+ castToType({ value }) {
31
+ if (value == null)
32
+ return value;
33
+
34
+ let dateTime = this.deserialize(value);
35
+ if (!dateTime.isValid())
36
+ throw new TypeError(`DateTimeType::castToType: Value provided ("${value}") can not be cast into a date.`);
37
+
38
+ return dateTime;
39
+ }
40
+
41
+ isValidValue(value) {
42
+ return moment(value).isValid();
43
+ }
44
+
45
+ toString(connection) {
46
+ return (connection)
47
+ ? this.toConnectionType(connection)
48
+ : 'DATETIME';
49
+ }
50
+
51
+ serialize(_value, connection) {
52
+ let value = _value;
53
+ if (value == null)
54
+ return (connection) ? null : value;
55
+
56
+ if (!(value instanceof moment))
57
+ value = this.deserialize(value);
58
+
59
+ if (connection)
60
+ return connection.convertDateToDBTime(value);
61
+
62
+ if (this.format)
63
+ return value.format(this.format);
64
+
65
+ return value.toISOString();
66
+ }
67
+
68
+ deserialize(_value) {
69
+ let value = _value;
70
+ if (value == null)
71
+ return value;
72
+
73
+ if (Nife.instanceOf(value, 'string') && this.format) {
74
+ value = moment(_value, this.format);
75
+
76
+ if (!value.isValid())
77
+ value = moment(_value);
78
+ } else {
79
+ value = moment(_value);
80
+ }
81
+
82
+ if (!value.isValid())
83
+ throw new TypeError(`DateType::deserialize: Value provided ("${_value}") can not be cast into a date.`);
84
+
85
+ return value;
86
+ }
87
+ }
88
+
89
+ module.exports = {
90
+ DATETIME: Type.wrapConstructor(DateTimeType),
91
+ DateTimeType,
92
+ };
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ const Nife = require('nife');
4
+ const Type = require('../type');
5
+
6
+ class FloatType extends Type {
7
+ static getDisplayName() {
8
+ return 'FLOAT';
9
+ }
10
+
11
+ getDisplayName() {
12
+ return this.constructor.getDisplayName();
13
+ }
14
+
15
+ constructor(length, precision) {
16
+ super(length, precision);
17
+
18
+ this.length = length;
19
+ this.precision = precision;
20
+ }
21
+
22
+ castToType({ value }) {
23
+ if (value == null)
24
+ return value;
25
+
26
+ let number = parseFloat(('' + value));
27
+ if (!isFinite(number))
28
+ throw new TypeError(`FloatType::castToType: Value provided ("${value}") can not be cast into an floating point number.`);
29
+
30
+ return number;
31
+ }
32
+
33
+ isValidValue(value) {
34
+ return (Nife.instanceOf(value, 'number') && isFinite(value));
35
+ }
36
+
37
+ toString(connection) {
38
+ return (connection)
39
+ ? this.toConnectionType(connection)
40
+ : 'FLOAT';
41
+ }
42
+ }
43
+
44
+ module.exports = {
45
+ FLOAT: Type.wrapConstructor(FloatType),
46
+ FloatType,
47
+ };
@@ -0,0 +1,208 @@
1
+ 'use strict';
2
+
3
+ const Nife = require('nife');
4
+ const Type = require('../type');
5
+ const ModelUtils = require('../../utils/model-utils');
6
+
7
+ class ForeignKeyType extends Type {
8
+ static isForeignKey() {
9
+ return true;
10
+ }
11
+
12
+ static getDisplayName() {
13
+ return 'FOREIGN_KEY';
14
+ }
15
+
16
+ getDisplayName() {
17
+ return this.constructor.getDisplayName();
18
+ }
19
+
20
+ constructor(_fullyQualifiedName, _options) {
21
+ if (arguments.length === 0)
22
+ throw new TypeError('ForeignKeyType::constructor: You must specify a fully qualified field name, or provide complete options.');
23
+
24
+ let fullyQualifiedName = _fullyQualifiedName;
25
+ let options = _options || {};
26
+
27
+ if (arguments.length === 1) {
28
+ if (Nife.instanceOf(fullyQualifiedName, 'object'))
29
+ options = fullyQualifiedName;
30
+ } else {
31
+ let def = ModelUtils.parseQualifiedName(fullyQualifiedName);
32
+ if (Nife.isEmpty(def.modelName))
33
+ throw new TypeError('ForeignKeyType::constructor: No model found. You must specify a model.');
34
+
35
+ options = Object.assign(
36
+ {},
37
+ options,
38
+ {
39
+ modelName: def.modelName,
40
+ fieldName: def.fieldNames[0],
41
+ fullyQualifiedName,
42
+ },
43
+ );
44
+ }
45
+
46
+ super(fullyQualifiedName, options);
47
+
48
+ this.options = options;
49
+ this.fullyQualifiedName = fullyQualifiedName;
50
+ }
51
+
52
+ castToType(args) {
53
+ let { value } = args;
54
+ if (value == null)
55
+ return value;
56
+
57
+ let targetField = this.getTargetField(args.connection);
58
+ if (!targetField) {
59
+ let field = this.getField();
60
+ let debugFieldStr = '';
61
+
62
+ if (field) {
63
+ let Model = field.Model;
64
+ if (Model)
65
+ debugFieldStr = ` "${Model.getModelName()}:${field.fieldName}"`;
66
+ }
67
+
68
+ throw new TypeError(`ForeignKeyType::castToType: Target field not defined${debugFieldStr}.`);
69
+ }
70
+
71
+ return targetField.type.castToType(args);
72
+ }
73
+
74
+ isValidValue(value, options) {
75
+ let targetField = this.getTargetField(options && options.connection);
76
+ if (!targetField)
77
+ throw new TypeError('ForeignKeyType::isValidValue: Target field not defined.');
78
+
79
+ return targetField.type.isValidValue(value, options);
80
+ }
81
+
82
+ parseOptionsAndCheckForErrors(SourceModel, sourceField, connection) {
83
+ let options = this.options;
84
+ let fullyQualifiedName = this.fullyQualifiedName;
85
+ let Model = options.Model;
86
+ let Field = options.Field;
87
+ let fieldName = options.fieldName;
88
+
89
+ if (!Model) {
90
+ if (options.modelName) {
91
+ Model = connection.getModel(options.modelName);
92
+ } else {
93
+ let def = ModelUtils.parseQualifiedName(fullyQualifiedName);
94
+ Model = connection.getModel(def.modelName);
95
+
96
+ if (!fieldName)
97
+ fieldName = def.fieldNames[0];
98
+ }
99
+ }
100
+
101
+ if (!Model)
102
+ throw new TypeError(`ForeignKeyType::parseOptionsAndCheckForErrors: No target model found for field "${SourceModel.getModelName()}:${sourceField.fieldName}". You must specify a model.`);
103
+
104
+ if (!Field) {
105
+ let modelName = Model.getModelName();
106
+
107
+ if (options.fieldName) {
108
+ Field = connection.getField(options.fieldName, modelName);
109
+ } else {
110
+ let def = ModelUtils.parseQualifiedName(fullyQualifiedName);
111
+ Field = connection.getField(def.fieldNames[0], modelName);
112
+ }
113
+ }
114
+
115
+ if (!Field)
116
+ throw new TypeError(`ForeignKeyType::parseOptionsAndCheckForErrors: No target field found for "${SourceModel.getModelName()}:${sourceField.fieldName}". You must specify a field.`);
117
+
118
+ return {
119
+ Model,
120
+ Field,
121
+ };
122
+ }
123
+
124
+ initialize(connection, self) {
125
+ if (this.targetModel)
126
+ return;
127
+
128
+ super.initialize(connection, self);
129
+
130
+ if (!connection)
131
+ throw new Error('ForeignKeyType::initialize: A connection is required to use the ForeignKeyType.');
132
+
133
+ let {
134
+ Model,
135
+ Field,
136
+ } = this.parseOptionsAndCheckForErrors(this.getModel(), this.getField(), connection);
137
+
138
+ Object.defineProperties(this, {
139
+ 'targetModel': {
140
+ writable: true,
141
+ enumberable: false,
142
+ configurable: true,
143
+ value: Model,
144
+ },
145
+ 'targetField': {
146
+ writable: true,
147
+ enumberable: false,
148
+ configurable: true,
149
+ value: Field,
150
+ },
151
+ });
152
+ }
153
+
154
+ getOptions() {
155
+ return this.options;
156
+ }
157
+
158
+ getTargetModel(connection) {
159
+ if (connection && !this.targetModel)
160
+ this.initialize(connection);
161
+
162
+ return this.targetModel;
163
+ }
164
+
165
+ getTargetModelName(connection) {
166
+ let targetModel = this.getTargetModel(connection);
167
+ return targetModel.getModelName();
168
+ }
169
+
170
+ getTargetField(connection) {
171
+ if (connection && !this.targetModel)
172
+ this.initialize(connection);
173
+
174
+ return this.targetField;
175
+ }
176
+
177
+ getTargetFieldName(connection) {
178
+ let targetField = this.getTargetField(connection);
179
+ if (!targetField)
180
+ return;
181
+
182
+ return targetField.fieldName;
183
+ }
184
+
185
+ toConnectionType(connection, options) {
186
+ let targetField = this.getTargetField(connection);
187
+ if (!targetField)
188
+ return '';
189
+
190
+ return targetField.type.toConnectionType(connection, options);
191
+ }
192
+
193
+ toString(...args) {
194
+ if (args.length === 0)
195
+ return 'ForeignKeyType {}';
196
+
197
+ let targetField = this.getTargetField(...args);
198
+ if (!targetField)
199
+ return '';
200
+
201
+ return targetField.type.toConnectionType(...args);
202
+ }
203
+ }
204
+
205
+ module.exports = {
206
+ FOREIGN_KEY: Type.wrapConstructor(ForeignKeyType),
207
+ ForeignKeyType,
208
+ };
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ const { BIGINT, BigIntType } = require('./bigint-type');
4
+ const { BLOB, BlobType } = require('./blob-type');
5
+ const { BOOLEAN, BooleanType } = require('./boolean-type');
6
+ const { CHAR, CharType } = require('./char-type');
7
+ const { DATE, DateType } = require('./date-type');
8
+ const { DATETIME, DateTimeType } = require('./datetime-type');
9
+ const { FLOAT, FloatType } = require('./float-type');
10
+ const { FOREIGN_KEY, ForeignKeyType } = require('./foreign-key-type');
11
+ const { INTEGER, IntegerType } = require('./integer-type');
12
+ const { STRING, StringType } = require('./string-type');
13
+ const { TEXT, TextType } = require('./text-type');
14
+ const { UUIDV1, UUIDV1Type } = require('./uuid-v1-type');
15
+ const { UUIDV3, UUIDV3Type } = require('./uuid-v3-type');
16
+ const { UUIDV4, UUIDV4Type } = require('./uuid-v4-type');
17
+ const { UUIDV5, UUIDV5Type } = require('./uuid-v5-type');
18
+ const { XID, XIDType } = require('./xid-type');
19
+
20
+ module.exports = {
21
+ BigIntType,
22
+ BlobType,
23
+ BooleanType,
24
+ CharType,
25
+ DateTimeType,
26
+ DateType,
27
+ FloatType,
28
+ ForeignKeyType,
29
+ IntegerType,
30
+ StringType,
31
+ TextType,
32
+ UUIDV1Type,
33
+ UUIDV3Type,
34
+ UUIDV4Type,
35
+ UUIDV5Type,
36
+ XIDType,
37
+ BIGINT,
38
+ BLOB,
39
+ BOOLEAN,
40
+ CHAR,
41
+ DATE,
42
+ DATETIME,
43
+ FLOAT,
44
+ FOREIGN_KEY,
45
+ INTEGER,
46
+ STRING,
47
+ TEXT,
48
+ UUIDV1,
49
+ UUIDV3,
50
+ UUIDV4,
51
+ UUIDV5,
52
+ XID,
53
+ };
@@ -0,0 +1,51 @@
1
+ 'use strict';
2
+
3
+ const Nife = require('nife');
4
+ const Type = require('../type');
5
+ const { AUTO_INCREMENT } = require('../helpers/default-helpers');
6
+
7
+ class IntegerType extends Type {
8
+ static Default = {
9
+ AUTO_INCREMENT,
10
+ };
11
+
12
+ static getDisplayName() {
13
+ return 'INTEGER';
14
+ }
15
+
16
+ getDisplayName() {
17
+ return this.constructor.getDisplayName();
18
+ }
19
+
20
+ constructor(length) {
21
+ super(length);
22
+
23
+ this.length = length || null;
24
+ }
25
+
26
+ castToType({ value }) {
27
+ if (value == null)
28
+ return value;
29
+
30
+ let number = parseFloat(('' + value).replace(/[^\d.e-]/g, ''));
31
+ if (!isFinite(number))
32
+ throw new TypeError(`IntegerType::castToType: Value provided ("${value}") can not be cast into an integer.`);
33
+
34
+ return Math.round(number);
35
+ }
36
+
37
+ isValidValue(value) {
38
+ return (Nife.instanceOf(value, 'number') && isFinite(value));
39
+ }
40
+
41
+ toString(connection) {
42
+ return (connection)
43
+ ? this.toConnectionType(connection)
44
+ : 'INTEGER';
45
+ }
46
+ }
47
+
48
+ module.exports = {
49
+ INTEGER: Type.wrapConstructor(IntegerType),
50
+ IntegerType,
51
+ };
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ const Nife = require('nife');
4
+ const Type = require('../type');
5
+
6
+ const DEFAULT_STRING_LENGTH = 256;
7
+
8
+ class StringType extends Type {
9
+ static getDisplayName() {
10
+ return 'STRING';
11
+ }
12
+
13
+ getDisplayName() {
14
+ return this.constructor.getDisplayName();
15
+ }
16
+
17
+ constructor(length) {
18
+ super(length);
19
+
20
+ this.length = length || DEFAULT_STRING_LENGTH;
21
+ }
22
+
23
+ castToType({ value }) {
24
+ if (value == null)
25
+ return value;
26
+
27
+ return ('' + value);
28
+ }
29
+
30
+ isValidValue(value) {
31
+ return Nife.instanceOf(value, 'string');
32
+ }
33
+
34
+ toString(connection) {
35
+ return (connection)
36
+ ? this.toConnectionType(connection)
37
+ : `VARCHAR(${this.length})`;
38
+ }
39
+ }
40
+
41
+ module.exports = {
42
+ STRING: Type.wrapConstructor(StringType),
43
+ StringType,
44
+ };
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ const Nife = require('nife');
4
+ const Type = require('../type');
5
+
6
+ const DEFAULT_STRING_LENGTH = 65565;
7
+
8
+ class TextType extends Type {
9
+ static getDisplayName() {
10
+ return 'TEXT';
11
+ }
12
+
13
+ getDisplayName() {
14
+ return this.constructor.getDisplayName();
15
+ }
16
+
17
+ constructor(length) {
18
+ super(length);
19
+
20
+ this.length = length || DEFAULT_STRING_LENGTH;
21
+ }
22
+
23
+ castToType({ value }) {
24
+ if (value == null)
25
+ return value;
26
+
27
+ return ('' + value);
28
+ }
29
+
30
+ isValidValue(value) {
31
+ return Nife.instanceOf(value, 'string');
32
+ }
33
+
34
+ toString(connection) {
35
+ return (connection)
36
+ ? this.toConnectionType(connection)
37
+ : 'TEXT';
38
+ }
39
+ }
40
+
41
+ module.exports = {
42
+ TEXT: Type.wrapConstructor(TextType),
43
+ TextType,
44
+ };