@scx-js/scx-data 0.2.4 → 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.
package/query/Not.js CHANGED
@@ -17,6 +17,7 @@ class Not extends QueryLike {
17
17
  toQuery() {
18
18
  return new QueryImpl().where(this);
19
19
  }
20
+
20
21
  }
21
22
 
22
23
  export {Not};
package/query/Or.js CHANGED
@@ -5,11 +5,11 @@ class Or extends Junction {
5
5
 
6
6
  // 因为 js 不允许 循环依赖 所以 这两个方法迁移到子类
7
7
  and(...clauses) {
8
- return this.add(new And().add(clauses));
8
+ return this.add(new And().add(...clauses));
9
9
  }
10
10
 
11
11
  or(...clauses) {
12
- return this.add(new Or().add(clauses));
12
+ return this.add(new Or().add(...clauses));
13
13
  }
14
14
 
15
15
  }
package/query/OrderBy.js CHANGED
@@ -1,4 +1,3 @@
1
- import {isBlank} from "@scx-js/scx-common";
2
1
  import {QueryImpl} from "./QueryImpl.js";
3
2
  import {QueryLike} from "./QueryLike.js";
4
3
 
@@ -8,20 +7,19 @@ class OrderBy extends QueryLike {
8
7
  #orderByType;
9
8
  #useExpression;
10
9
 
11
- constructor(name, orderByType, useExpression) {
10
+ constructor(selector, orderByType, useExpression) {
12
11
  super();
13
- if (isBlank(name)) {
14
- throw new Error("OrderBy 参数错误 : 名称 不能为空 !!!");
12
+ if (selector == null) {
13
+ throw new Error("selector cannot be null");
15
14
  }
16
15
  if (orderByType == null) {
17
- throw new Error("OrderBy 参数错误 : orderByType 不能为空 !!!");
16
+ throw new Error("orderByType cannot be null");
18
17
  }
19
- this.#selector = name;
18
+ this.#selector = selector;
20
19
  this.#orderByType = orderByType;
21
20
  this.#useExpression = useExpression;
22
21
  }
23
22
 
24
-
25
23
  selector() {
26
24
  return this.#selector;
27
25
  }
@@ -1,8 +1,22 @@
1
- const ASC = "ASC";
1
+ class OrderByType {
2
2
 
3
- const DESC = "DESC";
3
+ #value;
4
+
5
+ constructor(value) {
6
+ this.#value = value;
7
+ }
8
+
9
+ value() {
10
+ return this.#value;
11
+ }
12
+
13
+ }
14
+
15
+ const ASC = new OrderByType("ASC");
16
+ const DESC = new OrderByType("DESC");
4
17
 
5
18
  export {
19
+ OrderByType,
6
20
  ASC,
7
21
  DESC,
8
22
  };
@@ -22,15 +22,30 @@ import {ASC, DESC} from "./OrderByType.js";
22
22
  import {Not} from "./Not.js";
23
23
  import {And} from "./And.js";
24
24
  import {Or} from "./Or.js";
25
- import {checkUseExpression, checkUseExpressionValue} from "./BuildControl.js";
26
- import {ofSkipIfInfo} from "./SkipIfInfo.js";
25
+ import {toSkipIfInfo, toUseExpression, toUseExpressionValue} from "./BuildControl.js";
27
26
 
28
- function query(oldQuery) {
29
- return new QueryImpl(oldQuery);
27
+ function query() {
28
+ return new QueryImpl();
30
29
  }
31
30
 
32
- function where(whereClauses) {
33
- return new QueryImpl().where(whereClauses);
31
+ function query_(oldQuery) {
32
+ if (oldQuery == null) {
33
+ throw new Error("oldQuery cannot be null");
34
+ }
35
+ let query = new QueryImpl();
36
+ query.where(oldQuery.getWhere());
37
+ query.orderBys(...oldQuery.getOrderBys());
38
+ if (oldQuery.getOffset() != null) {
39
+ query.offset(oldQuery.getOffset());
40
+ }
41
+ if (oldQuery.getLimit() != null) {
42
+ query.limit(oldQuery.getLimit());
43
+ }
44
+ return query;
45
+ }
46
+
47
+ function where(where) {
48
+ return new QueryImpl().where(where);
34
49
  }
35
50
 
36
51
  function orderBys(...orderBys) {
@@ -57,29 +72,29 @@ function not(clause) {
57
72
  return new Not(clause);
58
73
  }
59
74
 
75
+ function whereClause(expression, ...params) {
76
+ return new WhereClause(expression, ...params);
77
+ }
78
+
60
79
  function condition(fieldName, conditionType, value, ...controls) {
61
- let useExpression = checkUseExpression(...controls);
62
- let useExpressionValue = checkUseExpressionValue(...controls);
63
- let skipIfInfo = ofSkipIfInfo(...controls);
80
+ let useExpression = toUseExpression(...controls);
81
+ let useExpressionValue = toUseExpressionValue(...controls);
82
+ let skipIfInfo = toSkipIfInfo(...controls);
64
83
  return new Condition(fieldName, conditionType, value, null, useExpression, useExpressionValue, skipIfInfo);
65
84
  }
66
85
 
67
86
  function condition2(fieldName, conditionType, value1, value2, ...controls) {
68
- let useExpression = checkUseExpression(...controls);
69
- let useExpressionValue = checkUseExpressionValue(...controls);
70
- let skipIfInfo = ofSkipIfInfo(...controls);
87
+ let useExpression = toUseExpression(...controls);
88
+ let useExpressionValue = toUseExpressionValue(...controls);
89
+ let skipIfInfo = toSkipIfInfo(...controls);
71
90
  return new Condition(fieldName, conditionType, value1, value2, useExpression, useExpressionValue, skipIfInfo);
72
91
  }
73
92
 
74
93
  function orderBy(selector, orderByType, ...controls) {
75
- let useExpression = checkUseExpression(...controls);
94
+ let useExpression = toUseExpression(...controls);
76
95
  return new OrderBy(selector, orderByType, useExpression);
77
96
  }
78
97
 
79
- function whereClause(expression, ...params) {
80
- return new WhereClause(expression, ...params);
81
- }
82
-
83
98
  function asc(name, ...options) {
84
99
  return orderBy(name, ASC, ...options);
85
100
  }
@@ -144,11 +159,12 @@ function notBetween(fieldName, value1, value2, ...options) {
144
159
  return condition2(fieldName, NOT_BETWEEN, value1, value2, ...options);
145
160
  }
146
161
 
147
-
148
162
  export {
149
163
  query,
164
+ query_,
150
165
  where,
151
166
  orderBy,
167
+ orderBys,
152
168
  offset,
153
169
  limit,
154
170
  and,
@@ -171,4 +187,6 @@ export {
171
187
  between,
172
188
  notBetween,
173
189
  whereClause,
190
+ condition,
191
+ condition2,
174
192
  };
@@ -1,5 +1,5 @@
1
1
  import {Query} from "./Query.js";
2
- import {checkUseExpression} from "./BuildControl.js";
2
+ import {toUseExpression} from "./BuildControl.js";
3
3
  import {OrderBy} from "./OrderBy.js";
4
4
  import {ASC, DESC} from "./OrderByType.js";
5
5
 
@@ -10,10 +10,7 @@ class QueryImpl extends Query {
10
10
  #offset;
11
11
  #limit;
12
12
 
13
- /**
14
- * 创建 Query 对象
15
- */
16
- constructor(old) {
13
+ constructor() {
17
14
  super();
18
15
  this.#where = null;
19
16
  this.#orderBys = [];
@@ -26,16 +23,23 @@ class QueryImpl extends Query {
26
23
  return this;
27
24
  }
28
25
 
29
-
30
26
  orderBys(...orderBys) {
31
- this.#orderBys = [...orderBys];
27
+ if (orderBys == null) {
28
+ throw new Error("orderBys cannot be null");
29
+ }
30
+ let newOrderBys = [];
31
+ for (let orderBy of orderBys) {
32
+ if (orderBy != null) {
33
+ newOrderBys.push(orderBy);
34
+ }
35
+ }
36
+ this.#orderBys = newOrderBys;
32
37
  return this;
33
38
  }
34
39
 
35
-
36
40
  offset(offset) {
37
41
  if (offset < 0) {
38
- throw new Error("Limit 参数错误 : offset (偏移量) 不能小于 0 !!!");
42
+ throw new Error("offset must be >= 0");
39
43
  }
40
44
  this.#offset = offset;
41
45
  return this;
@@ -43,7 +47,7 @@ class QueryImpl extends Query {
43
47
 
44
48
  limit(limit) {
45
49
  if (limit < 0) {
46
- throw new Error("Limit 参数错误 : limit (行长度) 不能小于 0 !!!");
50
+ throw new Error("limit must be >= 0");
47
51
  }
48
52
  this.#limit = limit;
49
53
  return this;
@@ -86,26 +90,42 @@ class QueryImpl extends Query {
86
90
  }
87
91
 
88
92
  orderBy(...orderBys) {
89
- this.#orderBys.push(...orderBys);
93
+ if (orderBys == null) {
94
+ throw new Error("orderBys cannot be null");
95
+ }
96
+ for (let orderBy of orderBys) {
97
+ if (orderBy != null) {
98
+ this.#orderBys.push(orderBy);
99
+ }
100
+ }
90
101
  return this;
91
102
  }
92
103
 
93
-
94
104
  asc(selector, ...controls) {
95
- let useExpression = checkUseExpression(...controls);
96
- let o = new OrderBy(selector, ASC, useExpression);
97
- this.orderBy(o);
105
+ this.orderBy(asc(selector, ...controls));
98
106
  return this;
99
107
  }
100
108
 
101
-
102
109
  desc(selector, ...controls) {
103
- let useExpression = checkUseExpression(...controls);
104
- let o = new OrderBy(selector, DESC, useExpression);
105
- this.orderBy(o);
110
+ this.orderBy(desc(selector, ...controls));
106
111
  return this;
107
112
  }
108
113
 
109
114
  }
110
115
 
116
+ // 防止循环依赖 QueryBuilder 这里拷贝一份
117
+
118
+ function orderBy(selector, orderByType, ...controls) {
119
+ let useExpression = toUseExpression(...controls);
120
+ return new OrderBy(selector, orderByType, useExpression);
121
+ }
122
+
123
+ function asc(name, ...options) {
124
+ return orderBy(name, ASC, ...options);
125
+ }
126
+
127
+ function desc(name, ...options) {
128
+ return orderBy(name, DESC, ...options);
129
+ }
130
+
111
131
  export {QueryImpl};
@@ -17,7 +17,7 @@ class QueryLike extends Query {
17
17
  }
18
18
 
19
19
  orderBys(...orderBys) {
20
- this.query().orderBy(...orderBys);
20
+ this.query().orderBys(...orderBys);
21
21
  return this;
22
22
  }
23
23
 
@@ -52,6 +52,11 @@ class QueryLike extends Query {
52
52
  return this;
53
53
  }
54
54
 
55
+ clearOrderBys() {
56
+ this.query().clearOrderBys();
57
+ return this;
58
+ }
59
+
55
60
  clearOffset() {
56
61
  this.query().clearOffset();
57
62
  return this;
@@ -78,6 +83,7 @@ class QueryLike extends Query {
78
83
  }
79
84
 
80
85
  toQuery() {
86
+
81
87
  }
82
88
 
83
89
  }
@@ -1,42 +1,35 @@
1
- import {SKIP_IF_BLANK_STRING, SKIP_IF_EMPTY_LIST, SKIP_IF_EMPTY_STRING, SKIP_IF_NULL} from "./BuildControl.js";
2
-
3
1
  class SkipIfInfo {
4
- skipIfNull;
5
- skipIfEmptyList;
6
- skipIfEmptyString;
7
- skipIfBlankString;
2
+
3
+ #skipIfNull;
4
+ #skipIfEmptyList;
5
+ #skipIfEmptyString;
6
+ #skipIfBlankString;
8
7
 
9
8
  constructor(skipIfNull, skipIfEmptyList, skipIfEmptyString, skipIfBlankString) {
10
- this.skipIfNull = skipIfNull;
11
- this.skipIfEmptyList = skipIfEmptyList;
12
- this.skipIfEmptyString = skipIfEmptyString;
13
- this.skipIfBlankString = skipIfBlankString;
9
+ this.#skipIfNull = skipIfNull;
10
+ this.#skipIfEmptyList = skipIfEmptyList;
11
+ this.#skipIfEmptyString = skipIfEmptyString;
12
+ this.#skipIfBlankString = skipIfBlankString;
14
13
  }
15
14
 
16
- }
15
+ skipIfNull() {
16
+ return this.#skipIfNull;
17
+ }
17
18
 
18
- function ofSkipIfInfo(...controls) {
19
- let skipIfNull = false;
20
- let skipIfEmptyList = false;
21
- let skipIfEmptyString = false;
22
- let skipIfBlankString = false;
23
- for (let control of controls) {
24
- if (control === SKIP_IF_NULL) {
25
- skipIfNull = true;
26
- } else if (control === SKIP_IF_EMPTY_LIST) {
27
- skipIfEmptyList = true;
28
- } else if (control === SKIP_IF_EMPTY_STRING) {
29
- skipIfEmptyString = true;
30
- } else if (control === SKIP_IF_BLANK_STRING) {
31
- skipIfBlankString = true;
32
- }
19
+ skipIfEmptyList() {
20
+ return this.#skipIfEmptyList;
33
21
  }
34
22
 
35
- return new SkipIfInfo(skipIfNull, skipIfEmptyList, skipIfEmptyString, skipIfBlankString);
23
+ skipIfEmptyString() {
24
+ return this.#skipIfEmptyString;
25
+ }
26
+
27
+ skipIfBlankString() {
28
+ return this.#skipIfBlankString;
29
+ }
36
30
 
37
31
  }
38
32
 
39
33
  export {
40
34
  SkipIfInfo,
41
- ofSkipIfInfo,
42
35
  };
@@ -6,16 +6,12 @@ class WhereClause extends QueryLike {
6
6
  #expression;
7
7
  #params;
8
8
 
9
- constructor(expression, params) {
9
+ constructor(expression, ...params) {
10
10
  super();
11
11
  this.#expression = expression;
12
12
  this.#params = params;
13
13
  }
14
14
 
15
- isEmpty() {
16
- return (this.#expression == null || this.#expression.isEmpty()) && (this.#params == null || this.#params.length === 0);
17
- }
18
-
19
15
  expression() {
20
16
  return this.#expression;
21
17
  }
@@ -0,0 +1,73 @@
1
+ /// FieldPolicyNodeConverter
2
+ ///
3
+ /// - FieldPolicy -> Node 采用严格编码.
4
+
5
+ // ************************* FieldPolicy *************************
6
+
7
+ /// fieldPolicy 不允许 null.
8
+ function fieldPolicyToNode(fieldPolicy) {
9
+ if (fieldPolicy == null) {
10
+ throw new Error("fieldPolicy cannot be null");
11
+ }
12
+
13
+ return {
14
+ "@type": "FieldPolicy",
15
+ "filterMode": filterModeToNode(fieldPolicy.getFilterMode()),
16
+ "fieldNames": fieldPolicy.getFieldNames(),
17
+ "virtualFields": virtualFieldsToNode(fieldPolicy.getVirtualFields()),
18
+ "assignFields": assignFieldsToNode(fieldPolicy.getAssignFields()),
19
+ "ignoreNull": fieldPolicy.getIgnoreNull(),
20
+ "ignoreNulls": fieldPolicy.getIgnoreNulls(),
21
+ };
22
+ }
23
+
24
+ // ************************* FilterMode *************************
25
+
26
+ /// filterMode 永不可能为 null.
27
+ function filterModeToNode(filterMode) {
28
+ return filterMode.value();
29
+ }
30
+
31
+ // ************************* VirtualField *************************
32
+
33
+ /// virtualFields 永不可能为 null.
34
+ function virtualFieldsToNode(virtualFields) {
35
+ let node = [];
36
+ for (let virtualField of virtualFields) {
37
+ node.push(virtualFieldToNode(virtualField));
38
+ }
39
+ return node;
40
+ }
41
+
42
+ /// virtualField 永不可能为 null.
43
+ function virtualFieldToNode(virtualField) {
44
+ return {
45
+ "@type": "VirtualField",
46
+ "virtualFieldName": virtualField.virtualFieldName(),
47
+ "expression": virtualField.expression(),
48
+ };
49
+ }
50
+
51
+ // ************************* AssignField *************************
52
+
53
+ /// assignFields 永不可能为 null.
54
+ function assignFieldsToNode(assignFields) {
55
+ let node = [];
56
+ for (let assignField of assignFields) {
57
+ node.push(assignFieldToNode(assignField));
58
+ }
59
+ return node;
60
+ }
61
+
62
+ /// assignField 永不可能为 null.
63
+ function assignFieldToNode(assignField) {
64
+ return {
65
+ "@type": "AssignField",
66
+ "fieldName": assignField.fieldName(),
67
+ "expression": assignField.expression(),
68
+ };
69
+ }
70
+
71
+ export {
72
+ fieldPolicyToNode,
73
+ };
@@ -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
- };