Store(キー値保存)
Plugin 説明内容の英語表記部分について Plugin の各章は、原文データからページ内容の一部が自動生成されているため、英語表記のままの部分があります。
このプラグインは、「永続的なキー値の保存」を提供します。これは、アプリケーションの状態を管理するための数あるオプションの一つです。これ以外のオプションの詳細については、状態管理の概要 の章をご覧ください。
この「保存 Store」プラグインを使用すると、アプリの状態をファイルに保存・永続化でき、そのファイルはアプリの再起動時などを含め、必要に応じて保存・読み込みできます。このプロセスは非同期であるため、コード内で処理する必要があることに注意してください。WebView でも Rust でも使用できます。
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | ||
| linux | ||
| macos | ||
| android | ||
| ios |
はじめに、「Store」プラグインをインストールしてください。
自分のプロジェクトのパッケージ・マネージャーを使用して依存関係を追加します:
npm run tauri add storeyarn run tauri add storepnpm tauri add storedeno task tauri add storebun tauri add storecargo tauri add store-
src-tauriフォルダで次のコマンドを実行して、このプラグインをCargo.toml内のプロジェクトの依存関係に追加します:cargo add tauri-plugin-store -
追加したプラグインを初期化するために
lib.rsを修正します:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_store::Builder::new().build()).run(tauri::generate_context!()).expect("error while running tauri application");} -
お好みの JavaScript パッケージ・マネージャーを使用して、「JavaScript Guest」バインディングをインストールします:
npm install @tauri-apps/plugin-storeyarn add @tauri-apps/plugin-storepnpm add @tauri-apps/plugin-storedeno add npm:@tauri-apps/plugin-storebun add @tauri-apps/plugin-store
import { load } from '@tauri-apps/plugin-store';// `"withGlobalTauri": true`, を使用する場合は、// const { load } = window.__TAURI__.store; を使用できます
// 新しい「Store」を作成するか、既存の「Store」をロードします。// 同じパスを持つ `Store` がすでに作成されている場合には、オプション設定が無視されることに注意してください。const store = await load('store.json', { autoSave: false });
// キー値を設定await store.set('some-key', { value: 5 });
// キー値を取得const val = await store.get<{ value: number }>('some-key');console.log(val); // { value: 5 }
// 変更を行なった後、「Store」を手動で保存できます。// 手動保存しない場合には、正常終了時に保存されます。// もし `autoSave`を「数値」に設定するか「空のまま」にしておくと、// `autoSave` はデバウンス遅延(デフォルトでは 100 ミリ秒)後に変更内容をディスクに保存します。《下記訳注参照》await store.save();use tauri::Wry;use tauri_plugin_store::StoreExt;use serde_json::json;
#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_store::Builder::default().build()) .setup(|app| { // 新しい「Store」を作成するか、既存の「Store」をロードします。 // これにより、「Store」がアプリのリソース・テーブルに追加されます // そのため、その後の(rust と js の両方からの)`store` 呼び出しは、 // 同じ「Store」を再利用します。 let store = app.store("store.json")?;
// 値は「serde_json::Value」のインスタンスでなければならないことに注意してください。 // そうしないと、JavaScript バインディングとの互換性がなくなります。 store.set("some-key", json!({ "value": 5 }));
// 「store」から値を取得 let value = store.get("some-key").expect("Failed to get value from store"); println!("{}", value); // {"value":5}
// リソース・テーブルから「store」を削除 store.close_resource();
Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}デバウンス遅延 debounce delay: 特定のイベントが短時間に連続して発生(バウンス)した際に(たとえばキーボード入力時のチャタリング)、意図しない余計な入力信号を無視する(デバウンス)ために。タイマーによる処理遅延時間を設定してそのような入力イベントを無視し、最後の入力だけが一度だけ実行されるようにする手法。
高レベル JavaScript API LazyStore もあり、これは最初のアクセス時にのみ「store」をロードします。
import { LazyStore } from '@tauri-apps/plugin-store';
const store = new LazyStore('settings.json');import { Store } from '@tauri-apps/plugin-store';import { LazyStore } from '@tauri-apps/plugin-store';with_store(app.handle().clone(), stores, path, |store| { store.insert("some-key".to_string(), json!({ "value": 5 }))?; Ok(())});let store = app.store(path)?;store.set("some-key".to_string(), json!({ "value": 5 }));デフォルトでは、潜在的に危険なプラグイン・コマンドとそのスコープ(有効範囲)はすべてブロックされており、アクセスできません。これらを有効にするには、capabilities 設定でアクセス権限を変更する必要があります。
詳細については「セキュリティ・レベル Capabilities」の章を参照してください。また、プラグインのアクセス権限を設定するには「プライグン・アクセス権の使用」の章のステップ・バイ・ステップ・ガイドを参照してください。
{ "permissions": [ ..., "store:default", ]}Default Permission
This permission set configures what kind of operations are available from the store plugin.
Granted Permissions
All operations are enabled by default.
This default permission set includes the following:
allow-loadallow-get-storeallow-setallow-getallow-hasallow-deleteallow-clearallow-resetallow-keysallow-valuesallow-entriesallow-lengthallow-reloadallow-save
Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the clear command without any pre-configured scope. |
|
|
Denies the clear command without any pre-configured scope. |
|
|
Enables the delete command without any pre-configured scope. |
|
|
Denies the delete command without any pre-configured scope. |
|
|
Enables the entries command without any pre-configured scope. |
|
|
Denies the entries command without any pre-configured scope. |
|
|
Enables the get command without any pre-configured scope. |
|
|
Denies the get command without any pre-configured scope. |
|
|
Enables the get_store command without any pre-configured scope. |
|
|
Denies the get_store command without any pre-configured scope. |
|
|
Enables the has command without any pre-configured scope. |
|
|
Denies the has command without any pre-configured scope. |
|
|
Enables the keys command without any pre-configured scope. |
|
|
Denies the keys command without any pre-configured scope. |
|
|
Enables the length command without any pre-configured scope. |
|
|
Denies the length command without any pre-configured scope. |
|
|
Enables the load command without any pre-configured scope. |
|
|
Denies the load command without any pre-configured scope. |
|
|
Enables the reload command without any pre-configured scope. |
|
|
Denies the reload command without any pre-configured scope. |
|
|
Enables the reset command without any pre-configured scope. |
|
|
Denies the reset command without any pre-configured scope. |
|
|
Enables the save command without any pre-configured scope. |
|
|
Denies the save command without any pre-configured scope. |
|
|
Enables the set command without any pre-configured scope. |
|
|
Denies the set command without any pre-configured scope. |
|
|
Enables the values command without any pre-configured scope. |
|
|
Denies the values command without any pre-configured scope. |
【※ この日本語版は、「Nov 10, 2025 英語版」に基づいています】
© 2025 Tauri Contributors. CC-BY / MIT