-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
OmkarBansod02
wants to merge
12
commits into
refinedev:next
Choose a base branch
from
OmkarBansod02:fix/supabase-realtime-multiple-filters
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9a0a393
fix: handle multiple filters in Supabase liveProvider with warnings a…
OmkarBansod02 d9d7edd
chore: add changeset for recent changes
OmkarBansod02 6600f6b
chore: update changeset
OmkarBansod02 89e14aa
chore: update warning message
OmkarBansod02 2dd33b1
chore: update changeset
OmkarBansod02 2cf3427
fix(supabase): implement warn-once to reduce duplicate warnings for m…
OmkarBansod02 4d3291a
fix(supabase): update supported operators to use ne instead of neq
OmkarBansod02 f9e968e
fix(supabase): prevent unnecessary warnings for multiple filters when…
OmkarBansod02 fd447ca
fix(supabase): add warning for unsupported filters
OmkarBansod02 19d371f
fix(supabase): remove unnecessary else clause and handle unsupported …
OmkarBansod02 70a8594
Merge branch 'next' into fix/supabase-realtime-multiple-filters
alicanerdurmaz 8924e3b
Merge branch 'next' into fix/supabase-realtime-multiple-filters
BatuhanW File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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", | ||||||||||||||||||||||
]; | ||||||||||||||||||||||
|
||||||||||||||||||||||
export const liveProvider = ( | ||||||||||||||||||||||
supabaseClient: SupabaseClient<any, any, any>, | ||||||||||||||||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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(","); | ||||||||||||||||||||||
|
@@ -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); | ||||||||||||||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.