In this tutorial we will show you how to map a local UUID to a remote sequential (auto-incrementing) ID.
Schema
lists
and todos
tablesCreate SQL triggers
Update Sync Rules
Update client to use UUIDs.
AppSchema.ts
fts_setup.ts
SupabaseConnector.ts
lists.tsx
page.tsx
SearchBarWidget.tsx
TodoListsWidget.tsx
lists
table by adding a uuid
column, which will be the secondary ID, andtodos
table by adding a uuid
column, and a list_uuid
foreign key column which references the uuid
column in the lists
table.list_id
and list_uuid
in the todos
table, with the id
and uuid
columns in the lists
table.
We can achieve this by creating SQL triggers.
list_id
and list_uuid
in the todos
table by ensuring that they remain synchronized with the id
and uuid
columns in the lists
table;
even if changes are made to either field.
We will create the following two triggers that cover either scenario of updating the list_id
or list_uuid
in the todos
table:
update_integer_id
, andupdate_uuid_column
Trigger 1: update_integer_id
update_integer_id
trigger ensures that whenever a list_uuid
value is inserted or updated in the todos
table,
the corresponding list_id
is fetched from the lists
table and updated automatically. It also validates that the list_uuid
exists in the lists
table; otherwise, it raises an exception.Trigger 2: update_uuid_column
update_uuid_column
trigger ensures that whenever a list_id
value is inserted or updated in the todos table, the corresponding list_uuid
is fetched from the
lists
table and updated automatically. It also validates that the list_id
exists in the lists
table.parameters
and data
queries to use the new uuid
columns.
The parameters
query is updated by removing the list_id
alias (this is removed to avoid any confusion between the list_id
column in the todos
table), and
the data
query is updated to use the uuid
column as the id
column for the lists
and todos
tables. We also explicitly define which columns to select, as list_id
is no longer required in the client.
list_id
column in the todos
table.
We start by updating AppSchema.ts
and replacing list_id
with list_uuid
in the todos
table.
uploadData
function in SupabaseConnector.ts
needs to be updated to use the new uuid
column in both tables.
list_id
with list_uuid
.