#sqlite #tauri-plugin #tauri-app #database-access

tauri-plugin-rusqlite

Tauri Plugin based on Rusqlite

3 releases

0.4.4 Apr 19, 2024
0.4.2 Mar 23, 2024
0.4.1 Mar 21, 2024

#1381 in Database interfaces

30 downloads per month

MIT license

28KB
598 lines

Tauri Plugin Rusqlite

This plugin enables access to an SQLite database within Tauri applications. It is inspired by the tauri-plugin-sqlite plugin but is based on rusqlite.

Example

npm run tauri dev

Installation

Rust

  • Enter the src-tauri directory in your project's structure.

  • Execute the following command to add tauri-plugin-rusqlite to your project's dependencies:

cargo add tauri-plugin-rusqlite
  • Open the main.rs file located in the src-tauri/src directory of your project. Modify the main function to include the plugin initialization code.
fn main() {
    tauri::Builder::default()
        .plugin(tauri_plugin_rusqlite::init()) // add this
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Webview

  • Navigate to the root directory of your source code.

  • Execute the following command in your terminal:

npm i tauri-plugin-rusqlite-api

Usage

Import plugin

import Rusqlite from 'tauri-plugin-rusqlite-api'

Open database

const database = await Rusqlite.openInMemory("test.db");

or

const database = await Rusqlite.openInPath("./folder/test.db");

Init database

let scripts = [{
    name: "create_table", 
    sql: "CREATE TABLE test (id INTEGER PRIMARY KEY, integer_value INTEGER, real_value REAL, text_value TEXT, blob_value BLOB); CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);"
}];
await database.migration(scripts);

or

await database.batch(
    "CREATE TABLE test (id INTEGER PRIMARY KEY, integer_value INTEGER, real_value REAL, text_value TEXT, blob_value BLOB); CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);");

Drop table

await database.batch("DROP TABLE test;");

Insert or Update

await database.update("INSERT INTO test (integer_value, real_value, text_value, blob_value) VALUES (:integer_value, :real_value, :text_value, :blob_value)", 
new Map([
    [":integer_value", parseInt(target.integer_value.value)], 
    [":real_value", parseFloat(target.real_value.value)], 
    [":text_value", target.text_value.value],
    [":blob_value", target.blob_value.value]
]));

Select

let result = await database.select("SELECT * FROM test", new Map());
...
result.map((item) => {
    return {
        id: item.id,
        ...
    };
});

Close database

await database.close();

License

MIT

Dependencies

~42–85MB
~1.5M SLoC