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

fix: improve Supabase liveProvider filter handling #6573

Open
wants to merge 12 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/shiny-panthers-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@refinedev/supabase": patch
---

fix: handle multiple filters in Supabase liveProvider.
This update addresses the handling of multiple filters in the Supabase liveProvider. It ensures only the first filter is applied and introduces a configurable `meta.realtimeFilter` option for custom filter behavior. A warning is logged when multiple filters are detected.

[Resolves #6360](https://github.com/refinedev/refine/issues/6360)
49 changes: 42 additions & 7 deletions packages/supabase/src/liveProvider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ import type {
} from "@supabase/supabase-js";
import { liveTypes, supabaseTypes } from "../types";
import { mapOperator } from "../utils";
import warnOnce from "warn-once";

const supportedOperators = [
"eq",
"ne",
"nin",
"ina",
"nina",
"contains",
"ncontains",
"containss",
"ncontainss",
"between",
"nbetween",
"null",
"nnull",
];
Comment on lines +11 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is the correct list of supported filters. Please check out Supabase's docs here Available Filters and our filter map implementation to which of these are valid.


export const liveProvider = (
supabaseClient: SupabaseClient<any, any, any>,
Expand Down Expand Up @@ -49,19 +66,37 @@ export const liveProvider = (
}
};

const mapFilter = (filters?: CrudFilters): string | undefined => {
const mapFilter = (
filters?: CrudFilters,
meta?: any,
): string | undefined => {
if (!filters || filters?.length === 0) {
return;
}

return filters
if (filters.length > 1 && !meta?.realtimeFilter) {
warnOnce(
true,
`Warning: Multiple filters detected for resource "${resource}". Supabase Realtime currently supports only a single filter. The first filter will be applied. To customize this behavior, use the 'meta.realtimeFilter' property.`,
);
}
Comment on lines +77 to +82
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (filters.length > 1 && !meta?.realtimeFilter) {
warnOnce(
true,
`Warning: Multiple filters detected for resource "${resource}". Supabase Realtime currently supports only a single filter. The first filter will be applied. To customize this behavior, use the 'meta.realtimeFilter' property.`,
);
}
warnOnce(
filters.length > 1 && !meta?.realtimeFilter,
`Warning: Multiple filters detected for resource "${resource}". Supabase Realtime currently supports only a single filter. The first filter will be applied. To customize this behavior, use the 'meta.realtimeFilter' property.`,
);

I think this looks more correct use for warnOnce 🤔


const effectiveFilter = meta?.realtimeFilter
? [meta.realtimeFilter]
: [filters[0]];

return effectiveFilter
.map((filter: CrudFilter): string | undefined => {
if ("field" in filter) {
return `${filter.field}=${mapOperator(filter.operator)}.${
filter.value
}`;
if (supportedOperators.includes(filter.operator)) {
return `${filter.field}=${mapOperator(filter.operator)}.${
filter.value
}`;
}
warnOnce(true, `Unsupported filter operator: ${filter.operator}`);
return undefined;
}
return;
return undefined;
})
.filter(Boolean)
.join(",");
Expand All @@ -70,7 +105,7 @@ export const liveProvider = (
const events = types
.map((x) => supabaseTypes[x])
.sort((a, b) => a.localeCompare(b));
const filter = mapFilter(params?.filters);
const filter = mapFilter(params?.filters, meta);
const ch = `${channel}:${events.join("|")}${filter ? `:${filter}` : ""}`;

let client = supabaseClient.channel(ch);
Expand Down
Loading