Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(supabase): filter array column by array (ina and nina operators) #5922

Merged
merged 13 commits into from
May 24, 2024
Merged
7 changes: 7 additions & 0 deletions .changeset/few-ads-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@refinedev/core": minor
"@refinedev/supabase": minor
---

Added ina and nina CrudOperators. Added filtering by these operators to Supabase data provider
#5902
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type CrudOperators =
| "gte"
| "in"
| "nin"
| "ina"
| "nina"
| "contains"
| "ncontains"
| "containss"
Expand Down Expand Up @@ -234,7 +236,7 @@ filter = [
];
```

Here the query will look like:
Here the query will look like:
`"status" == "published" AND ("createdAt" == "2022-01-01" OR "createdAt" == "2022-01-02")`

## Handle filters in a data provider
Expand Down
2 changes: 2 additions & 0 deletions documentation/docs/core/interface-references/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type CrudOperators =
| "gte" // Greater than or equal to
| "in" // Included in an array
| "nin" // Not included in an array
| "ina" // Column contains every element in an array
| "nina" // Column doesn't contain every element in an array
| "contains" // Contains
| "ncontains" // Doesn't contain
| "containss" // Contains, case sensitive
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/contexts/data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ export type CrudOperators =
| "gte"
| "in"
| "nin"
| "ina"
| "nina"
| "contains"
| "ncontains"
| "containss"
Expand Down
17 changes: 16 additions & 1 deletion packages/supabase/src/utils/generateFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ export const generateFilter = (filter: CrudFilter, query: any) => {
return query.neq(filter.field, filter.value);
case "in":
return query.in(filter.field, filter.value);
case "ina":
return query.contains(filter.field, filter.value);
case "nina":
return query.not(
filter.field,
"cs",
`{${filter.value.map((val: any) => `"${val}"`).join(",")}}`,
);

case "gt":
return query.gt(filter.field, filter.value);
case "gte":
Expand All @@ -35,7 +44,13 @@ export const generateFilter = (filter: CrudFilter, query: any) => {
item.operator !== "and" &&
"field" in item
) {
return `${item.field}.${mapOperator(item.operator)}.${item.value}`;
let value = item.value;

if (item.operator === "ina" || item.operator === "nina") {
value = `{${item.value.map((val: any) => `"${val}"`).join(",")}}`;
}

return `${item.field}.${mapOperator(item.operator)}.${value}`;
}
return;
})
Expand Down
4 changes: 4 additions & 0 deletions packages/supabase/src/utils/mapOperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const mapOperator = (operator: CrudOperators) => {
return "is";
case "nnull":
return "not.is";
case "ina":
return "cs";
case "nina":
return "not.cs";
case "between":
case "nbetween":
throw Error(`Operator ${operator} is not supported`);
Expand Down