-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.ts
207 lines (189 loc) · 5.49 KB
/
match.ts
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
import { DeepPartial, Prettify } from "./utils.js";
import BetterGoose from "./index.js";
import { XOR_6 } from "./utils.js";
export type MatchStage<Coll> = Record<"$match", MatchStageInput<Coll>>;
type objectInsideArray<Prefix extends string, K, U> =
| FlattenWithValues<
U,
`${Prefix}${string & K}.$.` | `${Prefix}${string & K}.${number}.`
>
| FlattenWithValues<U, `${Prefix}${string & K}.`>
| {
[Q in `${Prefix}${string & K}`]?: XOR_6<
arraySize,
XOR_6<
elemMatch<U>,
XOR_6<ComparisonOperators<U>, ComparisonOperators<U[]>>
>
>;
};
// type primitivesInsideArray<Prefix extends string, K, U> = {
// [P in `${Prefix}${string & K}`]: U extends string
// ? string extends U
// ?
// | string
// | RegExp
// | ComparisonOperators<U>
// | ComparisonOperators<U[]>
// | elemMatch<U>
// | arraySize
// | U[]
// : U
// : U[] | ComparisonOperators<U> | arraySize;
// };
type primitivesInsideArray<Prefix extends string, K, U> = {
[P in `${Prefix}${string & K}`]: U extends string
? string extends U
? XOR_6<
string | RegExp,
XOR_6<
arraySize,
XOR_6<
elemMatch<U>,
XOR_6<
elemMatch<U[]>,
XOR_6<ComparisonOperators<U>, ComparisonOperators<U[]>>
>
>
>
>
: U
: U[] | ComparisonOperators<U> | arraySize;
};
type justObjects<Prefix extends string, K, T> =
| FlattenWithValues<T, `${Prefix}${string & K}.`>
| { [H in `${Prefix}${string & K}`]: T };
export type FlattenWithValues<T, Prefix extends string = ""> = {
[K in keyof T]: T[K] extends (infer U)[]
? U extends object
? /**
* This part is for array of objects
*/
objectInsideArray<Prefix, K, U>
: /**
* This part is for array of primitives
*/
{
[P in `${Prefix}${string & K}`]: U extends string
? string extends U
? XOR_6<
string | RegExp,
XOR_6<
arraySize,
XOR_6<
elemMatch<U>,
XOR_6<
elemMatch<U[]>,
XOR_6<ComparisonOperators<U>, ComparisonOperators<U[]>>
>
>
>
>
: U
: U[] | ComparisonOperators<U> | arraySize;
}
: T[K] extends object
? /**
* This part is for objects
*/
justObjects<Prefix, K, T[K]>
: {
[P in `${Prefix}${string & K}`]: T[K] extends string
? string extends T[K]
? // this is for non enum strings
string | RegExp | ComparisonOperators<string | RegExp>
: T[K] | ComparisonOperators<T[K]>
: T[K] | ComparisonOperators<T[K]>;
};
}[keyof T];
type FlattenWithValues2<T, Prefix extends string = ""> = T extends object
? {
[K in keyof T]: T[K] extends (infer U)[]
? U extends object
? objectInsideArray<Prefix, K, U>
: {
[P in `${Prefix}${string & K}`]: U extends string
? string extends U
? XOR_6<
string | RegExp,
XOR_6<
arraySize,
XOR_6<
elemMatch<U>,
XOR_6<
elemMatch<U[]>,
XOR_6<
ComparisonOperators<U>,
ComparisonOperators<U[]>
>
>
>
>
>
: U
: U[] | ComparisonOperators<U> | arraySize;
}
: T[K] extends object
? justObjects<Prefix, K, T[K]>
: {
[P in `${Prefix}${string & K}`]: T[K] extends string
? string extends T[K]
? string | RegExp | ComparisonOperators<string | RegExp>
: T[K] | ComparisonOperators<T[K]>
: T[K] | ComparisonOperators<T[K]>;
};
}[keyof T]
: T | ComparisonOperators<T>;
// LHS for match stage is flatten keys
// RHS for match stage
/**
* #1. Equality matches
* Simple equality: { field: value }
* Multiple field equality: { field1: value1, field2: value2 }
*/
/**
* #2. Comparison Operators:
* $eq: Equal to
* $ne: Not equal to
* $gt: Greater than
* $gte: Greater than or equal to
* $lt: Less than
* $lte: Less than or equal to
* $in: In an array
* $nin: Not in an array
*/
type ComparisonOperators<T> = {
$eq?: T;
$ne?: T;
$gt?: T;
$gte?: T;
$lt?: T;
$lte?: T;
$in?: T[];
$nin?: T[];
};
/**
* #3. Logical query operators
* $and
* $or
* $not
* $nor
*/
type LogicalOperators<Coll> = {
$and?: MatchStageStructure<Coll>[];
$or?: MatchStageStructure<Coll>[];
$not?: MatchStageStructure<Coll>;
$nor?: MatchStageStructure<Coll>[];
};
/**
* $size operator is used when result is of type array
*/
type arraySize = Record<"$size", number>;
/**
* $elemMatch operator can only used on any array fields
*/
type elemMatch<T> = { $elemMatch: MatchStageInput<T> };
type MatchStageStructure<Coll> =
| FlattenWithValues2<Coll>
| LogicalOperators<Coll>;
export type MatchStageInput<Coll> = Prettify<MatchStageStructure<Coll>>;