[RFC] Query builder immutability and safe branching semantics across Supabase SDKs #50049
Replies: 3 comments 1 reply
|
I think the strongest version of this proposal is to make the contract explicit before trying to make every SDK behave identically. The main question is whether a builder is intended to be a reusable query description or a single-use mutable request. Both models can work, but silently sharing a mutable builder across tasks is the dangerous case. A practical path could be:
I would also avoid promising identical method behavior across all languages until the underlying request-builder types have the same lifecycle and ownership model. An explicit clone API plus clear documentation would solve the immediate race/cross-contamination problem while leaving room for copy-on-write in SDKs where it fits naturally. |
|
Thanks for the thoughtful and practical breakdown, @edwardsong08! This phased roadmap makes total sense:
We can start by prototyping the |
|
Btw, in js sdk builders are mutable in some places as well. For example, methods like It might be useful to include a simple cross-SDK test case where two queries are derived from the same base builder, and verify what the expected branching semantics should be. If a |
Uh oh!
There was an error while loading. Please reload this page.
Motivation & Background
In data-layer development, branching a base query builder instance is a standard pattern for pagination, dynamic filtering, and conditional querying:
However, across non-JS Supabase SDKs (such as
supabase-pyandsupabase-swift), filter and modifier methods mutate the internal builder instance in place instead of returning a copy-on-write clone.When branching as above, subsequent calls mutate
base_query, causing race conditions, corrupted query parameters, and unexpected results across asynchronous tasks.Proposed Design
eq,in_,order,range,limit) should return a new shallow clone of the builder with isolated query parameters and headers..clone()Helper: Provide an explicit.clone()method on PostgREST query builders for developers who wish to clone instances ahead of mutation.supabase-js,supabase-py,supabase-flutter,supabase-swift, andsupabase-kt.Benefits
Would love to hear maintainers and community thoughts on adopting standardized immutability across the SDKs!
All reactions