mythix-orm 1.7.2 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,17 +5,74 @@ const Type = require('../type');
5
5
 
6
6
  const DEFAULT_STRING_LENGTH = 256;
7
7
 
8
+ /// `STRING` type.
9
+ ///
10
+ /// This represents a "string" of any size for the
11
+ /// underlying database driver, usually a "VARCHAR" type.
12
+ ///
13
+ /// If no "length" is specified when you create the type,
14
+ /// then a default length of `256` is assumed.
15
+ ///
16
+ /// Example:
17
+ /// class Strings extends Model {
18
+ /// static fields = {
19
+ /// string1: Types.STRING(16),
20
+ /// string2: Types.STRING, // default length = 256
21
+ /// string3: new Types.StringType(64),
22
+ /// };
23
+ /// }
24
+ ///
25
+ /// See: Type
8
26
  class StringType 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
+ ///
36
+ /// Return: string
37
+ /// Return the string value `'STRING'`
9
38
  static getDisplayName() {
10
39
  return 'STRING';
11
40
  }
12
41
 
42
+ /// Construct a new `STRING` type.
43
+ ///
44
+ /// The `length` argument specifies
45
+ /// the number of characters to use for this string
46
+ /// in the database.
47
+ ///
48
+ /// Return: StringType
49
+ ///
50
+ /// Arguments:
51
+ /// length?: number
52
+ /// How many characters to use in the underlying database to store the value.
13
53
  constructor(length) {
14
54
  super(length);
15
55
 
16
56
  this.length = length || DEFAULT_STRING_LENGTH;
17
57
  }
18
58
 
59
+ /// Cast provided value to underlying type.
60
+ ///
61
+ /// This will cast the incoming value to the
62
+ /// underlying type of this field, a `string`
63
+ /// primitive. A `null` or `undefined`
64
+ /// value will simply be returned.
65
+ ///
66
+ /// See <see>Type.castToType</see> for a more
67
+ /// detailed description.
68
+ ///
69
+ /// Return: string | null | undefined
70
+ /// Return the incoming `value`, cast to this
71
+ /// type. `null` and `undefined` are simply
72
+ /// returned without casting.
73
+ ///
74
+ /// Arguments:
75
+ /// context: <see name="CastToTypeContext">Type.castToType</see>
19
76
  castToType({ value }) {
20
77
  if (value == null)
21
78
  return value;
@@ -23,10 +80,41 @@ class StringType extends Type {
23
80
  return ('' + value);
24
81
  }
25
82
 
83
+ /// Check if the provided value is valid.
84
+ ///
85
+ /// This will check if the provided value is
86
+ /// a `string` (or `String`) instance.
87
+ /// Both an empty string, and a string that is nothing
88
+ /// except whitespace are considered "valid".
89
+ ///
90
+ /// Return: boolean
91
+ ///
92
+ /// Arguments:
93
+ /// value: any
94
+ /// The value to check.
26
95
  isValidValue(value) {
27
96
  return Nife.instanceOf(value, 'string');
28
97
  }
29
98
 
99
+ /// Stringify the type itself.
100
+ ///
101
+ /// If a `connection` argument is provided, then this
102
+ /// will go through the connection to generate the type
103
+ /// for the underlying database. If no connection is
104
+ /// provided, then a "standard" SQL type will be returned
105
+ /// for this type instead. The "standard" type returned
106
+ /// when no `connection` is provided is `'VARCHAR'`.
107
+ ///
108
+ /// Return: string
109
+ ///
110
+ /// Arguments:
111
+ /// connection?: <see>Connection</see>
112
+ /// An optional connection. If provided, send this
113
+ /// type through <see>Type.toConnectionType</see>
114
+ /// to have the connection itself generate the underlying
115
+ /// type for the database. If `connection` is not provided,
116
+ /// then this will simply return a "standard" generic matching
117
+ /// SQL type.
30
118
  toString(connection) {
31
119
  return (connection)
32
120
  ? this.toConnectionType(connection)
@@ -3,19 +3,83 @@
3
3
  const Nife = require('nife');
4
4
  const Type = require('../type');
5
5
 
6
- const DEFAULT_STRING_LENGTH = 65565;
6
+ const DEFAULT_TEXT_LENGTH = 65565;
7
7
 
8
+ /// `TEXT` type.
9
+ ///
10
+ /// This represents a "text" of any size for the
11
+ /// underlying database driver, usually a "TEXT" type.
12
+ ///
13
+ /// If no "length" is specified when you create the type,
14
+ /// then a default length of `65565` is assumed.
15
+ ///
16
+ /// In most databases, a "TEXT" type is used for storing
17
+ /// large text blobs. Most databases will store this type
18
+ /// outside of the engine in a separate file space, and
19
+ /// so this type can be a little slower than other types,
20
+ /// however the tradeoff is that you can store a large
21
+ /// amount of text.
22
+ ///
23
+ /// Example:
24
+ /// class TextBlobs extends Model {
25
+ /// static fields = {
26
+ /// text1: Types.TEXT(65535),
27
+ /// text2: Types.TEXT, // default length = 65535
28
+ /// text3: new Types.TextType(65535),
29
+ /// };
30
+ /// }
31
+ ///
32
+ /// See: Type
8
33
  class TextType extends Type {
34
+ /// Get the "display" name for this type.
35
+ ///
36
+ /// This method is called from <see>Model.toString</see>
37
+ /// when stringifying the model for representation.
38
+ ///
39
+ /// Note:
40
+ /// This is also an instance method that can be called from
41
+ /// an instance of the type.
42
+ ///
43
+ /// Return: string
44
+ /// Return the string value `'TEXT'`
9
45
  static getDisplayName() {
10
46
  return 'TEXT';
11
47
  }
12
48
 
49
+ /// Construct a new `TEXT` type.
50
+ ///
51
+ /// The `length` argument specifies
52
+ /// the number of characters to use for
53
+ /// this string in the database.
54
+ ///
55
+ /// Return: StringType
56
+ ///
57
+ /// Arguments:
58
+ /// length?: number
59
+ /// How many characters to use in the underlying database to store the value.
13
60
  constructor(length) {
14
61
  super(length);
15
62
 
16
- this.length = length || DEFAULT_STRING_LENGTH;
63
+ this.length = length || DEFAULT_TEXT_LENGTH;
17
64
  }
18
65
 
66
+ /// Cast provided value to underlying type.
67
+ ///
68
+ /// This will cast the incoming value to the
69
+ /// underlying type of this field, a `string`
70
+ /// primitive. A `null` or `undefined`
71
+ /// value will simply be returned.
72
+ ///
73
+ /// See <see>Type.castToType</see> for a more
74
+ /// detailed description.
75
+ ///
76
+ /// Return: string | null | undefined
77
+ /// Return the incoming `value`, cast to this
78
+ /// type. `null` and `undefined` are simply
79
+ /// returned without casting.
80
+ ///
81
+ /// Arguments:
82
+ /// context: <see name="CastToTypeContext">Type.castToType</see>
19
83
  castToType({ value }) {
20
84
  if (value == null)
21
85
  return value;
@@ -23,10 +87,41 @@ class TextType extends Type {
23
87
  return ('' + value);
24
88
  }
25
89
 
90
+ /// Check if the provided value is valid.
91
+ ///
92
+ /// This will check if the provided value is
93
+ /// a `string` (or `String`) instance.
94
+ /// Both an empty string, and a string that is nothing
95
+ /// except whitespace are considered "valid".
96
+ ///
97
+ /// Return: boolean
98
+ ///
99
+ /// Arguments:
100
+ /// value: any
101
+ /// The value to check.
26
102
  isValidValue(value) {
27
103
  return Nife.instanceOf(value, 'string');
28
104
  }
29
105
 
106
+ /// Stringify the type itself.
107
+ ///
108
+ /// If a `connection` argument is provided, then this
109
+ /// will go through the connection to generate the type
110
+ /// for the underlying database. If no connection is
111
+ /// provided, then a "standard" SQL type will be returned
112
+ /// for this type instead. The "standard" type returned
113
+ /// when no `connection` is provided is `'TEXT'`.
114
+ ///
115
+ /// Return: string
116
+ ///
117
+ /// Arguments:
118
+ /// connection?: <see>Connection</see>
119
+ /// An optional connection. If provided, send this
120
+ /// type through <see>Type.toConnectionType</see>
121
+ /// to have the connection itself generate the underlying
122
+ /// type for the database. If `connection` is not provided,
123
+ /// then this will simply return a "standard" generic matching
124
+ /// SQL type.
30
125
  toString(connection) {
31
126
  return (connection)
32
127
  ? this.toConnectionType(connection)
@@ -7,6 +7,55 @@ const { defaultValueFlags } = require('../helpers/default-helpers');
7
7
 
8
8
  let CALLABLE_PROP_NAMES = [ 'node', 'clockseq', 'msecs', 'nsecs', 'random', 'buffer', 'offset' ];
9
9
 
10
+ /// `UUID` (version 1) type.
11
+ ///
12
+ /// This represents a string based UUID,
13
+ /// using UUID version 1. The underlying
14
+ /// database type is the same as <see>StringType</see>,
15
+ /// which is usually a "VARCHAR" type.
16
+ ///
17
+ /// An optional "prefix" can be specified for this
18
+ /// type, which will allow you to prefix your ids.
19
+ ///
20
+ /// This type will automatically decide its own "length"
21
+ /// based on the length of a UUIDV1, plus the length of
22
+ /// any prefix you provide.
23
+ ///
24
+ /// See the [uuid](https://www.npmjs.com/package/uuid)
25
+ /// module to properly understand the options for this
26
+ /// field type.
27
+ ///
28
+ /// Example:
29
+ /// class UUIDs extends Model {
30
+ /// static fields = {
31
+ /// uuid1: Types.UUIDV1({
32
+ /// prefix: 'USER_',
33
+ /// node: ...,
34
+ /// clockseq: ...,
35
+ /// msecs: ...,
36
+ /// nsecs: ...,
37
+ /// random: ...,
38
+ /// rng: ...,
39
+ /// buffer: ...,
40
+ /// offset: ...,
41
+ /// }),
42
+ /// uuid2: new Types.UUIDV1Type({
43
+ /// prefix: 'USER_',
44
+ /// ...,
45
+ /// }),
46
+ /// uuidWithDefault: {
47
+ /// type: Types.UUIDV1({ ... }),
48
+ /// defaultValue: Types.UUIDV1.Default.UUIDV1,
49
+ /// },
50
+ /// };
51
+ /// }
52
+ ///
53
+ /// Properties:
54
+ /// Default: object = { UUIDV1 }
55
+ /// `UUIDV1` is a method that can be used as the `defaultValue` of a <see>Field</see>
56
+ /// to have this field auto-generate UUIDs for new models.
57
+ ///
58
+ /// See: Type
10
59
  class UUIDV1Type extends UUIDBaseType {
11
60
  static Default = {
12
61
  UUIDV1: defaultValueFlags(function(context) {
@@ -23,10 +72,35 @@ class UUIDV1Type extends UUIDBaseType {
23
72
  }),
24
73
  };
25
74
 
75
+ /// Get the "display" name for this type.
76
+ ///
77
+ /// This method is called from <see>Model.toString</see>
78
+ /// when stringifying the model for representation.
79
+ ///
80
+ /// Note:
81
+ /// This is also an instance method that can be called from
82
+ /// an instance of the type.
83
+ ///
84
+ /// Return: string
85
+ /// Return the string value `'UUIDV1'`
26
86
  static getDisplayName() {
27
87
  return 'UUIDV1';
28
88
  }
29
89
 
90
+ /// This is an internal method that is used by the
91
+ /// type. It prepares the arguments needed for the
92
+ /// [uuid](https://www.npmjs.com/package/uuid) module
93
+ /// based on the options provided to the type when
94
+ /// created. It will return arguments that can be
95
+ /// properly passed to [uuid.v1](https://www.npmjs.com/package/uuid#uuidv1options-buffer-offset).
96
+ ///
97
+ /// Return: Array<any>
98
+ /// An array of arguments to pass to `uuid.v1`
99
+ ///
100
+ /// Arguments:
101
+ /// options: object
102
+ /// The "options" object provided to the type when
103
+ /// it was initially created.
30
104
  getArgsForUUID(options) {
31
105
  let uuidOptions = {};
32
106
 
@@ -53,6 +127,23 @@ class UUIDV1Type extends UUIDBaseType {
53
127
  return args;
54
128
  }
55
129
 
130
+ /// This is an internal method that is used by the
131
+ /// type. It validates the arguments that will be passed
132
+ /// to the [uuid.v1](https://www.npmjs.com/package/uuid#uuidv1options-buffer-offset) method.
133
+ /// If incorrect options were provided to the type
134
+ /// when the type was specified, then this will throw
135
+ /// a validation exception.
136
+ ///
137
+ /// Note:
138
+ /// The provided "options" are not validated until
139
+ /// the first UUID is generated for the first time.
140
+ ///
141
+ /// Return: undefined
142
+ ///
143
+ /// Arguments:
144
+ /// uuidArgs: Array<any>
145
+ /// The arguments that will be passed to [uuid.v1](https://www.npmjs.com/package/uuid#uuidv1options-buffer-offset).
146
+ /// If an issue is detected than an exception will be thrown.
56
147
  validateOptions(uuidArgs) {
57
148
  let uuidOptions = uuidArgs[0];
58
149
 
@@ -66,6 +157,38 @@ class UUIDV1Type extends UUIDBaseType {
66
157
  throw new Error('UUIDV1Type::Default::UUIDV1: One of "options.rng" or "option.random" options are required for the specified type. Try "type: Types.UUIDV1({ rng: () => Array[16] })" or "type: Types.UUIDV1({ random: Function | Array[16] })" for your type instead.');
67
158
  }
68
159
 
160
+ /// Cast provided value to underlying type.
161
+ ///
162
+ /// This will cast the incoming value to the
163
+ /// underlying type of this field, a `UUIDV1`
164
+ /// string primitive. A `null` or `undefined`
165
+ /// value will simply be returned.
166
+ ///
167
+ /// The provided value will be given to [uuid.validate](https://www.npmjs.com/package/uuid#uuidvalidatestr)
168
+ /// to validate that it is a correct UUIDV1. If the provided value
169
+ /// is not a valid UUIDV1, then this method will throw an exception.
170
+ ///
171
+ /// If your UUID is prefixed with the `prefix`
172
+ /// option, then that will be stripped off first
173
+ /// before the provided value is handed off to
174
+ /// [uuid.validate](https://www.npmjs.com/package/uuid#uuidvalidatestr)
175
+ /// for validation. The prefix will be added back after validation.
176
+ ///
177
+ /// Note:
178
+ /// If a valid UUID is given without a prefix, then the
179
+ /// specified prefix (if any) will simply be added to
180
+ /// the returned result.
181
+ ///
182
+ /// See <see>Type.castToType</see> for a more
183
+ /// detailed description.
184
+ ///
185
+ /// Return: string | null | undefined
186
+ /// Return the incoming `value`, cast to this
187
+ /// type. `null` and `undefined` are simply
188
+ /// returned without casting.
189
+ ///
190
+ /// Arguments:
191
+ /// context: <see name="CastToTypeContext">Type.castToType</see>
69
192
  castToType(args) {
70
193
  let { value } = args;
71
194
  if (value == null)
@@ -77,6 +200,16 @@ class UUIDV1Type extends UUIDBaseType {
77
200
  return this.addPrefix(value);
78
201
  }
79
202
 
203
+ /// Check if the provided value is valid.
204
+ ///
205
+ /// This will check if the provided value is
206
+ /// a valid UUIDV1, ignoring any prefix.
207
+ ///
208
+ /// Return: boolean
209
+ ///
210
+ /// Arguments:
211
+ /// value: any
212
+ /// The value to check.
80
213
  isValidValue(value) {
81
214
  if (!Nife.instanceOf(value, 'string'))
82
215
  return false;
@@ -7,6 +7,51 @@ const { defaultValueFlags } = require('../helpers/default-helpers');
7
7
 
8
8
  let CALLABLE_PROP_NAMES = [ 'name', 'namespace', 'buffer', 'offset' ];
9
9
 
10
+ /// `UUID` (version 3) type.
11
+ ///
12
+ /// This represents a string based UUID,
13
+ /// using UUID version 3. The underlying
14
+ /// database type is the same as <see>StringType</see>,
15
+ /// which is usually a "VARCHAR" type.
16
+ ///
17
+ /// An optional "prefix" can be specified for this
18
+ /// type, which will allow you to prefix your ids.
19
+ ///
20
+ /// This type will automatically decide its own "length"
21
+ /// based on the length of a UUIDV3, plus the length of
22
+ /// any prefix you provide.
23
+ ///
24
+ /// See the [uuid](https://www.npmjs.com/package/uuid)
25
+ /// module to properly understand the options for this
26
+ /// field type.
27
+ ///
28
+ /// Example:
29
+ /// class UUIDs extends Model {
30
+ /// static fields = {
31
+ /// uuid1: Types.UUIDV3({
32
+ /// prefix: 'USER_',
33
+ /// name: ...,
34
+ /// namespace: ...,
35
+ /// buffer: ...,
36
+ /// offset: ...,
37
+ /// }),
38
+ /// uuid2: new Types.UUIDV3Type({
39
+ /// prefix: 'USER_',
40
+ /// ...,
41
+ /// }),
42
+ /// uuidWithDefault: {
43
+ /// type: Types.UUIDV3({ ... }),
44
+ /// defaultValue: Types.UUIDV3.Default.UUIDV3,
45
+ /// },
46
+ /// };
47
+ /// }
48
+ ///
49
+ /// Properties:
50
+ /// Default: object = { UUIDV3 }
51
+ /// `UUIDV3` is a method that can be used as the `defaultValue` of a <see>Field</see>
52
+ /// to have this field auto-generate UUIDs for new models.
53
+ ///
54
+ /// See: Type
10
55
  class UUIDV3Type extends UUIDBaseType {
11
56
  static Default = {
12
57
  UUIDV3: defaultValueFlags(function(context) {
@@ -23,10 +68,35 @@ class UUIDV3Type extends UUIDBaseType {
23
68
  }),
24
69
  };
25
70
 
71
+ /// Get the "display" name for this type.
72
+ ///
73
+ /// This method is called from <see>Model.toString</see>
74
+ /// when stringifying the model for representation.
75
+ ///
76
+ /// Note:
77
+ /// This is also an instance method that can be called from
78
+ /// an instance of the type.
79
+ ///
80
+ /// Return: string
81
+ /// Return the string value `'UUIDV3'`
26
82
  static getDisplayName() {
27
83
  return 'UUIDV3';
28
84
  }
29
85
 
86
+ /// This is an internal method that is used by the
87
+ /// type. It prepares the arguments needed for the
88
+ /// [uuid](https://www.npmjs.com/package/uuid) module
89
+ /// based on the options provided to the type when
90
+ /// created. It will return arguments that can be
91
+ /// properly passed to [uuid.v3](https://www.npmjs.com/package/uuid#uuidv3name-namespace-buffer-offset).
92
+ ///
93
+ /// Return: Array<any>
94
+ /// An array of arguments to pass to `uuid.v3`
95
+ ///
96
+ /// Arguments:
97
+ /// options: object
98
+ /// The "options" object provided to the type when
99
+ /// it was initially created.
30
100
  getArgsForUUID(options) {
31
101
  let uuidOptions = {};
32
102
 
@@ -62,6 +132,23 @@ class UUIDV3Type extends UUIDBaseType {
62
132
  return args;
63
133
  }
64
134
 
135
+ /// This is an internal method that is used by the
136
+ /// type. It validates the arguments that will be passed
137
+ /// to the [uuid.v3](https://www.npmjs.com/package/uuid#uuidv3name-namespace-buffer-offset) method.
138
+ /// If incorrect options were provided to the type
139
+ /// when the type was specified, then this will throw
140
+ /// a validation exception.
141
+ ///
142
+ /// Note:
143
+ /// The provided "options" are not validated until
144
+ /// the first UUID is generated for the first time.
145
+ ///
146
+ /// Return: undefined
147
+ ///
148
+ /// Arguments:
149
+ /// uuidArgs: Array<any>
150
+ /// The arguments that will be passed to [uuid.v3](https://www.npmjs.com/package/uuid#uuidv3name-namespace-buffer-offset).
151
+ /// If an issue is detected than an exception will be thrown.
65
152
  validateOptions(uuidArgs) {
66
153
  let [ name, namespace ] = uuidArgs;
67
154
 
@@ -75,6 +162,38 @@ class UUIDV3Type extends UUIDBaseType {
75
162
  throw new Error('UUIDV3Type::Default::UUIDV3: "options.namespace" option is required for the specified type. Try "type: Types.UUIDV3({ namespace: Function | String | Array[16] })" for your type instead.');
76
163
  }
77
164
 
165
+ /// Cast provided value to underlying type.
166
+ ///
167
+ /// This will cast the incoming value to the
168
+ /// underlying type of this field, a `UUIDV3`
169
+ /// string primitive. A `null` or `undefined`
170
+ /// value will simply be returned.
171
+ ///
172
+ /// The provided value will be given to [uuid.validate](https://www.npmjs.com/package/uuid#uuidvalidatestr)
173
+ /// to validate that it is a correct UUIDV3. If the provided value
174
+ /// is not a valid UUIDV3, then this method will throw an exception.
175
+ ///
176
+ /// If your UUID is prefixed with the `prefix`
177
+ /// option, then that will be stripped off first
178
+ /// before the provided value is handed off to
179
+ /// [uuid.validate](https://www.npmjs.com/package/uuid#uuidvalidatestr)
180
+ /// for validation. The prefix will be added back after validation.
181
+ ///
182
+ /// Note:
183
+ /// If a valid UUID is given without a prefix, then the
184
+ /// specified prefix (if any) will simply be added to
185
+ /// the returned result.
186
+ ///
187
+ /// See <see>Type.castToType</see> for a more
188
+ /// detailed description.
189
+ ///
190
+ /// Return: string | null | undefined
191
+ /// Return the incoming `value`, cast to this
192
+ /// type. `null` and `undefined` are simply
193
+ /// returned without casting.
194
+ ///
195
+ /// Arguments:
196
+ /// context: <see name="CastToTypeContext">Type.castToType</see>
78
197
  castToType(args) {
79
198
  let { value } = args;
80
199
  if (value == null)
@@ -86,6 +205,16 @@ class UUIDV3Type extends UUIDBaseType {
86
205
  return this.addPrefix(value);
87
206
  }
88
207
 
208
+ /// Check if the provided value is valid.
209
+ ///
210
+ /// This will check if the provided value is
211
+ /// a valid UUIDV3, ignoring any prefix.
212
+ ///
213
+ /// Return: boolean
214
+ ///
215
+ /// Arguments:
216
+ /// value: any
217
+ /// The value to check.
89
218
  isValidValue(value) {
90
219
  if (!Nife.instanceOf(value, 'string'))
91
220
  return false;