@scx-js/scx-data 0.1.9 → 0.2.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/index.js CHANGED
@@ -8,8 +8,10 @@ export * from "./query/serializer/OrderBySerializer.js";
8
8
  export * from "./query/serializer/QuerySerializer.js";
9
9
  export * from "./query/serializer/WhereSerializer.js";
10
10
  export * from "./query/GroupBy.js";
11
- export * from "./query/Logic.js";
12
- export * from "./query/LogicType.js";
11
+ export * from "./query/Junction.js";
12
+ export * from "./query/And.js";
13
+ export * from "./query/Or.js";
14
+ export * from "./query/Not.js";
13
15
  export * from "./query/OrderBy.js";
14
16
  export * from "./query/OrderByType.js";
15
17
  export * from "./query/Query.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scx-js/scx-data",
3
- "version": "0.1.9",
3
+ "version": "0.2.1",
4
4
  "description": "SCX Data",
5
5
  "license": "MIT",
6
6
  "author": "scx567888",
@@ -11,6 +11,6 @@
11
11
  "url": "https://github.com/scx567888/scx-js.git"
12
12
  },
13
13
  "dependencies": {
14
- "@scx-js/scx-common": "0.1.9"
14
+ "@scx-js/scx-common": "0.2.1"
15
15
  }
16
16
  }
package/query/And.js ADDED
@@ -0,0 +1,16 @@
1
+ import {Junction} from "./Junction.js";
2
+ import {Or} from "./Or.js";
3
+
4
+ class And extends Junction {
5
+
6
+ and(...clauses) {
7
+ return this.add(new And().add(clauses));
8
+ }
9
+
10
+ or(...clauses) {
11
+ return this.add(new Or().add(clauses));
12
+ }
13
+
14
+ }
15
+
16
+ export {And};
@@ -1,42 +1,35 @@
1
1
  import {QueryLike} from "./QueryLike.js";
2
2
  import {
3
3
  BETWEEN,
4
- EQUAL,
5
- GREATER_THAN,
6
- GREATER_THAN_OR_EQUAL,
4
+ EQ,
5
+ GT,
6
+ GTE,
7
7
  IN,
8
- IS_NOT_NULL,
9
- IS_NULL,
10
8
  JSON_CONTAINS,
11
9
  JSON_OVERLAPS,
12
- LESS_THAN,
13
- LESS_THAN_OR_EQUAL,
14
10
  LIKE,
15
11
  LIKE_REGEX,
12
+ LT,
13
+ LTE,
14
+ NE,
16
15
  NOT_BETWEEN,
17
- NOT_EQUAL,
18
16
  NOT_IN,
19
17
  NOT_LIKE,
20
18
  NOT_LIKE_REGEX,
21
19
  } from "./WhereType.js";
22
20
  import {Where} from "./Where.js";
23
21
  import {QueryImpl} from "./QueryImpl.js";
22
+ import {Not} from "./Not.js";
24
23
 
25
- class Logic extends QueryLike {
24
+ class Junction extends QueryLike {
26
25
 
27
- #logicType;
28
26
  #clauses;
29
27
 
30
- constructor(logicType) {
28
+ constructor() {
31
29
  super();
32
- this.#logicType = logicType;
33
30
  this.#clauses = [];
34
31
  }
35
32
 
36
- logicType() {
37
- return this.#logicType;
38
- }
39
-
40
33
  clauses() {
41
34
  return this.#clauses;
42
35
  }
@@ -61,36 +54,27 @@ class Logic extends QueryLike {
61
54
  }
62
55
 
63
56
  eq(fieldName, value, ...options) {
64
- return this.add(new Where(fieldName, EQUAL, value, null, ...options));
57
+ return this.add(new Where(fieldName, EQ, value, null, ...options));
65
58
  }
66
59
 
67
60
  ne(fieldName, value, ...options) {
68
- return this.add(new Where(fieldName, NOT_EQUAL, value, null, ...options));
61
+ return this.add(new Where(fieldName, NE, value, null, ...options));
69
62
  }
70
63
 
71
64
  lt(fieldName, value, ...options) {
72
- return this.add(new Where(fieldName, LESS_THAN, value, null, ...options));
65
+ return this.add(new Where(fieldName, LT, value, null, ...options));
73
66
  }
74
67
 
75
- le(fieldName, value, ...options) {
76
- return this.add(new Where(fieldName, LESS_THAN_OR_EQUAL, value, null, ...options));
68
+ lte(fieldName, value, ...options) {
69
+ return this.add(new Where(fieldName, LTE, value, null, ...options));
77
70
  }
78
71
 
79
72
  gt(fieldName, value, ...options) {
80
- return this.add(new Where(fieldName, GREATER_THAN, value, null, ...options));
81
- }
82
-
83
- ge(fieldName, value, ...options) {
84
- return this.add(new Where(fieldName, GREATER_THAN_OR_EQUAL, value, null, ...options));
85
- }
86
-
87
-
88
- isNull(fieldName, ...options) {
89
- return this.add(new Where(fieldName, IS_NULL, null, null, ...options));
73
+ return this.add(new Where(fieldName, GT, value, null, ...options));
90
74
  }
91
75
 
92
- isNotNull(fieldName, ...options) {
93
- return this.add(new Where(fieldName, IS_NOT_NULL, null, null, ...options));
76
+ gte(fieldName, value, ...options) {
77
+ return this.add(new Where(fieldName, GTE, value, null, ...options));
94
78
  }
95
79
 
96
80
  like(fieldName, value, ...options) {
@@ -133,6 +117,18 @@ class Logic extends QueryLike {
133
117
  return this.add(new Where(fieldName, JSON_OVERLAPS, value, null, ...options));
134
118
  }
135
119
 
120
+ and(...clauses) {
121
+ // 因为 js 不允许 循环依赖 所以 这个方法迁移到子类实现
122
+ }
123
+
124
+ or(...clauses) {
125
+ // 因为 js 不允许 循环依赖 所以 这个方法迁移到子类实现
126
+ }
127
+
128
+ not(clause) {
129
+ return this.add(new Not(clause));
130
+ }
131
+
136
132
  toQuery() {
137
133
  return new QueryImpl().where(this);
138
134
  }
@@ -140,5 +136,5 @@ class Logic extends QueryLike {
140
136
  }
141
137
 
142
138
  export {
143
- Logic,
139
+ Junction,
144
140
  };
package/query/Not.js ADDED
@@ -0,0 +1,22 @@
1
+ import {QueryLike} from "./QueryLike.js";
2
+ import {QueryImpl} from "./QueryImpl.js";
3
+
4
+ class Not extends QueryLike {
5
+
6
+ #clause;
7
+
8
+ constructor(clause) {
9
+ super();
10
+ this.#clause = clause;
11
+ }
12
+
13
+ clause() {
14
+ return this.#clause;
15
+ }
16
+
17
+ toQuery() {
18
+ return new QueryImpl().where(this);
19
+ }
20
+ }
21
+
22
+ export {Not};
package/query/Or.js ADDED
@@ -0,0 +1,17 @@
1
+ import {Junction} from "./Junction.js";
2
+ import {And} from "./And.js";
3
+
4
+ class Or extends Junction {
5
+
6
+ // 因为 js 不允许 循环依赖 所以 这两个方法迁移到子类
7
+ and(...clauses) {
8
+ return this.add(new And().add(clauses));
9
+ }
10
+
11
+ or(...clauses) {
12
+ return this.add(new Or().add(clauses));
13
+ }
14
+
15
+ }
16
+
17
+ export {Or};
package/query/Query.js CHANGED
@@ -1,6 +1,6 @@
1
1
  class Query {
2
2
 
3
- where(...whereClauses) {
3
+ where(where) {
4
4
  return this;
5
5
  }
6
6
 
@@ -55,10 +55,6 @@ class Query {
55
55
  return this;
56
56
  }
57
57
 
58
- addWhere(...whereClauses) {
59
- return this;
60
- }
61
-
62
58
  addGroupBy(...groupByClauses) {
63
59
  return this;
64
60
  }
@@ -67,10 +63,6 @@ class Query {
67
63
  return this;
68
64
  }
69
65
 
70
- removeWhereIf(filter) {
71
- return this;
72
- }
73
-
74
66
  removeGroupByIf(filter) {
75
67
  return this;
76
68
  }
@@ -1,37 +1,36 @@
1
1
  import {WhereClause} from "./WhereClause.js";
2
2
  import {
3
3
  BETWEEN,
4
- EQUAL,
5
- GREATER_THAN,
6
- GREATER_THAN_OR_EQUAL,
4
+ EQ,
5
+ GT,
6
+ GTE,
7
7
  IN,
8
- IS_NOT_NULL,
9
- IS_NULL,
10
8
  JSON_CONTAINS,
11
9
  JSON_OVERLAPS,
12
- LESS_THAN,
13
- LESS_THAN_OR_EQUAL,
14
10
  LIKE,
15
11
  LIKE_REGEX,
12
+ LT,
13
+ LTE,
14
+ NE,
16
15
  NOT_BETWEEN,
17
- NOT_EQUAL,
18
16
  NOT_IN,
19
17
  NOT_LIKE,
20
18
  NOT_LIKE_REGEX,
21
19
  } from "./WhereType.js";
22
20
  import {Where} from "./Where.js";
23
21
  import {QueryImpl} from "./QueryImpl.js";
24
- import {AND, OR} from "./LogicType.js";
25
22
  import {OrderBy} from "./OrderBy.js";
26
23
  import {ASC, DESC} from "./OrderByType.js";
27
- import {Logic} from "./Logic.js";
24
+ import {Not} from "./Not.js";
25
+ import {And} from "./And.js";
26
+ import {Or} from "./Or.js";
28
27
 
29
28
  function query(oldQuery) {
30
29
  return new QueryImpl(oldQuery);
31
30
  }
32
31
 
33
- function where(...whereClauses) {
34
- return new QueryImpl().where(...whereClauses);
32
+ function where(whereClauses) {
33
+ return new QueryImpl().where(whereClauses);
35
34
  }
36
35
 
37
36
  function groupBy(...groupByClauses) {
@@ -51,11 +50,15 @@ function limit(numberOfRows) {
51
50
  }
52
51
 
53
52
  function and(...clauses) {
54
- return new Logic(AND).add(...clauses);
53
+ return new And().add(...clauses);
55
54
  }
56
55
 
57
56
  function or(...clauses) {
58
- return new Logic(OR).add(...clauses);
57
+ return new Or().add(...clauses);
58
+ }
59
+
60
+ function not(clause) {
61
+ return new Not(clause);
59
62
  }
60
63
 
61
64
  function asc(name, ...options) {
@@ -67,36 +70,27 @@ function desc(name, ...options) {
67
70
  }
68
71
 
69
72
  function eq(fieldName, value, ...options) {
70
- return new Where(fieldName, EQUAL, value, null, ...options);
73
+ return new Where(fieldName, EQ, value, null, ...options);
71
74
  }
72
75
 
73
76
  function ne(fieldName, value, ...options) {
74
- return new Where(fieldName, NOT_EQUAL, value, null, ...options);
77
+ return new Where(fieldName, NE, value, null, ...options);
75
78
  }
76
79
 
77
-
78
80
  function lt(fieldName, value, ...options) {
79
- return new Where(fieldName, LESS_THAN, value, null, ...options);
81
+ return new Where(fieldName, LT, value, null, ...options);
80
82
  }
81
83
 
82
- function le(fieldName, value, ...options) {
83
- return new Where(fieldName, LESS_THAN_OR_EQUAL, value, null, ...options);
84
+ function lte(fieldName, value, ...options) {
85
+ return new Where(fieldName, LTE, value, null, ...options);
84
86
  }
85
87
 
86
88
  function gt(fieldName, value, ...options) {
87
- return new Where(fieldName, GREATER_THAN, value, null, ...options);
88
- }
89
-
90
- function ge(fieldName, value, ...options) {
91
- return new Where(fieldName, GREATER_THAN_OR_EQUAL, value, null, ...options);
92
- }
93
-
94
- function isNull(fieldName, ...options) {
95
- return new Where(fieldName, IS_NULL, null, null, ...options);
89
+ return new Where(fieldName, GT, value, null, ...options);
96
90
  }
97
91
 
98
- function isNotNull(fieldName, ...options) {
99
- return new Where(fieldName, IS_NOT_NULL, null, null, ...options);
92
+ function gte(fieldName, value, ...options) {
93
+ return new Where(fieldName, GTE, value, null, ...options);
100
94
  }
101
95
 
102
96
  function like(fieldName, value, ...options) {
@@ -153,16 +147,15 @@ export {
153
147
  limit,
154
148
  and,
155
149
  or,
150
+ not,
156
151
  asc,
157
152
  desc,
158
153
  eq,
159
154
  ne,
160
155
  lt,
161
- le,
156
+ lte,
162
157
  gt,
163
- ge,
164
- isNull,
165
- isNotNull,
158
+ gte,
166
159
  like,
167
160
  notLike,
168
161
  likeRegex,
@@ -15,16 +15,15 @@ class QueryImpl extends Query {
15
15
  */
16
16
  constructor(old) {
17
17
  super();
18
- this.#where = [];
18
+ this.#where = null;
19
19
  this.#groupBy = [];
20
20
  this.#orderBy = [];
21
21
  this.#offset = null;
22
22
  this.#limit = null;
23
23
  }
24
24
 
25
- where(...whereClauses) {
26
- this.clearWhere();
27
- this.addWhere(...whereClauses);
25
+ where(where) {
26
+ this.#where = where;
28
27
  return this;
29
28
  }
30
29
 
@@ -73,7 +72,7 @@ class QueryImpl extends Query {
73
72
  }
74
73
 
75
74
  clearWhere() {
76
- this.#where = [];
75
+ this.#where = null;
77
76
  return this;
78
77
  }
79
78
 
@@ -97,20 +96,6 @@ class QueryImpl extends Query {
97
96
  return this;
98
97
  }
99
98
 
100
- addWhere(...whereClauses) {
101
- for (let whereClause of whereClauses) {
102
- if (whereClause == null) {
103
- continue;
104
- }
105
- if (Array.isArray(whereClause)) {
106
- this.addWhere(...whereClause);
107
- continue;
108
- }
109
- this.#where.push(whereClause);
110
- }
111
- return this;
112
- }
113
-
114
99
  addGroupBy(...groupByClauses) {
115
100
  for (let groupByClause of groupByClauses) {
116
101
  if (groupByClause == null) {
@@ -11,8 +11,8 @@ class QueryLike extends Query {
11
11
  return this.#query;
12
12
  }
13
13
 
14
- where(...whereClauses) {
15
- this.query().where(...whereClauses);
14
+ where(where) {
15
+ this.query().where(where);
16
16
  return this;
17
17
  }
18
18
 
@@ -81,11 +81,6 @@ class QueryLike extends Query {
81
81
  return this;
82
82
  }
83
83
 
84
- addWhere(...whereClauses) {
85
- this.query().addWhere(whereClauses);
86
- return this;
87
- }
88
-
89
84
  addGroupBy(...groupByClauses) {
90
85
  this.query().addGroupBy(groupByClauses);
91
86
  return this;
@@ -96,11 +91,6 @@ class QueryLike extends Query {
96
91
  return this;
97
92
  }
98
93
 
99
- removeWhereIf(filter) {
100
- this.query().removeWhereIf(filter);
101
- return this;
102
- }
103
-
104
94
  removeGroupByIf(filter) {
105
95
  this.query().removeGroupByIf(filter);
106
96
  return this;
@@ -1,42 +1,32 @@
1
- /**
2
- * 为空
3
- */
4
- const IS_NULL = "IS_NULL";
5
-
6
- /**
7
- * 不为空
8
- */
9
- const IS_NOT_NULL = "IS_NOT_NULL";
10
-
11
1
  /**
12
2
  * 等于
13
3
  */
14
- const EQUAL = "EQUAL";
4
+ const EQ = "EQ";
15
5
 
16
6
  /**
17
7
  * 不等于
18
8
  */
19
- const NOT_EQUAL = "NOT_EQUAL";
9
+ const NE = "NE";
20
10
 
21
11
  /**
22
12
  * 小于
23
13
  */
24
- const LESS_THAN = "LESS_THAN";
14
+ const LT = "LT";
25
15
 
26
16
  /**
27
17
  * 小于等于
28
18
  */
29
- const LESS_THAN_OR_EQUAL = "LESS_THAN";
19
+ const LTE = "LTE";
30
20
 
31
21
  /**
32
22
  * 大于
33
23
  */
34
- const GREATER_THAN = "GREATER_THAN";
24
+ const GT = "GT";
35
25
 
36
26
  /**
37
27
  * 大于等于
38
28
  */
39
- const GREATER_THAN_OR_EQUAL = "GREATER_THAN";
29
+ const GTE = "GTE";
40
30
 
41
31
  /**
42
32
  * Like
@@ -88,14 +78,12 @@ const JSON_OVERLAPS = "JSON_OVERLAPS";
88
78
 
89
79
 
90
80
  export {
91
- IS_NULL,
92
- IS_NOT_NULL,
93
- EQUAL,
94
- NOT_EQUAL,
95
- LESS_THAN,
96
- LESS_THAN_OR_EQUAL,
97
- GREATER_THAN,
98
- GREATER_THAN_OR_EQUAL,
81
+ EQ,
82
+ NE,
83
+ LT,
84
+ LTE,
85
+ GT,
86
+ GTE,
99
87
  LIKE,
100
88
  NOT_LIKE,
101
89
  LIKE_REGEX,
@@ -2,7 +2,9 @@ import {Where} from "../Where.js";
2
2
  import {isArray} from "@scx-js/scx-common";
3
3
  import {Query} from "../Query.js";
4
4
  import {WhereClause} from "../WhereClause.js";
5
- import {Logic} from "../Logic.js";
5
+ import {And} from "../And.js";
6
+ import {Or} from "../Or.js";
7
+ import {Not} from "../Not.js";
6
8
 
7
9
  class WhereSerializer {
8
10
 
@@ -13,8 +15,14 @@ class WhereSerializer {
13
15
  if (obj instanceof WhereClause) {
14
16
  return this.serializeWhereClause(obj);
15
17
  }
16
- if (obj instanceof Logic) {
17
- return this.serializeLogic(obj);
18
+ if (obj instanceof And) {
19
+ return this.serializeAnd(obj);
20
+ }
21
+ if (obj instanceof Or) {
22
+ return this.serializeOr(obj);
23
+ }
24
+ if (obj instanceof Not) {
25
+ return this.serializeNot(obj);
18
26
  }
19
27
  if (obj instanceof Where) {
20
28
  return this.serializeWhere(obj);
@@ -40,14 +48,27 @@ class WhereSerializer {
40
48
  };
41
49
  }
42
50
 
43
- serializeLogic(l) {
51
+ serializeAnd(l) {
44
52
  return {
45
- "@type": "Logic",
46
- "logicType": l.logicType(),
53
+ "@type": "And",
47
54
  "clauses": this.serializeAll(l.clauses()),
48
55
  };
49
56
  }
50
57
 
58
+ serializeOr(l) {
59
+ return {
60
+ "@type": "Or",
61
+ "clauses": this.serializeAll(l.clauses()),
62
+ };
63
+ }
64
+
65
+ serializeNot(l) {
66
+ return {
67
+ "@type": "Not",
68
+ "clause": this.serialize(l.clause()),
69
+ };
70
+ }
71
+
51
72
  serializeWhere(w) {
52
73
  return {
53
74
  "@type": "Where",
@@ -1,7 +0,0 @@
1
- const OR = "OR";
2
- const AND = "AND";
3
-
4
- export {
5
- OR,
6
- AND,
7
- };