1 Star 1 Fork 1

5ina / tauri-plugin-sqlite-store

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Tauri Plugin SqliteStore

:fr: This page in French

Add SQLite persistence to your Tauri applications and access any SQLite database on the computer.

Install

Tauri plugins with an API require 2 steps

Install plugin

In the file src-tauri/Cargo.toml, add the dependency to the list

[dependencies]
tauri-plugin-sqlite-store = { git = "https://github.com/Sylvaner/tauri-plugin-sqlite-store", branch = "main" }

It will be compiled automatically when compiling the application.

Install API

Access to features is with an API that makes it easier to use. Depending on your package manager:

pnpm add https://github.com/Sylvaner/tauri-plugin-sqlite-store
# ou
npm add https://github.com/Sylvaner/tauri-plugin-sqlite-store
# ou
yarn add https://github.com/Sylvaner/tauri-plugin-sqlite-store

Configuration

Register the plugin in your application

In the main function in file src-tauri/src/main.rs, register the plugin :

fn main() {
    tauri::Builder::default()
        .plugin(tauri_plugin_sqlite_store::init()) // Add this line
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Add file access permissions

Simple usage

The plugin creates a file store.sqlite in the APP_DATA directory. To allow this file to be written, you must modify the file tauri.conf.json to add permissions to this directory:

  "tauri": {
    "allowlist": {
      "all": false,
      "fs": {                   // Début 
        "readFile": true,       //
        "writeFile": true,      //
        "createDir": true,      //
        "scope": ["$APPDATA/*"] //
      },                        // Fin
      "shell": {
        "all": false,
        "open": true
      }
    },

Access to a database in a specific location

Accès à une base de données dans un emplacement spécifique

In this case, you must add the access permissions to the directory or to all the files on the computer (not recommended). In the file tauri.conf.json:

  "tauri": {
    "allowlist": {
      "all": false,
      "fs": {              // Début 
        "readFile": true,  //
        "writeFile": true, //
        "createDir": true, //
        "scope": ["**"]    //
      },                   // Fin
      "shell": {
        "all": false,
        "open": true
      }
    },

Usage

Import the class SQLite into your code so you can use it directly

Utilisation simple

  • Import SqliteStore
  • Load store
  • Execute queries
import { SqliteStore } from 'tauri-plugin-sqlite-store';

async function initDb() {
    const db = await SQliteStore.load();
    await db.execute(`CREATE DATABASE test (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT NOT NULL
    )`);
    await db.execute(`INSERT INTO test VALUES (NULL, ?1)`, ['John']);
    await db.execute(`INSERT INTO test VALUES (NULL, ?1)`, ['Bill']);
    const rows = await db.select<{id, name}>('SELECT * FROM test');
    for (row of rows) {
        console.log(`${row.name} - Id: ${row.id}`);
    }
}

Access to a database in a specific location

The method of opening a database is the only thing that changes:

const db = await SQliteStore.open('/path/to/the/db');

Features

All features are accessible from SQLiteStore:

  • load - Load local store
  • open - Open a specific database
  • select - Read a dataset
  • selectFirst - Read the first result of a dataset
  • execute - Run a query
  • batch - Run a list of queries
  • setPragma - Run a PRAGMA query

Tables creation

Help is provided for creating tables with a générateur de requêtes.

import { SqliteStore } from 'tauri-plugin-sqlite-store';

async function initDb() {
    const db = await SQliteStore.load();
    // `CREATE DATABASE test (
    //     id INTEGER PRIMARY KEY AUTOINCREMENT,
    //     name TEXT NOT NULL
    //)`
    const gen = db.getTableGenerator('test');
    gen.addColumn('id', 'INTEGER', {primaryKey: true, autoIncrement: true});
    gen.addColumn('name', 'TEXT', {notNull: true});
    await db.execute(gen.getCreateTableQuery());

    // `CREATE DATABASE same_test (
    //     id INTEGER PRIMARY KEY AUTOINCREMENT,
    //     name TEXT NOT NULL
    //)`
    await db.create({
      name: 'same_test',
      columns: [
        {
          name: 'id',
          type: 'INTEGER',
          options: {
            primaryKey: true,
            autoIncrement: true
          }
        },
        {
          name: 'name',
          type: 'TEXT',
          options: {
            notNull: true
          }
        }
      ]
    });
}

Documentation

The API documentation is accessible directly from GitHub: Documentation

Exemple

Dans le répertoire example se trouve une application fonctionnelle avec le plugin installé. Pour le lancer :

cd example
yarn install
yarn tauri dev

The important files are:

  • src/App.svelte : API Usage
  • src-tauri/src/main.rs : API registration
  • src-tauri/tauri.conf.json : Configuration permissions

Build the package

yarn install
yarn build

Files are generated in the folder webview-dist.

Thanks

License

MIT or MIT/Apache 2.0 where applicable.

MIT License Copyright (c) 2023 Sylvain Dangin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Tauri Plugin SqliteStore 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/5ina/tauri-plugin-sqlite-store.git
git@gitee.com:5ina/tauri-plugin-sqlite-store.git
5ina
tauri-plugin-sqlite-store
tauri-plugin-sqlite-store
main

搜索帮助