Add SQLite + Migrations to Tauri 2 | Plugin SQL Tutorial
About this lesson
Give your Tauri 2 app a real database. tauri-plugin-sql ships SQLite, a migration system, and a JS API that runs from your React code — no native drivers, no bundling pain, no separate database server. Declare your schema as a list of versioned Migration structs in Rust, and the plugin runs them in order on launch. Source code: https://github.com/GoCelesteAI/tauri_markit We build Markit, a bookmark manager that lives in SQLite. v1 of the schema creates the bookmarks table. v2 adds a visited_at column. The plugin tracks which version your local DB is at and applies just the upgrades that haven't run yet. Add a bookmark, click it, quit, relaunch — the row is still there, the visited mark is still there, and the schema is exactly where you left it. What You'll Learn: - tauri-plugin-sql with the sqlite feature — what Cargo.toml actually needs and what the plugin gives you in return. - Declaring migrations in Rust — Migration { version, description, sql, kind: MigrationKind::Up } and what each field actually does. - add_migrations("sqlite:markit.db", migrations) — the plugin handles the OS app data directory, the file path, the connection, and version tracking. You only think in schema. - Capability gating — sql:default, sql:allow-execute, sql:allow-select. What each unlocks, why you want both narrow ones in production. - The JS API — Database.load, db.execute for writes with positional parameters, db.select for typed reads with TypeScript generics. - The load-then-render pattern — useEffect runs load() once, the plugin runs any pending migrations, then refresh() populates state. Same pattern as the store plugin from the previous tutorial. - Why positional parameters are not just style — $1, $2, $3 placeholders are the right shape against SQL injection. Timestamps: 0:00 - The proof: three bookmarks, click one, quit, relaunch, still there 0:28 - Cargo.toml — tauri-plugin-sql with the sqlite feature 0:55 - Capabilities — sql:default, allow-execute, allow-select 1:25 -
DeepCamp AI