@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.
package/query/Junction.js CHANGED
@@ -15,10 +15,11 @@ import {
15
15
  NOT_LIKE,
16
16
  NOT_LIKE_REGEX,
17
17
  } from "./ConditionType.js";
18
+ import {QueryImpl} from "./QueryImpl.js";
19
+ import {Not} from "./Not.js";
20
+ import {WhereClause} from "./WhereClause.js";
21
+ import {toSkipIfInfo, toUseExpression, toUseExpressionValue} from "./BuildControl.js";
18
22
  import {Condition} from "./Condition.js";
19
- // import {QueryImpl} from "./QueryImpl.js";
20
- // import {Not} from "./Not.js";
21
- // import {WhereClause} from "./WhereClause.js";
22
23
 
23
24
  class Junction extends QueryLike {
24
25
 
@@ -33,16 +34,15 @@ class Junction extends QueryLike {
33
34
  return this.#clauses;
34
35
  }
35
36
 
36
- add(...logicCauses) {
37
- for (let logicCause of logicCauses) {
38
- if (logicCause == null) {
39
- continue;
40
- }
41
- if (Array.isArray(logicCause)) {
42
- this.add(...logicCause);
43
- continue;
37
+ /// null 项会被忽略.
38
+ add(...clauses) {
39
+ if (clauses == null) {
40
+ throw new Error("clauses cannot be null");
41
+ }
42
+ for (let clause of clauses) {
43
+ if (clause != null) {
44
+ this.#clauses.push(clause);
44
45
  }
45
- this.#clauses.push(logicCause);
46
46
  }
47
47
  return this;
48
48
  }
@@ -52,90 +52,98 @@ class Junction extends QueryLike {
52
52
  return this;
53
53
  }
54
54
 
55
+ and(...clauses) {
56
+ // 因为 js 不允许 循环依赖 所以 这个方法迁移到子类实现
57
+ }
58
+
59
+ or(...clauses) {
60
+ // 因为 js 不允许 循环依赖 所以 这个方法迁移到子类实现
61
+ }
62
+
63
+ not(clause) {
64
+ return this.add(new Not(clause));
65
+ }
66
+
67
+ whereClause(expression, ...params) {
68
+ return this.add(new WhereClause(expression, ...params));
69
+ }
70
+
55
71
  eq(fieldName, value, ...options) {
56
- return this.add(new Condition(fieldName, EQ, value, null, ...options));
72
+ return this.add(condition(fieldName, EQ, value, ...options));
57
73
  }
58
74
 
59
75
  ne(fieldName, value, ...options) {
60
- return this.add(new Condition(fieldName, NE, value, null, ...options));
76
+ return this.add(condition(fieldName, NE, value, ...options));
61
77
  }
62
78
 
63
79
  lt(fieldName, value, ...options) {
64
- return this.add(new Condition(fieldName, LT, value, null, ...options));
80
+ return this.add(condition(fieldName, LT, value, ...options));
65
81
  }
66
82
 
67
83
  lte(fieldName, value, ...options) {
68
- return this.add(new Condition(fieldName, LTE, value, null, ...options));
84
+ return this.add(condition(fieldName, LTE, value, ...options));
69
85
  }
70
86
 
71
87
  gt(fieldName, value, ...options) {
72
- return this.add(new Condition(fieldName, GT, value, null, ...options));
88
+ return this.add(condition(fieldName, GT, value, ...options));
73
89
  }
74
90
 
75
91
  gte(fieldName, value, ...options) {
76
- return this.add(new Condition(fieldName, GTE, value, null, ...options));
92
+ return this.add(condition(fieldName, GTE, value, ...options));
77
93
  }
78
94
 
79
95
  like(fieldName, value, ...options) {
80
- return this.add(new Condition(fieldName, LIKE, value, null, ...options));
96
+ return this.add(condition(fieldName, LIKE, value, ...options));
81
97
  }
82
98
 
83
99
  notLike(fieldName, value, ...options) {
84
- return this.add(new Condition(fieldName, NOT_LIKE, value, null, ...options));
100
+ return this.add(condition(fieldName, NOT_LIKE, value, ...options));
85
101
  }
86
102
 
87
103
  likeRegex(fieldName, value, ...options) {
88
- return this.add(new Condition(fieldName, LIKE_REGEX, value, null, ...options));
104
+ return this.add(condition(fieldName, LIKE_REGEX, value, ...options));
89
105
  }
90
106
 
91
107
  notLikeRegex(fieldName, value, ...options) {
92
- return this.add(new Condition(fieldName, NOT_LIKE_REGEX, value, null, ...options));
108
+ return this.add(condition(fieldName, NOT_LIKE_REGEX, value, ...options));
93
109
  }
94
110
 
95
111
  in(fieldName, value, ...options) {
96
- return this.add(new Condition(fieldName, IN, value, null, ...options));
112
+ return this.add(condition(fieldName, IN, value, ...options));
97
113
  }
98
114
 
99
115
  notIn(fieldName, value, ...options) {
100
- return this.add(new Condition(fieldName, NOT_IN, value, null, ...options));
116
+ return this.add(condition(fieldName, NOT_IN, value, ...options));
101
117
  }
102
118
 
103
119
  between(fieldName, value1, value2, ...options) {
104
- return this.add(new Condition(fieldName, BETWEEN, value1, value2, ...options));
120
+ return this.add(condition2(fieldName, BETWEEN, value1, value2, ...options));
105
121
  }
106
122
 
107
123
  notBetween(fieldName, value1, value2, ...options) {
108
- return this.add(new Condition(fieldName, NOT_BETWEEN, value1, value2, ...options));
124
+ return this.add(condition2(fieldName, NOT_BETWEEN, value1, value2, ...options));
109
125
  }
110
126
 
111
- jsonContains(fieldName, value, ...options) {
112
- return this.add(new Condition(fieldName, JSON_CONTAINS, value, null, ...options));
113
- }
114
-
115
- jsonOverlaps(fieldName, value, ...options) {
116
- return this.add(new Condition(fieldName, JSON_OVERLAPS, value, null, ...options));
117
- }
118
-
119
- and(...clauses) {
120
- // 因为 js 不允许 循环依赖 所以 这个方法迁移到子类实现
121
- }
122
-
123
- or(...clauses) {
124
- // 因为 js 不允许 循环依赖 所以 这个方法迁移到子类实现
127
+ toQuery() {
128
+ return new QueryImpl().where(this);
125
129
  }
126
130
 
127
- not(clause) {
128
- return this.add(new Not(clause));
129
- }
131
+ }
130
132
 
131
- whereClause(expression, ...params) {
132
- return this.add(new WhereClause(expression, params));
133
- }
133
+ // 防止循环依赖 QueryBuilder 这里拷贝一份
134
134
 
135
- toQuery() {
136
- return new QueryImpl().where(this);
137
- }
135
+ function condition(fieldName, conditionType, value, ...controls) {
136
+ let useExpression = toUseExpression(...controls);
137
+ let useExpressionValue = toUseExpressionValue(...controls);
138
+ let skipIfInfo = toSkipIfInfo(...controls);
139
+ return new Condition(fieldName, conditionType, value, null, useExpression, useExpressionValue, skipIfInfo);
140
+ }
138
141
 
142
+ function condition2(fieldName, conditionType, value1, value2, ...controls) {
143
+ let useExpression = toUseExpression(...controls);
144
+ let useExpressionValue = toUseExpressionValue(...controls);
145
+ let skipIfInfo = toSkipIfInfo(...controls);
146
+ return new Condition(fieldName, conditionType, value1, value2, useExpression, useExpressionValue, skipIfInfo);
139
147
  }
140
148
 
141
149
  export {
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
+ };