@scx-js/scx-data 0.2.3 → 0.3.0

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.
@@ -0,0 +1,163 @@
1
+ import {WhereClause} from "../query/WhereClause.js";
2
+ import {Not} from "../query/Not.js";
3
+ import {Or} from "../query/Or.js";
4
+ import {And} from "../query/And.js";
5
+ import {Condition} from "../query/Condition.js";
6
+
7
+ /// QueryNodeConverter
8
+ ///
9
+ /// - Query -> Node 采用严格编码.
10
+
11
+ // ************************* Query *************************
12
+
13
+ /// query 不允许 null.
14
+ function queryToNode(query) {
15
+ if (query == null) {
16
+ throw new Error("query cannot be null");
17
+ }
18
+
19
+ return {
20
+ "@type": "Query",
21
+ "where": whereToNode(query.getWhere()),
22
+ "orderBys": orderBysToNode(query.getOrderBys()),
23
+ "offset": query.getOffset(),
24
+ "limit": query.getLimit(),
25
+ };
26
+ }
27
+
28
+ // ************************* Where *************************
29
+
30
+ /// where 可能是 null, 保持为 NullNode
31
+ function whereToNode(where) {
32
+ if (where == null) {
33
+ return null;
34
+ }
35
+ if (where instanceof Condition) {
36
+ return conditionToNode(where);
37
+ }
38
+ if (where instanceof And) {
39
+ return andToNode(where);
40
+ }
41
+ if (where instanceof Or) {
42
+ return orToNode(where);
43
+ }
44
+ if (where instanceof Not) {
45
+ return notToNode(where);
46
+ }
47
+ if (where instanceof WhereClause) {
48
+ return whereClauseToNode(where);
49
+ }
50
+ throw new Error("Unknown Where type: " + where);
51
+ }
52
+
53
+ // ************************* OrderBy *************************
54
+
55
+ /// orderBys 永不可能为 null.
56
+ function orderBysToNode(orderBys) {
57
+ let node = [];
58
+ for (let orderBy of orderBys) {
59
+ node.push(orderByToNode(orderBy));
60
+ }
61
+ return node;
62
+ }
63
+
64
+ /// orderBy 永不可能为 null.
65
+ function orderByToNode(orderBy) {
66
+ return {
67
+ "@type": "OrderBy",
68
+ "selector": orderBy.selector(),
69
+ "orderByType": orderByTypeToNode(orderBy.orderByType()),
70
+ "useExpression": orderBy.useExpression(),
71
+ };
72
+ }
73
+
74
+ /// orderByType 永不可能为 null.
75
+ function orderByTypeToNode(orderByType) {
76
+ return orderByType.value();
77
+ }
78
+
79
+ // ************************* Condition *************************
80
+
81
+ /// condition 永不可能为 null.
82
+ function conditionToNode(condition) {
83
+ return {
84
+ "@type": "Condition",
85
+ "selector": condition.selector(),
86
+ "conditionType": conditionTypeToNode(condition.conditionType()),
87
+ "value1": condition.value1(),
88
+ "value2": condition.value2(),
89
+ "useExpression": condition.useExpression(),
90
+ "useExpressionValue": condition.useExpressionValue(),
91
+ "skipIfInfo": skipIfInfoToNode(condition.skipIfInfo()),
92
+ };
93
+ }
94
+
95
+ /// conditionType 永不可能为 null.
96
+ function conditionTypeToNode(conditionType) {
97
+ return conditionType.value();
98
+ }
99
+
100
+ // ************************* And/Or *************************
101
+
102
+ /// and 永不可能为 null.
103
+ function andToNode(and) {
104
+ return {
105
+ "@type": "And",
106
+ "clauses": clausesToNode(and.clauses()),
107
+ };
108
+ }
109
+
110
+ /// or 永不可能为 null.
111
+ function orToNode(or) {
112
+ return {
113
+ "@type": "Or",
114
+ "clauses": clausesToNode(or.clauses()),
115
+ };
116
+ }
117
+
118
+ /// clauses 永不可能为 null.
119
+ function clausesToNode(clauses) {
120
+ let node = [];
121
+ for (let clause of clauses) {
122
+ node.push(whereToNode(clause));
123
+ }
124
+ return node;
125
+ }
126
+
127
+ // ************************* Not *************************
128
+
129
+ /// not 永不可能为 null.
130
+ function notToNode(not) {
131
+ return {
132
+ "@type": "Not",
133
+ "clause": whereToNode(not.clause()),
134
+ };
135
+ }
136
+
137
+ // ************************* WhereClause *************************
138
+
139
+ /// whereClause 永不可能为 null.
140
+ function whereClauseToNode(whereClause) {
141
+ return {
142
+ "@type": "WhereClause",
143
+ "expression": whereClause.expression(),
144
+ "params": whereClause.params(),
145
+ };
146
+ }
147
+
148
+ // ************************* SkipIfInfo *************************
149
+
150
+ /// skipIfInfo 永不可能为 null.
151
+ function skipIfInfoToNode(skipIfInfo) {
152
+ return {
153
+ "@type": "SkipIfInfo",
154
+ "skipIfNull": skipIfInfo.skipIfNull(),
155
+ "skipIfEmptyList": skipIfInfo.skipIfEmptyList(),
156
+ "skipIfEmptyString": skipIfInfo.skipIfEmptyString(),
157
+ "skipIfBlankString": skipIfInfo.skipIfBlankString(),
158
+ };
159
+ }
160
+
161
+ export {
162
+ queryToNode,
163
+ };
@@ -1,47 +0,0 @@
1
- import {FieldPolicy} from "../field_policy/FieldPolicy.js";
2
-
3
- function serializeFieldPolicyToJson(fieldPolicy) {
4
- return JSON.stringify(serializeFieldPolicy(fieldPolicy));
5
- }
6
-
7
- function serializeFieldPolicy(fieldPolicy) {
8
- return {
9
- "@type": "FieldPolicy",
10
- "filterMode": fieldPolicy.getFilterMode(),
11
- "fieldNames": fieldPolicy.getFieldNames(),
12
- "virtualFields": serializeVirtualFields(fieldPolicy.getVirtualFields()),
13
- "ignoreNull": fieldPolicy.getIgnoreNull(),
14
- "ignoreNulls": fieldPolicy.getIgnoreNulls(),
15
- "assignFields": serializeAssignFields(fieldPolicy.getAssignFields()),
16
- };
17
- }
18
-
19
- function serializeVirtualField(virtualField) {
20
- return {
21
- "@type": "VirtualField",
22
- "expression": virtualField.expression(),
23
- "virtualFieldName": virtualField.virtualFieldName(),
24
- };
25
- }
26
-
27
-
28
- function serializeAssignField(assignField) {
29
- return {
30
- "@type": "AssignField",
31
- "fieldName": assignField.fieldName(),
32
- "expression": assignField.expression(),
33
- };
34
- }
35
-
36
- function serializeVirtualFields(virtualFields) {
37
- return virtualFields.map(s => serializeVirtualField(s));
38
- }
39
-
40
- function serializeAssignFields(assignFields) {
41
- return assignFields.map(s => serializeAssignField(s));
42
- }
43
-
44
- export {
45
- serializeFieldPolicyToJson,
46
- serializeFieldPolicy,
47
- };
@@ -1,120 +0,0 @@
1
- import {WhereClause} from "../query/WhereClause.js";
2
- import {And} from "../query/And.js";
3
- import {Or} from "../query/Or.js";
4
- import {Not} from "../query/Not.js";
5
- import {Condition} from "../query/Condition.js";
6
-
7
- function serializeQueryToJson(query) {
8
- return JSON.stringify(serializeQuery(query));
9
- }
10
-
11
- function serializeQuery(query) {
12
- return {
13
- "@type": "Query",
14
- where: serializeWhere(query.getWhere()),
15
- orderBys: serializeOrderBys(query.getOrderBys()),
16
- offset: query.getOffset(),
17
- limit: query.getLimit(),
18
- };
19
- }
20
-
21
- function serializeWhere(obj) {
22
- if (!obj) {
23
- return null;
24
- }
25
-
26
- if (obj instanceof WhereClause) {
27
- return serializeWhereClause(obj);
28
- }
29
- if (obj instanceof And) {
30
- return serializeAnd(obj);
31
- }
32
- if (obj instanceof Or) {
33
- return serializeOr(obj);
34
- }
35
-
36
- if (obj instanceof Not) {
37
- return serializeNot(obj);
38
- }
39
-
40
- if (obj instanceof Condition) {
41
- return serializeCondition(obj);
42
- }
43
-
44
- throw new Error(`Unknown Where type: ${obj}`);
45
-
46
- }
47
-
48
- function serializeWhereClause(w) {
49
- return {
50
- "@type": "WhereClause",
51
- expression: w.expression(),
52
- params: w.params(),
53
- };
54
- }
55
-
56
- function serializeAnd(a) {
57
- return {
58
- "@type": "And",
59
- clauses: serializeWhereAll(a.clauses()),
60
- };
61
- }
62
-
63
- function serializeOr(o) {
64
- return {
65
- "@type": "Or",
66
- clauses: serializeWhereAll(o.clauses()),
67
- };
68
- }
69
-
70
- function serializeNot(n) {
71
- return {
72
- "@type": "Not",
73
- clause: serializeWhere(n.clause()),
74
- };
75
- }
76
-
77
- function serializeCondition(w) {
78
- return {
79
- "@type": "Condition",
80
- selector: w.selector(),
81
- conditionType: w.conditionType(),
82
- value1: w.value1(),
83
- value2: w.value2(),
84
- useExpression: w.useExpression(),
85
- useExpressionValue: w.useExpressionValue(),
86
- skipIfInfo: serializeSkipIfInfo(w.skipIfInfo()),
87
- };
88
- }
89
-
90
- function serializeWhereAll(objs) {
91
- return objs.map(obj => serializeWhere(obj));
92
- }
93
-
94
- function serializeOrderBys(objs) {
95
- return objs.map(obj => serializeOrderBy(obj));
96
- }
97
-
98
- function serializeOrderBy(orderBy) {
99
- return {
100
- "@type": "OrderBy",
101
- selector: orderBy.selector(),
102
- orderByType: orderBy.orderByType(),
103
- useExpression: orderBy.useExpression(),
104
- };
105
- }
106
-
107
- function serializeSkipIfInfo(info) {
108
- return {
109
- "@type": "SkipIfInfo",
110
- skipIfNull: info.skipIfNull,
111
- skipIfEmptyList: info.skipIfEmptyList,
112
- skipIfEmptyString: info.skipIfEmptyString,
113
- skipIfBlankString: info.skipIfBlankString,
114
- };
115
- }
116
-
117
- export {
118
- serializeQueryToJson,
119
- serializeQuery,
120
- };