@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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 scx567888
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ <p align="center">
2
+ <img src="https://js.scx.dev/scx-logo/scx-data-logo.svg" width="300px" alt="scx-data-logo"/>
3
+ </p>
4
+ <p align="center">
5
+ <a target="_blank" href="https://github.com/scx-js/scx-data/actions/workflows/ci.yml">
6
+ <img src="https://github.com/scx-js/scx-data/actions/workflows/ci.yml/badge.svg" alt="CI"/>
7
+ </a>
8
+ <a target="_blank" href="https://www.npmjs.com/package/@scx-js/scx-data">
9
+ <img src="https://img.shields.io/npm/v/@scx-js/scx-data.svg?color=ff69b4" alt="npm"/>
10
+ </a>
11
+ <a target="_blank" href="https://github.com/scx-js/scx-data">
12
+ <img src="https://img.shields.io/github/languages/code-size/scx-js/scx-data?color=orange" alt="code-size"/>
13
+ </a>
14
+ <a target="_blank" href="https://github.com/scx-js/scx-data/issues">
15
+ <img src="https://img.shields.io/github/issues/scx-js/scx-data" alt="issues"/>
16
+ </a>
17
+ <a target="_blank" href="https://github.com/scx-js/scx-data/blob/master/LICENSE">
18
+ <img src="https://img.shields.io/github/license/scx-js/scx-data" alt="license"/>
19
+ </a>
20
+ </p>
@@ -10,10 +10,10 @@ class AssignField extends FieldPolicyLike {
10
10
  constructor(fieldName, expression) {
11
11
  super();
12
12
  if (fieldName == null) {
13
- throw new Error("fieldName is null");
13
+ throw new Error("fieldName cannot be null");
14
14
  }
15
15
  if (expression == null) {
16
- throw new Error("expression is null");
16
+ throw new Error("expression cannot be null");
17
17
  }
18
18
  this.#fieldName = fieldName;
19
19
  this.#expression = expression;
@@ -28,7 +28,7 @@ class AssignField extends FieldPolicyLike {
28
28
  }
29
29
 
30
30
  toFieldPolicy() {
31
- //排除 0个 就是包含所有
31
+ // 默认包含所有 (includeAll)
32
32
  return new FieldPolicyImpl(EXCLUDED).assignFields(this);
33
33
  }
34
34
 
@@ -9,9 +9,11 @@ class FieldPolicy {
9
9
  }
10
10
 
11
11
  getFilterMode() {
12
+
12
13
  }
13
14
 
14
15
  getFieldNames() {
16
+
15
17
  }
16
18
 
17
19
  clearFieldNames() {
@@ -23,6 +25,7 @@ class FieldPolicy {
23
25
  }
24
26
 
25
27
  getVirtualFields() {
28
+
26
29
  }
27
30
 
28
31
  clearVirtualFields() {
@@ -46,12 +49,15 @@ class FieldPolicy {
46
49
  }
47
50
 
48
51
  getIgnoreNull() {
52
+
49
53
  }
50
54
 
51
55
  getIgnoreNulls() {
56
+
52
57
  }
53
58
 
54
59
  getAssignFields() {
60
+
55
61
  }
56
62
 
57
63
  clearIgnoreNulls() {
@@ -19,21 +19,32 @@ function exclude(...fieldNames) {
19
19
  return includeAll().exclude(...fieldNames);
20
20
  }
21
21
 
22
- /// 默认包含所有
22
+ /// 默认包含所有 (includeAll)
23
23
  function ignoreNull(ignoreNull) {
24
24
  return includeAll().ignoreNull(ignoreNull);
25
25
  }
26
26
 
27
- /// 默认包含所有
27
+ /// 默认包含所有 (includeAll)
28
28
  function ignoreNull_(fieldName, ignoreNull) {
29
29
  return includeAll().ignoreNull_(fieldName, ignoreNull);
30
30
  }
31
31
 
32
- /// 默认包含所有
32
+ /// 默认包含所有 (includeAll)
33
+ function assignFields(...assignFields) {
34
+ return includeAll().assignFields(...assignFields);
35
+ }
36
+
37
+ /// 默认包含所有 (includeAll)
38
+ function virtualFields(...virtualFields) {
39
+ return includeAll().virtualFields(...virtualFields);
40
+ }
41
+
42
+ /// 默认包含所有 (includeAll)
33
43
  function assignField(fieldName, expression) {
34
44
  return new AssignField(fieldName, expression);
35
45
  }
36
46
 
47
+ /// 默认包含所有 (includeAll)
37
48
  function virtualField(virtualFieldName, expression) {
38
49
  return new VirtualField(virtualFieldName, expression);
39
50
  }
@@ -45,6 +56,8 @@ export {
45
56
  exclude,
46
57
  ignoreNull,
47
58
  ignoreNull_,
59
+ assignFields,
60
+ virtualFields,
48
61
  assignField,
49
62
  virtualField,
50
63
  };
@@ -14,11 +14,14 @@ class FieldPolicyImpl extends FieldPolicy {
14
14
 
15
15
  constructor(filterMode) {
16
16
  super();
17
+ if (filterMode == null) {
18
+ throw new Error("filterMode cannot be null");
19
+ }
17
20
  this.#filterMode = filterMode;
18
- this.#fieldNames = new Set();
21
+ this.#fieldNames = new Set(); // 需要保证顺序
19
22
  this.#virtualFields = [];
20
23
  this.#assignFields = [];
21
- this.#ignoreNulls = new Map();
24
+ this.#ignoreNulls = new Map(); // 需要保证顺序
22
25
  this.#ignoreNull = true;
23
26
  }
24
27
 
@@ -51,22 +54,17 @@ class FieldPolicyImpl extends FieldPolicy {
51
54
  return this;
52
55
  }
53
56
 
54
- addFieldNames(...fieldNames) {
55
- for (const name of fieldNames) {
56
- this.#fieldNames.add(name);
57
+ virtualFields(...virtualFields) {
58
+ if (virtualFields == null) {
59
+ throw new Error("virtualFields cannot be null");
57
60
  }
58
- return this;
59
- }
60
-
61
- removeFieldNames(...fieldNames) {
62
- for (const name of fieldNames) {
63
- this.#fieldNames.delete(name);
61
+ let newVirtualFields = [];
62
+ for (let virtualField of virtualFields) {
63
+ if (virtualField != null) {
64
+ newVirtualFields.push(virtualField);
65
+ }
64
66
  }
65
- return this;
66
- }
67
-
68
- virtualFields(...virtualFields) {
69
- this.#virtualFields = [...virtualFields];
67
+ this.#virtualFields = newVirtualFields;
70
68
  return this;
71
69
  }
72
70
 
@@ -90,12 +88,24 @@ class FieldPolicyImpl extends FieldPolicy {
90
88
  }
91
89
 
92
90
  ignoreNull_(fieldName, ignoreNull) {
91
+ if (fieldName == null) {
92
+ throw new Error("fieldName cannot be null");
93
+ }
93
94
  this.#ignoreNulls.set(fieldName, ignoreNull);
94
95
  return this;
95
96
  }
96
97
 
97
98
  assignFields(...assignFields) {
98
- this.#assignFields = [...assignFields];
99
+ if (assignFields == null) {
100
+ throw new Error("assignFields cannot be null");
101
+ }
102
+ let newAssignFields = [];
103
+ for (let assignField of assignFields) {
104
+ if (assignField != null) {
105
+ newAssignFields.push(assignField);
106
+ }
107
+ }
108
+ this.#assignFields = newAssignFields;
99
109
  return this;
100
110
  }
101
111
 
@@ -122,6 +132,9 @@ class FieldPolicyImpl extends FieldPolicy {
122
132
  }
123
133
 
124
134
  removeIgnoreNull(fieldName) {
135
+ if (fieldName == null) {
136
+ throw new Error("fieldName cannot be null");
137
+ }
125
138
  this.#ignoreNulls.delete(fieldName);
126
139
  return this;
127
140
  }
@@ -131,6 +144,30 @@ class FieldPolicyImpl extends FieldPolicy {
131
144
  return this;
132
145
  }
133
146
 
147
+ addFieldNames(...fieldNames) {
148
+ if (fieldNames == null) {
149
+ throw new Error("fieldNames cannot be null");
150
+ }
151
+ for (const fieldName of fieldNames) {
152
+ if (fieldName != null) {
153
+ this.#fieldNames.add(fieldName);
154
+ }
155
+ }
156
+ return this;
157
+ }
158
+
159
+ removeFieldNames(...fieldNames) {
160
+ if (fieldNames == null) {
161
+ throw new Error("fieldNames cannot be null");
162
+ }
163
+ for (const fieldName of fieldNames) {
164
+ if (fieldName != null) {
165
+ this.#fieldNames.delete(fieldName);
166
+ }
167
+ }
168
+ return this;
169
+ }
170
+
134
171
  }
135
172
 
136
173
  export {FieldPolicyImpl};
@@ -1,5 +1,22 @@
1
- const INCLUDED = "INCLUDED";
1
+ class FilterMode {
2
2
 
3
- const EXCLUDED = "EXCLUDED";
3
+ #value;
4
4
 
5
- export {INCLUDED, EXCLUDED};
5
+ constructor(value) {
6
+ this.#value = value;
7
+ }
8
+
9
+ value() {
10
+ return this.#value;
11
+ }
12
+
13
+ }
14
+
15
+ const INCLUDED = new FilterMode("INCLUDED");
16
+ const EXCLUDED = new FilterMode("EXCLUDED");
17
+
18
+ export {
19
+ FilterMode,
20
+ INCLUDED,
21
+ EXCLUDED,
22
+ };
@@ -10,10 +10,10 @@ class VirtualField extends FieldPolicyLike {
10
10
  constructor(virtualFieldName, expression) {
11
11
  super();
12
12
  if (virtualFieldName == null) {
13
- throw new Error("virtualFieldName is null");
13
+ throw new Error("virtualFieldName cannot be null");
14
14
  }
15
15
  if (expression == null) {
16
- throw new Error("expression is null");
16
+ throw new Error("expression cannot be null");
17
17
  }
18
18
  this.#virtualFieldName = virtualFieldName;
19
19
  this.#expression = expression;
@@ -28,7 +28,7 @@ class VirtualField extends FieldPolicyLike {
28
28
  }
29
29
 
30
30
  toFieldPolicy() {
31
- //排除 0个 就是包含所有
31
+ // 默认包含所有 (includeAll)
32
32
  return new FieldPolicyImpl(EXCLUDED).virtualFields(this);
33
33
  }
34
34
 
package/index.js CHANGED
@@ -5,19 +5,20 @@ export * from "./field_policy/FieldPolicyImpl.js";
5
5
  export * from "./field_policy/FieldPolicyLike.js";
6
6
  export * from "./field_policy/FilterMode.js";
7
7
  export * from "./field_policy/VirtualField.js";
8
- export * from "./query/Junction.js";
9
8
  export * from "./query/And.js";
10
- export * from "./query/Or.js";
9
+ export * from "./query/BuildControl.js";
10
+ export * from "./query/Condition.js";
11
+ export * from "./query/ConditionType.js";
12
+ export * from "./query/Junction.js";
11
13
  export * from "./query/Not.js";
14
+ export * from "./query/Or.js";
12
15
  export * from "./query/OrderBy.js";
13
16
  export * from "./query/OrderByType.js";
14
17
  export * from "./query/Query.js";
15
18
  export * from "./query/QueryBuilder.js";
16
19
  export * from "./query/QueryImpl.js";
17
20
  export * from "./query/QueryLike.js";
18
- export * from "./query/BuildControl.js";
19
- export * from "./query/Condition.js";
21
+ export * from "./query/SkipIfInfo.js";
20
22
  export * from "./query/WhereClause.js";
21
- export * from "./query/ConditionType.js";
22
- export * from "./serialization/QuerySerializer.js";
23
- export * from "./serialization/FieldPolicySerializer.js";
23
+ export * from "./x/FieldPolicyNodeConverter.js";
24
+ export * from "./x/QueryNodeConverter.js";
package/package.json CHANGED
@@ -1,16 +1,13 @@
1
1
  {
2
2
  "name": "@scx-js/scx-data",
3
- "version": "0.2.3",
3
+ "version": "0.3.0",
4
4
  "description": "SCX Data",
5
5
  "license": "MIT",
6
6
  "author": "scx567888",
7
- "main": "index.js",
7
+ "exports": "./index.js",
8
8
  "type": "module",
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/scx567888/scx-js.git"
12
- },
13
- "dependencies": {
14
- "@scx-js/scx-common": "0.2.3"
11
+ "url": "git+https://github.com/scx-js/scx-data.git"
15
12
  }
16
13
  }
package/query/And.js CHANGED
@@ -4,11 +4,11 @@ import {Or} from "./Or.js";
4
4
  class And extends Junction {
5
5
 
6
6
  and(...clauses) {
7
- return this.add(new And().add(clauses));
7
+ return this.add(new And().add(...clauses));
8
8
  }
9
9
 
10
10
  or(...clauses) {
11
- return this.add(new Or().add(clauses));
11
+ return this.add(new Or().add(...clauses));
12
12
  }
13
13
 
14
14
  }
@@ -1,3 +1,5 @@
1
+ import {SkipIfInfo} from "./SkipIfInfo.js";
2
+
1
3
  class BuildControl {
2
4
 
3
5
  #value;
@@ -15,8 +17,28 @@ const SKIP_IF_BLANK_STRING = new BuildControl("SKIP_IF_BLANK_STRING");
15
17
  const USE_EXPRESSION = new BuildControl("USE_EXPRESSION");
16
18
  const USE_EXPRESSION_VALUE = new BuildControl("USE_EXPRESSION_VALUE");
17
19
 
20
+ function toSkipIfInfo(...controls) {
21
+
22
+ let skipIfNull = false;
23
+ let skipIfEmptyList = false;
24
+ let skipIfEmptyString = false;
25
+ let skipIfBlankString = false;
26
+ for (let control of controls) {
27
+ if (control === SKIP_IF_NULL) {
28
+ skipIfNull = true;
29
+ } else if (control === SKIP_IF_EMPTY_LIST) {
30
+ skipIfEmptyList = true;
31
+ } else if (control === SKIP_IF_EMPTY_STRING) {
32
+ skipIfEmptyString = true;
33
+ } else if (control === SKIP_IF_BLANK_STRING) {
34
+ skipIfBlankString = true;
35
+ }
36
+ }
37
+
38
+ return new SkipIfInfo(skipIfNull, skipIfEmptyList, skipIfEmptyString, skipIfBlankString);
39
+ }
18
40
 
19
- function checkUseExpression(...controls) {
41
+ function toUseExpression(...controls) {
20
42
  for (let control of controls) {
21
43
  if (control === USE_EXPRESSION) {
22
44
  return true;
@@ -25,7 +47,7 @@ function checkUseExpression(...controls) {
25
47
  return false;
26
48
  }
27
49
 
28
- function checkUseExpressionValue(...controls) {
50
+ function toUseExpressionValue(...controls) {
29
51
  for (let control of controls) {
30
52
  if (control === USE_EXPRESSION_VALUE) {
31
53
  return true;
@@ -40,6 +62,9 @@ export {
40
62
  SKIP_IF_EMPTY_LIST,
41
63
  SKIP_IF_BLANK_STRING,
42
64
  SKIP_IF_EMPTY_STRING,
43
- checkUseExpression,
44
- checkUseExpressionValue,
65
+ USE_EXPRESSION,
66
+ USE_EXPRESSION_VALUE,
67
+ toSkipIfInfo,
68
+ toUseExpression,
69
+ toUseExpressionValue,
45
70
  };
@@ -1,5 +1,4 @@
1
1
  import {QueryLike} from "./QueryLike.js";
2
- import {isBlank} from "@scx-js/scx-common";
3
2
  import {QueryImpl} from "./QueryImpl.js";
4
3
 
5
4
  class Condition extends QueryLike {
@@ -14,13 +13,14 @@ class Condition extends QueryLike {
14
13
 
15
14
  constructor(selector, conditionType, value1, value2, useExpression, useExpressionValue, skipIfInfo) {
16
15
  super();
17
- //名称不能为空
18
- if (isBlank(selector)) {
19
- throw new Error("Where 参数错误 : 名称 不能为空 !!!");
16
+ if (selector == null) {
17
+ throw new Error("selector cannot be null");
20
18
  }
21
- //类型也不能为空
22
19
  if (conditionType == null) {
23
- throw new Error("Where 参数错误 : whereType 不能为空 !!!");
20
+ throw new Error("conditionType cannot be null");
21
+ }
22
+ if (skipIfInfo == null) {
23
+ throw new Error("skipIfInfo cannot be null");
24
24
  }
25
25
  this.#selector = selector;
26
26
  this.#conditionType = conditionType;
@@ -59,10 +59,6 @@ class Condition extends QueryLike {
59
59
  return this.#skipIfInfo;
60
60
  }
61
61
 
62
- isEmpty() {
63
-
64
- }
65
-
66
62
  toQuery() {
67
63
  return new QueryImpl().where(this);
68
64
  }
@@ -1,75 +1,34 @@
1
- /**
2
- * 等于
3
- */
4
- const EQ = "EQ";
5
-
6
- /**
7
- * 不等于
8
- */
9
- const NE = "NE";
10
-
11
- /**
12
- * 小于
13
- */
14
- const LT = "LT";
15
-
16
- /**
17
- * 小于等于
18
- */
19
- const LTE = "LTE";
20
-
21
- /**
22
- * 大于
23
- */
24
- const GT = "GT";
25
-
26
- /**
27
- * 大于等于
28
- */
29
- const GTE = "GTE";
30
-
31
- /**
32
- * Like
33
- */
34
- const LIKE = "LIKE";
35
-
36
- /**
37
- * Not Like
38
- */
39
- const NOT_LIKE = "NOT_LIKE";
40
-
41
- /**
42
- * Like 正则表达式
43
- */
44
- const LIKE_REGEX = "LIKE_REGEX";
45
-
46
- /**
47
- * Like 正则表达式
48
- */
49
- const NOT_LIKE_REGEX = "NOT_LIKE_REGEX";
50
-
51
- /**
52
- * IN
53
- */
54
- const IN = "IN";
55
-
56
- /**
57
- * NOT IN
58
- */
59
- const NOT_IN = "NOT_IN";
60
-
61
- /**
62
- * 在之间
63
- */
64
- const BETWEEN = "BETWEEN";
65
-
66
- /**
67
- * 不在之间
68
- */
69
- const NOT_BETWEEN = "NOT_BETWEEN";
70
-
1
+ class ConditionType {
2
+
3
+ #value;
4
+
5
+ constructor(value) {
6
+ this.#value = value;
7
+ }
8
+
9
+ value() {
10
+ return this.#value;
11
+ }
12
+
13
+ }
14
+
15
+ const EQ = new ConditionType("EQ");
16
+ const NE = new ConditionType("NE");
17
+ const LT = new ConditionType("LT");
18
+ const LTE = new ConditionType("LTE");
19
+ const GT = new ConditionType("GT");
20
+ const GTE = new ConditionType("GTE");
21
+ const LIKE = new ConditionType("LIKE");
22
+ const NOT_LIKE = new ConditionType("NOT_LIKE");
23
+ const LIKE_REGEX = new ConditionType("LIKE_REGEX");
24
+ const NOT_LIKE_REGEX = new ConditionType("NOT_LIKE_REGEX");
25
+ const IN = new ConditionType("IN");
26
+ const NOT_IN = new ConditionType("NOT_IN");
27
+ const BETWEEN = new ConditionType("BETWEEN");
28
+ const NOT_BETWEEN = new ConditionType("NOT_BETWEEN");
71
29
 
72
30
  export {
31
+ ConditionType,
73
32
  EQ,
74
33
  NE,
75
34
  LT,