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.
- package/.eslintrc.js +94 -0
- package/.vscode/launch.json +34 -0
- package/.vscode/settings.json +10 -0
- package/LICENSE +21 -0
- package/README.md +15 -0
- package/lib/connection/connection-base.js +877 -0
- package/lib/connection/index.js +11 -0
- package/lib/connection/literals/average-literal.js +14 -0
- package/lib/connection/literals/count-literal.js +18 -0
- package/lib/connection/literals/distinct-literal.js +14 -0
- package/lib/connection/literals/index.js +23 -0
- package/lib/connection/literals/literal-base.js +62 -0
- package/lib/connection/literals/literal-field-base.js +45 -0
- package/lib/connection/literals/literal.js +11 -0
- package/lib/connection/literals/max-literal.js +14 -0
- package/lib/connection/literals/min-literal.js +14 -0
- package/lib/connection/literals/sum-literal.js +14 -0
- package/lib/connection/query-generator-base.js +864 -0
- package/lib/errors/base-error.js +14 -0
- package/lib/errors/connection/access-denied-error.js +13 -0
- package/lib/errors/connection/connection-acquire-timeout-error.js +13 -0
- package/lib/errors/connection/connection-refused-error.js +13 -0
- package/lib/errors/connection/connection-timed-out-error.js +13 -0
- package/lib/errors/connection/host-not-found-error.js +13 -0
- package/lib/errors/connection/host-not-reachable-error.js +13 -0
- package/lib/errors/connection/index.js +19 -0
- package/lib/errors/connection/invalid-connection-error.js +13 -0
- package/lib/errors/connection-base-error.js +13 -0
- package/lib/errors/database/exclusion-constraint-error.js +13 -0
- package/lib/errors/database/foreign-key-constraint-error.js +13 -0
- package/lib/errors/database/index.js +13 -0
- package/lib/errors/database/timeout-error.js +13 -0
- package/lib/errors/database/unknown-constraint-error.js +13 -0
- package/lib/errors/database-base-error.js +17 -0
- package/lib/errors/index.js +44 -0
- package/lib/field.js +18 -0
- package/lib/index.js +43 -0
- package/lib/model.js +1374 -0
- package/lib/proxy-class/index.js +3 -0
- package/lib/proxy-class/proxy-class.js +269 -0
- package/lib/query-engine/field-scope.js +99 -0
- package/lib/query-engine/index.js +13 -0
- package/lib/query-engine/model-scope.js +198 -0
- package/lib/query-engine/query-engine-base.js +325 -0
- package/lib/query-engine/query-engine.js +212 -0
- package/lib/types/concrete/bigint-type.js +62 -0
- package/lib/types/concrete/blob-type.js +46 -0
- package/lib/types/concrete/boolean-type.js +41 -0
- package/lib/types/concrete/char-type.js +39 -0
- package/lib/types/concrete/date-type.js +87 -0
- package/lib/types/concrete/datetime-type.js +92 -0
- package/lib/types/concrete/float-type.js +47 -0
- package/lib/types/concrete/foreign-key-type.js +208 -0
- package/lib/types/concrete/index.js +53 -0
- package/lib/types/concrete/integer-type.js +51 -0
- package/lib/types/concrete/string-type.js +44 -0
- package/lib/types/concrete/text-type.js +44 -0
- package/lib/types/concrete/uuid-base.js +77 -0
- package/lib/types/concrete/uuid-v1-type.js +99 -0
- package/lib/types/concrete/uuid-v3-type.js +108 -0
- package/lib/types/concrete/uuid-v4-type.js +90 -0
- package/lib/types/concrete/uuid-v5-type.js +108 -0
- package/lib/types/concrete/xid-type.js +58 -0
- package/lib/types/helpers/default-helpers.js +127 -0
- package/lib/types/helpers/index.js +35 -0
- package/lib/types/index.js +94 -0
- package/lib/types/type.js +244 -0
- package/lib/types/virtual/index.js +14 -0
- package/lib/types/virtual/model-type.js +141 -0
- package/lib/types/virtual/models-type.js +264 -0
- package/lib/types/virtual/relational-type-base.js +323 -0
- package/lib/utils/index.js +65 -0
- package/lib/utils/misc-utils.js +73 -0
- package/lib/utils/model-utils.js +704 -0
- package/lib/utils/query-utils.js +186 -0
- package/package.json +63 -0
- package/playground/test01.js +15 -0
- package/spec/connection/connection-base-spec.js +126 -0
- package/spec/connection/literals/average-literal-spec.js +45 -0
- package/spec/connection/literals/count-literal-spec.js +42 -0
- package/spec/connection/literals/distinct-literal-spec.js +42 -0
- package/spec/connection/literals/literal-spec.js +26 -0
- package/spec/connection/literals/max-literal-spec.js +42 -0
- package/spec/connection/literals/min-literal-spec.js +42 -0
- package/spec/connection/literals/sum-literal-spec.js +42 -0
- package/spec/helpers/default-helpers-spec.js +108 -0
- package/spec/model-spec.js +736 -0
- package/spec/proxy-class/proxy-class-spec.js +173 -0
- package/spec/query-engine/__snapshots__/QueryEngine-operations-query_operations_and_chaining-can_chain_query_conditions-001.snapshot +94 -0
- package/spec/query-engine/__snapshots__/QueryEngine-operations-query_operations_and_chaining-can_construct_a_query_context_with_a_model_call-001.snapshot +35 -0
- package/spec/query-engine/__snapshots__/QueryEngine-operations-query_operations_and_chaining-can_construct_a_query_context_with_a_model_sub-001.snapshot +35 -0
- package/spec/query-engine/__snapshots__/QueryEngine-operations-query_operations_and_chaining-can_set_a_default_scope_on_a_model-001.snapshot +57 -0
- package/spec/query-engine/__snapshots__/QueryEngine-operations-query_operations_and_chaining-can_unscope_default_scope_on_a_model-001.snapshot +35 -0
- package/spec/query-engine/query-engine-spec.js +99 -0
- package/spec/support/jasmine.json +13 -0
- package/spec/support/models/blob-test-model.js +19 -0
- package/spec/support/models/extended-user-model.js +38 -0
- package/spec/support/models/index.js +27 -0
- package/spec/support/models/number-model.js +24 -0
- package/spec/support/models/role-model.js +26 -0
- package/spec/support/models/role-thing-model.js +41 -0
- package/spec/support/models/scoped-user-model.js +13 -0
- package/spec/support/models/time-model.js +36 -0
- package/spec/support/models/user-model.js +70 -0
- package/spec/support/models/user-role-model.js +36 -0
- package/spec/support/models/user-thing-model.js +46 -0
- package/spec/support/models/validation-test-model.js +40 -0
- package/spec/support/snapshots.js +293 -0
- package/spec/support/test-helpers.js +13 -0
- package/spec/types/concrete/bigint-type-spec.js +84 -0
- package/spec/types/concrete/boolean-type-spec.js +83 -0
- package/spec/types/concrete/date-type-spec.js +85 -0
- package/spec/types/concrete/datetime-type-spec.js +87 -0
- package/spec/types/concrete/float-type-spec.js +71 -0
- package/spec/types/concrete/foreign-key-type-spec.js +64 -0
- package/spec/types/concrete/integer-type-spec.js +71 -0
- package/spec/types/concrete/string-type-spec.js +91 -0
- package/spec/types/concrete/uuid-v1-type-spec.js +73 -0
- package/spec/types/concrete/uuid-v4-type-spec.js +65 -0
- package/spec/types/type-spec.js +101 -0
- package/spec/types/virtual/model-types-spec.js +401 -0
- package/spec/utils/misc-utils-spec.js +61 -0
- package/spec/utils/model-utils-spec.js +55 -0
- package/spec/utils/query-utils-spec.js +105 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/* eslint-disable no-magic-numbers */
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/* global describe, it, expect, beforeAll */
|
|
6
|
+
|
|
7
|
+
const { Types, ConnectionBase } = require('../../../lib');
|
|
8
|
+
|
|
9
|
+
describe('BooleanType', () => {
|
|
10
|
+
let connection;
|
|
11
|
+
|
|
12
|
+
beforeAll(async () => {
|
|
13
|
+
connection = new ConnectionBase({
|
|
14
|
+
bindModels: false,
|
|
15
|
+
models: require('../../support/models'),
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('toConnectionType', () => {
|
|
20
|
+
it('can convert to connection type when connection is defined', () => {
|
|
21
|
+
let type = new Types.BooleanType();
|
|
22
|
+
expect(type.toConnectionType(connection)).toEqual('BOOLEAN');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
26
|
+
let type = new Types.BooleanType();
|
|
27
|
+
expect(type.toConnectionType()).toEqual('BOOLEAN');
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('toString', () => {
|
|
32
|
+
it('can convert to connection type when connection is defined', () => {
|
|
33
|
+
let type = new Types.BooleanType();
|
|
34
|
+
expect(type.toString(connection)).toEqual('BOOLEAN');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
38
|
+
let type = new Types.BooleanType();
|
|
39
|
+
expect(type.toString()).toEqual('BOOLEAN');
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('can construct from class', () => {
|
|
44
|
+
let type = new Types.BooleanType();
|
|
45
|
+
expect(type.toString()).toEqual('BOOLEAN');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('can construct from type helper', () => {
|
|
49
|
+
let type = Types.BOOLEAN();
|
|
50
|
+
expect(type.toString()).toEqual('BOOLEAN');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('can cast to type', () => {
|
|
54
|
+
let type = Types.BOOLEAN();
|
|
55
|
+
let value = type.castToType({ value: 1 });
|
|
56
|
+
expect(typeof value).toEqual('boolean');
|
|
57
|
+
expect(value).toEqual(true);
|
|
58
|
+
|
|
59
|
+
value = type.castToType({ value: 0 });
|
|
60
|
+
expect(typeof value).toEqual('boolean');
|
|
61
|
+
expect(value).toEqual(false);
|
|
62
|
+
|
|
63
|
+
value = type.castToType({ value: -1 });
|
|
64
|
+
expect(typeof value).toEqual('boolean');
|
|
65
|
+
expect(value).toEqual(true);
|
|
66
|
+
|
|
67
|
+
value = type.castToType({ value: false });
|
|
68
|
+
expect(typeof value).toEqual('boolean');
|
|
69
|
+
expect(value).toEqual(false);
|
|
70
|
+
|
|
71
|
+
value = type.castToType({ value: true });
|
|
72
|
+
expect(typeof value).toEqual('boolean');
|
|
73
|
+
expect(value).toEqual(true);
|
|
74
|
+
|
|
75
|
+
value = type.castToType({ value: 'false' });
|
|
76
|
+
expect(typeof value).toEqual('boolean');
|
|
77
|
+
expect(value).toEqual(false);
|
|
78
|
+
|
|
79
|
+
value = type.castToType({ value: 'true' });
|
|
80
|
+
expect(typeof value).toEqual('boolean');
|
|
81
|
+
expect(value).toEqual(true);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/* eslint-disable no-magic-numbers */
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/* global describe, it, expect, beforeAll */
|
|
6
|
+
|
|
7
|
+
const moment = require('moment');
|
|
8
|
+
const { Types, ConnectionBase } = require('../../../lib');
|
|
9
|
+
|
|
10
|
+
describe('DateType', () => {
|
|
11
|
+
let connection;
|
|
12
|
+
|
|
13
|
+
beforeAll(async () => {
|
|
14
|
+
connection = new ConnectionBase({
|
|
15
|
+
bindModels: false,
|
|
16
|
+
models: require('../../support/models'),
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('toConnectionType', () => {
|
|
21
|
+
it('can convert to connection type when connection is defined', () => {
|
|
22
|
+
let type = new Types.DateType();
|
|
23
|
+
expect(type.toConnectionType(connection)).toEqual('DATE');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
27
|
+
let type = new Types.DateType();
|
|
28
|
+
expect(type.toConnectionType()).toEqual('DATE');
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('toString', () => {
|
|
33
|
+
it('can convert to connection type when connection is defined', () => {
|
|
34
|
+
let type = new Types.DateType();
|
|
35
|
+
expect(type.toString(connection)).toEqual('DATE');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
39
|
+
let type = new Types.DateType();
|
|
40
|
+
expect(type.toString()).toEqual('DATE');
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('can construct from class', () => {
|
|
45
|
+
let type = new Types.DateType();
|
|
46
|
+
expect(type.toString()).toEqual('DATE');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('can construct from type helper', () => {
|
|
50
|
+
let type = Types.DATE();
|
|
51
|
+
expect(type.toString()).toEqual('DATE');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('can cast to type', () => {
|
|
55
|
+
let type = Types.DATE();
|
|
56
|
+
let value = type.castToType({ value: '2001-01-01' });
|
|
57
|
+
expect(value).toBeInstanceOf(moment);
|
|
58
|
+
expect(value.utc(true).toISOString()).toEqual('2001-01-01T00:00:00.000Z');
|
|
59
|
+
|
|
60
|
+
value = type.castToType({ value: undefined });
|
|
61
|
+
expect(value).toBe(undefined);
|
|
62
|
+
|
|
63
|
+
value = type.castToType({ value: null });
|
|
64
|
+
expect(value).toBe(null);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('can cast to type with format', () => {
|
|
68
|
+
let type = Types.DATE('MM.DD.YYYY');
|
|
69
|
+
let value = type.castToType({ value: '07.23.2022' });
|
|
70
|
+
expect(value).toBeInstanceOf(moment);
|
|
71
|
+
expect(value.utc(true).toISOString()).toEqual('2022-07-23T00:00:00.000Z');
|
|
72
|
+
expect(type.serialize(value)).toEqual('07.23.2022');
|
|
73
|
+
|
|
74
|
+
value = type.castToType({ value: undefined });
|
|
75
|
+
expect(value).toBe(undefined);
|
|
76
|
+
|
|
77
|
+
value = type.castToType({ value: null });
|
|
78
|
+
expect(value).toBe(null);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('should throw an error on bad datetime string', () => {
|
|
82
|
+
let type = Types.DATE();
|
|
83
|
+
expect(() => type.castToType({ value: 'derp' })).toThrow(new TypeError('DateType::deserialize: Value provided ("derp") can not be cast into a date.'));
|
|
84
|
+
});
|
|
85
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/* eslint-disable no-magic-numbers */
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/* global describe, it, expect, beforeAll */
|
|
6
|
+
|
|
7
|
+
const moment = require('moment');
|
|
8
|
+
const { Types, ConnectionBase } = require('../../../lib');
|
|
9
|
+
|
|
10
|
+
describe('DateTimeType', () => {
|
|
11
|
+
let connection;
|
|
12
|
+
|
|
13
|
+
beforeAll(async () => {
|
|
14
|
+
connection = new ConnectionBase({
|
|
15
|
+
bindModels: false,
|
|
16
|
+
models: require('../../support/models'),
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('toConnectionType', () => {
|
|
21
|
+
it('can convert to connection type when connection is defined', () => {
|
|
22
|
+
let type = new Types.DateTimeType();
|
|
23
|
+
expect(type.toConnectionType(connection)).toEqual('DATETIME');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
27
|
+
let type = new Types.DateTimeType();
|
|
28
|
+
expect(type.toConnectionType()).toEqual('DATETIME');
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('toString', () => {
|
|
33
|
+
it('can convert to connection type when connection is defined', () => {
|
|
34
|
+
let type = new Types.DateTimeType();
|
|
35
|
+
expect(type.toString(connection)).toEqual('DATETIME');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
39
|
+
let type = new Types.DateTimeType();
|
|
40
|
+
expect(type.toString()).toEqual('DATETIME');
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('can construct from class', () => {
|
|
45
|
+
let type = new Types.DateTimeType();
|
|
46
|
+
expect(type.toString()).toEqual('DATETIME');
|
|
47
|
+
expect(type.length).toBe(null);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('can construct from type helper', () => {
|
|
51
|
+
let type = Types.DATETIME(6);
|
|
52
|
+
expect(type.toString()).toEqual('DATETIME');
|
|
53
|
+
expect(type.length).toBe(6);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('can cast to type', () => {
|
|
57
|
+
let type = Types.DATETIME();
|
|
58
|
+
let value = type.castToType({ value: '2001-01-01' });
|
|
59
|
+
expect(value).toBeInstanceOf(moment);
|
|
60
|
+
expect(value.utc(true).toISOString()).toEqual('2001-01-01T00:00:00.000Z');
|
|
61
|
+
|
|
62
|
+
value = type.castToType({ value: undefined });
|
|
63
|
+
expect(value).toBe(undefined);
|
|
64
|
+
|
|
65
|
+
value = type.castToType({ value: null });
|
|
66
|
+
expect(value).toBe(null);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('can cast to type with format', () => {
|
|
70
|
+
let type = Types.DATETIME(null, 'MM.DD.YYYY / HH:mm:ss.SSS');
|
|
71
|
+
let value = type.castToType({ value: '07.23.2022 / 10:15:45.125' });
|
|
72
|
+
expect(value).toBeInstanceOf(moment);
|
|
73
|
+
expect(value.utc(true).toISOString()).toEqual('2022-07-23T10:15:45.125Z');
|
|
74
|
+
expect(type.serialize(value)).toEqual('07.23.2022 / 10:15:45.125');
|
|
75
|
+
|
|
76
|
+
value = type.castToType({ value: undefined });
|
|
77
|
+
expect(value).toBe(undefined);
|
|
78
|
+
|
|
79
|
+
value = type.castToType({ value: null });
|
|
80
|
+
expect(value).toBe(null);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should throw an error on bad datetime string', () => {
|
|
84
|
+
let type = Types.DATETIME();
|
|
85
|
+
expect(() => type.castToType({ value: 'derp' })).toThrow(new TypeError('DateType::deserialize: Value provided ("derp") can not be cast into a date.'));
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/* eslint-disable no-magic-numbers */
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/* global describe, it, expect, beforeAll */
|
|
6
|
+
|
|
7
|
+
const { Types, ConnectionBase } = require('../../../lib');
|
|
8
|
+
|
|
9
|
+
describe('FloatType', () => {
|
|
10
|
+
let connection;
|
|
11
|
+
|
|
12
|
+
beforeAll(async () => {
|
|
13
|
+
connection = new ConnectionBase({
|
|
14
|
+
bindModels: false,
|
|
15
|
+
models: require('../../support/models'),
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('toConnectionType', () => {
|
|
20
|
+
it('can convert to connection type when connection is defined', () => {
|
|
21
|
+
let type = new Types.FloatType();
|
|
22
|
+
expect(type.toConnectionType(connection)).toEqual('FLOAT');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
26
|
+
let type = new Types.FloatType();
|
|
27
|
+
expect(type.toConnectionType()).toEqual('FLOAT');
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('toString', () => {
|
|
32
|
+
it('can convert to connection type when connection is defined', () => {
|
|
33
|
+
let type = new Types.FloatType();
|
|
34
|
+
expect(type.toString(connection)).toEqual('FLOAT');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
38
|
+
let type = new Types.FloatType();
|
|
39
|
+
expect(type.toString()).toEqual('FLOAT');
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('can construct from class', () => {
|
|
44
|
+
let type = new Types.FloatType();
|
|
45
|
+
expect(type.toString()).toEqual('FLOAT');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('can construct from type helper', () => {
|
|
49
|
+
let type = Types.FLOAT();
|
|
50
|
+
expect(type.toString()).toEqual('FLOAT');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('can cast to type', () => {
|
|
54
|
+
let type = Types.FLOAT();
|
|
55
|
+
let value = type.castToType({ value: '1234' });
|
|
56
|
+
expect(typeof value).toEqual('number');
|
|
57
|
+
expect(value).toEqual(1234);
|
|
58
|
+
|
|
59
|
+
value = type.castToType({ value: '-1234.6' });
|
|
60
|
+
expect(typeof value).toEqual('number');
|
|
61
|
+
expect(value).toEqual(-1234.6);
|
|
62
|
+
|
|
63
|
+
value = type.castToType({ value: undefined });
|
|
64
|
+
expect(value).toBe(undefined);
|
|
65
|
+
|
|
66
|
+
value = type.castToType({ value: null });
|
|
67
|
+
expect(value).toBe(null);
|
|
68
|
+
|
|
69
|
+
expect(() => type.castToType({ value: 'derp' })).toThrow(new TypeError('FloatType::castToType: Value provided ("derp") can not be cast into an floating point number.'));
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/* eslint-disable no-magic-numbers */
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/* global describe, it, expect, beforeAll */
|
|
6
|
+
|
|
7
|
+
const Model = require('../../../lib/model');
|
|
8
|
+
const {
|
|
9
|
+
Types,
|
|
10
|
+
ConnectionBase,
|
|
11
|
+
} = require('../../../lib');
|
|
12
|
+
|
|
13
|
+
describe('ForeignKeyType', () => {
|
|
14
|
+
let connection;
|
|
15
|
+
let FKModel;
|
|
16
|
+
|
|
17
|
+
beforeAll(async () => {
|
|
18
|
+
connection = new ConnectionBase({
|
|
19
|
+
bindModels: false,
|
|
20
|
+
models: Object.assign({}, require('../../support/models'), {
|
|
21
|
+
FKModel: class FKModel extends Model {
|
|
22
|
+
static fields = {
|
|
23
|
+
'id': {
|
|
24
|
+
type: Types.UUIDV4,
|
|
25
|
+
defaultValue: Types.UUIDV4.Default.UUIDV4,
|
|
26
|
+
allowNull: false,
|
|
27
|
+
primaryKey: true,
|
|
28
|
+
},
|
|
29
|
+
'userID': {
|
|
30
|
+
type: Types.FOREIGN_KEY('User:id'),
|
|
31
|
+
allowNull: true,
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
}),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
let models = connection.getModels();
|
|
39
|
+
|
|
40
|
+
FKModel = models.FKModel;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('toString', () => {
|
|
44
|
+
it('should be empty if a connection is provided', () => {
|
|
45
|
+
let type = FKModel.fields.userID.type;
|
|
46
|
+
expect(type.toString(connection)).toEqual('VARCHAR(36)');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should display field type without any arguments', () => {
|
|
50
|
+
let type = FKModel.fields.userID.type;
|
|
51
|
+
expect(type.toString()).toEqual('ForeignKeyType {}');
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('can construct from class', () => {
|
|
56
|
+
let type = new Types.ForeignKeyType('User:id');
|
|
57
|
+
expect(type.toString()).toEqual('ForeignKeyType {}');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('can construct from type helper', () => {
|
|
61
|
+
let type = Types.FOREIGN_KEY('User:id');
|
|
62
|
+
expect(type.toString()).toEqual('ForeignKeyType {}');
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/* eslint-disable no-magic-numbers */
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/* global describe, it, expect, beforeAll */
|
|
6
|
+
|
|
7
|
+
const { Types, ConnectionBase } = require('../../../lib');
|
|
8
|
+
|
|
9
|
+
describe('IntegerType', () => {
|
|
10
|
+
let connection;
|
|
11
|
+
|
|
12
|
+
beforeAll(async () => {
|
|
13
|
+
connection = new ConnectionBase({
|
|
14
|
+
bindModels: false,
|
|
15
|
+
models: require('../../support/models'),
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('toConnectionType', () => {
|
|
20
|
+
it('can convert to connection type when connection is defined', () => {
|
|
21
|
+
let type = new Types.IntegerType();
|
|
22
|
+
expect(type.toConnectionType(connection)).toEqual('INTEGER');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
26
|
+
let type = new Types.IntegerType();
|
|
27
|
+
expect(type.toConnectionType()).toEqual('INTEGER');
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('toString', () => {
|
|
32
|
+
it('can convert to connection type when connection is defined', () => {
|
|
33
|
+
let type = new Types.IntegerType();
|
|
34
|
+
expect(type.toString(connection)).toEqual('INTEGER');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
38
|
+
let type = new Types.IntegerType();
|
|
39
|
+
expect(type.toString()).toEqual('INTEGER');
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('can construct from class', () => {
|
|
44
|
+
let type = new Types.IntegerType();
|
|
45
|
+
expect(type.toString()).toEqual('INTEGER');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('can construct from type helper', () => {
|
|
49
|
+
let type = Types.INTEGER();
|
|
50
|
+
expect(type.toString()).toEqual('INTEGER');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('can cast to type', () => {
|
|
54
|
+
let type = Types.INTEGER();
|
|
55
|
+
let value = type.castToType({ value: '1234' });
|
|
56
|
+
expect(typeof value).toEqual('number');
|
|
57
|
+
expect(value).toEqual(1234);
|
|
58
|
+
|
|
59
|
+
value = type.castToType({ value: '-1234.6' });
|
|
60
|
+
expect(typeof value).toEqual('number');
|
|
61
|
+
expect(value).toEqual(-1235);
|
|
62
|
+
|
|
63
|
+
value = type.castToType({ value: undefined });
|
|
64
|
+
expect(value).toBe(undefined);
|
|
65
|
+
|
|
66
|
+
value = type.castToType({ value: null });
|
|
67
|
+
expect(value).toBe(null);
|
|
68
|
+
|
|
69
|
+
expect(() => type.castToType({ value: 'derp' })).toThrow(new TypeError('IntegerType::castToType: Value provided ("derp") can not be cast into an integer.'));
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/* eslint-disable no-magic-numbers */
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/* global describe, it, expect, beforeAll */
|
|
6
|
+
|
|
7
|
+
const { Types, ConnectionBase } = require('../../../lib');
|
|
8
|
+
|
|
9
|
+
describe('StringType', () => {
|
|
10
|
+
let connection;
|
|
11
|
+
|
|
12
|
+
beforeAll(async () => {
|
|
13
|
+
connection = new ConnectionBase({
|
|
14
|
+
bindModels: false,
|
|
15
|
+
models: require('../../support/models'),
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('toConnectionType', () => {
|
|
20
|
+
it('can convert to connection type when connection is defined', () => {
|
|
21
|
+
let type = new Types.StringType();
|
|
22
|
+
expect(type.toConnectionType(connection)).toEqual('VARCHAR(256)');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('can convert to connection type when connection is defined with length', () => {
|
|
26
|
+
let type = new Types.StringType(64);
|
|
27
|
+
expect(type.toConnectionType(connection)).toEqual('VARCHAR(64)');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
31
|
+
let type = new Types.StringType();
|
|
32
|
+
expect(type.toConnectionType()).toEqual('VARCHAR(256)');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('can convert to connection type when connection is undefined with length', () => {
|
|
36
|
+
let type = new Types.StringType(64);
|
|
37
|
+
expect(type.toConnectionType()).toEqual('VARCHAR(64)');
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe('toString', () => {
|
|
42
|
+
it('can convert to connection type when connection is defined', () => {
|
|
43
|
+
let type = new Types.StringType();
|
|
44
|
+
expect(type.toString(connection)).toEqual('VARCHAR(256)');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('can convert to connection type when connection is defined with length', () => {
|
|
48
|
+
let type = new Types.StringType(64);
|
|
49
|
+
expect(type.toString(connection)).toEqual('VARCHAR(64)');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
53
|
+
let type = new Types.StringType();
|
|
54
|
+
expect(type.toString()).toEqual('VARCHAR(256)');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('can convert to connection type when connection is undefined with length', () => {
|
|
58
|
+
let type = new Types.StringType(64);
|
|
59
|
+
expect(type.toString()).toEqual('VARCHAR(64)');
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('can construct from class', () => {
|
|
64
|
+
let type = new Types.StringType(123);
|
|
65
|
+
expect(type.toString()).toEqual('VARCHAR(123)');
|
|
66
|
+
expect(type.length).toBe(123);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('can construct from type helper', () => {
|
|
70
|
+
let type = Types.STRING(123);
|
|
71
|
+
expect(type.toString()).toEqual('VARCHAR(123)');
|
|
72
|
+
expect(type.length).toBe(123);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('can cast to type', () => {
|
|
76
|
+
let type = Types.STRING(123);
|
|
77
|
+
let value = type.castToType({ value: 123 });
|
|
78
|
+
expect(typeof value).toEqual('string');
|
|
79
|
+
expect(value).toEqual('123');
|
|
80
|
+
|
|
81
|
+
value = type.castToType({ value: {} });
|
|
82
|
+
expect(typeof value).toEqual('string');
|
|
83
|
+
expect(value).toEqual('[object Object]');
|
|
84
|
+
|
|
85
|
+
value = type.castToType({ value: undefined });
|
|
86
|
+
expect(value).toBe(undefined);
|
|
87
|
+
|
|
88
|
+
value = type.castToType({ value: null });
|
|
89
|
+
expect(value).toBe(null);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/* eslint-disable no-magic-numbers */
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/* global describe, it, expect, beforeAll */
|
|
6
|
+
|
|
7
|
+
const { Types, ConnectionBase } = require('../../../lib');
|
|
8
|
+
const { UUID_REGEXP } = require('../../support/test-helpers');
|
|
9
|
+
|
|
10
|
+
describe('UUIDV1Type', () => {
|
|
11
|
+
let connection;
|
|
12
|
+
|
|
13
|
+
beforeAll(async () => {
|
|
14
|
+
connection = new ConnectionBase({
|
|
15
|
+
bindModels: false,
|
|
16
|
+
models: require('../../support/models'),
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('toConnectionType', () => {
|
|
21
|
+
it('can convert to connection type when connection is defined', () => {
|
|
22
|
+
let type = new Types.UUIDV1Type();
|
|
23
|
+
expect(type.toConnectionType(connection)).toEqual('VARCHAR(36)');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
27
|
+
let type = new Types.UUIDV1Type();
|
|
28
|
+
expect(type.toConnectionType()).toEqual('VARCHAR(36)');
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('toString', () => {
|
|
33
|
+
it('can convert to connection type when connection is defined', () => {
|
|
34
|
+
let type = new Types.UUIDV1Type();
|
|
35
|
+
expect(type.toString(connection)).toEqual('VARCHAR(36)');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
39
|
+
let type = new Types.UUIDV1Type();
|
|
40
|
+
expect(type.toString()).toEqual('VARCHAR(36)');
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('can construct from class', () => {
|
|
45
|
+
let type = new Types.UUIDV1Type();
|
|
46
|
+
expect(type.toString()).toEqual('VARCHAR(36)');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('can construct from type helper', () => {
|
|
50
|
+
let type = Types.UUIDV1();
|
|
51
|
+
expect(type.toString()).toEqual('VARCHAR(36)');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('can cast to type', () => {
|
|
55
|
+
let type = Types.UUIDV1({
|
|
56
|
+
node: [ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab ],
|
|
57
|
+
clockseq: 0x1234,
|
|
58
|
+
msecs: new Date('2011-11-01').getTime(),
|
|
59
|
+
nsecs: 5678,
|
|
60
|
+
random: new Array(16),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
expect(() => type.castToType({ value: '1234' })).toThrow(new TypeError('UUIDV1Type::castToType: Provided value "1234" is not a valid UUID.'));
|
|
64
|
+
|
|
65
|
+
let uuid = Types.UUIDV1.Default.UUIDV1({ field: { type } });
|
|
66
|
+
|
|
67
|
+
expect(uuid).toMatch(UUID_REGEXP);
|
|
68
|
+
expect(type.castToType({ value: uuid })).toEqual(uuid);
|
|
69
|
+
|
|
70
|
+
expect(type.castToType({ value: undefined })).toBe(undefined);
|
|
71
|
+
expect(type.castToType({ value: null })).toBe(null);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/* eslint-disable no-magic-numbers */
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/* global describe, it, expect, beforeAll */
|
|
6
|
+
|
|
7
|
+
const { Types, ConnectionBase } = require('../../../lib');
|
|
8
|
+
const { UUID_REGEXP } = require('../../support/test-helpers');
|
|
9
|
+
|
|
10
|
+
describe('UUIDV4Type', () => {
|
|
11
|
+
let connection;
|
|
12
|
+
|
|
13
|
+
beforeAll(async () => {
|
|
14
|
+
connection = new ConnectionBase({
|
|
15
|
+
bindModels: false,
|
|
16
|
+
models: require('../../support/models'),
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('toConnectionType', () => {
|
|
21
|
+
it('can convert to connection type when connection is defined', () => {
|
|
22
|
+
let type = new Types.UUIDV4Type();
|
|
23
|
+
expect(type.toConnectionType(connection)).toEqual('VARCHAR(36)');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
27
|
+
let type = new Types.UUIDV4Type();
|
|
28
|
+
expect(type.toConnectionType()).toEqual('VARCHAR(36)');
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('toString', () => {
|
|
33
|
+
it('can convert to connection type when connection is defined', () => {
|
|
34
|
+
let type = new Types.UUIDV4Type();
|
|
35
|
+
expect(type.toString(connection)).toEqual('VARCHAR(36)');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('can convert to connection type when connection is undefined', () => {
|
|
39
|
+
let type = new Types.UUIDV4Type();
|
|
40
|
+
expect(type.toString()).toEqual('VARCHAR(36)');
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('can construct from class', () => {
|
|
45
|
+
let type = new Types.UUIDV4Type();
|
|
46
|
+
expect(type.toString()).toEqual('VARCHAR(36)');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('can construct from type helper', () => {
|
|
50
|
+
let type = Types.UUIDV4();
|
|
51
|
+
expect(type.toString()).toEqual('VARCHAR(36)');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('can cast to type', () => {
|
|
55
|
+
let type = Types.UUIDV4();
|
|
56
|
+
expect(() => type.castToType({ value: '1234' })).toThrow(new TypeError('UUIDV4Type::castToType: Provided value "1234" is not a valid UUID.'));
|
|
57
|
+
|
|
58
|
+
let uuid = Types.UUIDV4.Default.UUIDV4({ field: { type } });
|
|
59
|
+
expect(uuid).toMatch(UUID_REGEXP);
|
|
60
|
+
expect(type.castToType({ value: uuid })).toEqual(uuid);
|
|
61
|
+
|
|
62
|
+
expect(type.castToType({ value: undefined })).toBe(undefined);
|
|
63
|
+
expect(type.castToType({ value: null })).toBe(null);
|
|
64
|
+
});
|
|
65
|
+
});
|