-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
generateFilters.ts
174 lines (156 loc) · 3.78 KB
/
generateFilters.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
import type {
ConditionalFilter,
CrudOperators,
LogicalFilter,
} from "@refinedev/core";
import camelcase from "camelcase";
import setWith from "lodash/setWith";
import type { NamingConvention } from "src/dataProvider";
export type HasuraFilterCondition =
| "_and"
| "_not"
| "_or"
| "_eq"
| "_gt"
| "_gte"
| "_lt"
| "_lte"
| "_neq"
| "_in"
| "_nin"
| "_like"
| "_nlike"
| "_ilike"
| "_nilike"
| "_is_null"
| "_similar"
| "_nsimilar"
| "_regex"
| "_iregex";
export type HasuraCrudOperators = CrudOperators | "not";
export type HasuraLogicalFilter = Omit<LogicalFilter, "operator"> & {
operator: Exclude<HasuraCrudOperators, "or" | "and" | "not">;
};
export type HasuraConditionalFilter = Omit<
ConditionalFilter,
"operator" | "value"
> & {
operator: Extract<HasuraCrudOperators, "or" | "and" | "not">;
value: HasuraCrudFilters;
};
export type HasuraCrudFilter = HasuraLogicalFilter | HasuraConditionalFilter;
export type HasuraCrudFilters = HasuraCrudFilter[];
const hasuraFilters: Partial<
Record<HasuraCrudOperators, HasuraFilterCondition | undefined>
> = {
eq: "_eq",
ne: "_neq",
lt: "_lt",
gt: "_gt",
lte: "_lte",
gte: "_gte",
in: "_in",
nin: "_nin",
contains: "_ilike",
ncontains: "_nilike",
containss: "_like",
ncontainss: "_nlike",
null: "_is_null",
or: "_or",
and: "_and",
not: "_not",
nnull: "_is_null",
startswith: "_iregex",
nstartswith: "_iregex",
endswith: "_iregex",
nendswith: "_iregex",
startswiths: "_similar",
nstartswiths: "_nsimilar",
endswiths: "_similar",
nendswiths: "_nsimilar",
};
export const handleFilterValue = (
operator: HasuraCrudOperators,
value: any,
) => {
switch (operator) {
case "startswiths":
case "nstartswiths":
return `${value}%`;
case "endswiths":
case "nendswiths":
return `%${value}`;
case "startswith":
return `^${value}`;
case "nstartswith":
return `^(?!${value})`;
case "endswith":
return `${value}$`;
case "nendswith":
return `(?<!${value})$`;
case "nnull":
return false;
case "contains":
case "containss":
case "ncontains":
case "ncontainss":
return `%${value}%`;
default:
return value;
}
};
const convertHasuraOperatorToGraphqlDefaultNaming = (
hasuraOperator?: HasuraFilterCondition,
) => {
return hasuraOperator ? `_${camelcase(hasuraOperator)}` : undefined;
};
export const generateNestedFilterQuery = (
filter: HasuraCrudFilter,
namingConvention: NamingConvention = "hasura-default",
): any => {
const { operator } = filter;
if (
operator !== "or" &&
operator !== "and" &&
operator !== "not" &&
"field" in filter
) {
const { field, value } = filter;
const defaultNamingConvention = namingConvention === "hasura-default";
const hasuraOperator = defaultNamingConvention
? hasuraFilters[filter.operator]
: convertHasuraOperatorToGraphqlDefaultNaming(
hasuraFilters[filter.operator],
);
if (!hasuraOperator) {
throw new Error(`Operator ${operator} is not supported`);
}
const fieldsArray = field.split(".");
const fieldsWithOperator = [...fieldsArray, hasuraOperator];
const filteredValue = handleFilterValue(operator, value);
return {
...setWith({}, fieldsWithOperator, filteredValue, Object),
};
}
return {
[`_${operator}`]: filter.value.map((f) =>
generateNestedFilterQuery(f, namingConvention),
),
};
};
export const generateFilters: any = (
filters?: HasuraCrudFilters,
namingConvention: NamingConvention = "hasura-default",
) => {
if (!filters) {
return undefined;
}
const nestedQuery = generateNestedFilterQuery(
{
operator: "and",
value: filters,
},
namingConvention,
);
return nestedQuery;
};