You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
thrownewError(`Field '${(filtersasIAdminForthSingleFilter).field}' not found in resource '${resource.resourceId}'. ${similar ? `Did you mean '${similar}'?` : ''}`);
Virtual column can also be used as a shorthand for a complex filtering.
86
+
Lets say we want to divide apartments into two types: "base" ones and "luxury" and then allow admins to filter apartments by this category. Condition for being a "luxury" apartment is either having more then 80 sq.m area or costing more then 100k.
87
+
One way to do it is to actually add a real column to a table and then fill it every time new apartment is added. A more simple way is to add a virtual column and then use `list.beforeDatasourceRequest` hook to replace filtering on this column with desired one.
88
+
For this purpose following changes will be required for apartments config:
89
+
90
+
```ts title='./resources/apartments.ts'
91
+
...
92
+
resourceId:'aparts',
93
+
...
94
+
hooks: {
95
+
...
96
+
list: {
97
+
beforeDatasourceRequest:async ({ query }: { query: any }) => {
showIn: { all:false, filter:true }, // hide it from display everywhere, except filter page
124
+
enum: [
125
+
{
126
+
value:'base',
127
+
label:'Base',
128
+
},
129
+
{
130
+
value:'luxury',
131
+
label:'Luxury'
132
+
},
133
+
],
134
+
filterOptions: {
135
+
multiselect:false, // allow to only select one category when filtering
136
+
},
137
+
},
138
+
...
139
+
]
140
+
```
141
+
This way, when admin selects, for example, "Luxury" option for "Apartment Type" filter, it will be replace with a more complex "or" filter.
142
+
143
+
### Custom SQL queries with `insecureRawSQL`
144
+
145
+
Rarely the sec of Filters supported by AdminForth is not enough for your needs.
146
+
In this case you can use `insecureRawSQL` to write your own part of where clause.
147
+
148
+
However the vital concern that the SQL passed to DB as is, so if you substitute any user inputs it will not be escaped and can lead to SQL injection. To miticate the issue we recommend using `sqlstring` package which will escape the inputs for you.
149
+
150
+
```bash
151
+
npm i sqlstring
152
+
```
153
+
154
+
Then you can use it like this:
155
+
156
+
```ts title='./resources/apartments.ts'
157
+
importsqlstringfrom'sqlstring';
158
+
...
159
+
160
+
beforeDatasourceRequest:async ({ query }: { query: any }) => {
161
+
query.filters=query.filters.map((filter:any) => {
162
+
// replace apartment_type filter with complex one
163
+
if (filter.field==='some_json_b_field') {
164
+
return {
165
+
// check if some_json_b_field->'$.some_field' is equal to filter.value
0 commit comments