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.
@@ -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;