new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Tauri · Plugins and security · all subjects

sql

22 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

SQL plugin database engines supported

The SQL plugin supports three database engines: SQLite, MySQL, and PostgreSQL. Each engine is enabled via a separate Cargo feature.

SQL plugin automatic setup command

To automatically install the SQL plugin, run one of these commands in the project root depending on the package manager: npm run tauri add sql, yarn run tauri add sql, pnpm tauri add sql, bun tauri add sql, deno task tauri add sql, or cargo tauri add sql.

SQL plugin manual Rust setup

To manually set up the SQL plugin in Rust: run 'cargo add tauri-plugin-sql' in the src-tauri folder, then modify lib.rs to initialize the plugin by adding '.plugin(tauri_plugin_sql::Builder::default().build())' to the tauri::Builder chain.

SQL plugin JavaScript bindings installation

Install the JavaScript guest bindings using the package manager: npm install @tauri-apps/plugin-sql, yarn add @tauri-apps/plugin-sql, pnpm add @tauri-apps/plugin-sql, deno add npm:@tauri-apps/plugin-sql, or bun add @tauri-apps/plugin-sql.

Enable SQLite engine feature

To enable SQLite support in the SQL plugin, run 'cargo add tauri-plugin-sql --features sqlite' in the src-tauri folder.

Enable MySQL engine feature

To enable MySQL support in the SQL plugin, run 'cargo add tauri-plugin-sql --features mysql' in the src-tauri folder.

Enable PostgreSQL engine feature

To enable PostgreSQL support in the SQL plugin, run 'cargo add tauri-plugin-sql --features postgres' in the src-tauri folder.

SQLite database loading and query example

Example of loading a SQLite database and executing a query: import Database from '@tauri-apps/plugin-sql'; const db = await Database.load('sqlite:test.db'); await db.execute('INSERT INTO ...'); The database path is relative to tauri::api::path::BaseDirectory::AppConfig. When using withGlobalTauri: true, access the SQL plugin via window.__TAURI__.sql.

MySQL database loading and query example

Example of loading a MySQL database and executing a query: import Database from '@tauri-apps/plugin-sql'; const db = await Database.load('mysql://user:password@host/test'); await db.execute('INSERT INTO ...'); When using withGlobalTauri: true, access the SQL plugin via window.__TAURI__.sql.

PostgreSQL database loading and query example

Example of loading a PostgreSQL database and executing a query: import Database from '@tauri-apps/plugin-sql'; const db = await Database.load('postgres://user:password@host/test'); await db.execute('INSERT INTO ...'); When using withGlobalTauri: true, access the SQL plugin via window.__TAURI__.sql.

SQLite query parameter syntax

In SQLite, use the "$#" syntax for parameter substitution in queries, where the numbers are 1-indexed. Example: 'INSERT into todos (id, title, status) VALUES ($1, $2, $3)' with parameters [todos.id, todos.title, todos.status].

MySQL query parameter syntax

In MySQL, use "?" for parameter substitution in queries. Example: 'INSERT into todos (id, title, status) VALUES (?, ?, ?)' with parameters [todos.id, todos.title, todos.status].

PostgreSQL query parameter syntax

In PostgreSQL, use the "$#" syntax for parameter substitution in queries, where the numbers are 1-indexed. Example: 'INSERT into todos (id, title, status) VALUES ($1, $2, $3)' with parameters [todos.id, todos.title, todos.status].

Migration struct definition

A Migration in tauri-plugin-sql is defined with: version (unique integer), description (string), sql (the SQL to execute), and kind (MigrationKind::Up or MigrationKind::Down).

Migration example with inline SQL

Example of defining a migration with inline SQL: use tauri_plugin_sql::{Migration, MigrationKind}; let migration = Migration { version: 1, description: "create_initial_tables", sql: "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);", kind: MigrationKind::Up, };

Migration example with SQL from file

Example of defining a migration by including SQL from a file: use tauri_plugin_sql::{Migration, MigrationKind}; let migration = Migration { version: 1, description: "create_initial_tables", sql: include_str!("../drizzle/0000_graceful_boomer.sql"), kind: MigrationKind::Up, };

Adding migrations to plugin builder

Example of registering migrations with the SQL plugin builder: use tauri_plugin_sql::{Builder, Migration, MigrationKind}; let migrations = vec![ Migration { version: 1, description: "create_initial_tables", sql: "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);", kind: MigrationKind::Up, } ]; tauri::Builder::default() .plugin( tauri_plugin_sql::Builder::default() .add_migrations("sqlite:mydatabase.db", migrations) .build(), ) ...

SQL plugin configuration for migrations in tauri.conf.json

To apply migrations when the plugin initializes, add the connection string to src-tauri/tauri.conf.json under the sql plugin configuration with the "preload" key containing an array of connection strings. Example: {"plugins": {"sql": {"preload": ["sqlite:mydatabase.db"]}}}

Client-side migration execution

Calling Database.load() on the client side also runs migrations for the given connection string. Example: const db = await Database.load('sqlite:mydatabase.db');

Migration atomicity guarantee

All migrations in the SQL plugin are executed within a transaction, ensuring atomicity. If any migration fails, the entire transaction is rolled back, leaving the database in a consistent state.

Migration best practices

Each migration must have a unique version number to ensure correct ordering. Write migrations to be idempotent so they can be safely re-run without causing errors or unintended consequences. Thoroughly test migrations to ensure they work as expected and do not compromise database integrity.

SQL plugin default permissions

By default, all potentially dangerous SQL plugin commands and scopes are blocked. To enable them, modify the permissions in the capabilities configuration file (src-tauri/capabilities/default.json) to include the required permissions such as "sql:default" and "sql:allow-execute".

Give your agent this brain