semanticdb-core 1.0.2 → 1.0.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.
package/README.md
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import { RepresentationType } from '../common/representation';
|
|
2
2
|
import { SchemaType } from '../schema/schema';
|
|
3
|
-
|
|
4
|
-
[key: string]: any;
|
|
5
|
-
};
|
|
3
|
+
import { QueryType } from './types/query';
|
|
6
4
|
export interface PredItemType {
|
|
7
|
-
pred?: string | PredItemType;
|
|
8
5
|
operator?: string;
|
|
6
|
+
pred?: string | PredItemType;
|
|
9
7
|
name?: string;
|
|
8
|
+
schema?: string;
|
|
10
9
|
query?: QueryType;
|
|
10
|
+
args?: {
|
|
11
|
+
[key: string]: any;
|
|
12
|
+
};
|
|
13
|
+
params?: any[];
|
|
11
14
|
type?: string;
|
|
12
|
-
schema?: string;
|
|
13
15
|
by?: string;
|
|
14
|
-
params?: any[];
|
|
15
16
|
}
|
|
16
17
|
export interface GroupbyItemType {
|
|
17
18
|
_id: string;
|
|
@@ -24,10 +25,13 @@ export interface LogicformType {
|
|
|
24
25
|
query?: QueryType;
|
|
25
26
|
preds?: PredItemType[];
|
|
26
27
|
groupby?: GroupbyItemType[];
|
|
27
|
-
sort?:
|
|
28
|
+
sort?: {
|
|
29
|
+
[key: string]: 1 | -1;
|
|
30
|
+
};
|
|
28
31
|
having?: QueryType;
|
|
29
32
|
limit?: number;
|
|
30
33
|
limitBy?: number;
|
|
31
34
|
skip?: number;
|
|
32
35
|
representation?: RepresentationType;
|
|
36
|
+
entity_id?: string;
|
|
33
37
|
}
|
|
@@ -1,2 +1,33 @@
|
|
|
1
|
-
import { QueryType } from './
|
|
1
|
+
import { QueryType } from './types/query';
|
|
2
|
+
/**
|
|
3
|
+
* 判断一个维度是否在查询中被使用
|
|
4
|
+
* @param query - 查询对象
|
|
5
|
+
* @param dimension - 维度名称,支持链式结构(如:a_b_c)
|
|
6
|
+
* @returns 如果维度在查询中被使用则返回 true,否则返回 false
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* isDimensionInQuery({ customer: { name: "张三" } }, "customer_name") // true
|
|
10
|
+
* isDimensionInQuery({ "$or": [{ "a": "b" }] }, "$or") // false
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
2
13
|
export declare const isDimensionInQuery: (query: QueryType, dimension: string) => boolean;
|
|
14
|
+
/**
|
|
15
|
+
* 将嵌套的查询对象扁平化
|
|
16
|
+
* @param query - 查询对象
|
|
17
|
+
* @returns 扁平化后的查询对象
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* // Input
|
|
21
|
+
* {
|
|
22
|
+
* customer: {
|
|
23
|
+
* schema: 'customer',
|
|
24
|
+
* query: { name: '张三' }
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
* // Output
|
|
28
|
+
* {
|
|
29
|
+
* 'customer_name': '张三'
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare const getFlattenQuery: (query: QueryType) => QueryType;
|
|
@@ -1,36 +1,108 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isDimensionInQuery = void 0;
|
|
3
|
+
exports.getFlattenQuery = exports.isDimensionInQuery = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* 判断一个维度是否在查询中被使用
|
|
6
|
+
* @param query - 查询对象
|
|
7
|
+
* @param dimension - 维度名称,支持链式结构(如:a_b_c)
|
|
8
|
+
* @returns 如果维度在查询中被使用则返回 true,否则返回 false
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* isDimensionInQuery({ customer: { name: "张三" } }, "customer_name") // true
|
|
12
|
+
* isDimensionInQuery({ "$or": [{ "a": "b" }] }, "$or") // false
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
4
15
|
const isDimensionInQuery = (query, dimension) => {
|
|
16
|
+
var _a, _b, _c, _d;
|
|
5
17
|
const chain = dimension.split('_');
|
|
6
18
|
let chainedQuery = query;
|
|
7
19
|
for (let i = 0; i < chain.length; i++) {
|
|
8
20
|
const item = chain[i];
|
|
9
|
-
|
|
21
|
+
// TBD: 如果是$or, $and怎么办呢?
|
|
22
|
+
if (item === '$or' || item === '$and')
|
|
23
|
+
return false;
|
|
24
|
+
if (typeof chainedQuery !== 'object')
|
|
25
|
+
return false;
|
|
26
|
+
if (!(chainedQuery && 'item' in chainedQuery)) {
|
|
10
27
|
return false;
|
|
11
28
|
}
|
|
12
29
|
// last one
|
|
13
30
|
if (i === chain.length - 1) {
|
|
14
|
-
if (typeof chainedQuery[item] !== 'object' || !chainedQuery[item]
|
|
31
|
+
if (typeof chainedQuery[item] !== 'object' || !('schema' in chainedQuery[item])) {
|
|
15
32
|
if (typeof chainedQuery[item] === 'object' && '$exists' in chainedQuery[item]) {
|
|
16
33
|
return false;
|
|
17
34
|
}
|
|
18
35
|
return true;
|
|
19
36
|
}
|
|
37
|
+
// 接下去chainedQuery[item]都是LogicformType
|
|
20
38
|
if (chainedQuery[item].entity_id) {
|
|
21
39
|
return true;
|
|
22
40
|
}
|
|
41
|
+
// TBD: 如果是$or, $and怎么办呢?
|
|
42
|
+
if (((_b = (_a = chainedQuery[item]) === null || _a === void 0 ? void 0 : _a.query) === null || _b === void 0 ? void 0 : _b.$or) || ((_d = (_c = chainedQuery[item]) === null || _c === void 0 ? void 0 : _c.query) === null || _d === void 0 ? void 0 : _d.$and))
|
|
43
|
+
return false;
|
|
23
44
|
// TODO: 如果是name property,也行的,现在先简单搞一个
|
|
24
|
-
if (chainedQuery[item].query &&
|
|
45
|
+
if (chainedQuery[item].query &&
|
|
46
|
+
!('$or' in chainedQuery[item].query) &&
|
|
47
|
+
!('$and' in chainedQuery[item].query) &&
|
|
48
|
+
'别名' in chainedQuery[item].query // TODO: 这里是一个临时的方案,后面需要改成动态获取property
|
|
49
|
+
) {
|
|
25
50
|
return true;
|
|
26
51
|
}
|
|
27
52
|
return false;
|
|
28
53
|
}
|
|
29
|
-
if (typeof chainedQuery[item] !== 'object' || !chainedQuery[item]
|
|
54
|
+
if (typeof chainedQuery[item] !== 'object' || !('schema' in chainedQuery[item])) {
|
|
30
55
|
return false;
|
|
31
56
|
}
|
|
32
|
-
|
|
57
|
+
if (chainedQuery[item].query) {
|
|
58
|
+
chainedQuery = chainedQuery[item].query;
|
|
59
|
+
}
|
|
33
60
|
}
|
|
34
61
|
return true;
|
|
35
62
|
};
|
|
36
63
|
exports.isDimensionInQuery = isDimensionInQuery;
|
|
64
|
+
/**
|
|
65
|
+
* 将嵌套的查询对象扁平化
|
|
66
|
+
* @param query - 查询对象
|
|
67
|
+
* @returns 扁平化后的查询对象
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* // Input
|
|
71
|
+
* {
|
|
72
|
+
* customer: {
|
|
73
|
+
* schema: 'customer',
|
|
74
|
+
* query: { name: '张三' }
|
|
75
|
+
* }
|
|
76
|
+
* }
|
|
77
|
+
* // Output
|
|
78
|
+
* {
|
|
79
|
+
* 'customer_name': '张三'
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
const getFlattenQuery = (query) => {
|
|
84
|
+
const flattenQuery = {};
|
|
85
|
+
for (const [k, v] of Object.entries(query)) {
|
|
86
|
+
if (k === '$and' || k === '$or') {
|
|
87
|
+
// complex的暂时先不管
|
|
88
|
+
flattenQuery[k] = v;
|
|
89
|
+
}
|
|
90
|
+
else if (typeof v === 'object' &&
|
|
91
|
+
'schema' in v &&
|
|
92
|
+
'query' in v &&
|
|
93
|
+
!('groupby' in v) && // 不是复合的logicform
|
|
94
|
+
!('entity_id' in v) // entity_id的话,也没必要收起来
|
|
95
|
+
) {
|
|
96
|
+
// 这里是一个logicform
|
|
97
|
+
const flattenSubQuery = (0, exports.getFlattenQuery)(v.query);
|
|
98
|
+
for (const [sk, sv] of Object.entries(flattenSubQuery)) {
|
|
99
|
+
flattenQuery[`${k}_${sk}`] = sv;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
flattenQuery[k] = v;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return flattenQuery;
|
|
107
|
+
};
|
|
108
|
+
exports.getFlattenQuery = getFlattenQuery;
|
|
@@ -12,6 +12,17 @@ const findPropertyByName = (schemaID, propertyName, schemasDict) => {
|
|
|
12
12
|
const chain = propertyName.split('_');
|
|
13
13
|
let currentSchemaID = schemaID;
|
|
14
14
|
let property = undefined;
|
|
15
|
+
if (propertyName === '_id') {
|
|
16
|
+
return {
|
|
17
|
+
_sid: '_id',
|
|
18
|
+
_id: '_id',
|
|
19
|
+
name: '_id',
|
|
20
|
+
type: 'string',
|
|
21
|
+
primal_type: 'string',
|
|
22
|
+
constraints: {},
|
|
23
|
+
ui: {},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
15
26
|
for (let i = 0; i < chain.length; i++) {
|
|
16
27
|
const schema = schemasDict[currentSchemaID];
|
|
17
28
|
if (!schema) {
|