@steroidsjs/nest 1.3.6 → 1.3.7
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/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Brackets } from 'typeorm';
|
|
2
1
|
import { WhereExpressionBuilder } from 'typeorm/query-builder/WhereExpressionBuilder';
|
|
3
2
|
export declare type IConditionOperatorSingle = '=' | '>' | '>=' | '=>' | '<' | '<=' | '=<' | 'like' | 'ilike' | 'between' | 'in' | 'and' | '&&' | 'or' | '||';
|
|
4
3
|
export declare type IConditionOperatorAndOr = 'and' | '&&' | 'or' | '||';
|
|
5
4
|
export declare type ICondition = Record<string, unknown> | [IConditionOperatorAndOr, ...any[]] | [IConditionOperatorSingle, string, ...any[]] | ICondition[] | ((qb: WhereExpressionBuilder) => any);
|
|
6
5
|
export declare class ConditionHelper {
|
|
7
|
-
static
|
|
6
|
+
static toTypeOrmFilter(condition: ICondition): any;
|
|
7
|
+
static toTypeOrm(condition: ICondition, filterEmpty?: boolean): any;
|
|
8
8
|
}
|
|
@@ -1,15 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ConditionHelper = void 0;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
4
5
|
const typeorm_1 = require("typeorm");
|
|
5
|
-
const
|
|
6
|
+
const emptyCondition = {};
|
|
7
|
+
const isEmpty = value => value === null || typeof value === 'undefined' || value === emptyCondition;
|
|
6
8
|
class ConditionHelper {
|
|
7
|
-
static
|
|
9
|
+
static toTypeOrmFilter(condition) {
|
|
10
|
+
return ConditionHelper.toTypeOrm(condition, true);
|
|
11
|
+
}
|
|
12
|
+
static toTypeOrm(condition, filterEmpty = false) {
|
|
8
13
|
if (typeof condition === 'function') {
|
|
9
14
|
return new typeorm_1.Brackets(condition);
|
|
10
15
|
}
|
|
11
16
|
if (typeof condition === 'object' && !Array.isArray(condition)) {
|
|
12
|
-
|
|
17
|
+
const result = Object.keys(condition).reduce((obj, key) => {
|
|
18
|
+
const value = condition[key];
|
|
19
|
+
if (!filterEmpty || !isEmpty(value)) {
|
|
20
|
+
(0, lodash_1.set)(obj, key, value);
|
|
21
|
+
}
|
|
22
|
+
return obj;
|
|
23
|
+
}, {});
|
|
24
|
+
return Object.keys(result).length === 0 ? emptyCondition : result;
|
|
13
25
|
}
|
|
14
26
|
if (Array.isArray(condition) && condition.length > 1 && typeof condition[0] === 'string') {
|
|
15
27
|
let operator = condition[0].toLowerCase();
|
|
@@ -17,50 +29,61 @@ class ConditionHelper {
|
|
|
17
29
|
if (isNot) {
|
|
18
30
|
operator = operator.replace(/^not\s+/, '');
|
|
19
31
|
}
|
|
32
|
+
const objectWhere = (isNot, isEmpty, key, value) => {
|
|
33
|
+
return !filterEmpty || !isEmpty
|
|
34
|
+
? (0, lodash_1.set)({}, key, isNot ? (0, typeorm_1.Not)(value) : value)
|
|
35
|
+
: emptyCondition;
|
|
36
|
+
};
|
|
20
37
|
const key = condition[1];
|
|
21
38
|
const value = condition[2];
|
|
22
39
|
switch (operator) {
|
|
23
40
|
case '=':
|
|
24
|
-
return
|
|
41
|
+
return objectWhere(isNot, isEmpty(value), key, value);
|
|
25
42
|
case '>':
|
|
26
|
-
return
|
|
43
|
+
return objectWhere(isNot, isEmpty(value), key, (0, typeorm_1.MoreThan)(value));
|
|
27
44
|
case '>=':
|
|
28
45
|
case '=>':
|
|
29
|
-
return
|
|
46
|
+
return objectWhere(isNot, isEmpty(value), key, (0, typeorm_1.MoreThanOrEqual)(value));
|
|
30
47
|
case '<':
|
|
31
|
-
return
|
|
48
|
+
return objectWhere(isNot, isEmpty(value), key, (0, typeorm_1.LessThan)(value));
|
|
32
49
|
case '<=':
|
|
33
50
|
case '=<':
|
|
34
|
-
return
|
|
51
|
+
return objectWhere(isNot, isEmpty(value), key, (0, typeorm_1.LessThanOrEqual)(value));
|
|
35
52
|
case 'like':
|
|
36
|
-
return { [key]: notCondition(isNot, (0, typeorm_1.Like)(value)) };
|
|
37
53
|
case 'ilike':
|
|
38
|
-
|
|
54
|
+
const likeMethod = operator === 'ilike' ? typeorm_1.ILike : typeorm_1.Like;
|
|
55
|
+
return objectWhere(isNot, isEmpty(value), key, likeMethod(value.indexOf('%') !== -1 ? value : '%' + value + '%'));
|
|
39
56
|
case 'between':
|
|
40
|
-
return
|
|
57
|
+
return objectWhere(isNot, isEmpty(condition[2] || condition[3]), key, (0, typeorm_1.Between)(condition[2], condition[3]));
|
|
41
58
|
case 'in':
|
|
42
|
-
if (!Array.isArray(value)) {
|
|
59
|
+
if (value && !Array.isArray(value)) {
|
|
43
60
|
throw Error('Wrong value for IN operator: ' + JSON.stringify(value));
|
|
44
61
|
}
|
|
45
|
-
return
|
|
62
|
+
return objectWhere(isNot, isEmpty(value) || value.length === 0, key, (0, typeorm_1.In)(value));
|
|
46
63
|
case 'and':
|
|
47
64
|
case '&&':
|
|
48
|
-
if (isNot) {
|
|
49
|
-
throw Error('Unsupport NOT for AND operator. Operator: ' + operator);
|
|
50
|
-
}
|
|
51
|
-
return new typeorm_1.Brackets(query2 => {
|
|
52
|
-
condition.slice(1).forEach(item => {
|
|
53
|
-
query2.andWhere(ConditionHelper.toTypeOrm(item));
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
65
|
case 'or':
|
|
57
66
|
case '||':
|
|
58
67
|
if (isNot) {
|
|
59
|
-
throw Error('Unsupport NOT for OR operator. Operator: ' + operator);
|
|
68
|
+
throw Error('Unsupport NOT for AND/OR operator. Operator: ' + operator);
|
|
69
|
+
}
|
|
70
|
+
const isOr = ['or', '||'].includes(operator);
|
|
71
|
+
const values = condition.slice(1)
|
|
72
|
+
.map(item => ConditionHelper.toTypeOrm(item, filterEmpty))
|
|
73
|
+
.filter(value => !isEmpty(value));
|
|
74
|
+
if (values.length === 0) {
|
|
75
|
+
return emptyCondition;
|
|
60
76
|
}
|
|
61
|
-
return new typeorm_1.Brackets(query2 => {
|
|
62
|
-
|
|
63
|
-
|
|
77
|
+
return new typeorm_1.Brackets((query2) => {
|
|
78
|
+
const parentQuery = query2.parentQueryBuilder;
|
|
79
|
+
query2.expressionMap.joinAttributes = parentQuery.expressionMap.joinAttributes;
|
|
80
|
+
values.forEach(value => {
|
|
81
|
+
if (isOr) {
|
|
82
|
+
query2.orWhere(value);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
query2.andWhere(value);
|
|
86
|
+
}
|
|
64
87
|
});
|
|
65
88
|
});
|
|
66
89
|
default:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConditionHelper.js","sourceRoot":"","sources":["../../../../src/usecases/helpers/ConditionHelper.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"ConditionHelper.js","sourceRoot":"","sources":["../../../../src/usecases/helpers/ConditionHelper.ts"],"names":[],"mappings":";;;AAAA,mCAAmC;AACnC,qCAAoI;AAYpI,MAAM,cAAc,GAAG,EAAE,CAAC;AAC1B,MAAM,OAAO,GAAG,KAAK,CAAC,EAAE,CAAC,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,WAAW,IAAI,KAAK,KAAK,cAAc,CAAC;AAEpG,MAAa,eAAe;IACxB,MAAM,CAAC,eAAe,CAAC,SAAqB;QACxC,OAAO,eAAe,CAAC,SAAS,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACtD,CAAC;IAED,MAAM,CAAC,SAAS,CAAC,SAAqB,EAAE,WAAW,GAAG,KAAK;QAEvD,IAAI,OAAO,SAAS,KAAK,UAAU,EAAE;YACjC,OAAO,IAAI,kBAAQ,CAAC,SAAS,CAAC,CAAC;SAClC;QAGD,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE;YAC5D,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,MAAM,CAA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