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,173 @@
|
|
|
1
|
+
/* eslint-disable max-classes-per-file */
|
|
2
|
+
/* eslint-disable new-cap */
|
|
3
|
+
/* eslint-disable no-magic-numbers */
|
|
4
|
+
|
|
5
|
+
'use strict';
|
|
6
|
+
|
|
7
|
+
/* global describe, it, expect */
|
|
8
|
+
|
|
9
|
+
const ProxyClass = require('../../lib/proxy-class');
|
|
10
|
+
|
|
11
|
+
describe('ProxyClass', () => {
|
|
12
|
+
it('can overload getter', () => {
|
|
13
|
+
let props = [];
|
|
14
|
+
let derpCalled = false;
|
|
15
|
+
let helloCalled = false;
|
|
16
|
+
|
|
17
|
+
class ProxyTest extends ProxyClass {
|
|
18
|
+
[ProxyClass.GET](target, prop) {
|
|
19
|
+
props.push(prop);
|
|
20
|
+
return super[ProxyClass.GET].apply(this, arguments);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
derp() {
|
|
24
|
+
derpCalled = true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
hello() {
|
|
28
|
+
helloCalled = true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let test = new ProxyTest();
|
|
33
|
+
test.derp();
|
|
34
|
+
test.hello();
|
|
35
|
+
|
|
36
|
+
expect(props).toEqual([ 'derp', 'hello' ]);
|
|
37
|
+
expect(derpCalled).toBe(true);
|
|
38
|
+
expect(helloCalled).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('can overload setter', () => {
|
|
42
|
+
let props = [];
|
|
43
|
+
|
|
44
|
+
class ProxyTest extends ProxyClass {
|
|
45
|
+
[ProxyClass.SET](target, prop, value, receiver) {
|
|
46
|
+
props.push(prop);
|
|
47
|
+
return super[ProxyClass.SET].apply(this, [ target, prop, `${value}_modified`, receiver ]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let test = new ProxyTest();
|
|
52
|
+
test.derp = 'stuff';
|
|
53
|
+
test.hello = 'world';
|
|
54
|
+
|
|
55
|
+
expect(props).toEqual([ 'derp', 'hello' ]);
|
|
56
|
+
expect(test.derp).toEqual('stuff_modified');
|
|
57
|
+
expect(test.hello).toEqual('world_modified');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('can be callable', () => {
|
|
61
|
+
let givenArgs;
|
|
62
|
+
|
|
63
|
+
class ProxyTest extends ProxyClass {
|
|
64
|
+
[ProxyClass.CALLABLE](...args) {
|
|
65
|
+
givenArgs = args;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let test = new ProxyTest();
|
|
70
|
+
test('hello', 'world');
|
|
71
|
+
|
|
72
|
+
expect(givenArgs).toEqual([ 'hello', 'world' ]);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('can modify it to be callable', () => {
|
|
76
|
+
let givenArgs;
|
|
77
|
+
|
|
78
|
+
class ProxyTest extends ProxyClass {
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let test = new ProxyTest();
|
|
82
|
+
expect(() => test('hello', 'world')).toThrow(new TypeError('test is not a function'));
|
|
83
|
+
|
|
84
|
+
test = test.__call((...args) => {
|
|
85
|
+
givenArgs = args;
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('hello', 'world');
|
|
89
|
+
expect(givenArgs).toEqual([ 'hello', 'world' ]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('can setup an autocaller', () => {
|
|
93
|
+
let autoCallArgs;
|
|
94
|
+
let givenArgs;
|
|
95
|
+
|
|
96
|
+
class ProxyTestResult extends ProxyClass {
|
|
97
|
+
hello() {
|
|
98
|
+
return 'world';
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
class ProxyTest extends ProxyClass {
|
|
103
|
+
getHello() {
|
|
104
|
+
return new ProxyTestResult().__call((...args) => {
|
|
105
|
+
givenArgs = args;
|
|
106
|
+
}).__autoCall((...args) => {
|
|
107
|
+
autoCallArgs = args;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Should not call autocaller
|
|
113
|
+
let test = new ProxyTest();
|
|
114
|
+
test.getHello()('hello', 'world');
|
|
115
|
+
|
|
116
|
+
expect(givenArgs).toEqual([ 'hello', 'world' ]);
|
|
117
|
+
expect(autoCallArgs).toBe(undefined);
|
|
118
|
+
|
|
119
|
+
// Should call autocaller
|
|
120
|
+
givenArgs = undefined;
|
|
121
|
+
autoCallArgs = undefined;
|
|
122
|
+
|
|
123
|
+
test = new ProxyTest();
|
|
124
|
+
|
|
125
|
+
// .stuff should trigger autocall
|
|
126
|
+
let magic = test.getHello();
|
|
127
|
+
magic.stuff;
|
|
128
|
+
|
|
129
|
+
expect(givenArgs).toBe(undefined);
|
|
130
|
+
expect(autoCallArgs).toEqual([]);
|
|
131
|
+
|
|
132
|
+
// Should not call autocaller a second time
|
|
133
|
+
givenArgs = undefined;
|
|
134
|
+
autoCallArgs = undefined;
|
|
135
|
+
magic.derp;
|
|
136
|
+
|
|
137
|
+
expect(givenArgs).toBe(undefined);
|
|
138
|
+
expect(autoCallArgs).toBe(undefined);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('can specify a method as autocall', () => {
|
|
142
|
+
let givenArgs;
|
|
143
|
+
let callCount = 0;
|
|
144
|
+
|
|
145
|
+
class ProxyTest extends ProxyClass {
|
|
146
|
+
NOT = ProxyClass.autoCall(function(...args) {
|
|
147
|
+
callCount++;
|
|
148
|
+
givenArgs = args;
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Should not be called just by fetching the method name
|
|
153
|
+
let test = new ProxyTest();
|
|
154
|
+
test.NOT;
|
|
155
|
+
|
|
156
|
+
expect(callCount).toEqual(0);
|
|
157
|
+
|
|
158
|
+
// Should be called once when called directly
|
|
159
|
+
callCount = 0;
|
|
160
|
+
|
|
161
|
+
test.NOT('hello', 'world');
|
|
162
|
+
expect(callCount).toEqual(1);
|
|
163
|
+
expect(givenArgs).toEqual([ 'hello', 'world' ]);
|
|
164
|
+
|
|
165
|
+
// Should be called once when called indirectly
|
|
166
|
+
callCount = 0;
|
|
167
|
+
givenArgs = undefined;
|
|
168
|
+
|
|
169
|
+
test.NOT.derp;
|
|
170
|
+
expect(callCount).toEqual(1);
|
|
171
|
+
expect(givenArgs).toEqual([]);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"operator": "MODEL",
|
|
4
|
+
"modelName": "User",
|
|
5
|
+
"dialect": "none",
|
|
6
|
+
"rootModelName": "User",
|
|
7
|
+
"currentScopeName": "model"
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"operator": "FIELD",
|
|
11
|
+
"fieldName": "id",
|
|
12
|
+
"Field": {
|
|
13
|
+
"type": {},
|
|
14
|
+
"allowNull": false,
|
|
15
|
+
"primaryKey": true,
|
|
16
|
+
"fieldName": "id",
|
|
17
|
+
"columnName": "id"
|
|
18
|
+
},
|
|
19
|
+
"rootFieldName": "id",
|
|
20
|
+
"rootField": {
|
|
21
|
+
"type": {},
|
|
22
|
+
"allowNull": false,
|
|
23
|
+
"primaryKey": true,
|
|
24
|
+
"fieldName": "id",
|
|
25
|
+
"columnName": "id"
|
|
26
|
+
},
|
|
27
|
+
"currentScopeName": "field"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"value": 1,
|
|
31
|
+
"condition": true,
|
|
32
|
+
"operator": "EQ",
|
|
33
|
+
"inverseOperator": "NEQ"
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"logical": true,
|
|
37
|
+
"operator": "AND",
|
|
38
|
+
"and": true,
|
|
39
|
+
"or": false,
|
|
40
|
+
"not": false
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
"operator": "FIELD",
|
|
44
|
+
"fieldName": "firstName",
|
|
45
|
+
"Field": {
|
|
46
|
+
"type": {
|
|
47
|
+
"length": 64
|
|
48
|
+
},
|
|
49
|
+
"allowNull": true,
|
|
50
|
+
"index": true,
|
|
51
|
+
"fieldName": "firstName",
|
|
52
|
+
"columnName": "firstName"
|
|
53
|
+
},
|
|
54
|
+
"currentScopeName": "field"
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"value": "Test",
|
|
58
|
+
"condition": true,
|
|
59
|
+
"operator": "EQ",
|
|
60
|
+
"inverseOperator": "NEQ"
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
"logical": true,
|
|
64
|
+
"operator": "AND",
|
|
65
|
+
"and": true,
|
|
66
|
+
"or": false,
|
|
67
|
+
"not": false
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
"logical": true,
|
|
71
|
+
"operator": "NOT",
|
|
72
|
+
"not": true
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"operator": "FIELD",
|
|
76
|
+
"fieldName": "lastName",
|
|
77
|
+
"Field": {
|
|
78
|
+
"type": {
|
|
79
|
+
"length": 64
|
|
80
|
+
},
|
|
81
|
+
"allowNull": true,
|
|
82
|
+
"index": true,
|
|
83
|
+
"fieldName": "lastName",
|
|
84
|
+
"columnName": "lastName"
|
|
85
|
+
},
|
|
86
|
+
"currentScopeName": "field"
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"value": "Stuff",
|
|
90
|
+
"condition": true,
|
|
91
|
+
"operator": "EQ",
|
|
92
|
+
"inverseOperator": "NEQ"
|
|
93
|
+
}
|
|
94
|
+
]
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"operator": "MODEL",
|
|
4
|
+
"modelName": "User",
|
|
5
|
+
"dialect": "none",
|
|
6
|
+
"rootModelName": "User",
|
|
7
|
+
"currentScopeName": "model"
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"operator": "FIELD",
|
|
11
|
+
"fieldName": "id",
|
|
12
|
+
"Field": {
|
|
13
|
+
"type": {},
|
|
14
|
+
"allowNull": false,
|
|
15
|
+
"primaryKey": true,
|
|
16
|
+
"fieldName": "id",
|
|
17
|
+
"columnName": "id"
|
|
18
|
+
},
|
|
19
|
+
"rootFieldName": "id",
|
|
20
|
+
"rootField": {
|
|
21
|
+
"type": {},
|
|
22
|
+
"allowNull": false,
|
|
23
|
+
"primaryKey": true,
|
|
24
|
+
"fieldName": "id",
|
|
25
|
+
"columnName": "id"
|
|
26
|
+
},
|
|
27
|
+
"currentScopeName": "field"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"value": "test",
|
|
31
|
+
"condition": true,
|
|
32
|
+
"operator": "EQ",
|
|
33
|
+
"inverseOperator": "NEQ"
|
|
34
|
+
}
|
|
35
|
+
]
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"operator": "MODEL",
|
|
4
|
+
"modelName": "User",
|
|
5
|
+
"dialect": "none",
|
|
6
|
+
"rootModelName": "User",
|
|
7
|
+
"currentScopeName": "model"
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"operator": "FIELD",
|
|
11
|
+
"fieldName": "id",
|
|
12
|
+
"Field": {
|
|
13
|
+
"type": {},
|
|
14
|
+
"allowNull": false,
|
|
15
|
+
"primaryKey": true,
|
|
16
|
+
"fieldName": "id",
|
|
17
|
+
"columnName": "id"
|
|
18
|
+
},
|
|
19
|
+
"rootFieldName": "id",
|
|
20
|
+
"rootField": {
|
|
21
|
+
"type": {},
|
|
22
|
+
"allowNull": false,
|
|
23
|
+
"primaryKey": true,
|
|
24
|
+
"fieldName": "id",
|
|
25
|
+
"columnName": "id"
|
|
26
|
+
},
|
|
27
|
+
"currentScopeName": "field"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"value": "test",
|
|
31
|
+
"condition": true,
|
|
32
|
+
"operator": "EQ",
|
|
33
|
+
"inverseOperator": "NEQ"
|
|
34
|
+
}
|
|
35
|
+
]
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"operator": "MODEL",
|
|
4
|
+
"modelName": "ScopedUser",
|
|
5
|
+
"dialect": "none",
|
|
6
|
+
"rootModelName": "ScopedUser",
|
|
7
|
+
"currentScopeName": "model"
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"operator": "FIELD",
|
|
11
|
+
"fieldName": "firstName",
|
|
12
|
+
"Field": {
|
|
13
|
+
"type": {
|
|
14
|
+
"length": 64
|
|
15
|
+
},
|
|
16
|
+
"allowNull": true,
|
|
17
|
+
"index": true,
|
|
18
|
+
"fieldName": "firstName",
|
|
19
|
+
"columnName": "firstName"
|
|
20
|
+
},
|
|
21
|
+
"rootFieldName": "firstName",
|
|
22
|
+
"rootField": {
|
|
23
|
+
"type": {
|
|
24
|
+
"length": 64
|
|
25
|
+
},
|
|
26
|
+
"allowNull": true,
|
|
27
|
+
"index": true,
|
|
28
|
+
"fieldName": "firstName",
|
|
29
|
+
"columnName": "firstName"
|
|
30
|
+
},
|
|
31
|
+
"currentScopeName": "field"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"value": "Bob",
|
|
35
|
+
"condition": true,
|
|
36
|
+
"operator": "EQ",
|
|
37
|
+
"inverseOperator": "NEQ"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"operator": "FIELD",
|
|
41
|
+
"fieldName": "id",
|
|
42
|
+
"Field": {
|
|
43
|
+
"type": {},
|
|
44
|
+
"allowNull": false,
|
|
45
|
+
"primaryKey": true,
|
|
46
|
+
"fieldName": "id",
|
|
47
|
+
"columnName": "id"
|
|
48
|
+
},
|
|
49
|
+
"currentScopeName": "field"
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
"value": "test",
|
|
53
|
+
"condition": true,
|
|
54
|
+
"operator": "EQ",
|
|
55
|
+
"inverseOperator": "NEQ"
|
|
56
|
+
}
|
|
57
|
+
]
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"operator": "MODEL",
|
|
4
|
+
"modelName": "ScopedUser",
|
|
5
|
+
"dialect": "none",
|
|
6
|
+
"rootModelName": "ScopedUser",
|
|
7
|
+
"currentScopeName": "model"
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"operator": "FIELD",
|
|
11
|
+
"fieldName": "id",
|
|
12
|
+
"Field": {
|
|
13
|
+
"type": {},
|
|
14
|
+
"allowNull": false,
|
|
15
|
+
"primaryKey": true,
|
|
16
|
+
"fieldName": "id",
|
|
17
|
+
"columnName": "id"
|
|
18
|
+
},
|
|
19
|
+
"rootFieldName": "id",
|
|
20
|
+
"rootField": {
|
|
21
|
+
"type": {},
|
|
22
|
+
"allowNull": false,
|
|
23
|
+
"primaryKey": true,
|
|
24
|
+
"fieldName": "id",
|
|
25
|
+
"columnName": "id"
|
|
26
|
+
},
|
|
27
|
+
"currentScopeName": "field"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"value": "test",
|
|
31
|
+
"condition": true,
|
|
32
|
+
"operator": "EQ",
|
|
33
|
+
"inverseOperator": "NEQ"
|
|
34
|
+
}
|
|
35
|
+
]
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/* eslint-disable no-magic-numbers */
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/* global describe, it, expect, beforeAll */
|
|
6
|
+
|
|
7
|
+
const Nife = require('nife');
|
|
8
|
+
const { ConnectionBase, QueryEngine } = require('../../lib');
|
|
9
|
+
const matchesSnapshot = require('../support/snapshots');
|
|
10
|
+
|
|
11
|
+
describe('QueryEngine', () => {
|
|
12
|
+
let connection;
|
|
13
|
+
let User;
|
|
14
|
+
let ScopedUser;
|
|
15
|
+
|
|
16
|
+
beforeAll(() => {
|
|
17
|
+
connection = new ConnectionBase({
|
|
18
|
+
bindModels: false,
|
|
19
|
+
models: require('../support/models'),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
let models = connection.getModels();
|
|
23
|
+
User = models.User;
|
|
24
|
+
ScopedUser = models.ScopedUser;
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const getFilteredContext = (parts) => {
|
|
28
|
+
return parts.map((part) => {
|
|
29
|
+
return Object.assign({}, part, {
|
|
30
|
+
contextID: undefined,
|
|
31
|
+
modelContext: undefined,
|
|
32
|
+
fieldContext: undefined,
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// fdescribe('operations', () => {
|
|
38
|
+
// it('can live happily without a connection', () => {
|
|
39
|
+
// expect((new QueryEngine().User.id('derp'))._getRawQuery()).toBe([ ]);
|
|
40
|
+
// });
|
|
41
|
+
// });
|
|
42
|
+
|
|
43
|
+
describe('isQueryContext', () => {
|
|
44
|
+
it('can validly detect a query context', () => {
|
|
45
|
+
expect(QueryEngine.isQueryContext({})).toBe(false);
|
|
46
|
+
expect(QueryEngine.isQueryContext(User.where)).toBe(false);
|
|
47
|
+
expect(QueryEngine.isQueryContext(User.where._getRawQueryContext())).toBe(true);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('queryContextType', () => {
|
|
52
|
+
it('can validly detect a query context type', () => {
|
|
53
|
+
expect(QueryEngine.queryContextType(User.where._getRawQueryContext())).toEqual({
|
|
54
|
+
hasCondition: false,
|
|
55
|
+
hasField: false,
|
|
56
|
+
hasModel: true,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
expect(QueryEngine.queryContextType(User.where.id._getRawQueryContext())).toEqual({
|
|
60
|
+
hasCondition: false,
|
|
61
|
+
hasField: true,
|
|
62
|
+
hasModel: true,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
expect(QueryEngine.queryContextType(User.where.id.EQ('test')._getRawQueryContext())).toEqual({
|
|
66
|
+
hasCondition: true,
|
|
67
|
+
hasField: true,
|
|
68
|
+
hasModel: true,
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe('query operations and chaining', () => {
|
|
74
|
+
it('can set a default scope on a model', () => {
|
|
75
|
+
let context = ScopedUser.where.id.EQ('test')._getRawQuery();
|
|
76
|
+
expect(matchesSnapshot(getFilteredContext(context))).toEqual(true);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('can unscope default scope on a model', () => {
|
|
80
|
+
let context = ScopedUser.where.unscoped().id.EQ('test')._getRawQuery();
|
|
81
|
+
expect(matchesSnapshot(getFilteredContext(context))).toEqual(true);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('can construct a query context with a model sub-key', () => {
|
|
85
|
+
let context = User.where.id.EQ('test')._getRawQuery();
|
|
86
|
+
expect(matchesSnapshot(getFilteredContext(context))).toEqual(true);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('can construct a query context with a model call', () => {
|
|
90
|
+
let context = User.where('id').EQ('test')._getRawQuery();
|
|
91
|
+
expect(matchesSnapshot(getFilteredContext(context))).toEqual(true);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('can chain query conditions', () => {
|
|
95
|
+
let context = User.where.id.EQ(1).AND.firstName.EQ('Test').AND.NOT.lastName.EQ('Stuff')._getRawQuery();
|
|
96
|
+
expect(matchesSnapshot(getFilteredContext(context))).toEqual(true);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Model, Types } = require('../../../lib');
|
|
4
|
+
|
|
5
|
+
class BlobTest extends Model {
|
|
6
|
+
static fields = {
|
|
7
|
+
'id': {
|
|
8
|
+
type: Types.UUIDV4,
|
|
9
|
+
defaultValue: Types.UUIDV4.Default.UUIDV4,
|
|
10
|
+
allowNull: false,
|
|
11
|
+
primaryKey: true,
|
|
12
|
+
},
|
|
13
|
+
'data': {
|
|
14
|
+
type: Types.BLOB,
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = BlobTest;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Types } = require('../../../lib');
|
|
4
|
+
const User = require('./user-model');
|
|
5
|
+
|
|
6
|
+
class ExtendedUser extends User {
|
|
7
|
+
static fields = User.mergeFields({
|
|
8
|
+
'id': {
|
|
9
|
+
type: Types.INTEGER,
|
|
10
|
+
defaultValue: Types.INTEGER.Default.AUTO_INCREMENT,
|
|
11
|
+
primaryKey: true,
|
|
12
|
+
allowNull: false,
|
|
13
|
+
unique: true, // should be ignored when create table query is generated
|
|
14
|
+
},
|
|
15
|
+
'createdAt': {
|
|
16
|
+
type: Types.DATETIME(null, 'MM.DD.YYYY HH:mm:ss'),
|
|
17
|
+
defaultValue: Types.DATETIME.Default.NOW,
|
|
18
|
+
allowNull: false,
|
|
19
|
+
},
|
|
20
|
+
'updatedAt': {
|
|
21
|
+
type: Types.DATETIME(null, 'MM.DD.YYYY HH:mm:ss'),
|
|
22
|
+
defaultValue: Types.DATETIME.Default.NOW.UPDATE,
|
|
23
|
+
allowNull: false,
|
|
24
|
+
},
|
|
25
|
+
'email': {
|
|
26
|
+
type: Types.STRING(256),
|
|
27
|
+
allowNull: false,
|
|
28
|
+
unique: true,
|
|
29
|
+
},
|
|
30
|
+
'playerType': {
|
|
31
|
+
type: Types.STRING(256),
|
|
32
|
+
defaultValue: 'wizard',
|
|
33
|
+
allowNull: false,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = ExtendedUser;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const BlobTest = require('./blob-test-model');
|
|
4
|
+
const ExtendedUser = require('./extended-user-model');
|
|
5
|
+
const Number = require('./number-model');
|
|
6
|
+
const Role = require('./role-model');
|
|
7
|
+
const RoleThing = require('./role-thing-model');
|
|
8
|
+
const ScopedUser = require('./scoped-user-model');
|
|
9
|
+
const User = require('./user-model');
|
|
10
|
+
const UserRole = require('./user-role-model');
|
|
11
|
+
const UserThing = require('./user-thing-model');
|
|
12
|
+
const ValidationTest = require('./validation-test-model');
|
|
13
|
+
const Time = require('./time-model');
|
|
14
|
+
|
|
15
|
+
module.exports = {
|
|
16
|
+
BlobTest,
|
|
17
|
+
ExtendedUser,
|
|
18
|
+
Number,
|
|
19
|
+
Role,
|
|
20
|
+
RoleThing,
|
|
21
|
+
ScopedUser,
|
|
22
|
+
User,
|
|
23
|
+
UserRole,
|
|
24
|
+
UserThing,
|
|
25
|
+
ValidationTest,
|
|
26
|
+
Time,
|
|
27
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Model, Types } = require('../../../lib');
|
|
4
|
+
|
|
5
|
+
class Number extends Model {
|
|
6
|
+
static fields = {
|
|
7
|
+
'id': {
|
|
8
|
+
type: Types.UUIDV4,
|
|
9
|
+
defaultValue: Types.UUIDV4.Default.UUIDV4,
|
|
10
|
+
allowNull: false,
|
|
11
|
+
primaryKey: true,
|
|
12
|
+
},
|
|
13
|
+
'numberInt': {
|
|
14
|
+
type: Types.INTEGER,
|
|
15
|
+
index: true,
|
|
16
|
+
},
|
|
17
|
+
'numberFloat': {
|
|
18
|
+
type: Types.FLOAT,
|
|
19
|
+
index: true,
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = Number;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Model, Types } = require('../../../lib');
|
|
4
|
+
|
|
5
|
+
class Role extends Model {
|
|
6
|
+
static fields = {
|
|
7
|
+
'id': {
|
|
8
|
+
type: Types.UUIDV4,
|
|
9
|
+
defaultValue: Types.UUIDV4.Default.UUIDV4,
|
|
10
|
+
allowNull: false,
|
|
11
|
+
primaryKey: true,
|
|
12
|
+
},
|
|
13
|
+
'name': {
|
|
14
|
+
type: Types.STRING(64),
|
|
15
|
+
allowNull: false,
|
|
16
|
+
index: true,
|
|
17
|
+
},
|
|
18
|
+
'user': {
|
|
19
|
+
type: Types.Model('User', ({ User, self }) => {
|
|
20
|
+
return User.$.primaryRoleID.EQ(self.primaryRoleID);
|
|
21
|
+
}),
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = Role;
|