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.
- package/lib/connection/connection-base.d.ts +6 -2
- package/lib/connection/connection-base.js +84 -6
- package/lib/field.js +9 -1
- package/lib/model.d.ts +16 -4
- package/lib/model.js +223 -31
- package/lib/proxy-class/proxy-class.js +3 -0
- package/lib/types/concrete/date-type.js +4 -0
- package/lib/types/concrete/datetime-type.js +4 -0
- package/lib/types/concrete/serialized-type.js +1 -1
- package/lib/types/concrete/string-type.js +88 -0
- package/lib/types/concrete/text-type.js +97 -2
- package/lib/types/concrete/uuid-v1-type.js +133 -0
- package/lib/types/concrete/uuid-v3-type.js +129 -0
- package/lib/types/concrete/uuid-v4-type.js +129 -0
- package/lib/types/concrete/uuid-v5-type.js +129 -0
- package/lib/types/concrete/xid-type.js +89 -0
- package/lib/utils/async-store.d.ts +4 -0
- package/lib/utils/async-store.js +60 -0
- package/lib/utils/index.d.ts +2 -0
- package/lib/utils/index.js +15 -0
- package/package.json +1 -1
|
@@ -7,6 +7,51 @@ const { defaultValueFlags } = require('../helpers/default-helpers');
|
|
|
7
7
|
|
|
8
8
|
let CALLABLE_PROP_NAMES = [ 'random', 'buffer', 'offset' ];
|
|
9
9
|
|
|
10
|
+
/// `UUID` (version 4) type.
|
|
11
|
+
///
|
|
12
|
+
/// This represents a string based UUID,
|
|
13
|
+
/// using UUID version 4. 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 UUIDV4, 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.UUIDV4({
|
|
32
|
+
/// prefix: 'USER_',
|
|
33
|
+
/// random: ...,
|
|
34
|
+
/// rng: ...,
|
|
35
|
+
/// buffer: ...,
|
|
36
|
+
/// offset: ...,
|
|
37
|
+
/// }),
|
|
38
|
+
/// uuid2: new Types.UUIDV4Type({
|
|
39
|
+
/// prefix: 'USER_',
|
|
40
|
+
/// ...,
|
|
41
|
+
/// }),
|
|
42
|
+
/// uuidWithDefault: {
|
|
43
|
+
/// type: Types.UUIDV4({ ... }),
|
|
44
|
+
/// defaultValue: Types.UUIDV4.Default.UUIDV4,
|
|
45
|
+
/// },
|
|
46
|
+
/// };
|
|
47
|
+
/// }
|
|
48
|
+
///
|
|
49
|
+
/// Properties:
|
|
50
|
+
/// Default: object = { UUIDV4 }
|
|
51
|
+
/// `UUIDV4` 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 UUIDV4Type extends UUIDBaseType {
|
|
11
56
|
static Default = {
|
|
12
57
|
UUIDV4: defaultValueFlags(function(context) {
|
|
@@ -23,10 +68,35 @@ class UUIDV4Type 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 `'UUIDV4'`
|
|
26
82
|
static getDisplayName() {
|
|
27
83
|
return 'UUIDV4';
|
|
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.v1](https://www.npmjs.com/package/uuid#uuidv4options-buffer-offset).
|
|
92
|
+
///
|
|
93
|
+
/// Return: Array<any>
|
|
94
|
+
/// An array of arguments to pass to `uuid.v1`
|
|
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
|
|
|
@@ -53,10 +123,59 @@ class UUIDV4Type extends UUIDBaseType {
|
|
|
53
123
|
return args;
|
|
54
124
|
}
|
|
55
125
|
|
|
126
|
+
/// This is an internal method that is used by the
|
|
127
|
+
/// type. It validates the arguments that will be passed
|
|
128
|
+
/// to the [uuid.v4](https://www.npmjs.com/package/uuid#uuidv4options-buffer-offset) method.
|
|
129
|
+
/// If incorrect options were provided to the type
|
|
130
|
+
/// when the type was specified, then this will throw
|
|
131
|
+
/// a validation exception.
|
|
132
|
+
///
|
|
133
|
+
/// Note:
|
|
134
|
+
/// The provided "options" are not validated until
|
|
135
|
+
/// the first UUID is generated for the first time.
|
|
136
|
+
///
|
|
137
|
+
/// Return: undefined
|
|
138
|
+
///
|
|
139
|
+
/// Arguments:
|
|
140
|
+
/// uuidArgs: Array<any>
|
|
141
|
+
/// The arguments that will be passed to [uuid.v4](https://www.npmjs.com/package/uuid#uuidv4options-buffer-offset).
|
|
142
|
+
/// If an issue is detected than an exception will be thrown.
|
|
56
143
|
validateOptions() {
|
|
57
144
|
// No validation required for V4
|
|
58
145
|
}
|
|
59
146
|
|
|
147
|
+
/// Cast provided value to underlying type.
|
|
148
|
+
///
|
|
149
|
+
/// This will cast the incoming value to the
|
|
150
|
+
/// underlying type of this field, a `UUIDV4`
|
|
151
|
+
/// string primitive. A `null` or `undefined`
|
|
152
|
+
/// value will simply be returned.
|
|
153
|
+
///
|
|
154
|
+
/// The provided value will be given to [uuid.validate](https://www.npmjs.com/package/uuid#uuidvalidatestr)
|
|
155
|
+
/// to validate that it is a correct UUIDV4. If the provided value
|
|
156
|
+
/// is not a valid UUIDV4, then this method will throw an exception.
|
|
157
|
+
///
|
|
158
|
+
/// If your UUID is prefixed with the `prefix`
|
|
159
|
+
/// option, then that will be stripped off first
|
|
160
|
+
/// before the provided value is handed off to
|
|
161
|
+
/// [uuid.validate](https://www.npmjs.com/package/uuid#uuidvalidatestr)
|
|
162
|
+
/// for validation. The prefix will be added back after validation.
|
|
163
|
+
///
|
|
164
|
+
/// Note:
|
|
165
|
+
/// If a valid UUID is given without a prefix, then the
|
|
166
|
+
/// specified prefix (if any) will simply be added to
|
|
167
|
+
/// the returned result.
|
|
168
|
+
///
|
|
169
|
+
/// See <see>Type.castToType</see> for a more
|
|
170
|
+
/// detailed description.
|
|
171
|
+
///
|
|
172
|
+
/// Return: string | null | undefined
|
|
173
|
+
/// Return the incoming `value`, cast to this
|
|
174
|
+
/// type. `null` and `undefined` are simply
|
|
175
|
+
/// returned without casting.
|
|
176
|
+
///
|
|
177
|
+
/// Arguments:
|
|
178
|
+
/// context: <see name="CastToTypeContext">Type.castToType</see>
|
|
60
179
|
castToType(args) {
|
|
61
180
|
let { value } = args;
|
|
62
181
|
if (value == null)
|
|
@@ -68,6 +187,16 @@ class UUIDV4Type extends UUIDBaseType {
|
|
|
68
187
|
return this.addPrefix(value);
|
|
69
188
|
}
|
|
70
189
|
|
|
190
|
+
/// Check if the provided value is valid.
|
|
191
|
+
///
|
|
192
|
+
/// This will check if the provided value is
|
|
193
|
+
/// a valid UUIDV4, ignoring any prefix.
|
|
194
|
+
///
|
|
195
|
+
/// Return: boolean
|
|
196
|
+
///
|
|
197
|
+
/// Arguments:
|
|
198
|
+
/// value: any
|
|
199
|
+
/// The value to check.
|
|
71
200
|
isValidValue(value) {
|
|
72
201
|
if (!Nife.instanceOf(value, 'string'))
|
|
73
202
|
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 5) type.
|
|
11
|
+
///
|
|
12
|
+
/// This represents a string based UUID,
|
|
13
|
+
/// using UUID version 5. 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 UUIDV5, 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.UUIDV5({
|
|
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.UUIDV5({ ... }),
|
|
44
|
+
/// defaultValue: Types.UUIDV5.Default.UUIDV5,
|
|
45
|
+
/// },
|
|
46
|
+
/// };
|
|
47
|
+
/// }
|
|
48
|
+
///
|
|
49
|
+
/// Properties:
|
|
50
|
+
/// Default: object = { UUIDV5 }
|
|
51
|
+
/// `UUIDV5` 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 UUIDV5Type extends UUIDBaseType {
|
|
11
56
|
static Default = {
|
|
12
57
|
UUIDV5: defaultValueFlags(function(context) {
|
|
@@ -23,10 +68,35 @@ class UUIDV5Type 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 `'UUIDV5'`
|
|
26
82
|
static getDisplayName() {
|
|
27
83
|
return 'UUIDV5';
|
|
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.v5](https://www.npmjs.com/package/uuid#uuidv5name-namespace-buffer-offset).
|
|
92
|
+
///
|
|
93
|
+
/// Return: Array<any>
|
|
94
|
+
/// An array of arguments to pass to `uuid.v5`
|
|
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 UUIDV5Type 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.v5](https://www.npmjs.com/package/uuid#uuidv5name-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.v5](https://www.npmjs.com/package/uuid#uuidv5name-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 UUIDV5Type extends UUIDBaseType {
|
|
|
75
162
|
throw new Error('UUIDV5Type::Default::UUIDV5: "options.namespace" option is required for the specified type. Try "type: Types.UUIDV5({ 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 `UUIDV5`
|
|
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 UUIDV5. If the provided value
|
|
174
|
+
/// is not a valid UUIDV5, 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 UUIDV5Type 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 UUIDV5, 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;
|
|
@@ -5,6 +5,44 @@ const XID = require('xid-js');
|
|
|
5
5
|
const UUIDBaseType = require('./uuid-base');
|
|
6
6
|
const { defaultValueFlags } = require('../helpers/default-helpers');
|
|
7
7
|
|
|
8
|
+
/// `XID` type.
|
|
9
|
+
///
|
|
10
|
+
/// This represents a string based XID. The underlying
|
|
11
|
+
/// database type is the same as <see>StringType</see>,
|
|
12
|
+
/// which is usually a "VARCHAR" type.
|
|
13
|
+
///
|
|
14
|
+
/// An optional "prefix" can be specified for this
|
|
15
|
+
/// type, which will allow you to prefix your ids.
|
|
16
|
+
///
|
|
17
|
+
/// This type will automatically decide its own "length"
|
|
18
|
+
/// based on the length of an XID, plus the length of
|
|
19
|
+
/// any prefix you provide.
|
|
20
|
+
///
|
|
21
|
+
/// This type uses the [xid-js](https://www.npmjs.com/package/xid-js)
|
|
22
|
+
/// module to generate XIDs.
|
|
23
|
+
///
|
|
24
|
+
/// Example:
|
|
25
|
+
/// class XIDs extends Model {
|
|
26
|
+
/// static fields = {
|
|
27
|
+
/// xid1: Types.XID({
|
|
28
|
+
/// prefix: 'USER_',
|
|
29
|
+
/// }),
|
|
30
|
+
/// xid2: new Types.XIDType({
|
|
31
|
+
/// prefix: 'USER_',
|
|
32
|
+
/// }),
|
|
33
|
+
/// xidWithDefault: {
|
|
34
|
+
/// type: Types.XID({ ... }),
|
|
35
|
+
/// defaultValue: Types.XID.Default.XID,
|
|
36
|
+
/// },
|
|
37
|
+
/// };
|
|
38
|
+
/// }
|
|
39
|
+
///
|
|
40
|
+
/// Properties:
|
|
41
|
+
/// Default: object = { XID }
|
|
42
|
+
/// `XID` is a method that can be used as the `defaultValue` of a <see>Field</see>
|
|
43
|
+
/// to have this field auto-generate XIDs for new models.
|
|
44
|
+
///
|
|
45
|
+
/// See: Type
|
|
8
46
|
class XIDType extends UUIDBaseType {
|
|
9
47
|
static Default = {
|
|
10
48
|
XID: defaultValueFlags(function(context) {
|
|
@@ -16,10 +54,51 @@ class XIDType extends UUIDBaseType {
|
|
|
16
54
|
}),
|
|
17
55
|
};
|
|
18
56
|
|
|
57
|
+
/// Get the "display" name for this type.
|
|
58
|
+
///
|
|
59
|
+
/// This method is called from <see>Model.toString</see>
|
|
60
|
+
/// when stringifying the model for representation.
|
|
61
|
+
///
|
|
62
|
+
/// Note:
|
|
63
|
+
/// This is also an instance method that can be called from
|
|
64
|
+
/// an instance of the type.
|
|
65
|
+
///
|
|
66
|
+
/// Return: string
|
|
67
|
+
/// Return the string value `'XID'`
|
|
19
68
|
static getDisplayName() {
|
|
20
69
|
return 'XID';
|
|
21
70
|
}
|
|
22
71
|
|
|
72
|
+
/// Cast provided value to underlying type.
|
|
73
|
+
///
|
|
74
|
+
/// This will cast the incoming value to the
|
|
75
|
+
/// underlying type of this field, a `XID`
|
|
76
|
+
/// string primitive. A `null` or `undefined`
|
|
77
|
+
/// value will simply be returned.
|
|
78
|
+
///
|
|
79
|
+
/// The provided value will be validated to ensure
|
|
80
|
+
/// it is a valid base32 XID value.
|
|
81
|
+
///
|
|
82
|
+
/// If your XID is prefixed with the `prefix`
|
|
83
|
+
/// option, then that will be stripped off first
|
|
84
|
+
/// before the provided value is validated.
|
|
85
|
+
/// The prefix will be added back after validation.
|
|
86
|
+
///
|
|
87
|
+
/// Note:
|
|
88
|
+
/// If a valid XID is given without a prefix, then the
|
|
89
|
+
/// specified prefix (if any) will simply be added to
|
|
90
|
+
/// the returned result.
|
|
91
|
+
///
|
|
92
|
+
/// See <see>Type.castToType</see> for a more
|
|
93
|
+
/// detailed description.
|
|
94
|
+
///
|
|
95
|
+
/// Return: string | null | undefined
|
|
96
|
+
/// Return the incoming `value`, cast to this
|
|
97
|
+
/// type. `null` and `undefined` are simply
|
|
98
|
+
/// returned without casting.
|
|
99
|
+
///
|
|
100
|
+
/// Arguments:
|
|
101
|
+
/// context: <see name="CastToTypeContext">Type.castToType</see>
|
|
23
102
|
castToType(args) {
|
|
24
103
|
let { value } = args;
|
|
25
104
|
if (value == null)
|
|
@@ -31,6 +110,16 @@ class XIDType extends UUIDBaseType {
|
|
|
31
110
|
return this.addPrefix(value);
|
|
32
111
|
}
|
|
33
112
|
|
|
113
|
+
/// Check if the provided value is valid.
|
|
114
|
+
///
|
|
115
|
+
/// This will check if the provided value is
|
|
116
|
+
/// a valid XID, ignoring any prefix.
|
|
117
|
+
///
|
|
118
|
+
/// Return: boolean
|
|
119
|
+
///
|
|
120
|
+
/// Arguments:
|
|
121
|
+
/// value: any
|
|
122
|
+
/// The value to check.
|
|
34
123
|
isValidValue(value) {
|
|
35
124
|
if (!Nife.instanceOf(value, 'string'))
|
|
36
125
|
return false;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function getContextStore(): any;
|
|
2
|
+
export declare function getContextValue(key: any, defaultValue: any): any;
|
|
3
|
+
export declare function setContextValue(key: any, value: any): void;
|
|
4
|
+
export declare function runInContext(context: any, callback: Function): Promise<any>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
let globalAsyncStore;
|
|
4
|
+
|
|
5
|
+
try {
|
|
6
|
+
const { AsyncLocalStorage } = require('async_hooks');
|
|
7
|
+
globalAsyncStore = new AsyncLocalStorage();
|
|
8
|
+
} catch (error) {
|
|
9
|
+
globalAsyncStore = {
|
|
10
|
+
getStore: () => undefined,
|
|
11
|
+
run: (context, callback) => callback(),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function getContextStore() {
|
|
16
|
+
return globalAsyncStore.getStore();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function getContextValue(key, defaultValue) {
|
|
20
|
+
let store = globalAsyncStore.getStore();
|
|
21
|
+
while (store) {
|
|
22
|
+
let { context } = store;
|
|
23
|
+
if (!context.has(key)) {
|
|
24
|
+
store = store.parent;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return context.get(key);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return defaultValue;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function setContextValue(key, value) {
|
|
35
|
+
let store = globalAsyncStore.getStore();
|
|
36
|
+
if (!store || !store.context)
|
|
37
|
+
return;
|
|
38
|
+
|
|
39
|
+
return store.context.set(key, value);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function runInContext(context, callback) {
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
globalAsyncStore.run({ parent: globalAsyncStore.getStore(), context }, async () => {
|
|
45
|
+
try {
|
|
46
|
+
let result = await callback();
|
|
47
|
+
resolve(result);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
reject(error);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
module.exports = {
|
|
56
|
+
getContextValue,
|
|
57
|
+
setContextValue,
|
|
58
|
+
runInContext,
|
|
59
|
+
getContextStore,
|
|
60
|
+
};
|
package/lib/utils/index.d.ts
CHANGED
package/lib/utils/index.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const MiscUtils = require('./misc-utils');
|
|
4
4
|
const ModelUtils = require('./model-utils');
|
|
5
5
|
const QueryUtils = require('./query-utils');
|
|
6
|
+
const AsyncStore = require('./async-store');
|
|
6
7
|
|
|
7
8
|
const {
|
|
8
9
|
collect,
|
|
@@ -32,10 +33,18 @@ const {
|
|
|
32
33
|
generateQueryFromFilter,
|
|
33
34
|
} = QueryUtils;
|
|
34
35
|
|
|
36
|
+
const {
|
|
37
|
+
getContextStore,
|
|
38
|
+
getContextValue,
|
|
39
|
+
setContextValue,
|
|
40
|
+
runInContext,
|
|
41
|
+
} = AsyncStore;
|
|
42
|
+
|
|
35
43
|
module.exports = {
|
|
36
44
|
MiscUtils,
|
|
37
45
|
ModelUtils,
|
|
38
46
|
QueryUtils,
|
|
47
|
+
AsyncStore,
|
|
39
48
|
|
|
40
49
|
// MiscUtils
|
|
41
50
|
collect,
|
|
@@ -61,4 +70,10 @@ module.exports = {
|
|
|
61
70
|
// QueryUtils
|
|
62
71
|
parseFilterFieldAndOperator,
|
|
63
72
|
generateQueryFromFilter,
|
|
73
|
+
|
|
74
|
+
// AsyncStore
|
|
75
|
+
getContextStore,
|
|
76
|
+
getContextValue,
|
|
77
|
+
setContextValue,
|
|
78
|
+
runInContext,
|
|
64
79
|
};
|