-
Notifications
You must be signed in to change notification settings - Fork 9
Tutorial/cascade delete #70
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
Changes from 3 commits
97865a4
46c108e
1d93a14
e16287d
53f588f
72a1ae3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
<Note> | ||
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. | ||
</Note> | ||
2. Implementing triggers (which is more complex) | ||
<Note> | ||
You create triggers on the internal tables (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) | ||
</Note> | ||
|
||
# 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]); | ||
}); | ||
}; | ||
``` | ||
|
||
<Note> | ||
An important thing to note is that the local SQLite database will always match the online database, as long as the tables are in the publication, when online. | ||
|
||
For example, if you delete a record from the `lists` table and Supabase cascade deletes a record from the `todo` table, PowerSync will also delete the `todo` record when online. | ||
|
||
</Note> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
title: "Overview" | ||
description: "A collection of tutorials showcasing various data management strategies and use cases." | ||
--- | ||
|
||
<CardGroup> | ||
<Card title="Cascading Delete" icon="database" href="/tutorials/client/data/cascading-delete" horizontal/> | ||
</CardGroup> |
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.
Maybe worth linking to https://docs.powersync.com/architecture/client-architecture#schema so they can get more details about the internal tables vs views thing