Add unique constraint support for tables and automatic upserts in Supabase connector#198
Add unique constraint support for tables and automatic upserts in Supabase connector#198fredpedersen wants to merge 1 commit intopowersync-ja:mainfrom
Conversation
…abase connector - Add `unique` property to Index class to define unique constraints - Update Table class with methods to retrieve unique columns and indexes - Create UpsertOptions data class for configuring upsert behavior - Enhance SupabaseConnector to automatically handle upserts based on schema - Detects unique constraints from table schema - Sets appropriate onConflict columns for Supabase upsert operations - Falls back to primary key conflict resolution when no unique indexes exist - Add extension functions and examples for working with unique constraints This allows developers to define unique constraints in their PowerSync schema and have the Supabase connector automatically handle upserts with proper conflict resolution without manual configuration.
rkistner
left a comment
There was a problem hiding this comment.
I think the toSqlDefinition function is isn't used anymore (not actually sure if it ever was), so this change won't have any effect. The actual indexes are created in the sqlite extension.
Apart from this, client-side unique constraints are problematic with the current sync implementation. If for example you remove a row, then create another one with the same unique columns, the sync process could apply the changes in a different order and end up violating the constraint, which would completely block the sync process.
We are planning some functionality to support syncing to custom tables, in which case you'd have more control over indexes and handling conflicts. You'd still have to be careful around the above behavior.
I do think we need to make the SupabaseConnector more customizable though, to allow having this type of functionality when uploading.
|
Thanks, good to know. My use case is I need to sync data from a Bluetooth
device, store it locally and have a copy on supabase too. The issue is the
device will only store recent data, local only the last month or so and
supabase will handle long term storage. I think I can use power sync rules
for the local and supabase storage, the issue is just if the device data
gets synced again after a fresh install of the app, and before the remote
dB gets synced to local. In those cases I need an update or replace to just
update the remote with the newly generated ssids for the local data.
I can't see any situation where I'd need to delete data, so hopefully that
will be ok.
I'm not sure if there's a cleaner way of doing this?
…On Mon, 2 Jun 2025, 10:46 Ralf Kistner, ***@***.***> wrote:
***@***.**** commented on this pull request.
I think the toSqlDefinition function is isn't used anymore (not actually
sure if it ever was), so this change won't have any effect. The actual
indexes are created in the sqlite extension
<https://github.com/powersync-ja/powersync-sqlite-core/>.
Apart from this, client-side unique constraints are problematic with the
current sync implementation. If for example you remove a row, then create
another one with the same unique columns, the sync process could apply the
changes in a different order and end up violating the constraint, which
would completely block the sync process.
We are planning some functionality to support syncing to custom tables, in
which case you'd have more control over indexes and handling conflicts.
You'd still have to be careful around the above behavior.
I do think we need to make the SupabaseConnector more customizable though,
to allow having this type of functionality when uploading.
—
Reply to this email directly, view it on GitHub
<#198 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AB6COEIIU4WJP4QLGFIYT733BQMQXAVCNFSM6AAAAAB6IPJXN6VHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDQOBXGU4DSNBYGY>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
uniqueproperty to Index class to define unique constraintsThis allows developers to define unique constraints in their PowerSync schema and have the Supabase connector automatically handle upserts with proper conflict resolution without manual configuration.
#196