diff --git a/mint.json b/mint.json index 59b0c272..8a48ebb2 100644 --- a/mint.json +++ b/mint.json @@ -390,6 +390,13 @@ "tutorials/client/performance/overview", "tutorials/client/performance/supabase-connector-performance" ] + }, + { + "group": "Data Management", + "pages": [ + "tutorials/client/data/overview", + "tutorials/client/data/cascading-delete" + ] } ] }, diff --git a/tutorials/client/attachments-and-files/overview.mdx b/tutorials/client/attachments-and-files/overview.mdx index b97bfc23..a820115a 100644 --- a/tutorials/client/attachments-and-files/overview.mdx +++ b/tutorials/client/attachments-and-files/overview.mdx @@ -4,6 +4,6 @@ description: "A collection of tutorials exploring storage strategies." --- - - + + diff --git a/tutorials/client/data/cascading-delete.mdx b/tutorials/client/data/cascading-delete.mdx new file mode 100644 index 00000000..5f8c5aef --- /dev/null +++ b/tutorials/client/data/cascading-delete.mdx @@ -0,0 +1,41 @@ +--- +title: "Cascading Delete" +description: "In this tutorial we will show you how to perform a cascading delete on the client." +keywords: ["data", "cascade", "delete"] +--- + +# Introduction + +Since PowerSync utilizes SQLite views instead of standard tables, SQLite features like constraints, foreign keys, or cascading deletes are not available. +Currently, there is no direct support for cascading deletes on the client. However, you can achieve this by either: +1. Manually deleting all the relevant tables within a **single transaction**, OR + + Every local mutation performed against SQLite via the PowerSync SDK will be returned in `uploadData`. So as long as you are using `.execute()` for the mutation, + the operation will be present in the upload queue. + +2. Implementing triggers (which is more complex) + + You create triggers on the [internal tables](https://docs.powersync.com/architecture/client-architecture#schema) (not the views defined by the client schema), similar to what is + done [here](https://github.com/powersync-ja/powersync-js/blob/e77b1abfbed91988de1f4c707c24855cd66b2219/demos/react-supabase-todolist/src/app/utils/fts_setup.ts#L50) + + +# Example + +The following example is taken from the [React Native To-Do List example app](https://github.com/powersync-ja/powersync-js/tree/main/demos/react-native-supabase-todolist). +It showcases how to delete a `list` and all its associated `todos` in a single transaction. + +```typescript + const deleteList = async (id: string) => { + await system.powersync.writeTransaction(async (tx) => { + // Delete associated todos + await tx.execute(`DELETE FROM ${TODO_TABLE} WHERE list_id = ?`, [id]); + // Delete list record + await tx.execute(`DELETE FROM ${LIST_TABLE} WHERE id = ?`, [id]); + }); + }; +``` + + + An important thing to note is that the local SQLite database will always match the backend database, as long as the tables are in the publication, when online. + For example, if you delete a record from the local `lists` table and Supabase cascade deletes a record from the `todo` table, PowerSync will also delete the local `todo` record when online. + \ No newline at end of file diff --git a/tutorials/client/data/overview.mdx b/tutorials/client/data/overview.mdx new file mode 100644 index 00000000..675da43f --- /dev/null +++ b/tutorials/client/data/overview.mdx @@ -0,0 +1,8 @@ +--- +title: "Overview" +description: "A collection of tutorials showcasing various data management strategies and use cases." +--- + + + + diff --git a/tutorials/client/performance/overview.mdx b/tutorials/client/performance/overview.mdx index 1bf2ff94..0def40e5 100644 --- a/tutorials/client/performance/overview.mdx +++ b/tutorials/client/performance/overview.mdx @@ -4,5 +4,5 @@ description: "A collection of tutorials exploring performance strategies." --- - + diff --git a/tutorials/overview.mdx b/tutorials/overview.mdx index 0273f2e9..4c7db353 100644 --- a/tutorials/overview.mdx +++ b/tutorials/overview.mdx @@ -4,6 +4,7 @@ description: "A collection of tutorials showcasing various storage attachment an --- - - + + +