@scx-js/scx-data 0.2.1 → 0.2.3

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,42 @@
1
+ import {SKIP_IF_BLANK_STRING, SKIP_IF_EMPTY_LIST, SKIP_IF_EMPTY_STRING, SKIP_IF_NULL} from "./BuildControl.js";
2
+
3
+ class SkipIfInfo {
4
+ skipIfNull;
5
+ skipIfEmptyList;
6
+ skipIfEmptyString;
7
+ skipIfBlankString;
8
+
9
+ constructor(skipIfNull, skipIfEmptyList, skipIfEmptyString, skipIfBlankString) {
10
+ this.skipIfNull = skipIfNull;
11
+ this.skipIfEmptyList = skipIfEmptyList;
12
+ this.skipIfEmptyString = skipIfEmptyString;
13
+ this.skipIfBlankString = skipIfBlankString;
14
+ }
15
+
16
+ }
17
+
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
+ }
33
+ }
34
+
35
+ return new SkipIfInfo(skipIfNull, skipIfEmptyList, skipIfEmptyString, skipIfBlankString);
36
+
37
+ }
38
+
39
+ export {
40
+ SkipIfInfo,
41
+ ofSkipIfInfo,
42
+ };
@@ -3,31 +3,21 @@ import {QueryImpl} from "./QueryImpl.js";
3
3
 
4
4
  class WhereClause extends QueryLike {
5
5
 
6
- #whereClause;
6
+ #expression;
7
7
  #params;
8
8
 
9
- constructor(whereClause, params) {
9
+ constructor(expression, params) {
10
10
  super();
11
- this.#whereClause = whereClause;
11
+ this.#expression = expression;
12
12
  this.#params = params;
13
13
  }
14
14
 
15
- /**
16
- * 拼接
17
- *
18
- * @param other a
19
- * @return WhereClause
20
- */
21
- concat(other) {
22
- return new WhereClause(this.#whereClause.concat(other.#whereClause), this.#params.concat(other.params));
23
- }
24
-
25
15
  isEmpty() {
26
- return (this.#whereClause == null || this.#whereClause.isEmpty()) && (this.#params == null || this.#params.length === 0);
16
+ return (this.#expression == null || this.#expression.isEmpty()) && (this.#params == null || this.#params.length === 0);
27
17
  }
28
18
 
29
- whereClause() {
30
- return this.#whereClause;
19
+ expression() {
20
+ return this.#expression;
31
21
  }
32
22
 
33
23
  params() {
@@ -0,0 +1,47 @@
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
+ };
@@ -0,0 +1,120 @@
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
+ };
@@ -1,30 +0,0 @@
1
- import {FieldPolicy} from "../FieldPolicy.js";
2
-
3
- class FieldPolicySerializer {
4
-
5
- serialize(obj) {
6
- if (obj instanceof FieldPolicy) {
7
- return this.serializeFieldPolicy(obj);
8
- }
9
- return obj;
10
- }
11
-
12
- serializeFieldPolicy(fieldPolicy) {
13
- return {
14
- "@type": "FieldPolicy",
15
- "filterMode": fieldPolicy.filterMode(),
16
- "fieldNames": fieldPolicy.fieldNames(),
17
- "ignoreNull": fieldPolicy.ignoreNull_(),
18
- "ignoreNulls": fieldPolicy.ignoreNulls(),
19
- "expressions": fieldPolicy.expressions(),
20
- };
21
- }
22
-
23
- }
24
-
25
- const FIELD_POLICY_SERIALIZER = new FieldPolicySerializer();
26
-
27
- export {
28
- FIELD_POLICY_SERIALIZER,
29
- FieldPolicySerializer,
30
- };
package/query/GroupBy.js DELETED
@@ -1,36 +0,0 @@
1
- import {isBlank} from "@scx-js/scx-common";
2
- import {QueryImpl} from "./QueryImpl.js";
3
- import {QueryLike} from "./QueryLike.js";
4
- import {ofInfo} from "./QueryOption.js";
5
-
6
- class GroupBy extends QueryLike {
7
-
8
- #name;
9
- #info;
10
-
11
- constructor(name, ...options) {
12
- super();
13
- if (isBlank(name)) {
14
- throw new Error("GroupBy 参数错误 : 名称 不能为空 !!!");
15
- }
16
- this.#name = name;
17
- this.#info = ofInfo(...options);
18
- }
19
-
20
- name() {
21
- return this.#name;
22
- }
23
-
24
- info() {
25
- return this.#info;
26
- }
27
-
28
- toQuery() {
29
- return new QueryImpl().groupBy(this);
30
- }
31
-
32
- }
33
-
34
- export {
35
- GroupBy,
36
- };
@@ -1,74 +0,0 @@
1
- class QueryOption {
2
-
3
- #value;
4
-
5
- constructor(value) {
6
- this.#value = value;
7
- }
8
-
9
-
10
- }
11
-
12
- const REPLACE = new QueryOption("REPLACE");
13
- const SKIP_IF_NULL = new QueryOption("SKIP_IF_NULL");
14
- const SKIP_IF_EMPTY_LIST = new QueryOption("SKIP_IF_EMPTY_LIST");
15
- const USE_ORIGINAL_NAME = new QueryOption("USE_ORIGINAL_NAME");
16
- const USE_JSON_EXTRACT = new QueryOption("USE_JSON_EXTRACT");
17
- const USE_ORIGINAL_VALUE = new QueryOption("USE_ORIGINAL_VALUE");
18
-
19
- function ofInfo(...queryOptions) {
20
- let replace = false;
21
- let skipIfNull = false;
22
- let skipIfEmptyList = false;
23
- let useOriginalName = false;
24
- let useJsonExtract = false;
25
- let useOriginalValue = false;
26
- for (let option of queryOptions) {
27
- if (option === REPLACE) {
28
- replace = true;
29
- } else if (option === SKIP_IF_NULL) {
30
- skipIfNull = true;
31
- } else if (option === SKIP_IF_EMPTY_LIST) {
32
- skipIfEmptyList = true;
33
- } else if (option === USE_ORIGINAL_NAME) {
34
- useOriginalName = true;
35
- } else if (option === USE_JSON_EXTRACT) {
36
- useJsonExtract = true;
37
- } else if (option === USE_ORIGINAL_VALUE) {
38
- useOriginalValue = true;
39
- }
40
- }
41
- return new Info(replace, skipIfNull, skipIfEmptyList, useOriginalName, useJsonExtract, useOriginalValue);
42
- }
43
-
44
- class Info {
45
- replace;
46
- skipIfNull;
47
- skipIfEmptyList;
48
- useOriginalName;
49
- useJsonExtract;
50
- useOriginalValue;
51
-
52
-
53
- constructor(replace, skipIfNull, skipIfEmptyList, useOriginalName, useJsonExtract, useOriginalValue) {
54
- this.replace = replace;
55
- this.skipIfNull = skipIfNull;
56
- this.skipIfEmptyList = skipIfEmptyList;
57
- this.useOriginalName = useOriginalName;
58
- this.useJsonExtract = useJsonExtract;
59
- this.useOriginalValue = useOriginalValue;
60
- }
61
-
62
- }
63
-
64
- export {
65
- QueryOption,
66
- REPLACE,
67
- SKIP_IF_NULL,
68
- SKIP_IF_EMPTY_LIST,
69
- USE_ORIGINAL_NAME,
70
- USE_JSON_EXTRACT,
71
- USE_ORIGINAL_VALUE,
72
- Info,
73
- ofInfo,
74
- };
package/query/Where.js DELETED
@@ -1,59 +0,0 @@
1
- import {QueryLike} from "./QueryLike.js";
2
- import {isBlank} from "@scx-js/scx-common";
3
- import {QueryImpl} from "./QueryImpl.js";
4
- import {ofInfo} from "./QueryOption.js";
5
-
6
- class Where extends QueryLike {
7
-
8
- #name;
9
- #whereType;
10
- #value1;
11
- #value2;
12
- #info;
13
-
14
- constructor(name, whereType, value1, value2, ...options) {
15
- super();
16
- //名称不能为空
17
- if (isBlank(name)) {
18
- throw new Error("Where 参数错误 : 名称 不能为空 !!!");
19
- }
20
- //类型也不能为空
21
- if (whereType == null) {
22
- throw new Error("Where 参数错误 : whereType 不能为空 !!!");
23
- }
24
- this.#name = name;
25
- this.#whereType = whereType;
26
- this.#value1 = value1;
27
- this.#value2 = value2;
28
- this.#info = ofInfo(...options);
29
- }
30
-
31
- name() {
32
- return this.#name;
33
- }
34
-
35
- whereType() {
36
- return this.#whereType;
37
- }
38
-
39
- value1() {
40
- return this.#value1;
41
- }
42
-
43
- value2() {
44
- return this.#value2;
45
- }
46
-
47
- info() {
48
- return this.#info;
49
- }
50
-
51
- toQuery() {
52
- return new QueryImpl().where(this);
53
- }
54
-
55
- }
56
-
57
- export {
58
- Where,
59
- };
@@ -1,50 +0,0 @@
1
- import {Query} from "../Query.js";
2
- import {isArray} from "@scx-js/scx-common";
3
- import {GroupBy} from "../GroupBy.js";
4
-
5
- class GroupBySerializer {
6
-
7
- serialize(obj) {
8
- if (obj instanceof String) {
9
- return this.serializeString(obj);
10
- }
11
- if (obj instanceof GroupBy) {
12
- return this.serializeGroupBy(obj);
13
- }
14
- if (obj instanceof Query) {
15
- return this.serializeQuery(obj.getWhere());
16
- }
17
- if (isArray(obj)) {
18
- return this.serializeAll(obj);
19
- }
20
- return obj;
21
- }
22
-
23
- serializeString(str) {
24
- return str;
25
- }
26
-
27
- serializeGroupBy(w) {
28
- return {
29
- "@type": "GroupBy",
30
- "name": w.name(),
31
- "info": w.info(),
32
- };
33
- }
34
-
35
- serializeQuery(w) {
36
- return this.serializeAll(w.getGroupBy());
37
- }
38
-
39
-
40
- serializeAll(objs) {
41
- const arr = [];
42
- for (let i = 0; i < objs.length; i = i + 1) {
43
- arr[i] = this.serialize(objs[i]);
44
- }
45
- return arr;
46
- }
47
-
48
- }
49
-
50
- export {GroupBySerializer};
@@ -1,54 +0,0 @@
1
- import {Query} from "../Query.js";
2
- import {isArray} from "@scx-js/scx-common";
3
- import {OrderBy} from "../OrderBy.js";
4
-
5
- class OrderBySerializer {
6
-
7
- serialize(obj) {
8
- if (obj instanceof String) {
9
- return this.serializeString(obj);
10
- }
11
- if (obj instanceof OrderBy) {
12
- return this.serializeOrderBy(obj);
13
- }
14
- if (obj instanceof Query) {
15
- return this.serializeQuery(obj.getWhere());
16
- }
17
- if (isArray(obj)) {
18
- return this.serializeAll(obj);
19
- }
20
- return obj;
21
- }
22
-
23
- serializeString(str) {
24
- return str;
25
- }
26
-
27
- serializeOrderBy(w) {
28
- return {
29
- "@type": "OrderBy",
30
- "name": w.name(),
31
- "orderByType": w.orderByType(),
32
- "info": w.info(),
33
- };
34
- }
35
-
36
- serializeQuery(w) {
37
- return this.serializeAll(w.getOrderBy());
38
- }
39
-
40
-
41
- serializeAll(objs) {
42
- const arr = [];
43
- for (let i = 0; i < objs.length; i = i + 1) {
44
- arr[i] = this.serialize(objs[i]);
45
- }
46
- return arr;
47
- }
48
-
49
-
50
- }
51
-
52
- export {
53
- OrderBySerializer,
54
- };
@@ -1,35 +0,0 @@
1
- import {WhereSerializer} from "./WhereSerializer.js";
2
- import {GroupBySerializer} from "./GroupBySerializer.js";
3
- import {OrderBySerializer} from "./OrderBySerializer.js";
4
-
5
- class QuerySerializer {
6
-
7
- #whereSerializer;
8
- #groupBySerializer;
9
- #orderBySerializer;
10
-
11
- constructor() {
12
- this.#whereSerializer = new WhereSerializer();
13
- this.#groupBySerializer = new GroupBySerializer();
14
- this.#orderBySerializer = new OrderBySerializer();
15
- }
16
-
17
- serializeQuery(query) {
18
- return {
19
- "@type": "Query",
20
- "where": this.#whereSerializer.serialize(query.getWhere()),
21
- "groupBy": this.#groupBySerializer.serialize(query.getGroupBy()),
22
- "orderBy": this.#orderBySerializer.serialize(query.getOrderBy()),
23
- "offset": query.getOffset(),
24
- "limit": query.getLimit(),
25
- };
26
- }
27
-
28
- }
29
-
30
- const QUERY_SERIALIZER = new QuerySerializer();
31
-
32
- export {
33
- QuerySerializer,
34
- QUERY_SERIALIZER,
35
- };
@@ -1,100 +0,0 @@
1
- import {Where} from "../Where.js";
2
- import {isArray} from "@scx-js/scx-common";
3
- import {Query} from "../Query.js";
4
- import {WhereClause} from "../WhereClause.js";
5
- import {And} from "../And.js";
6
- import {Or} from "../Or.js";
7
- import {Not} from "../Not.js";
8
-
9
- class WhereSerializer {
10
-
11
- serialize(obj) {
12
- if (obj instanceof String) {
13
- return this.serializeString(obj);
14
- }
15
- if (obj instanceof WhereClause) {
16
- return this.serializeWhereClause(obj);
17
- }
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);
26
- }
27
- if (obj instanceof Where) {
28
- return this.serializeWhere(obj);
29
- }
30
- if (obj instanceof Query) {
31
- return this.serializeQuery(obj.getWhere());
32
- }
33
- if (isArray(obj)) {
34
- return this.serializeAll(obj);
35
- }
36
- return obj;
37
- }
38
-
39
- serializeString(str) {
40
- return str;
41
- }
42
-
43
- serializeWhereClause(w) {
44
- return {
45
- "@type": "WhereClause",
46
- "whereClause": w.whereClause(),
47
- "params": w.params(),
48
- };
49
- }
50
-
51
- serializeAnd(l) {
52
- return {
53
- "@type": "And",
54
- "clauses": this.serializeAll(l.clauses()),
55
- };
56
- }
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
-
72
- serializeWhere(w) {
73
- return {
74
- "@type": "Where",
75
- "name": w.name(),
76
- "whereType": w.whereType(),
77
- "value1": w.value1(),
78
- "value2": w.value2(),
79
- "info": w.info(),
80
- };
81
- }
82
-
83
- serializeQuery(w) {
84
- return this.serializeAll(w.getWhere());
85
- }
86
-
87
-
88
- serializeAll(objs) {
89
- const arr = [];
90
- for (let i = 0; i < objs.length; i = i + 1) {
91
- arr[i] = this.serialize(objs[i]);
92
- }
93
- return arr;
94
- }
95
-
96
- }
97
-
98
- export {
99
- WhereSerializer,
100
- };