mythix-orm 1.5.2 → 1.5.5
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 +20 -2
- package/lib/model.js +2 -2
- package/lib/types/concrete/bigint-type.js +7 -1
- 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/package.json +1 -1
|
@@ -99,8 +99,26 @@ class ConnectionBase extends EventEmitter {
|
|
|
99
99
|
this.registerModels(options.models);
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
getDefaultOrder(
|
|
103
|
-
|
|
102
|
+
getDefaultOrder(Model, options) {
|
|
103
|
+
let order = Model.defaultOrder(options);
|
|
104
|
+
if (!order)
|
|
105
|
+
return;
|
|
106
|
+
|
|
107
|
+
order = Nife.arrayFlatten(Nife.toArray(order)).filter((value) => {
|
|
108
|
+
if (!value)
|
|
109
|
+
return false;
|
|
110
|
+
|
|
111
|
+
if (!Nife.instanceOf(value, 'string'))
|
|
112
|
+
return false;
|
|
113
|
+
|
|
114
|
+
return true;
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
if (Nife.isEmpty(order))
|
|
118
|
+
return;
|
|
119
|
+
|
|
120
|
+
let modelName = Model.getModelName();
|
|
121
|
+
return order.map((value) => ((value.indexOf(':') < 0) ? `${modelName}:${value}` : value));
|
|
104
122
|
}
|
|
105
123
|
|
|
106
124
|
_getFromModelCache(Model, key, defaultValue) {
|
package/lib/model.js
CHANGED
|
@@ -1343,7 +1343,7 @@ class Model {
|
|
|
1343
1343
|
/// query being operated on.
|
|
1344
1344
|
///
|
|
1345
1345
|
/// Note:
|
|
1346
|
-
/// Internally, Mythix ORM will call `this.connection.
|
|
1346
|
+
/// Internally, Mythix ORM will call `this.connection.defaultOrder(options)`,
|
|
1347
1347
|
/// which--if not overloaded--will simply call this method from the
|
|
1348
1348
|
/// model itself.
|
|
1349
1349
|
/// Note:
|
|
@@ -1360,7 +1360,7 @@ class Model {
|
|
|
1360
1360
|
/// Prefix a field name with `+` to specify ASCending order, i.e.
|
|
1361
1361
|
/// `[ "+User:id" ]`. Prefix the field name with `-` to specify
|
|
1362
1362
|
/// DESCending order, i.e. `[ "-User:id" ]`.
|
|
1363
|
-
static
|
|
1363
|
+
static defaultOrder(options) {
|
|
1364
1364
|
}
|
|
1365
1365
|
|
|
1366
1366
|
/// This method is called anytime a `Model.where` or `Model.$`
|
|
@@ -54,6 +54,9 @@ class BigIntType extends Type {
|
|
|
54
54
|
/// This method is called from <see>Model.toString</see>
|
|
55
55
|
/// when stringifying the model for representation.
|
|
56
56
|
///
|
|
57
|
+
/// Note:
|
|
58
|
+
/// This is also an instance method that can be called from
|
|
59
|
+
/// an instance of the type.
|
|
57
60
|
/// Return: string
|
|
58
61
|
/// Return the string value `'BIGINT'`
|
|
59
62
|
static getDisplayName() {
|
|
@@ -155,7 +158,10 @@ class BigIntType extends Type {
|
|
|
155
158
|
/// will go through the connection to generate the type
|
|
156
159
|
/// for the underlying database. If no connection is
|
|
157
160
|
/// provided, then a "standard" SQL type will be returned
|
|
158
|
-
/// for this type instead.
|
|
161
|
+
/// for this type instead. The "standard" type returned
|
|
162
|
+
/// when no `connection` is provided is `'BIGINT'` or
|
|
163
|
+
/// `'BIGINT(${this.length})'` if a `length` is provided to
|
|
164
|
+
/// the type.
|
|
159
165
|
///
|
|
160
166
|
/// Return: string
|
|
161
167
|
/// Arguments:
|
|
@@ -4,7 +4,36 @@
|
|
|
4
4
|
|
|
5
5
|
const Type = require('../type');
|
|
6
6
|
|
|
7
|
+
/// `BLOB` type.
|
|
8
|
+
///
|
|
9
|
+
/// This represents a "blob", or random binary data.
|
|
10
|
+
///
|
|
11
|
+
/// BlobType expects that the value you provide to the
|
|
12
|
+
/// field will be a `Buffer` instance. If it isn't it
|
|
13
|
+
/// will throw an error when `castToType` is called.
|
|
14
|
+
/// `null` and `undefined` are also acceptable values
|
|
15
|
+
/// for a BlobType field.
|
|
16
|
+
///
|
|
17
|
+
/// Example:
|
|
18
|
+
/// class Blobs extends Model {
|
|
19
|
+
/// static fields = {
|
|
20
|
+
/// blob1: Types.BLOB(1024),
|
|
21
|
+
/// blob2: new Types.BlobType(1024),
|
|
22
|
+
/// };
|
|
23
|
+
/// }
|
|
24
|
+
///
|
|
25
|
+
/// See: Type
|
|
7
26
|
class BlobType extends Type {
|
|
27
|
+
/// Get the "display" name for this type.
|
|
28
|
+
///
|
|
29
|
+
/// This method is called from <see>Model.toString</see>
|
|
30
|
+
/// when stringifying the model for representation.
|
|
31
|
+
///
|
|
32
|
+
/// Note:
|
|
33
|
+
/// This is also an instance method that can be called from
|
|
34
|
+
/// an instance of the type.
|
|
35
|
+
/// Return: string
|
|
36
|
+
/// Return the string value `'BLOB'`
|
|
8
37
|
static getDisplayName() {
|
|
9
38
|
return 'BLOB';
|
|
10
39
|
}
|
|
@@ -13,12 +42,40 @@ class BlobType extends Type {
|
|
|
13
42
|
return this.constructor.getDisplayName();
|
|
14
43
|
}
|
|
15
44
|
|
|
45
|
+
/// Construct a new `BLOB` type.
|
|
46
|
+
///
|
|
47
|
+
/// The `length` argument is the number
|
|
48
|
+
/// of bytes to use in the database to
|
|
49
|
+
/// store the data.
|
|
50
|
+
///
|
|
51
|
+
/// Return: BlobType
|
|
52
|
+
/// Arguments:
|
|
53
|
+
/// length?: number
|
|
54
|
+
/// How many bytes to use in the underlying database to store the value.
|
|
16
55
|
constructor(length) {
|
|
17
56
|
super(length);
|
|
18
57
|
|
|
19
58
|
this.length = length || null;
|
|
20
59
|
}
|
|
21
60
|
|
|
61
|
+
/// Cast provided value to underlying type.
|
|
62
|
+
///
|
|
63
|
+
/// Unlike most type casters, this doesn't
|
|
64
|
+
/// do any casting. You must provide either
|
|
65
|
+
/// a `Buffer`, `null`, or `undefined` as
|
|
66
|
+
/// the value to your blob field. If any
|
|
67
|
+
/// other type of value is provided then
|
|
68
|
+
/// an exception will be thrown.
|
|
69
|
+
///
|
|
70
|
+
/// See <see>Type.castToType</see> for a more
|
|
71
|
+
/// detailed description.
|
|
72
|
+
///
|
|
73
|
+
/// Return: Buffer | null | undefined
|
|
74
|
+
/// Return the incoming `value`, cast to this
|
|
75
|
+
/// type. `null` and `undefined` are simply
|
|
76
|
+
/// returned without casting.
|
|
77
|
+
/// Arguments:
|
|
78
|
+
/// context: <see name="CastToTypeContext">Type.castToType</see>
|
|
22
79
|
castToType({ value }) {
|
|
23
80
|
if (value == null)
|
|
24
81
|
return value;
|
|
@@ -29,10 +86,38 @@ class BlobType extends Type {
|
|
|
29
86
|
return value;
|
|
30
87
|
}
|
|
31
88
|
|
|
89
|
+
/// Check if the provided value is valid.
|
|
90
|
+
///
|
|
91
|
+
/// This will check if the provided value is
|
|
92
|
+
/// a `Buffer`. If it is, it will return `true`,
|
|
93
|
+
/// otherwise it will return `false`.
|
|
94
|
+
///
|
|
95
|
+
/// Return: boolean
|
|
96
|
+
/// Arguments:
|
|
97
|
+
/// value: any
|
|
98
|
+
/// The value to check.
|
|
32
99
|
isValidValue(value) {
|
|
33
100
|
return Buffer.isBuffer(value);
|
|
34
101
|
}
|
|
35
102
|
|
|
103
|
+
/// Stringify the type itself.
|
|
104
|
+
///
|
|
105
|
+
/// If a `connection` argument is provided, then this
|
|
106
|
+
/// will go through the connection to generate the type
|
|
107
|
+
/// for the underlying database. If no connection is
|
|
108
|
+
/// provided, then a "standard" SQL type will be returned
|
|
109
|
+
/// for this type instead. The "standard" type returned
|
|
110
|
+
/// when no `connection` is provided is `'BLOB'`.
|
|
111
|
+
///
|
|
112
|
+
/// Return: string
|
|
113
|
+
/// Arguments:
|
|
114
|
+
/// connection?: <see>Connection</see>
|
|
115
|
+
/// An optional connection. If provided, send this
|
|
116
|
+
/// type through <see>Type.toConnectionType</see>
|
|
117
|
+
/// to have the connection itself generate the underlying
|
|
118
|
+
/// type for the database. If `connection` is not provided,
|
|
119
|
+
/// then this will simply return a "standard" generic matching
|
|
120
|
+
/// SQL type.
|
|
36
121
|
toString(connection) {
|
|
37
122
|
return (connection)
|
|
38
123
|
? this.toConnectionType(connection)
|
|
@@ -2,7 +2,30 @@
|
|
|
2
2
|
|
|
3
3
|
const Type = require('../type');
|
|
4
4
|
|
|
5
|
+
/// `BOOLEAN` type.
|
|
6
|
+
///
|
|
7
|
+
/// This represents a "boolean" type for the underlying database.
|
|
8
|
+
///
|
|
9
|
+
/// Example:
|
|
10
|
+
/// class Booleans extends Model {
|
|
11
|
+
/// static fields = {
|
|
12
|
+
/// boolean1: Types.BOOLEAN,
|
|
13
|
+
/// boolean2: new Types.BooleanType(),
|
|
14
|
+
/// };
|
|
15
|
+
/// }
|
|
16
|
+
///
|
|
17
|
+
/// See: Type
|
|
5
18
|
class BooleanType extends Type {
|
|
19
|
+
/// Get the "display" name for this type.
|
|
20
|
+
///
|
|
21
|
+
/// This method is called from <see>Model.toString</see>
|
|
22
|
+
/// when stringifying the model for representation.
|
|
23
|
+
///
|
|
24
|
+
/// Note:
|
|
25
|
+
/// This is also an instance method that can be called from
|
|
26
|
+
/// an instance of the type.
|
|
27
|
+
/// Return: string
|
|
28
|
+
/// Return the string value `'BOOLEAN'`
|
|
6
29
|
static getDisplayName() {
|
|
7
30
|
return 'BOOLEAN';
|
|
8
31
|
}
|
|
@@ -11,23 +34,73 @@ class BooleanType extends Type {
|
|
|
11
34
|
return this.constructor.getDisplayName();
|
|
12
35
|
}
|
|
13
36
|
|
|
37
|
+
/// Cast provided value to underlying type.
|
|
38
|
+
///
|
|
39
|
+
/// This will cast the incoming value to the
|
|
40
|
+
/// underlying type of this field, a `boolean`
|
|
41
|
+
/// primitive. It has some special edge-cases
|
|
42
|
+
/// to be aware of. If a string value matching
|
|
43
|
+
/// `'true'` or `'TRUE'` is given, then this
|
|
44
|
+
/// will result in `true`. If a value matching
|
|
45
|
+
/// `'false'` or `'FALSE'` is provided, then
|
|
46
|
+
/// the result will be `false`. Otherwise,
|
|
47
|
+
/// the result of `!!value` is returned.
|
|
48
|
+
///
|
|
49
|
+
/// See <see>Type.castToType</see> for a more
|
|
50
|
+
/// detailed description.
|
|
51
|
+
///
|
|
52
|
+
/// Return: boolean | null | undefined
|
|
53
|
+
/// Return the incoming `value`, cast to this
|
|
54
|
+
/// type. `null` and `undefined` are simply
|
|
55
|
+
/// returned without casting.
|
|
56
|
+
/// Arguments:
|
|
57
|
+
/// context: <see name="CastToTypeContext">Type.castToType</see>
|
|
14
58
|
castToType({ value }) {
|
|
15
59
|
if (value == null)
|
|
16
60
|
return value;
|
|
17
61
|
|
|
18
|
-
if (value === 'true')
|
|
62
|
+
if (value === 'true' || value === 'TRUE')
|
|
19
63
|
return true;
|
|
20
64
|
|
|
21
|
-
if (value === 'false')
|
|
65
|
+
if (value === 'false' || value === 'FALSE')
|
|
22
66
|
return false;
|
|
23
67
|
|
|
24
68
|
return !!value;
|
|
25
69
|
}
|
|
26
70
|
|
|
71
|
+
/// Check if the provided value is valid.
|
|
72
|
+
///
|
|
73
|
+
/// This will check if the provided value is
|
|
74
|
+
/// either `true` or `false` exactly, as a primitive.
|
|
75
|
+
/// If `typeof value === 'boolean'` then this method
|
|
76
|
+
/// will return `true`, otherwise it will return `false`.
|
|
77
|
+
///
|
|
78
|
+
/// Return: boolean
|
|
79
|
+
/// Arguments:
|
|
80
|
+
/// value: any
|
|
81
|
+
/// The value to check.
|
|
27
82
|
isValidValue(value) {
|
|
28
83
|
return (value === true || value === false);
|
|
29
84
|
}
|
|
30
85
|
|
|
86
|
+
/// Stringify the type itself.
|
|
87
|
+
///
|
|
88
|
+
/// If a `connection` argument is provided, then this
|
|
89
|
+
/// will go through the connection to generate the type
|
|
90
|
+
/// for the underlying database. If no connection is
|
|
91
|
+
/// provided, then a "standard" SQL type will be returned
|
|
92
|
+
/// for this type instead. The "standard" type returned
|
|
93
|
+
/// when no `connection` is provided is `'BOOLEAN'`.
|
|
94
|
+
///
|
|
95
|
+
/// Return: string
|
|
96
|
+
/// Arguments:
|
|
97
|
+
/// connection?: <see>Connection</see>
|
|
98
|
+
/// An optional connection. If provided, send this
|
|
99
|
+
/// type through <see>Type.toConnectionType</see>
|
|
100
|
+
/// to have the connection itself generate the underlying
|
|
101
|
+
/// type for the database. If `connection` is not provided,
|
|
102
|
+
/// then this will simply return a "standard" generic matching
|
|
103
|
+
/// SQL type.
|
|
31
104
|
toString(connection) {
|
|
32
105
|
return (connection)
|
|
33
106
|
? this.toConnectionType(connection)
|
|
@@ -3,7 +3,31 @@
|
|
|
3
3
|
const Nife = require('nife');
|
|
4
4
|
const Type = require('../type');
|
|
5
5
|
|
|
6
|
+
/// `CHAR` type.
|
|
7
|
+
///
|
|
8
|
+
/// This represents a "char" type for the underlying database,
|
|
9
|
+
/// which is a single character.
|
|
10
|
+
///
|
|
11
|
+
/// Example:
|
|
12
|
+
/// class Booleans extends Model {
|
|
13
|
+
/// static fields = {
|
|
14
|
+
/// char1: Types.CHAR,
|
|
15
|
+
/// char2: new Types.CharType(),
|
|
16
|
+
/// };
|
|
17
|
+
/// }
|
|
18
|
+
///
|
|
19
|
+
/// See: Type
|
|
6
20
|
class CharType extends Type {
|
|
21
|
+
/// Get the "display" name for this type.
|
|
22
|
+
///
|
|
23
|
+
/// This method is called from <see>Model.toString</see>
|
|
24
|
+
/// when stringifying the model for representation.
|
|
25
|
+
///
|
|
26
|
+
/// Note:
|
|
27
|
+
/// This is also an instance method that can be called from
|
|
28
|
+
/// an instance of the type.
|
|
29
|
+
/// Return: string
|
|
30
|
+
/// Return the string value `'CHAR'`
|
|
7
31
|
static getDisplayName() {
|
|
8
32
|
return 'CHAR';
|
|
9
33
|
}
|
|
@@ -12,6 +36,25 @@ class CharType extends Type {
|
|
|
12
36
|
return this.constructor.getDisplayName();
|
|
13
37
|
}
|
|
14
38
|
|
|
39
|
+
/// Cast provided value to underlying type.
|
|
40
|
+
///
|
|
41
|
+
/// This will cast the incoming value to the
|
|
42
|
+
/// underlying type of this field, a `string`
|
|
43
|
+
/// primitive with a length of one character.
|
|
44
|
+
/// If the provided value is not a `string` or
|
|
45
|
+
/// a `String`, or if the string provided has
|
|
46
|
+
/// a character length greater than one, then
|
|
47
|
+
/// an exception will be thrown.
|
|
48
|
+
///
|
|
49
|
+
/// See <see>Type.castToType</see> for a more
|
|
50
|
+
/// detailed description.
|
|
51
|
+
///
|
|
52
|
+
/// Return: string | null | undefined
|
|
53
|
+
/// Return the incoming `value`, cast to this
|
|
54
|
+
/// type. `null` and `undefined` are simply
|
|
55
|
+
/// returned without casting.
|
|
56
|
+
/// Arguments:
|
|
57
|
+
/// context: <see name="CastToTypeContext">Type.castToType</see>
|
|
15
58
|
castToType({ value }) {
|
|
16
59
|
if (value == null)
|
|
17
60
|
return value;
|
|
@@ -22,10 +65,39 @@ class CharType extends Type {
|
|
|
22
65
|
return value.charAt(0);
|
|
23
66
|
}
|
|
24
67
|
|
|
68
|
+
/// Check if the provided value is valid.
|
|
69
|
+
///
|
|
70
|
+
/// This will check if the provided value is
|
|
71
|
+
/// a `string` or a `String` with a length
|
|
72
|
+
/// of one character. If it is, then this will
|
|
73
|
+
/// return `true`, otherwise it will return `false`.
|
|
74
|
+
///
|
|
75
|
+
/// Return: boolean
|
|
76
|
+
/// Arguments:
|
|
77
|
+
/// value: any
|
|
78
|
+
/// The value to check.
|
|
25
79
|
isValidValue(value) {
|
|
26
80
|
return (Nife.instanceOf(value, 'string') && value.length === 1);
|
|
27
81
|
}
|
|
28
82
|
|
|
83
|
+
/// Stringify the type itself.
|
|
84
|
+
///
|
|
85
|
+
/// If a `connection` argument is provided, then this
|
|
86
|
+
/// will go through the connection to generate the type
|
|
87
|
+
/// for the underlying database. If no connection is
|
|
88
|
+
/// provided, then a "standard" SQL type will be returned
|
|
89
|
+
/// for this type instead. The "standard" type returned
|
|
90
|
+
/// when no `connection` is provided is `'CHAR'`.
|
|
91
|
+
///
|
|
92
|
+
/// Return: string
|
|
93
|
+
/// Arguments:
|
|
94
|
+
/// connection?: <see>Connection</see>
|
|
95
|
+
/// An optional connection. If provided, send this
|
|
96
|
+
/// type through <see>Type.toConnectionType</see>
|
|
97
|
+
/// to have the connection itself generate the underlying
|
|
98
|
+
/// type for the database. If `connection` is not provided,
|
|
99
|
+
/// then this will simply return a "standard" generic matching
|
|
100
|
+
/// SQL type.
|
|
29
101
|
toString(connection) {
|
|
30
102
|
return (connection)
|
|
31
103
|
? this.toConnectionType(connection)
|
|
@@ -7,11 +7,56 @@ const { DATE_NOW } = require('../helpers/default-helpers');
|
|
|
7
7
|
|
|
8
8
|
moment.suppressDeprecationWarnings = true;
|
|
9
9
|
|
|
10
|
+
/// `DATE` type.
|
|
11
|
+
///
|
|
12
|
+
/// This represents a "date", a date without 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 Dates extends Model {
|
|
26
|
+
/// static fields = {
|
|
27
|
+
/// date1: Types.DATE('YYYY-MM-DD'),
|
|
28
|
+
/// date2: new Types.DateType(),
|
|
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.DATE.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.DATE.Default.NOW` = Remote database 'NOW' time, set on insert only.<br>
|
|
40
|
+
/// 2. `Types.DATE.Default.NOW.UPDATE` = Remote database 'NOW' time, set on insert and update.<br>
|
|
41
|
+
/// 3. `Types.DATE.Default.NOW.LOCAL` = Local (client-side) 'NOW' time, set on insert only.<br>
|
|
42
|
+
/// 4. `Types.DATE.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 DateType extends Type {
|
|
11
46
|
static Default = {
|
|
12
47
|
NOW: DATE_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 `'DATE'`
|
|
15
60
|
static getDisplayName() {
|
|
16
61
|
return 'DATE';
|
|
17
62
|
}
|
|
@@ -20,12 +65,52 @@ class DateType extends Type {
|
|
|
20
65
|
return this.constructor.getDisplayName();
|
|
21
66
|
}
|
|
22
67
|
|
|
68
|
+
/// Construct a new `DATE` 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 `DATE` 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
|
+
/// Return: DateType
|
|
82
|
+
/// Arguments:
|
|
83
|
+
/// format?: string
|
|
84
|
+
/// Specify the client-side formatting for this date field.
|
|
85
|
+
/// This will be used for parsing date strings, and for serializing
|
|
86
|
+
/// the field.
|
|
23
87
|
constructor(format) {
|
|
24
88
|
super(format);
|
|
25
89
|
|
|
26
90
|
this.format = format || 'YYYY-MM-DD';
|
|
27
91
|
}
|
|
28
92
|
|
|
93
|
+
/// Cast provided value to underlying type.
|
|
94
|
+
///
|
|
95
|
+
/// This will cast the incoming value to the
|
|
96
|
+
/// underlying type of this field, a [moment](https://momentjs.com/docs/)
|
|
97
|
+
/// instance. If the provided value results in
|
|
98
|
+
/// an invalid date, then an exception will be thrown.
|
|
99
|
+
/// Once a valid [moment](https://momentjs.com/docs/) instance
|
|
100
|
+
/// is constructed from the value provided,
|
|
101
|
+
/// `dateTime.startOf('day')` is returned, forcing
|
|
102
|
+
/// this `DATE` type to zero its time (start at the
|
|
103
|
+
/// beginning of the date specified).
|
|
104
|
+
///
|
|
105
|
+
/// See <see>Type.castToType</see> for a more
|
|
106
|
+
/// detailed description.
|
|
107
|
+
///
|
|
108
|
+
/// Return: moment | null | undefined
|
|
109
|
+
/// Return the incoming `value`, cast to this
|
|
110
|
+
/// type. `null` and `undefined` are simply
|
|
111
|
+
/// returned without casting.
|
|
112
|
+
/// Arguments:
|
|
113
|
+
/// context: <see name="CastToTypeContext">Type.castToType</see>
|
|
29
114
|
castToType({ value, connection }) {
|
|
30
115
|
if (value == null)
|
|
31
116
|
return value;
|
|
@@ -37,16 +122,71 @@ class DateType extends Type {
|
|
|
37
122
|
return dateTime.startOf('day');
|
|
38
123
|
}
|
|
39
124
|
|
|
125
|
+
/// Check if the provided value is valid.
|
|
126
|
+
///
|
|
127
|
+
/// This will check if the provided value is
|
|
128
|
+
/// a valid date. It does so by calling
|
|
129
|
+
/// `moment(value).isValid()`. If this check
|
|
130
|
+
/// results in `true`, then this method will
|
|
131
|
+
/// return `true`, otherwise it will return `false`.
|
|
132
|
+
///
|
|
133
|
+
/// See the [moment](https://momentjs.com/docs/) library documentation for more information.
|
|
134
|
+
///
|
|
135
|
+
/// Return: boolean
|
|
136
|
+
/// Arguments:
|
|
137
|
+
/// value: any
|
|
138
|
+
/// The value to check.
|
|
40
139
|
isValidValue(value) {
|
|
41
140
|
return moment(value).isValid();
|
|
42
141
|
}
|
|
43
142
|
|
|
143
|
+
/// Stringify the type itself.
|
|
144
|
+
///
|
|
145
|
+
/// If a `connection` argument is provided, then this
|
|
146
|
+
/// will go through the connection to generate the type
|
|
147
|
+
/// for the underlying database. If no connection is
|
|
148
|
+
/// provided, then a "standard" SQL type will be returned
|
|
149
|
+
/// for this type instead. The "standard" type returned
|
|
150
|
+
/// when no `connection` is provided is `'DATE'`.
|
|
151
|
+
///
|
|
152
|
+
/// Return: string
|
|
153
|
+
/// Arguments:
|
|
154
|
+
/// connection?: <see>Connection</see>
|
|
155
|
+
/// An optional connection. If provided, send this
|
|
156
|
+
/// type through <see>Type.toConnectionType</see>
|
|
157
|
+
/// to have the connection itself generate the underlying
|
|
158
|
+
/// type for the database. If `connection` is not provided,
|
|
159
|
+
/// then this will simply return a "standard" generic matching
|
|
160
|
+
/// SQL type.
|
|
44
161
|
toString(connection) {
|
|
45
162
|
return (connection)
|
|
46
163
|
? this.toConnectionType(connection)
|
|
47
164
|
: 'DATE';
|
|
48
165
|
}
|
|
49
166
|
|
|
167
|
+
/// Serialize the field value.
|
|
168
|
+
///
|
|
169
|
+
/// This will be called whenever the field's value
|
|
170
|
+
/// needs to be serialized. If a `connection` argument
|
|
171
|
+
/// is provided, then this method will assume that the
|
|
172
|
+
/// connection is serializing it for storage in the database.
|
|
173
|
+
/// In this case, <see>connection.convertDateToDBTime</see> is
|
|
174
|
+
/// called and provided the `value` for the connection to turn
|
|
175
|
+
/// the date into a proper value for the underlying database.
|
|
176
|
+
///
|
|
177
|
+
/// If no `connection` is provided, then the date will be serialized
|
|
178
|
+
/// according to the provided `format` specified by the user. If
|
|
179
|
+
/// no format was specified by the user, then it will be serialized
|
|
180
|
+
/// to [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format.
|
|
181
|
+
///
|
|
182
|
+
/// Return: string | null | undefined
|
|
183
|
+
/// Arguments:
|
|
184
|
+
/// value: moment
|
|
185
|
+
/// The date value to serialize.
|
|
186
|
+
/// connection?: <see>Connection</see>
|
|
187
|
+
/// A connection instance, which if provided, will
|
|
188
|
+
/// proxy serialization to the underlying database connection
|
|
189
|
+
/// via the <see>connection.convertDateToDBTime</see> method.
|
|
50
190
|
serialize(value, connection) {
|
|
51
191
|
if (value == null)
|
|
52
192
|
return (connection) ? null : value;
|
|
@@ -60,6 +200,34 @@ class DateType extends Type {
|
|
|
60
200
|
return value.toISOString();
|
|
61
201
|
}
|
|
62
202
|
|
|
203
|
+
/// Deserialize the field value.
|
|
204
|
+
///
|
|
205
|
+
/// This method is used to deserialize a provided
|
|
206
|
+
/// field value. If the value provided is a `moment`
|
|
207
|
+
/// instance, then simply return it. Otherwise,
|
|
208
|
+
/// if a string is provided, convert it to a `moment`
|
|
209
|
+
/// instance, using the `format` provided (if any).
|
|
210
|
+
///
|
|
211
|
+
/// The `connection` argument is not used by this
|
|
212
|
+
/// method, but is provided if the user wishes to
|
|
213
|
+
/// overload this method.
|
|
214
|
+
///
|
|
215
|
+
/// If `null` or `undefined` are provided as a value
|
|
216
|
+
/// then that value will simply be returned as-is.
|
|
217
|
+
///
|
|
218
|
+
/// Note:
|
|
219
|
+
/// This method is called by `castToType` to cast
|
|
220
|
+
/// incoming values into `moment` instances.
|
|
221
|
+
/// Return: moment
|
|
222
|
+
/// Arguments:
|
|
223
|
+
/// value: string | moment | null | undefined
|
|
224
|
+
/// The value to deserialize.
|
|
225
|
+
/// connection?: <see>Connection</see>
|
|
226
|
+
/// An optional connection that might be provided
|
|
227
|
+
/// depending on how and where this method is called from.
|
|
228
|
+
/// This method does nothing with the `connection`. It is
|
|
229
|
+
/// simply provided for if the user wishes to overload this
|
|
230
|
+
/// method.
|
|
63
231
|
// eslint-disable-next-line no-unused-vars
|
|
64
232
|
deserialize(_value, connection) {
|
|
65
233
|
let value = _value;
|
|
@@ -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
|
}
|