| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220 |
6x
16x
16x
16x
9x
3x
1x
1x
1x
1x
1x
1x
1x
1x
5x
1x
1x
1x
1x
1x
2x
2x
2x
1x
1x
7x
16x
4x
6x
2x
2x
4x
4x
4x
| // import { UpdateOperatorList } from "./constant";
export class Command {
public logicParam: object = {};
private placeholder = "{{{AAA}}}";
// private baseOperator = ['$eq', '$ne', '$gt', '$gte', '$lt', '$lte', '$in', '$nin'];
constructor(logicParam?: object) {
if (logicParam) {
this.logicParam = logicParam;
}
}
/**
* Query and Projection Operators
* https://docs.mongodb.com/manual/reference/operator/query/
* @param target
*/
eq(target: any) {
return new Command(this.baseOperate("$eq", target));
}
neq(target: any) {
return new Command(this.baseOperate("$ne", target));
}
gt(target: any) {
return new Command(this.baseOperate("$gt", target));
}
gte(target: any) {
return new Command(this.baseOperate("$gte", target));
}
lt(target: any) {
return new Command(this.baseOperate("$lt", target));
}
lte(target: any) {
return new Command(this.baseOperate("$lte", target));
}
in(target: any[]) {
return new Command(this.baseOperate("$in", target));
}
nin(target: any[]) {
return new Command(this.baseOperate("$nin", target));
}
regex(target: any) {
var result = new Command(this.baseOperate("$regex", target.regex))
result.logicParam[this.placeholder]['$options'] = target.options
return result
}
/**
* Update Operators
* https://docs.mongodb.com/manual/reference/operator/update/
* @param target
*/
mul(target: number) {
return new Command({ $mul: { [this.placeholder]: target } });
}
remove() {
return new Command({ $unset: { [this.placeholder]: "" } });
}
inc(target: number) {
return new Command({ $inc: { [this.placeholder]: target } });
}
set(target: any) {
return new Command({ $set: { [this.placeholder]: target } });
}
push(target: any) {
let value = target;
Eif (Array.isArray(target)) {
value = { $each: target };
}
return new Command({ $push: { [this.placeholder]: value } });
}
pop() {
return new Command({ $pop: { [this.placeholder]: 1 } });
}
unshift(target: any) {
let value = { $each: [target], $position: 0 };
if (Array.isArray(target)) {
value = { $each: target, $position: 0 };
}
return new Command({
$push: { [this.placeholder]: value }
});
}
shift() {
return new Command({ $pop: { [this.placeholder]: -1 } });
}
private baseOperate(operator: string, target: any): object {
return {
[this.placeholder]: { [operator]: target }
};
}
and(...targets: any[]) {
Iif (targets.length === 1 && Array.isArray(targets[0])) {
targets = targets[0]
}
return new Command(this.connectOperate("$and", targets));
}
or(...targets: any[]) {
// console.log(targets)
if (targets.length === 1 && Array.isArray(targets[0])) {
targets = targets[0]
}
return new Command(this.connectOperate("$or", targets));
}
// not(target: any) {
// return new Command(this.connectOperate("$not", target));
// }
private connectOperate(operator: string, targets: any[]) {
// console.log(targets);
let logicParams: object[] = [];
Iif (Object.keys(this.logicParam).length > 0) {
logicParams.push(this.logicParam);
}
for (let target of targets) {
Eif (target instanceof Command) {
// console.log(target)
Iif (Object.keys(target.logicParam).length === 0) {
continue;
}
logicParams.push(target.logicParam);
} else {
let tmp = {}
this.concatKeys(target, '', tmp);
// console.log(tmp)
let tmp1 = {}
for (let keys in tmp) {
let value = tmp[keys]
if (value instanceof Command) {
// const logicParam = value.logicParam
// logicParams.push(value.parse(keys))
Object.assign(tmp1, value.parse(keys))
// if (logicParam.hasOwnProperty('$or') || logicParam.hasOwnProperty('$and')) {
// logicParams.push(value.parse(keys))
// } else {
// logicParams.push({
// [keys]: logicParam
// });
// }
} else {
Object.assign(tmp1, {
[keys]: value
})
// logicParams.push({
// [keys]: value
// });
}
}
logicParams.push(tmp1)
}
}
this.logicParam = [];
// console.log(logicParams);
return {
[operator]: logicParams
};
}
parse(key?: string): object {
// if (UpdateOperatorList.indexOf(Object.keys(this.logicParam)[0]) > -1) {
return JSON.parse(
JSON.stringify(this.logicParam).replace(/{{{AAA}}}/g, key)
);
// }
}
public toString = (): object => {
return this.logicParam[0];
};
/**
* ??????
* @param obj
*/
public concatKeys(obj: object, key: string, res: object) {
let keys, value
for (let k in obj) {
if (typeof obj[k] === 'object' &&
obj[k] instanceof Command === false) {
keys = key ? key + '.' + k : k
this.concatKeys(obj[k], keys, res)
} else {
keys = key ? key + '.' + k : k
value = obj[k]
// console.log(res, keys, value)
Object.assign(res, { [keys]: value })
// console.log(keys, val)
}
}
}
}
|