-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmongoDbFilter.ts
202 lines (162 loc) · 7.69 KB
/
mongoDbFilter.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
import { isType, GraphQLObjectType, isLeafType, GraphQLLeafType } from 'graphql';
import { getTypeFields, getInnerType, isListField, addPrefixToProperties, GraphQLFieldsType } from './common';
import { warn, logOnError } from './logger';
type GraphQLLeafOperators = 'EQ' | 'GT' | 'GTE' | 'IN' | 'ALL' | 'LT' | 'LTE' | 'NE' | 'NEQ' | 'NEQ' | 'NIN' | 'REGEX' | 'OPTIONS' | 'NOT';
type MongoDbLeafOperators = '$eq' | '$gt' | '$gte' | '$in' | '$all' | '$lt' | '$lte' | '$ne' | '$neq' | '$nin' | '$regex' | '$options' | '$not';
type GraphQLRootOperators = 'OR' | 'AND' | 'NOR';
export type MongoDbRootOperators = '$or' | '$and' | '$nor';
type GraphQLOperators = GraphQLLeafOperators | GraphQLRootOperators;
type MongoDbOperators = MongoDbLeafOperators | MongoDbRootOperators;
export type GraphQLFilter = {
[key: string]: GraphQLFilter[] | GraphQLObjectFilter | GraphQLLeafFilter;
OR?: GraphQLFilter[];
AND?: GraphQLFilter[];
NOR?: GraphQLFilter[];
};
type GraphQLObjectFilter = {
[key: string]: GraphQLObjectFilter | GraphQLLeafFilter | ('exists' | 'not_exists');
opr?: 'exists' | 'not_exists';
};
type GraphQLLeafFilterInner = {
[key in GraphQLLeafOperators]?: any | any[];
};
type GraphQLLeafFilter = GraphQLLeafFilterInner & {
NOT?: GraphQLLeafFilterInner;
opr?: MongoDbLeafOperators;
value?: any;
values?: any[];
};
function isLeafTypeTypeGuard(fieldFilter: GraphQLObjectFilter | GraphQLLeafFilter, fieldType: GraphQLObjectType | GraphQLLeafType): fieldFilter is GraphQLLeafFilter {
return isLeafType(fieldType);
}
export type MongoDbFilter = {
[key: string]: MongoDbLeafFilter | { $elemMatch: MongoDbObjectFilter } | { $exists?: boolean } | MongoDbFilter[];
$or?: MongoDbFilter[];
$and?: MongoDbFilter[];
$nor?: MongoDbFilter[];
};
type MongoDbObjectFilter = {
[key: string]: MongoDbLeafFilter | { $elemMatch: MongoDbObjectFilter } | { $exists?: boolean } | boolean;
};
type MongoDbLeafFilter = {
[key in MongoDbLeafOperators]?: any | any[];
} & {
$not?: MongoDbLeafFilter | RegExp
};
const operatorsMongoDbKeys: { [key in GraphQLOperators]: MongoDbOperators } = {
EQ: '$eq',
GT: '$gt',
GTE: '$gte',
IN: '$in',
ALL: '$all',
LT: '$lt',
LTE: '$lte',
NE: '$ne',
NEQ: '$ne', // DEPRECATED
NIN: '$nin',
REGEX: '$regex',
OPTIONS: '$options',
NOT: '$not',
OR: '$or',
AND: '$and',
NOR: '$nor',
};
const rootOperators: GraphQLRootOperators[] = ['OR', 'AND', 'NOR']
export const getMongoDbFilter = logOnError((graphQLType: GraphQLFieldsType, graphQLFilter: GraphQLFilter = {}): MongoDbFilter => {
if (!isType(graphQLType)) throw 'First arg of getMongoDbFilter must be the base graphqlType to be parsed'
const filter = parseMongoDbFilter(graphQLType, graphQLFilter as GraphQLObjectFilter, ...rootOperators) as MongoDbFilter;
rootOperators
.map(key => ({ key, args: graphQLFilter[key] as GraphQLFilter[] }))
.filter(({ args }) => !!args && args.length > 0)
.forEach(({ key, args }) => filter[operatorsMongoDbKeys[key] as MongoDbRootOperators] =
args.map(_ => getMongoDbFilter(graphQLType, _)));
return filter;
});
function parseMongoDbFilter(type: GraphQLFieldsType, graphQLFilter: GraphQLObjectFilter, ...excludedFields: string[]): MongoDbObjectFilter {
const typeFields = getTypeFields(type)();
return Object.keys(graphQLFilter)
.filter(key => !excludedFields.includes(key))
.reduce((agg: MongoDbObjectFilter, key) => {
if (key === 'opr') {
return { ...agg, ...parseMongoExistsFilter(graphQLFilter[key]) } as MongoDbObjectFilter
}
const fieldFilter = graphQLFilter[key] as GraphQLObjectFilter | GraphQLLeafFilter;
const fieldType = getInnerType(typeFields[key].type) as GraphQLLeafType | GraphQLObjectType;
if (isLeafTypeTypeGuard(fieldFilter, fieldType)) {
const leafFilter = parseMongoDbLeafFilter(fieldFilter);
if (Object.keys(leafFilter).length > 0) {
return { ...agg, [key]: leafFilter };
}
} else {
const nestedFilter = parseMongoDbFilter(fieldType, fieldFilter, ...excludedFields);
if (Object.keys(nestedFilter).length > 0) {
if (isListField(typeFields[key].type)) {
return { ...agg, [key]: { '$elemMatch': nestedFilter } };
} else {
const { $exists, ...nestedObjectFilter } = nestedFilter;
const exists = typeof $exists === 'boolean' ? { [key]: { $exists } } : { };
return { ...agg, ...addPrefixToProperties(nestedObjectFilter, `${key}.`), ...exists};
}
}
}
return agg;
}, {} as MongoDbObjectFilter);
}
function parseMongoExistsFilter(exists: 'exists' | 'not_exists'): { $exists: boolean } {
return { $exists: exists === 'exists' ? true : false };
}
function parseMongoDbLeafFilter(graphQLLeafFilter: GraphQLLeafFilter, not: boolean = false): MongoDbLeafFilter {
const mongoDbScalarFilter: MongoDbLeafFilter = {};
Object.keys(graphQLLeafFilter)
.filter((key: keyof GraphQLLeafFilter) => key !== 'value' && key !== 'values' && key !== 'OPTIONS')
.forEach((key: keyof GraphQLLeafFilter) => {
////////////// DEPRECATED /////////////////////////////////////////
if (key === 'opr') {
Object.assign(mongoDbScalarFilter, parseMongoDbScalarFilterOpr(graphQLLeafFilter[key], graphQLLeafFilter));
return;
}
///////////////////////////////////////////////////////////////////
if (key === 'NOT') {
mongoDbScalarFilter[operatorsMongoDbKeys[key]] = parseMongoDbLeafNoteFilter(graphQLLeafFilter[key]);
return;
}
const element = graphQLLeafFilter[key];
mongoDbScalarFilter[operatorsMongoDbKeys[key]] = element;
if (key === 'REGEX') {
const options = graphQLLeafFilter['OPTIONS'];
if (not) {
mongoDbScalarFilter[operatorsMongoDbKeys[key]] = new RegExp(element, `g${options || ''}`);
} else if (!!options) {
mongoDbScalarFilter[operatorsMongoDbKeys['OPTIONS']] = graphQLLeafFilter['OPTIONS'];
}
}
});
return mongoDbScalarFilter;
}
function parseMongoDbLeafNoteFilter(graphQLLeafFilterInner: GraphQLLeafFilterInner): MongoDbLeafFilter | RegExp {
if (!graphQLLeafFilterInner.REGEX) {
return parseMongoDbLeafFilter(graphQLLeafFilterInner, true);
}
if (Object.keys(graphQLLeafFilterInner).length > (!!graphQLLeafFilterInner.OPTIONS ? 2 : 1)) {
throw "NOT operator can contain either REGEX [and OPTIONS], or every other operator."
}
return new RegExp(graphQLLeafFilterInner.REGEX, `g${graphQLLeafFilterInner.OPTIONS || ''}`);
}
////////////// DEPRECATED ///////////////////////////////////////////
let dperecatedMessageSent = false;
function parseMongoDbScalarFilterOpr(opr: MongoDbLeafOperators, graphQLFilter: GraphQLLeafFilter): {} {
if (!dperecatedMessageSent) {
warn('scalar filter "opr" field is deprecated, please switch to the operator fields');
dperecatedMessageSent = true;
}
if (["$in", "$nin", "$all"].includes(opr)) {
if (graphQLFilter['values']) {
return { [opr]: graphQLFilter['values'] };
}
}
else if (graphQLFilter['value'] !== undefined) {
return { [opr]: graphQLFilter['value'] };
}
return {};
}
/////////////////////////////////////////////////////////////////////