use livevue_rs::CacheKey; use serde::{Deserialize, Serialize}; // --------------------------------------------------------------------------- // Models // --------------------------------------------------------------------------- /// A single todo item. Maps to the `todos` SQLite table. #[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)] pub struct Todo { pub id: i64, pub title: String, pub completed: bool, } // --------------------------------------------------------------------------- // Read queries (go through cx.run / Query trait) // --------------------------------------------------------------------------- /// Fetch all todos, ordered by id. pub struct ListTodos; livevue_rs::query_all!(ListTodos { sql: "SELECT id, title, completed FROM todos ORDER BY id", params: [], output: Todo, cache: |_s: &ListTodos| CacheKey::Table { table: "todos" }, }); /// Fetch a single todo by id. pub struct GetTodo { pub id: i64, } livevue_rs::query_one!(GetTodo { sql: "SELECT id, title, completed FROM todos WHERE id = ?", params: [|s: &GetTodo| s.id], output: Todo, cache: |s: &GetTodo| CacheKey::PrimaryKey { table: "todos", id: s.id }, }); // --------------------------------------------------------------------------- // Write operations (mutation structs via mutation_exec!) // --------------------------------------------------------------------------- // // Each mutation struct implements Query with publish_key() set to the cache // key it invalidates. Use Store::exec() in action handlers — it executes the // mutation and publishes the key automatically. /// Insert a new todo. pub struct CreateTodo { pub title: String, } livevue_rs::mutation_exec!(CreateTodo { sql: "INSERT INTO todos (title, completed) VALUES (?, false)", params: [|s: &CreateTodo| s.title.clone()], publish: |_| CacheKey::Table { table: "todos" }, }); /// Toggle a todo's completed status. pub struct ToggleTodo { pub id: i64, } livevue_rs::mutation_exec!(ToggleTodo { sql: "UPDATE todos SET completed = NOT completed WHERE id = ?", params: [|s: &ToggleTodo| s.id], publish: |_| CacheKey::Table { table: "todos" }, }); /// Delete a todo by id. pub struct DeleteTodo { pub id: i64, } livevue_rs::mutation_exec!(DeleteTodo { sql: "DELETE FROM todos WHERE id = ?", params: [|s: &DeleteTodo| s.id], publish: |_| CacheKey::Table { table: "todos" }, });