Skip to content

fix: support multiple filters in Supabase realtime liveProvider #6827

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

Open
wants to merge 5 commits into
base: next
Choose a base branch
from
Open
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
35 changes: 20 additions & 15 deletions packages/supabase/src/liveProvider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,33 @@ export const liveProvider = (
}
};

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

return filters
.map((filter: CrudFilter): string | undefined => {
if ("field" in filter) {
return `${filter.field}=${mapOperator(filter.operator)}.${
filter.value
}`;
}
return;
})
.filter(Boolean)
.join(",");
if (!filters || filters.length === 0) return;

if (filters.length > 1) {
console.warn(
"[liveProvider] Supabase Realtime does not support multiple filters. Using the first filter only."
);
}

const firstFilter = filters.find((f) => "field" in f);
if (!firstFilter) return;

return `${firstFilter.field}=${mapOperator(firstFilter.operator)}.${firstFilter.value}`;
};

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