mythix-orm 1.5.1 → 1.5.4
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.
- package/lib/connection/connection-base.js +10 -2
- package/lib/field.js +217 -0
- package/lib/model.js +40 -40
- package/lib/types/concrete/bigint-type.js +123 -0
- package/lib/types/concrete/blob-type.js +85 -0
- package/lib/types/concrete/boolean-type.js +75 -2
- package/lib/types/concrete/char-type.js +72 -0
- package/lib/types/concrete/date-type.js +168 -0
- package/lib/types/concrete/datetime-type.js +172 -3
- package/lib/types/concrete/integer-type.js +100 -0
- package/lib/types/concrete/numeric-type.js +106 -0
- package/lib/types/helpers/default-helpers.js +6 -6
- package/lib/types/type.js +23 -0
- package/package.json +1 -1
|
@@ -7,11 +7,56 @@ const { DATETIME_NOW } = require('../helpers/default-helpers');
|
|
|
7
7
|
|
|
8
8
|
moment.suppressDeprecationWarnings = true;
|
|
9
9
|
|
|
10
|
+
/// `DATETIME` type.
|
|
11
|
+
///
|
|
12
|
+
/// This represents a "date time", a date with a time
|
|
13
|
+
/// in the underlying database.
|
|
14
|
+
///
|
|
15
|
+
/// Client-side storage for this field will be backed by
|
|
16
|
+
/// a [moment](https://momentjs.com/docs/) instance.
|
|
17
|
+
///
|
|
18
|
+
/// You can optionally provide a `format` argument when constructing
|
|
19
|
+
/// this type. **This is a client-side format only**. It doesn't
|
|
20
|
+
/// specify the format for storing the value in the database. Instead
|
|
21
|
+
/// it is used to parse provided date strings, and when serializing the
|
|
22
|
+
/// field (for example via <see>Model.toJSON</see>).
|
|
23
|
+
///
|
|
24
|
+
/// Example:
|
|
25
|
+
/// class DatesWithTimes extends Model {
|
|
26
|
+
/// static fields = {
|
|
27
|
+
/// dateTime1: Types.DATETIME('YYYY-MM-DD HH:mm:ss'),
|
|
28
|
+
/// dateTime2: new Types.DateTimeType(),
|
|
29
|
+
/// };
|
|
30
|
+
/// }
|
|
31
|
+
///
|
|
32
|
+
/// Properties:
|
|
33
|
+
/// Default: object = { NOW }
|
|
34
|
+
/// `NOW` is a method that can be used as the `defaultValue` of a <see>Field</see>
|
|
35
|
+
/// to have this field use "NOW" time. There are a number of different ways
|
|
36
|
+
/// to use this `defaultValue` method. If you use just `Types.DATETIME.Default.NOW`,
|
|
37
|
+
/// then it will use the underlying databases 'NOW' function, setting the field
|
|
38
|
+
/// to remote database time. Available also are the following:<br>
|
|
39
|
+
/// 1. `Types.DATETIME.Default.NOW` = Remote database 'NOW' time, set on insert only.<br>
|
|
40
|
+
/// 2. `Types.DATETIME.Default.NOW.UPDATE` = Remote database 'NOW' time, set on insert and update.<br>
|
|
41
|
+
/// 3. `Types.DATETIME.Default.NOW.LOCAL` = Local (client-side) 'NOW' time, set on insert only.<br>
|
|
42
|
+
/// 4. `Types.DATETIME.Default.NOW.LOCAL.UPDATE` = Local (client-side) 'NOW' time, set on insert and update.<br>
|
|
43
|
+
/// *Note: It is highly recommended that you **do not** use `LOCAL` time unless you know exactly what you are doing, as clock-drift between machines may become an issue.*
|
|
44
|
+
/// See: Type
|
|
10
45
|
class DateTimeType extends Type {
|
|
11
46
|
static Default = {
|
|
12
47
|
NOW: DATETIME_NOW,
|
|
13
48
|
};
|
|
14
49
|
|
|
50
|
+
/// Get the "display" name for this type.
|
|
51
|
+
///
|
|
52
|
+
/// This method is called from <see>Model.toString</see>
|
|
53
|
+
/// when stringifying the model for representation.
|
|
54
|
+
///
|
|
55
|
+
/// Note:
|
|
56
|
+
/// This is also an instance method that can be called from
|
|
57
|
+
/// an instance of the type.
|
|
58
|
+
/// Return: string
|
|
59
|
+
/// Return the string value `'DATETIME'`
|
|
15
60
|
static getDisplayName() {
|
|
16
61
|
return 'DATETIME';
|
|
17
62
|
}
|
|
@@ -20,13 +65,54 @@ class DateTimeType extends Type {
|
|
|
20
65
|
return this.constructor.getDisplayName();
|
|
21
66
|
}
|
|
22
67
|
|
|
23
|
-
|
|
24
|
-
|
|
68
|
+
/// Construct a new `DATETIME` type.
|
|
69
|
+
///
|
|
70
|
+
/// The `format` argument will specify the
|
|
71
|
+
/// date format for this field. See the
|
|
72
|
+
/// [moment](https://momentjs.com/docs/#/displaying/format/) docs
|
|
73
|
+
/// for a reference on this format.
|
|
74
|
+
///
|
|
75
|
+
/// This format **is not** the format that will be used
|
|
76
|
+
/// to store the `DATETIME` field in the database. Instead, this
|
|
77
|
+
/// is a client-side only format, to define how to parse
|
|
78
|
+
/// provided date strings, and to format on serialize
|
|
79
|
+
/// when calling <see>Model.toJSON</see>.
|
|
80
|
+
///
|
|
81
|
+
/// The `length` argument is used by some database drivers
|
|
82
|
+
/// to modify the precision of the underlying timestamp/datetime value.
|
|
83
|
+
///
|
|
84
|
+
/// Return: DateTimeType
|
|
85
|
+
/// Arguments:
|
|
86
|
+
/// format?: string
|
|
87
|
+
/// Specify the client-side formatting for this date field.
|
|
88
|
+
/// This will be used for parsing date strings, and for serializing
|
|
89
|
+
/// the field.
|
|
90
|
+
/// length?: number
|
|
91
|
+
/// Used by some database drivers to specify the precision of the time
|
|
92
|
+
/// when stored in the database.
|
|
93
|
+
constructor(format, length) {
|
|
94
|
+
super(format, length);
|
|
25
95
|
|
|
26
|
-
this.length = length || null;
|
|
27
96
|
this.format = format;
|
|
97
|
+
this.length = length || null;
|
|
28
98
|
}
|
|
29
99
|
|
|
100
|
+
/// Cast provided value to underlying type.
|
|
101
|
+
///
|
|
102
|
+
/// This will cast the incoming value to the
|
|
103
|
+
/// underlying type of this field, a [moment](https://momentjs.com/docs/)
|
|
104
|
+
/// instance. If the provided value results in
|
|
105
|
+
/// an invalid date, then an exception will be thrown.
|
|
106
|
+
///
|
|
107
|
+
/// See <see>Type.castToType</see> for a more
|
|
108
|
+
/// detailed description.
|
|
109
|
+
///
|
|
110
|
+
/// Return: moment | null | undefined
|
|
111
|
+
/// Return the incoming `value`, cast to this
|
|
112
|
+
/// type. `null` and `undefined` are simply
|
|
113
|
+
/// returned without casting.
|
|
114
|
+
/// Arguments:
|
|
115
|
+
/// context: <see name="CastToTypeContext">Type.castToType</see>
|
|
30
116
|
castToType({ value, connection }) {
|
|
31
117
|
if (value == null)
|
|
32
118
|
return value;
|
|
@@ -38,16 +124,71 @@ class DateTimeType extends Type {
|
|
|
38
124
|
return dateTime;
|
|
39
125
|
}
|
|
40
126
|
|
|
127
|
+
/// Check if the provided value is valid.
|
|
128
|
+
///
|
|
129
|
+
/// This will check if the provided value is
|
|
130
|
+
/// a valid date. It does so by calling
|
|
131
|
+
/// `moment(value).isValid()`. If this check
|
|
132
|
+
/// results in `true`, then this method will
|
|
133
|
+
/// return `true`, otherwise it will return `false`.
|
|
134
|
+
///
|
|
135
|
+
/// See the [moment](https://momentjs.com/docs/) library documentation for more information.
|
|
136
|
+
///
|
|
137
|
+
/// Return: boolean
|
|
138
|
+
/// Arguments:
|
|
139
|
+
/// value: any
|
|
140
|
+
/// The value to check.
|
|
41
141
|
isValidValue(value) {
|
|
42
142
|
return moment(value).isValid();
|
|
43
143
|
}
|
|
44
144
|
|
|
145
|
+
/// Stringify the type itself.
|
|
146
|
+
///
|
|
147
|
+
/// If a `connection` argument is provided, then this
|
|
148
|
+
/// will go through the connection to generate the type
|
|
149
|
+
/// for the underlying database. If no connection is
|
|
150
|
+
/// provided, then a "standard" SQL type will be returned
|
|
151
|
+
/// for this type instead. The "standard" type returned
|
|
152
|
+
/// when no `connection` is provided is `'DATETIME'`.
|
|
153
|
+
///
|
|
154
|
+
/// Return: string
|
|
155
|
+
/// Arguments:
|
|
156
|
+
/// connection?: <see>Connection</see>
|
|
157
|
+
/// An optional connection. If provided, send this
|
|
158
|
+
/// type through <see>Type.toConnectionType</see>
|
|
159
|
+
/// to have the connection itself generate the underlying
|
|
160
|
+
/// type for the database. If `connection` is not provided,
|
|
161
|
+
/// then this will simply return a "standard" generic matching
|
|
162
|
+
/// SQL type.
|
|
45
163
|
toString(connection) {
|
|
46
164
|
return (connection)
|
|
47
165
|
? this.toConnectionType(connection)
|
|
48
166
|
: 'DATETIME';
|
|
49
167
|
}
|
|
50
168
|
|
|
169
|
+
/// Serialize the field value.
|
|
170
|
+
///
|
|
171
|
+
/// This will be called whenever the field's value
|
|
172
|
+
/// needs to be serialized. If a `connection` argument
|
|
173
|
+
/// is provided, then this method will assume that the
|
|
174
|
+
/// connection is serializing it for storage in the database.
|
|
175
|
+
/// In this case, <see>connection.convertDateToDBTime</see> is
|
|
176
|
+
/// called and provided the `value` for the connection to turn
|
|
177
|
+
/// the date into a proper value for the underlying database.
|
|
178
|
+
///
|
|
179
|
+
/// If no `connection` is provided, then the date will be serialized
|
|
180
|
+
/// according to the provided `format` specified by the user. If
|
|
181
|
+
/// no format was specified by the user, then it will be serialized
|
|
182
|
+
/// to [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format.
|
|
183
|
+
///
|
|
184
|
+
/// Return: string | null | undefined
|
|
185
|
+
/// Arguments:
|
|
186
|
+
/// value: moment
|
|
187
|
+
/// The date value to serialize.
|
|
188
|
+
/// connection?: <see>Connection</see>
|
|
189
|
+
/// A connection instance, which if provided, will
|
|
190
|
+
/// proxy serialization to the underlying database connection
|
|
191
|
+
/// via the <see>connection.convertDateToDBTime</see> method.
|
|
51
192
|
serialize(_value, connection) {
|
|
52
193
|
let value = _value;
|
|
53
194
|
if (value == null)
|
|
@@ -65,6 +206,34 @@ class DateTimeType extends Type {
|
|
|
65
206
|
return value.toISOString();
|
|
66
207
|
}
|
|
67
208
|
|
|
209
|
+
/// Deserialize the field value.
|
|
210
|
+
///
|
|
211
|
+
/// This method is used to deserialize a provided
|
|
212
|
+
/// field value. If the value provided is a `moment`
|
|
213
|
+
/// instance, then simply return it. Otherwise,
|
|
214
|
+
/// if a string is provided, convert it to a `moment`
|
|
215
|
+
/// instance, using the `format` provided (if any).
|
|
216
|
+
///
|
|
217
|
+
/// The `connection` argument is not used by this
|
|
218
|
+
/// method, but is provided if the user wishes to
|
|
219
|
+
/// overload this method.
|
|
220
|
+
///
|
|
221
|
+
/// If `null` or `undefined` are provided as a value
|
|
222
|
+
/// then that value will simply be returned as-is.
|
|
223
|
+
///
|
|
224
|
+
/// Note:
|
|
225
|
+
/// This method is called by `castToType` to cast
|
|
226
|
+
/// incoming values into `moment` instances.
|
|
227
|
+
/// Return: moment
|
|
228
|
+
/// Arguments:
|
|
229
|
+
/// value: string | moment | null | undefined
|
|
230
|
+
/// The value to deserialize.
|
|
231
|
+
/// connection?: <see>Connection</see>
|
|
232
|
+
/// An optional connection that might be provided
|
|
233
|
+
/// depending on how and where this method is called from.
|
|
234
|
+
/// This method does nothing with the `connection`. It is
|
|
235
|
+
/// simply provided for if the user wishes to overload this
|
|
236
|
+
/// method.
|
|
68
237
|
// eslint-disable-next-line no-unused-vars
|
|
69
238
|
deserialize(_value, connection) {
|
|
70
239
|
let value = _value;
|
|
@@ -4,11 +4,51 @@ const Nife = require('nife');
|
|
|
4
4
|
const Type = require('../type');
|
|
5
5
|
const { AUTO_INCREMENT } = require('../helpers/default-helpers');
|
|
6
6
|
|
|
7
|
+
/// `INTEGER` type.
|
|
8
|
+
///
|
|
9
|
+
/// This represents an "integer" of any size (less than 64bits)
|
|
10
|
+
/// for the underlying database driver.
|
|
11
|
+
///
|
|
12
|
+
/// The `INTEGER` type is used for specifying all integer types.
|
|
13
|
+
/// Its `length` argument is used to specify the number of bytes
|
|
14
|
+
/// used in the backing storage of the field value. Each database
|
|
15
|
+
/// driver will interpret this `length` differently. For example,
|
|
16
|
+
/// a MySQL driver may choose to use a `TINYINT`, `SMALLINT`, a
|
|
17
|
+
/// `MEDIUMINT` based on the number of bytes specified.
|
|
18
|
+
///
|
|
19
|
+
/// Example:
|
|
20
|
+
/// class Integers extends Model {
|
|
21
|
+
/// static fields = {
|
|
22
|
+
/// integer1: Types.INTEGER(4),
|
|
23
|
+
/// integer2: Types.INTEGER,
|
|
24
|
+
/// integer3: new Types.BigIntType(1),
|
|
25
|
+
/// autoIncrementing: {
|
|
26
|
+
/// type: Types.INTEGER,
|
|
27
|
+
/// defaultValue: Types.INTEGER.Default.AUTO_INCREMENT,
|
|
28
|
+
/// },
|
|
29
|
+
/// };
|
|
30
|
+
/// }
|
|
31
|
+
///
|
|
32
|
+
/// Properties:
|
|
33
|
+
/// Default: object = { AUTO_INCREMENT }
|
|
34
|
+
/// `AUTO_INCREMENT` is a method that can be used as the `defaultValue` of a <see>Field</see>
|
|
35
|
+
/// to have this field auto-increment in the underlying database.
|
|
36
|
+
/// See: Type
|
|
7
37
|
class IntegerType extends Type {
|
|
8
38
|
static Default = {
|
|
9
39
|
AUTO_INCREMENT,
|
|
10
40
|
};
|
|
11
41
|
|
|
42
|
+
/// Get the "display" name for this type.
|
|
43
|
+
///
|
|
44
|
+
/// This method is called from <see>Model.toString</see>
|
|
45
|
+
/// when stringifying the model for representation.
|
|
46
|
+
///
|
|
47
|
+
/// Note:
|
|
48
|
+
/// This is also an instance method that can be called from
|
|
49
|
+
/// an instance of the type.
|
|
50
|
+
/// Return: string
|
|
51
|
+
/// Return the string value `'INTEGER'`
|
|
12
52
|
static getDisplayName() {
|
|
13
53
|
return 'INTEGER';
|
|
14
54
|
}
|
|
@@ -17,12 +57,43 @@ class IntegerType extends Type {
|
|
|
17
57
|
return this.constructor.getDisplayName();
|
|
18
58
|
}
|
|
19
59
|
|
|
60
|
+
/// Construct a new `BIGINT` type.
|
|
61
|
+
///
|
|
62
|
+
/// The `length` argument--as on all
|
|
63
|
+
/// integer types in Mythix ORM--specifies
|
|
64
|
+
/// the number of bytes to use for this type
|
|
65
|
+
/// in the database. Each underlying database
|
|
66
|
+
/// driver will interpret this value in a way that
|
|
67
|
+
/// makes sense to the database.
|
|
68
|
+
///
|
|
69
|
+
/// Return: IntegerType
|
|
70
|
+
/// Arguments:
|
|
71
|
+
/// length?: number
|
|
72
|
+
/// How many bytes to use in the underlying database to store the value.
|
|
73
|
+
/// Depending on the database driver, a length of `0` may be interpreted
|
|
74
|
+
/// as a single bit.
|
|
20
75
|
constructor(length) {
|
|
21
76
|
super(length);
|
|
22
77
|
|
|
23
78
|
this.length = length || null;
|
|
24
79
|
}
|
|
25
80
|
|
|
81
|
+
/// Cast provided value to underlying type.
|
|
82
|
+
///
|
|
83
|
+
/// This will cast the incoming value to the
|
|
84
|
+
/// underlying type of this field, a `number`
|
|
85
|
+
/// primitive. A `null` or `undefined`
|
|
86
|
+
/// value will simply be returned.
|
|
87
|
+
///
|
|
88
|
+
/// See <see>Type.castToType</see> for a more
|
|
89
|
+
/// detailed description.
|
|
90
|
+
///
|
|
91
|
+
/// Return: number | null | undefined
|
|
92
|
+
/// Return the incoming `value`, cast to this
|
|
93
|
+
/// type. `null` and `undefined` are simply
|
|
94
|
+
/// returned without casting.
|
|
95
|
+
/// Arguments:
|
|
96
|
+
/// context: <see name="CastToTypeContext">Type.castToType</see>
|
|
26
97
|
castToType({ value }) {
|
|
27
98
|
if (value == null)
|
|
28
99
|
return value;
|
|
@@ -34,10 +105,39 @@ class IntegerType extends Type {
|
|
|
34
105
|
return Math.round(number);
|
|
35
106
|
}
|
|
36
107
|
|
|
108
|
+
/// Check if the provided value is valid.
|
|
109
|
+
///
|
|
110
|
+
/// This will check if the provided value is
|
|
111
|
+
/// a `number` instance, and if it is finite.
|
|
112
|
+
/// If both conditions are true, then it will
|
|
113
|
+
/// return `true`.
|
|
114
|
+
///
|
|
115
|
+
/// Return: boolean
|
|
116
|
+
/// Arguments:
|
|
117
|
+
/// value: any
|
|
118
|
+
/// The value to check.
|
|
37
119
|
isValidValue(value) {
|
|
38
120
|
return (Nife.instanceOf(value, 'number') && isFinite(value));
|
|
39
121
|
}
|
|
40
122
|
|
|
123
|
+
/// Stringify the type itself.
|
|
124
|
+
///
|
|
125
|
+
/// If a `connection` argument is provided, then this
|
|
126
|
+
/// will go through the connection to generate the type
|
|
127
|
+
/// for the underlying database. If no connection is
|
|
128
|
+
/// provided, then a "standard" SQL type will be returned
|
|
129
|
+
/// for this type instead. The "standard" type returned
|
|
130
|
+
/// when no `connection` is provided is `'INTEGER'`.
|
|
131
|
+
///
|
|
132
|
+
/// Return: string
|
|
133
|
+
/// Arguments:
|
|
134
|
+
/// connection?: <see>Connection</see>
|
|
135
|
+
/// An optional connection. If provided, send this
|
|
136
|
+
/// type through <see>Type.toConnectionType</see>
|
|
137
|
+
/// to have the connection itself generate the underlying
|
|
138
|
+
/// type for the database. If `connection` is not provided,
|
|
139
|
+
/// then this will simply return a "standard" generic matching
|
|
140
|
+
/// SQL type.
|
|
41
141
|
toString(connection) {
|
|
42
142
|
return (connection)
|
|
43
143
|
? this.toConnectionType(connection)
|
|
@@ -6,7 +6,47 @@ const Type = require('../type');
|
|
|
6
6
|
const DEFAULT_TOTAL_DIGITS = 20;
|
|
7
7
|
const DEFAULT_DECIMAL_PLACES = 6;
|
|
8
8
|
|
|
9
|
+
/// `NUMERIC` type.
|
|
10
|
+
/// Also known as `NUMBER`, or `DECIMAL` in some databases.
|
|
11
|
+
///
|
|
12
|
+
/// This represents a "real" number of high precision and
|
|
13
|
+
/// accuracy in the underlying database driver. Unlike the
|
|
14
|
+
/// <see>RealType</see>, which suffers from precision and
|
|
15
|
+
/// rounding errors, this will be precise to very large
|
|
16
|
+
/// and very small values in most databases.
|
|
17
|
+
///
|
|
18
|
+
/// The `precision`, and `scale` arguments are named after
|
|
19
|
+
/// PostgreSQL, and need some explaining, as their names do
|
|
20
|
+
/// not match their intent very well. `precision` is the total
|
|
21
|
+
/// number of digits a number can have. For example, the number
|
|
22
|
+
/// 123.456 has a `precision` of `6`, because it contains six
|
|
23
|
+
/// digits. The `scale` specifies the number of digits that
|
|
24
|
+
/// are desired *after* the decimal place. A scale of `0` would
|
|
25
|
+
/// essentially mean that you want an integer (no decimal places),
|
|
26
|
+
/// whereas a `scale` of `2` would mean you want two decimal places.
|
|
27
|
+
/// It is important to call out that different database drivers
|
|
28
|
+
/// may interpret and use these values differently.
|
|
29
|
+
///
|
|
30
|
+
/// Example:
|
|
31
|
+
/// class Numeric extends Model {
|
|
32
|
+
/// static fields = {
|
|
33
|
+
/// numeric1: Types.NUMERIC(20, 6),
|
|
34
|
+
/// numeric2: Types.NUMERIC,
|
|
35
|
+
/// numeric3: new Types.NumericType(10, 3),
|
|
36
|
+
/// };
|
|
37
|
+
/// }
|
|
38
|
+
/// See: Type
|
|
9
39
|
class NumericType extends Type {
|
|
40
|
+
/// Get the "display" name for this type.
|
|
41
|
+
///
|
|
42
|
+
/// This method is called from <see>Model.toString</see>
|
|
43
|
+
/// when stringifying the model for representation.
|
|
44
|
+
///
|
|
45
|
+
/// Note:
|
|
46
|
+
/// This is also an instance method that can be called from
|
|
47
|
+
/// an instance of the type.
|
|
48
|
+
/// Return: string
|
|
49
|
+
/// Return the string value `'NUMERIC'`
|
|
10
50
|
static getDisplayName() {
|
|
11
51
|
return 'NUMERIC';
|
|
12
52
|
}
|
|
@@ -15,6 +55,27 @@ class NumericType extends Type {
|
|
|
15
55
|
return this.constructor.getDisplayName();
|
|
16
56
|
}
|
|
17
57
|
|
|
58
|
+
/// Construct a new `NUMERIC` type.
|
|
59
|
+
///
|
|
60
|
+
/// The `precision` argument specifies the
|
|
61
|
+
/// total number of digits (including decimal places)
|
|
62
|
+
/// allowed for each number. The `scale` argument
|
|
63
|
+
/// specifies the number of digits allowed after the decimal
|
|
64
|
+
/// place. A `scale` of `0` is valid, meaning you want no
|
|
65
|
+
/// digits after the decimal point.
|
|
66
|
+
///
|
|
67
|
+
/// Return: NumericType
|
|
68
|
+
/// Arguments:
|
|
69
|
+
/// precision?: number = 20
|
|
70
|
+
/// The total number of allowed digits in the number. This
|
|
71
|
+
/// includes decimal places. So a number of 123.456 would
|
|
72
|
+
/// need a `precision` of `6`, since it contains six total
|
|
73
|
+
/// digits.
|
|
74
|
+
/// scale?: number = 6
|
|
75
|
+
/// How many digits are allowed after the decimal point.
|
|
76
|
+
/// A value of `2` would allow two digits after the decimal
|
|
77
|
+
/// point. A value of `0` would allow no digits after the decimal
|
|
78
|
+
/// point, essentially turning this into an integer.
|
|
18
79
|
constructor(precision, scale) {
|
|
19
80
|
super(precision, scale);
|
|
20
81
|
|
|
@@ -22,6 +83,22 @@ class NumericType extends Type {
|
|
|
22
83
|
this.scale = scale || DEFAULT_DECIMAL_PLACES;
|
|
23
84
|
}
|
|
24
85
|
|
|
86
|
+
/// Cast provided value to underlying type.
|
|
87
|
+
///
|
|
88
|
+
/// This will cast the incoming value to the
|
|
89
|
+
/// underlying type of this field, a `number`
|
|
90
|
+
/// primitive. A `null` or `undefined`
|
|
91
|
+
/// value will simply be returned.
|
|
92
|
+
///
|
|
93
|
+
/// See <see>Type.castToType</see> for a more
|
|
94
|
+
/// detailed description.
|
|
95
|
+
///
|
|
96
|
+
/// Return: number | null | undefined
|
|
97
|
+
/// Return the incoming `value`, cast to this
|
|
98
|
+
/// type. `null` and `undefined` are simply
|
|
99
|
+
/// returned without casting.
|
|
100
|
+
/// Arguments:
|
|
101
|
+
/// context: <see name="CastToTypeContext">Type.castToType</see>
|
|
25
102
|
castToType({ value }) {
|
|
26
103
|
if (value == null)
|
|
27
104
|
return value;
|
|
@@ -33,10 +110,39 @@ class NumericType extends Type {
|
|
|
33
110
|
return number;
|
|
34
111
|
}
|
|
35
112
|
|
|
113
|
+
/// Check if the provided value is valid.
|
|
114
|
+
///
|
|
115
|
+
/// This will check if the provided value is
|
|
116
|
+
/// a `number` instance, and if it is finite.
|
|
117
|
+
/// If both conditions are true, then it will
|
|
118
|
+
/// return `true`.
|
|
119
|
+
///
|
|
120
|
+
/// Return: boolean
|
|
121
|
+
/// Arguments:
|
|
122
|
+
/// value: any
|
|
123
|
+
/// The value to check.
|
|
36
124
|
isValidValue(value) {
|
|
37
125
|
return (Nife.instanceOf(value, 'number') && isFinite(value));
|
|
38
126
|
}
|
|
39
127
|
|
|
128
|
+
/// Stringify the type itself.
|
|
129
|
+
///
|
|
130
|
+
/// If a `connection` argument is provided, then this
|
|
131
|
+
/// will go through the connection to generate the type
|
|
132
|
+
/// for the underlying database. If no connection is
|
|
133
|
+
/// provided, then a "standard" SQL type will be returned
|
|
134
|
+
/// for this type instead. The "standard" type returned
|
|
135
|
+
/// when no `connection` is provided is `'NUMERIC'`.
|
|
136
|
+
///
|
|
137
|
+
/// Return: string
|
|
138
|
+
/// Arguments:
|
|
139
|
+
/// connection?: <see>Connection</see>
|
|
140
|
+
/// An optional connection. If provided, send this
|
|
141
|
+
/// type through <see>Type.toConnectionType</see>
|
|
142
|
+
/// to have the connection itself generate the underlying
|
|
143
|
+
/// type for the database. If `connection` is not provided,
|
|
144
|
+
/// then this will simply return a "standard" generic matching
|
|
145
|
+
/// SQL type.
|
|
40
146
|
toString(connection) {
|
|
41
147
|
return (connection)
|
|
42
148
|
? this.toConnectionType(connection)
|
|
@@ -80,13 +80,13 @@ function generatePermutations(func, flags) {
|
|
|
80
80
|
return func.apply(this, args);
|
|
81
81
|
}, Object.assign({}, flags, { onUpdate: true }));
|
|
82
82
|
|
|
83
|
-
masterFunc.INSERT = defaultValueFlags(function(...args) {
|
|
84
|
-
|
|
85
|
-
}, Object.assign({}, flags, { onInsert: true }));
|
|
83
|
+
// masterFunc.INSERT = defaultValueFlags(function(...args) {
|
|
84
|
+
// return func.apply(this, args);
|
|
85
|
+
// }, Object.assign({}, flags, { onInsert: true }));
|
|
86
86
|
|
|
87
|
-
masterFunc.ALWAYS = defaultValueFlags(function(...args) {
|
|
88
|
-
|
|
89
|
-
}, Object.assign({}, flags, { onStore: true }));
|
|
87
|
+
// masterFunc.ALWAYS = defaultValueFlags(function(...args) {
|
|
88
|
+
// return func.apply(this, args);
|
|
89
|
+
// }, Object.assign({}, flags, { onStore: true }));
|
|
90
90
|
|
|
91
91
|
return masterFunc;
|
|
92
92
|
}
|
package/lib/types/type.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { checkDefaultValueFlags } = require('./helpers/default-helpers');
|
|
4
4
|
|
|
5
|
+
/// Base type class for all other field types.
|
|
5
6
|
class Type {
|
|
6
7
|
static _isMythixFieldType = true;
|
|
7
8
|
|
|
@@ -221,6 +222,28 @@ class Type {
|
|
|
221
222
|
this._Model = Model;
|
|
222
223
|
}
|
|
223
224
|
|
|
225
|
+
/// Cast a value to the underlying field type.
|
|
226
|
+
///
|
|
227
|
+
/// This method is implemented differently for
|
|
228
|
+
/// every field type. Its job is to cast any
|
|
229
|
+
/// value provided to the type. For many types,
|
|
230
|
+
/// if a `null` or `undefined` value is provided,
|
|
231
|
+
/// then that value will simply be returned.
|
|
232
|
+
///
|
|
233
|
+
/// Interface:
|
|
234
|
+
/// interface CastToTypeContext {
|
|
235
|
+
/// connection: Connection; // The connection of the model.
|
|
236
|
+
/// field: Field; // The field descriptor that has this type.
|
|
237
|
+
/// Model: class Model; // The parent Model class of the field.
|
|
238
|
+
/// self: Model; // The model instance.
|
|
239
|
+
/// value: any; // The value that should be cast.
|
|
240
|
+
/// };
|
|
241
|
+
///
|
|
242
|
+
/// Return: any
|
|
243
|
+
/// Arguments:
|
|
244
|
+
/// context: CastToTypeContext
|
|
245
|
+
/// The "context" passed to `castToType`. This contains
|
|
246
|
+
/// all that any type should need to cast a value.
|
|
224
247
|
castToType({ value }) {
|
|
225
248
|
return value;
|
|
226
249
|
}
|