mythix-orm 1.0.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.
Files changed (124) hide show
  1. package/.eslintrc.js +94 -0
  2. package/.vscode/launch.json +34 -0
  3. package/.vscode/settings.json +10 -0
  4. package/LICENSE +21 -0
  5. package/README.md +15 -0
  6. package/lib/connection/connection-base.js +877 -0
  7. package/lib/connection/index.js +11 -0
  8. package/lib/connection/literals/average-literal.js +14 -0
  9. package/lib/connection/literals/count-literal.js +18 -0
  10. package/lib/connection/literals/distinct-literal.js +14 -0
  11. package/lib/connection/literals/index.js +23 -0
  12. package/lib/connection/literals/literal-base.js +62 -0
  13. package/lib/connection/literals/literal-field-base.js +45 -0
  14. package/lib/connection/literals/literal.js +11 -0
  15. package/lib/connection/literals/max-literal.js +14 -0
  16. package/lib/connection/literals/min-literal.js +14 -0
  17. package/lib/connection/literals/sum-literal.js +14 -0
  18. package/lib/connection/query-generator-base.js +864 -0
  19. package/lib/errors/base-error.js +14 -0
  20. package/lib/errors/connection/access-denied-error.js +13 -0
  21. package/lib/errors/connection/connection-acquire-timeout-error.js +13 -0
  22. package/lib/errors/connection/connection-refused-error.js +13 -0
  23. package/lib/errors/connection/connection-timed-out-error.js +13 -0
  24. package/lib/errors/connection/host-not-found-error.js +13 -0
  25. package/lib/errors/connection/host-not-reachable-error.js +13 -0
  26. package/lib/errors/connection/index.js +19 -0
  27. package/lib/errors/connection/invalid-connection-error.js +13 -0
  28. package/lib/errors/connection-base-error.js +13 -0
  29. package/lib/errors/database/exclusion-constraint-error.js +13 -0
  30. package/lib/errors/database/foreign-key-constraint-error.js +13 -0
  31. package/lib/errors/database/index.js +13 -0
  32. package/lib/errors/database/timeout-error.js +13 -0
  33. package/lib/errors/database/unknown-constraint-error.js +13 -0
  34. package/lib/errors/database-base-error.js +17 -0
  35. package/lib/errors/index.js +44 -0
  36. package/lib/field.js +18 -0
  37. package/lib/index.js +43 -0
  38. package/lib/model.js +1374 -0
  39. package/lib/proxy-class/index.js +3 -0
  40. package/lib/proxy-class/proxy-class.js +269 -0
  41. package/lib/query-engine/field-scope.js +99 -0
  42. package/lib/query-engine/index.js +13 -0
  43. package/lib/query-engine/model-scope.js +198 -0
  44. package/lib/query-engine/query-engine-base.js +325 -0
  45. package/lib/query-engine/query-engine.js +212 -0
  46. package/lib/types/concrete/bigint-type.js +62 -0
  47. package/lib/types/concrete/blob-type.js +46 -0
  48. package/lib/types/concrete/boolean-type.js +41 -0
  49. package/lib/types/concrete/char-type.js +39 -0
  50. package/lib/types/concrete/date-type.js +87 -0
  51. package/lib/types/concrete/datetime-type.js +92 -0
  52. package/lib/types/concrete/float-type.js +47 -0
  53. package/lib/types/concrete/foreign-key-type.js +208 -0
  54. package/lib/types/concrete/index.js +53 -0
  55. package/lib/types/concrete/integer-type.js +51 -0
  56. package/lib/types/concrete/string-type.js +44 -0
  57. package/lib/types/concrete/text-type.js +44 -0
  58. package/lib/types/concrete/uuid-base.js +77 -0
  59. package/lib/types/concrete/uuid-v1-type.js +99 -0
  60. package/lib/types/concrete/uuid-v3-type.js +108 -0
  61. package/lib/types/concrete/uuid-v4-type.js +90 -0
  62. package/lib/types/concrete/uuid-v5-type.js +108 -0
  63. package/lib/types/concrete/xid-type.js +58 -0
  64. package/lib/types/helpers/default-helpers.js +127 -0
  65. package/lib/types/helpers/index.js +35 -0
  66. package/lib/types/index.js +94 -0
  67. package/lib/types/type.js +244 -0
  68. package/lib/types/virtual/index.js +14 -0
  69. package/lib/types/virtual/model-type.js +141 -0
  70. package/lib/types/virtual/models-type.js +264 -0
  71. package/lib/types/virtual/relational-type-base.js +323 -0
  72. package/lib/utils/index.js +65 -0
  73. package/lib/utils/misc-utils.js +73 -0
  74. package/lib/utils/model-utils.js +704 -0
  75. package/lib/utils/query-utils.js +186 -0
  76. package/package.json +63 -0
  77. package/playground/test01.js +15 -0
  78. package/spec/connection/connection-base-spec.js +126 -0
  79. package/spec/connection/literals/average-literal-spec.js +45 -0
  80. package/spec/connection/literals/count-literal-spec.js +42 -0
  81. package/spec/connection/literals/distinct-literal-spec.js +42 -0
  82. package/spec/connection/literals/literal-spec.js +26 -0
  83. package/spec/connection/literals/max-literal-spec.js +42 -0
  84. package/spec/connection/literals/min-literal-spec.js +42 -0
  85. package/spec/connection/literals/sum-literal-spec.js +42 -0
  86. package/spec/helpers/default-helpers-spec.js +108 -0
  87. package/spec/model-spec.js +736 -0
  88. package/spec/proxy-class/proxy-class-spec.js +173 -0
  89. package/spec/query-engine/__snapshots__/QueryEngine-operations-query_operations_and_chaining-can_chain_query_conditions-001.snapshot +94 -0
  90. package/spec/query-engine/__snapshots__/QueryEngine-operations-query_operations_and_chaining-can_construct_a_query_context_with_a_model_call-001.snapshot +35 -0
  91. package/spec/query-engine/__snapshots__/QueryEngine-operations-query_operations_and_chaining-can_construct_a_query_context_with_a_model_sub-001.snapshot +35 -0
  92. package/spec/query-engine/__snapshots__/QueryEngine-operations-query_operations_and_chaining-can_set_a_default_scope_on_a_model-001.snapshot +57 -0
  93. package/spec/query-engine/__snapshots__/QueryEngine-operations-query_operations_and_chaining-can_unscope_default_scope_on_a_model-001.snapshot +35 -0
  94. package/spec/query-engine/query-engine-spec.js +99 -0
  95. package/spec/support/jasmine.json +13 -0
  96. package/spec/support/models/blob-test-model.js +19 -0
  97. package/spec/support/models/extended-user-model.js +38 -0
  98. package/spec/support/models/index.js +27 -0
  99. package/spec/support/models/number-model.js +24 -0
  100. package/spec/support/models/role-model.js +26 -0
  101. package/spec/support/models/role-thing-model.js +41 -0
  102. package/spec/support/models/scoped-user-model.js +13 -0
  103. package/spec/support/models/time-model.js +36 -0
  104. package/spec/support/models/user-model.js +70 -0
  105. package/spec/support/models/user-role-model.js +36 -0
  106. package/spec/support/models/user-thing-model.js +46 -0
  107. package/spec/support/models/validation-test-model.js +40 -0
  108. package/spec/support/snapshots.js +293 -0
  109. package/spec/support/test-helpers.js +13 -0
  110. package/spec/types/concrete/bigint-type-spec.js +84 -0
  111. package/spec/types/concrete/boolean-type-spec.js +83 -0
  112. package/spec/types/concrete/date-type-spec.js +85 -0
  113. package/spec/types/concrete/datetime-type-spec.js +87 -0
  114. package/spec/types/concrete/float-type-spec.js +71 -0
  115. package/spec/types/concrete/foreign-key-type-spec.js +64 -0
  116. package/spec/types/concrete/integer-type-spec.js +71 -0
  117. package/spec/types/concrete/string-type-spec.js +91 -0
  118. package/spec/types/concrete/uuid-v1-type-spec.js +73 -0
  119. package/spec/types/concrete/uuid-v4-type-spec.js +65 -0
  120. package/spec/types/type-spec.js +101 -0
  121. package/spec/types/virtual/model-types-spec.js +401 -0
  122. package/spec/utils/misc-utils-spec.js +61 -0
  123. package/spec/utils/model-utils-spec.js +55 -0
  124. package/spec/utils/query-utils-spec.js +105 -0
@@ -0,0 +1,77 @@
1
+ 'use strict';
2
+
3
+ const Nife = require('nife');
4
+ const Type = require('../type');
5
+
6
+ class UUIDBaseType extends Type {
7
+ constructor(_options) {
8
+ let options = _options || {};
9
+
10
+ super(options);
11
+
12
+ Object.defineProperties(this, {
13
+ 'options': {
14
+ writable: true,
15
+ enumberable: false,
16
+ configurable: true,
17
+ value: options,
18
+ },
19
+ });
20
+ }
21
+
22
+ getOptions() {
23
+ if (typeof this.options === 'function')
24
+ return this.options(this);
25
+
26
+ return this.options;
27
+ }
28
+
29
+ getPrefix(_options) {
30
+ let options = _options || this.getOptions();
31
+ return options.prefix || '';
32
+ }
33
+
34
+ stripPrefix(value) {
35
+ if (!Nife.instanceOf(value, 'string'))
36
+ return { prefix: '', id: value };
37
+
38
+ let baseLength = this.getBaseLength();
39
+ let valueLen = value.length;
40
+ if (valueLen > baseLength) {
41
+ let diff = valueLen - baseLength;
42
+ return { prefix: value.substring(0, diff), id: value.substring(diff) };
43
+ }
44
+
45
+ return { prefix: '', id: value };
46
+ }
47
+
48
+ addPrefix(value) {
49
+ if (!Nife.instanceOf(value, 'string'))
50
+ return value;
51
+
52
+ let baseLength = this.getBaseLength();
53
+ if (value > baseLength)
54
+ return value;
55
+
56
+ let { id } = this.stripPrefix(value);
57
+ return `${this.getPrefix()}${id}`;
58
+ }
59
+
60
+ getBaseLength() {
61
+ // eslint-disable-next-line no-magic-numbers
62
+ return 36;
63
+ }
64
+
65
+ getTotalLength() {
66
+ let prefix = this.getPrefix();
67
+ return this.getBaseLength() + prefix.length;
68
+ }
69
+
70
+ toString(connection) {
71
+ return (connection)
72
+ ? this.toConnectionType(connection)
73
+ : `VARCHAR(${this.getTotalLength()})`;
74
+ }
75
+ }
76
+
77
+ module.exports = UUIDBaseType;
@@ -0,0 +1,99 @@
1
+ 'use strict';
2
+
3
+ const Nife = require('nife');
4
+ const UUID = require('uuid');
5
+ const UUIDBaseType = require('./uuid-base');
6
+ const { defaultValueFlags } = require('../helpers/default-helpers');
7
+
8
+ let CALLABLE_PROP_NAMES = [ 'node', 'clockseq', 'msecs', 'nsecs', 'random', 'buffer', 'offset' ];
9
+
10
+ class UUIDV1Type extends UUIDBaseType {
11
+ static Default = {
12
+ UUIDV1: defaultValueFlags(function(context) {
13
+ let field = context.field;
14
+ let type = field.type;
15
+ let options = type.getOptions();
16
+ let uuidArgs = type.getArgsForUUID(options);
17
+
18
+ // Will throw an error if something is wrong
19
+ type.validateOptions(uuidArgs);
20
+
21
+ let prefix = options.prefix || '';
22
+ return `${prefix}${UUID.v1(...uuidArgs)}`;
23
+ }),
24
+ };
25
+
26
+ static getDisplayName() {
27
+ return 'UUIDV1';
28
+ }
29
+
30
+ getDisplayName() {
31
+ return this.constructor.getDisplayName();
32
+ }
33
+
34
+ getArgsForUUID(options) {
35
+ let uuidOptions = {};
36
+
37
+ for (let i = 0, il = CALLABLE_PROP_NAMES.length; i < il; i++) {
38
+ let propName = CALLABLE_PROP_NAMES[i];
39
+ let value = options[propName];
40
+
41
+ if (typeof value === 'function')
42
+ value = value(this);
43
+
44
+ if (value !== undefined)
45
+ uuidOptions[propName] = value;
46
+ }
47
+
48
+ let args = [];
49
+ if (Nife.isNotEmpty(uuidOptions))
50
+ args.push(uuidOptions);
51
+
52
+ if (uuidOptions.buffer) {
53
+ args.push(uuidOptions.buffer);
54
+ args.push(uuidOptions.offset || 0);
55
+ }
56
+
57
+ return args;
58
+ }
59
+
60
+ validateOptions(uuidArgs) {
61
+ let uuidOptions = uuidArgs[0];
62
+
63
+ if (Nife.isEmpty(uuidOptions))
64
+ throw new Error('UUIDV1Type::Default::UUIDV1: "options" argument is required for the specified type. Try "type: Types.UUIDV1({ ...options })" for your type instead.');
65
+
66
+ if (uuidOptions.clockseq == null)
67
+ throw new Error('UUIDV1Type::Default::UUIDV1: "options.clockseq" option is required for the specified type. Try "type: Types.UUIDV1({ clockseq: Function | Number })" for your type instead.');
68
+
69
+ if (uuidOptions.rng == null && uuidOptions.random == null)
70
+ 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.');
71
+ }
72
+
73
+ castToType(args) {
74
+ let { value } = args;
75
+ if (value == null)
76
+ return value;
77
+
78
+ if (!this.isValidValue(value, args))
79
+ throw new TypeError(`UUIDV1Type::castToType: Provided value "${value}" is not a valid UUID.`);
80
+
81
+ return this.addPrefix(value);
82
+ }
83
+
84
+ isValidValue(value) {
85
+ if (!Nife.instanceOf(value, 'string'))
86
+ return false;
87
+
88
+ let { id } = this.stripPrefix(value);
89
+ if (!id)
90
+ return false;
91
+
92
+ return UUID.validate(id);
93
+ }
94
+ }
95
+
96
+ module.exports = {
97
+ UUIDV1: UUIDBaseType.wrapConstructor(UUIDV1Type),
98
+ UUIDV1Type,
99
+ };
@@ -0,0 +1,108 @@
1
+ 'use strict';
2
+
3
+ const Nife = require('nife');
4
+ const UUID = require('uuid');
5
+ const UUIDBaseType = require('./uuid-base');
6
+ const { defaultValueFlags } = require('../helpers/default-helpers');
7
+
8
+ let CALLABLE_PROP_NAMES = [ 'name', 'namespace', 'buffer', 'offset' ];
9
+
10
+ class UUIDV3Type extends UUIDBaseType {
11
+ static Default = {
12
+ UUIDV3: defaultValueFlags(function(context) {
13
+ let field = context.field;
14
+ let type = field.type;
15
+ let options = type.getOptions();
16
+ let uuidArgs = type.getArgsForUUID(options);
17
+
18
+ // Will throw an error if something is wrong
19
+ type.validateOptions(uuidArgs);
20
+
21
+ let prefix = options.prefix || '';
22
+ return `${prefix}${UUID.v3(...uuidArgs)}`;
23
+ }),
24
+ };
25
+
26
+ static getDisplayName() {
27
+ return 'UUIDV3';
28
+ }
29
+
30
+ getDisplayName() {
31
+ return this.constructor.getDisplayName();
32
+ }
33
+
34
+ getArgsForUUID(options) {
35
+ let uuidOptions = {};
36
+
37
+ for (let i = 0, il = CALLABLE_PROP_NAMES.length; i < il; i++) {
38
+ let propName = CALLABLE_PROP_NAMES[i];
39
+ let value = options[propName];
40
+
41
+ if (typeof value === 'function')
42
+ value = value(this);
43
+
44
+ if (value !== undefined)
45
+ uuidOptions[propName] = value;
46
+ }
47
+
48
+ let args = [];
49
+ if (Nife.isNotEmpty(uuidOptions.name))
50
+ args.push(uuidOptions.name);
51
+
52
+ if (Nife.isNotEmpty(uuidOptions.namespace))
53
+ args.push(uuidOptions.namespace);
54
+
55
+ if (uuidOptions.buffer) {
56
+ if (args.length < 1)
57
+ args.push(null);
58
+
59
+ if (args.length < 2)
60
+ args.push(null);
61
+
62
+ args.push(uuidOptions.buffer);
63
+ args.push(uuidOptions.offset || 0);
64
+ }
65
+
66
+ return args;
67
+ }
68
+
69
+ validateOptions(uuidArgs) {
70
+ let [ name, namespace ] = uuidArgs;
71
+
72
+ if (Nife.isEmpty(this.options))
73
+ throw new Error('UUIDV3Type::Default::UUIDV3: "options" argument is required for the specified type. Try "type: Types.UUIDV3({ ...options })" for your type instead.');
74
+
75
+ if (Nife.isEmpty(name))
76
+ throw new Error('UUIDV3Type::Default::UUIDV3: "options.name" option is required for the specified type. Try "type: Types.UUIDV3({ name: Function | String | Array })" for your type instead.');
77
+
78
+ if (Nife.isEmpty(namespace))
79
+ 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.');
80
+ }
81
+
82
+ castToType(args) {
83
+ let { value } = args;
84
+ if (value == null)
85
+ return value;
86
+
87
+ if (!this.isValidValue(value, args))
88
+ throw new TypeError(`UUIDV3Type::castToType: Provided value "${value}" is not a valid UUID.`);
89
+
90
+ return this.addPrefix(value);
91
+ }
92
+
93
+ isValidValue(value) {
94
+ if (!Nife.instanceOf(value, 'string'))
95
+ return false;
96
+
97
+ let { id } = this.stripPrefix(value);
98
+ if (!id)
99
+ return false;
100
+
101
+ return UUID.validate(id);
102
+ }
103
+ }
104
+
105
+ module.exports = {
106
+ UUIDV3: UUIDBaseType.wrapConstructor(UUIDV3Type),
107
+ UUIDV3Type,
108
+ };
@@ -0,0 +1,90 @@
1
+ 'use strict';
2
+
3
+ const Nife = require('nife');
4
+ const UUID = require('uuid');
5
+ const UUIDBaseType = require('./uuid-base');
6
+ const { defaultValueFlags } = require('../helpers/default-helpers');
7
+
8
+ let CALLABLE_PROP_NAMES = [ 'random', 'buffer', 'offset' ];
9
+
10
+ class UUIDV4Type extends UUIDBaseType {
11
+ static Default = {
12
+ UUIDV4: defaultValueFlags(function(context) {
13
+ let field = context.field;
14
+ let type = field.type;
15
+ let options = type.getOptions();
16
+ let uuidArgs = type.getArgsForUUID(options);
17
+
18
+ // Will throw an error if something is wrong
19
+ type.validateOptions(uuidArgs);
20
+
21
+ let prefix = options.prefix || '';
22
+ return `${prefix}${UUID.v4(...uuidArgs)}`;
23
+ }),
24
+ };
25
+
26
+ static getDisplayName() {
27
+ return 'UUIDV4';
28
+ }
29
+
30
+ getDisplayName() {
31
+ return this.constructor.getDisplayName();
32
+ }
33
+
34
+ getArgsForUUID(options) {
35
+ let uuidOptions = {};
36
+
37
+ for (let i = 0, il = CALLABLE_PROP_NAMES.length; i < il; i++) {
38
+ let propName = CALLABLE_PROP_NAMES[i];
39
+ let value = options[propName];
40
+
41
+ if (typeof value === 'function')
42
+ value = value(this);
43
+
44
+ if (value !== undefined)
45
+ uuidOptions[propName] = value;
46
+ }
47
+
48
+ let args = [];
49
+ if (Nife.isNotEmpty(uuidOptions))
50
+ args.push(uuidOptions);
51
+
52
+ if (uuidOptions.buffer) {
53
+ args.push(uuidOptions.buffer);
54
+ args.push(uuidOptions.offset || 0);
55
+ }
56
+
57
+ return args;
58
+ }
59
+
60
+ validateOptions() {
61
+ // No validation required for V4
62
+ }
63
+
64
+ castToType(args) {
65
+ let { value } = args;
66
+ if (value == null)
67
+ return value;
68
+
69
+ if (!this.isValidValue(value, args))
70
+ throw new TypeError(`UUIDV4Type::castToType: Provided value "${value}" is not a valid UUID.`);
71
+
72
+ return this.addPrefix(value);
73
+ }
74
+
75
+ isValidValue(value) {
76
+ if (!Nife.instanceOf(value, 'string'))
77
+ return false;
78
+
79
+ let { id } = this.stripPrefix(value);
80
+ if (!id)
81
+ return false;
82
+
83
+ return UUID.validate(id);
84
+ }
85
+ }
86
+
87
+ module.exports = {
88
+ UUIDV4: UUIDBaseType.wrapConstructor(UUIDV4Type),
89
+ UUIDV4Type,
90
+ };
@@ -0,0 +1,108 @@
1
+ 'use strict';
2
+
3
+ const Nife = require('nife');
4
+ const UUID = require('uuid');
5
+ const UUIDBaseType = require('./uuid-base');
6
+ const { defaultValueFlags } = require('../helpers/default-helpers');
7
+
8
+ let CALLABLE_PROP_NAMES = [ 'name', 'namespace', 'buffer', 'offset' ];
9
+
10
+ class UUIDV5Type extends UUIDBaseType {
11
+ static Default = {
12
+ UUIDV5: defaultValueFlags(function(context) {
13
+ let field = context.field;
14
+ let type = field.type;
15
+ let options = type.getOptions();
16
+ let uuidArgs = type.getArgsForUUID(options);
17
+
18
+ // Will throw an error if something is wrong
19
+ type.validateOptions(uuidArgs);
20
+
21
+ let prefix = options.prefix || '';
22
+ return `${prefix}${UUID.v5(...uuidArgs)}`;
23
+ }),
24
+ };
25
+
26
+ static getDisplayName() {
27
+ return 'UUIDV5';
28
+ }
29
+
30
+ getDisplayName() {
31
+ return this.constructor.getDisplayName();
32
+ }
33
+
34
+ getArgsForUUID(options) {
35
+ let uuidOptions = {};
36
+
37
+ for (let i = 0, il = CALLABLE_PROP_NAMES.length; i < il; i++) {
38
+ let propName = CALLABLE_PROP_NAMES[i];
39
+ let value = options[propName];
40
+
41
+ if (typeof value === 'function')
42
+ value = value(this);
43
+
44
+ if (value !== undefined)
45
+ uuidOptions[propName] = value;
46
+ }
47
+
48
+ let args = [];
49
+ if (Nife.isNotEmpty(uuidOptions.name))
50
+ args.push(uuidOptions.name);
51
+
52
+ if (Nife.isNotEmpty(uuidOptions.namespace))
53
+ args.push(uuidOptions.namespace);
54
+
55
+ if (uuidOptions.buffer) {
56
+ if (args.length < 1)
57
+ args.push(null);
58
+
59
+ if (args.length < 2)
60
+ args.push(null);
61
+
62
+ args.push(uuidOptions.buffer);
63
+ args.push(uuidOptions.offset || 0);
64
+ }
65
+
66
+ return args;
67
+ }
68
+
69
+ validateOptions(uuidArgs) {
70
+ let [ name, namespace ] = uuidArgs;
71
+
72
+ if (Nife.isEmpty(this.options))
73
+ throw new Error('UUIDV5Type::Default::UUIDV5: "options" argument is required for the specified type. Try "type: Types.UUIDV5({ ...options })" for your type instead.');
74
+
75
+ if (Nife.isEmpty(name))
76
+ throw new Error('UUIDV5Type::Default::UUIDV5: "options.name" option is required for the specified type. Try "type: Types.UUIDV5({ name: Function | String | Array })" for your type instead.');
77
+
78
+ if (Nife.isEmpty(namespace))
79
+ 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.');
80
+ }
81
+
82
+ castToType(args) {
83
+ let { value } = args;
84
+ if (value == null)
85
+ return value;
86
+
87
+ if (!this.isValidValue(value, args))
88
+ throw new TypeError(`UUIDV5Type::castToType: Provided value "${value}" is not a valid UUID.`);
89
+
90
+ return this.addPrefix(value);
91
+ }
92
+
93
+ isValidValue(value) {
94
+ if (!Nife.instanceOf(value, 'string'))
95
+ return false;
96
+
97
+ let { id } = this.stripPrefix(value);
98
+ if (!id)
99
+ return false;
100
+
101
+ return UUID.validate(id);
102
+ }
103
+ }
104
+
105
+ module.exports = {
106
+ UUIDV5: UUIDBaseType.wrapConstructor(UUIDV5Type),
107
+ UUIDV5Type,
108
+ };
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ const Nife = require('nife');
4
+ const XID = require('xid-js');
5
+ const UUIDBaseType = require('./uuid-base');
6
+ const { defaultValueFlags } = require('../helpers/default-helpers');
7
+
8
+ class XIDType extends UUIDBaseType {
9
+ static Default = {
10
+ XID: defaultValueFlags(function(context) {
11
+ let field = context.field;
12
+ let type = field.type;
13
+ let prefix = type.getPrefix();
14
+
15
+ return `${prefix}${XID.next()}`;
16
+ }),
17
+ };
18
+
19
+ static getDisplayName() {
20
+ return 'XID';
21
+ }
22
+
23
+ getDisplayName() {
24
+ return this.constructor.getDisplayName();
25
+ }
26
+
27
+ castToType(args) {
28
+ let { value } = args;
29
+ if (value == null)
30
+ return value;
31
+
32
+ if (!this.isValidValue(value, args))
33
+ throw new TypeError(`XIDType::castToType: Provided value "${value}" is not a valid XID.`);
34
+
35
+ return this.addPrefix(value);
36
+ }
37
+
38
+ isValidValue(value) {
39
+ if (!Nife.instanceOf(value, 'string'))
40
+ return false;
41
+
42
+ let { id } = this.stripPrefix(value);
43
+ if (!id)
44
+ return false;
45
+
46
+ return (/^[0-9abcdefghjkmnpqrstvwxyz]{20}$/).test(id);
47
+ }
48
+
49
+ getBaseLength() {
50
+ // eslint-disable-next-line no-magic-numbers
51
+ return 20;
52
+ }
53
+ }
54
+
55
+ module.exports = {
56
+ XID: UUIDBaseType.wrapConstructor(XIDType),
57
+ XIDType,
58
+ };
@@ -0,0 +1,127 @@
1
+ 'use strict';
2
+
3
+ const FLAG_ON_INITIALIZE = 0x01;
4
+ const FLAG_ON_INSERT = 0x02;
5
+ const FLAG_ON_UPDATE = 0x04;
6
+ const FLAG_ON_STORE = FLAG_ON_INSERT | FLAG_ON_UPDATE;
7
+ const FLAG_LITERAL = 0x08;
8
+ const FLAG_REMOTE = 0x10;
9
+
10
+ function defaultValueFlags(func, _flagsObj) {
11
+ let flags = FLAG_ON_INITIALIZE;
12
+ let flagsObj = _flagsObj || {};
13
+
14
+ if (flagsObj.onInsert === true)
15
+ flags |= FLAG_ON_INSERT;
16
+
17
+ if (flagsObj.onUpdate === true)
18
+ flags |= FLAG_ON_UPDATE;
19
+
20
+ if (flagsObj.onStore === true)
21
+ flags |= FLAG_ON_STORE;
22
+
23
+ if (flagsObj.literal === true)
24
+ flags |= FLAG_LITERAL;
25
+
26
+ if (flagsObj.remote === true)
27
+ flags |= FLAG_REMOTE;
28
+
29
+ if (flagsObj.remote === true || flagsObj.onInitialize === false)
30
+ flags = flags & ~FLAG_ON_INITIALIZE;
31
+ else if (flags > 1)
32
+ flags = flags & ~FLAG_ON_INITIALIZE;
33
+
34
+ func.mythixFlags = flags;
35
+
36
+ return func;
37
+ }
38
+
39
+ function getDefaultValueFlags(func) {
40
+ if (!func)
41
+ return 0;
42
+
43
+ return func.mythixFlags || 0;
44
+ }
45
+
46
+ function checkDefaultValueFlags(func, checkFlags) {
47
+ if (!checkFlags || !checkFlags.length || !func)
48
+ return false;
49
+
50
+ let flags = getDefaultValueFlags(func);
51
+ for (let i = 0, il = checkFlags.length; i < il; i++) {
52
+ let flag = checkFlags[i];
53
+
54
+ if ((flag === 'onInitialize' || flag === FLAG_ON_INITIALIZE) && !(flags & FLAG_ON_INITIALIZE))
55
+ return false;
56
+
57
+ if ((flag === 'onInsert' || flag === FLAG_ON_INSERT) && !(flags & FLAG_ON_INSERT))
58
+ return false;
59
+
60
+ if ((flag === 'onUpdate' || flag === FLAG_ON_UPDATE) && !(flags & FLAG_ON_UPDATE))
61
+ return false;
62
+
63
+ if ((flag === 'onStore' || flag === FLAG_ON_STORE) && (flags & FLAG_ON_STORE) !== FLAG_ON_STORE)
64
+ return false;
65
+
66
+ if ((flag === 'literal' || flag === FLAG_LITERAL) && !(flags & FLAG_LITERAL))
67
+ return false;
68
+
69
+ if ((flag === 'remote' || flag === FLAG_REMOTE) && !(flags & FLAG_REMOTE))
70
+ return false;
71
+ }
72
+
73
+ return true;
74
+ }
75
+
76
+ function generatePermutations(func, flags) {
77
+ const masterFunc = defaultValueFlags(func, flags);
78
+
79
+ masterFunc.UPDATE = defaultValueFlags(function(...args) {
80
+ return func.apply(this, args);
81
+ }, Object.assign({}, flags, { onUpdate: true }));
82
+
83
+ masterFunc.INSERT = defaultValueFlags(function(...args) {
84
+ return func.apply(this, args);
85
+ }, Object.assign({}, flags, { onInsert: true }));
86
+
87
+ masterFunc.ALWAYS = defaultValueFlags(function(...args) {
88
+ return func.apply(this, args);
89
+ }, Object.assign({}, flags, { onStore: true }));
90
+
91
+ return masterFunc;
92
+ }
93
+
94
+ const AUTO_INCREMENT = defaultValueFlags(function(context) {
95
+ return context.connection.getDefaultFieldValue('AUTO_INCREMENT', context);
96
+ }, { literal: true, remote: true });
97
+
98
+ const DATETIME_NOW = generatePermutations(function(context) {
99
+ return context.connection.getDefaultFieldValue('DATETIME_NOW', context);
100
+ }, { literal: true, remote: true });
101
+
102
+ const DATE_NOW = generatePermutations(function(context) {
103
+ return context.connection.getDefaultFieldValue('DATE_NOW', context);
104
+ }, { literal: true, remote: true });
105
+
106
+ DATETIME_NOW.LOCAL = generatePermutations(function(context) {
107
+ return context.connection.getDefaultFieldValue('DATETIME_NOW_LOCAL', context);
108
+ });
109
+
110
+ DATE_NOW.LOCAL = generatePermutations(function(context) {
111
+ return context.connection.getDefaultFieldValue('DATE_NOW_LOCAL', context);
112
+ });
113
+
114
+ module.exports = {
115
+ FLAG_ON_INITIALIZE,
116
+ FLAG_ON_INSERT,
117
+ FLAG_ON_UPDATE,
118
+ FLAG_ON_STORE,
119
+ FLAG_LITERAL,
120
+ FLAG_REMOTE,
121
+ defaultValueFlags,
122
+ getDefaultValueFlags,
123
+ checkDefaultValueFlags,
124
+ AUTO_INCREMENT,
125
+ DATETIME_NOW,
126
+ DATE_NOW,
127
+ };
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ const DefaultHelpers = require('./default-helpers');
4
+
5
+ const {
6
+ FLAG_ON_INITIALIZE,
7
+ FLAG_ON_INSERT,
8
+ FLAG_ON_UPDATE,
9
+ FLAG_ON_STORE,
10
+ FLAG_LITERAL,
11
+ FLAG_REMOTE,
12
+ defaultValueFlags,
13
+ getDefaultValueFlags,
14
+ checkDefaultValueFlags,
15
+ AUTO_INCREMENT,
16
+ DATETIME_NOW,
17
+ DATE_NOW,
18
+ } = DefaultHelpers;
19
+
20
+ module.exports = {
21
+ DefaultHelpers,
22
+
23
+ FLAG_ON_INITIALIZE,
24
+ FLAG_ON_INSERT,
25
+ FLAG_ON_UPDATE,
26
+ FLAG_ON_STORE,
27
+ FLAG_LITERAL,
28
+ FLAG_REMOTE,
29
+ defaultValueFlags,
30
+ getDefaultValueFlags,
31
+ checkDefaultValueFlags,
32
+ AUTO_INCREMENT,
33
+ DATETIME_NOW,
34
+ DATE_NOW,
35
+ };