-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoperators.ts
More file actions
160 lines (134 loc) · 3.49 KB
/
operators.ts
File metadata and controls
160 lines (134 loc) · 3.49 KB
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
/**
*
* AGGREGATE OPERATORS
*
*/
// SUM(DISTINCT? column) AS alias|column
// .select($sum(column, alias, distinct)
const $sum = (column: string, alias?: string, distinct?: boolean) => ({
[alias ?? column]: { $sum: { $expr: column, $distinct: distinct } },
});
// COUNT(DISTINCT? column) AS alias|column
// .select($count(column, alias, distinct)
const $count = (column: string, alias?: string, distinct?: boolean) => ({
[alias ?? column]: { $count: { $expr: column, $distinct: distinct } },
});
/**
*
* COLUMN OPERATORS
*
*/
// column AS alias
const $as = (column: string, alias: string) => ({
[column]: { $as: alias },
});
/**
*
* LOGICAL OPERATORS
*
*/
// condition AND condition AND ...
// .where(
// $and({ column: value }, { column: value })
// )
const $and = (...conditions: any) => ({
$and: conditions,
});
// condition OR condition OR ..
// .where(
// $or({ column: value }, { column: value })
// )
const $or = (...conditions: any) => ({
$or: conditions,
});
/**
*
* COMPARISON OPERATORS
*
*/
// column BETWEEN rangeStart and rangeEnd
const $between = (rangeStart: number, rangeEnd: number) => ({
$between: { $min: rangeStart, $max: rangeEnd },
});
// column IN (...conditionParams)
const inOperator = (...conditionParams: Array<string | number | boolean>) => ({
$in: conditionParams,
});
// column NOT IN (...conditionParams)
const $notIn = (...conditionParams: Array<string | number | boolean>) => ({
$nin: conditionParams,
});
// column = compareValue
const $eq = (compareValue: string | number) => ({ $eq: compareValue });
// column <> compareValue
const $neq = (compareValue: string | number) => ({ $neq: compareValue });
// column > compareValue
const $gt = (compareValue: number) => ({ $gt: compareValue });
// column >= compareValue
const $gte = (compareValue: number) => ({ $gte: compareValue });
// column < compareValue
const $lt = (compareValue: number) => ({ $lt: compareValue });
// column <= compareValue
const $lte = (compareValue: number) => ({ $lte: compareValue });
// column LIKE "%compareValue%"
const $contains = (compareValue: string) => ({ $contains: compareValue });
// column ILIKE "%compareValue%"
const $icontains = (compareValue: string) => ({ $icontains: compareValue });
// column LIKE "%compareValue"
const $startsWith = (compareValue: string) => ({ $startsWith: compareValue });
// column ILIKE "%compareValue"
const $istartsWith = (compareValue: string) => ({ $istartsWith: compareValue });
// column LIKE "compareValue%"
const $endsWith = (compareValue: string) => ({ $endsWith: compareValue });
// column ILIKE "compareValue%"
const $iendsWith = (compareValue: string) => ({ $iendsWith: compareValue });
// column LIKE "compareValue"
const $like = (compareValue: string) => ({ $like: compareValue });
// column ILIKE "compareValue"
const $ilike = (compareValue: string) => ({ $ilike: compareValue });
export {
$sum,
$sum as sum,
$count,
$count as count,
$as,
$as as as,
$and,
$and as and,
$or,
$or as or,
inOperator as $in,
inOperator as inOp,
$notIn,
$notIn as notIn,
$between,
$between as between,
$eq,
$eq as eq,
$neq,
$neq as neq,
$gt,
$gt as gt,
$gte,
$gte as gte,
$lt,
$lt as lt,
$lte,
$lte as lte,
$contains,
$contains as contains,
$icontains,
$icontains as icontains,
$startsWith,
$startsWith as startsWith,
$istartsWith,
$istartsWith as istartsWith,
$endsWith,
$endsWith as endsWith,
$iendsWith,
$iendsWith as iendsWith,
$like,
$like as like,
$ilike,
$ilike as ilike,
};