@statesync/tauri / createTauriFileBackend
Function: createTauriFileBackend()
ts
function createTauriFileBackend<T>(options): StorageBackend<T>;Defined in: persistence.ts:171
Creates a StorageBackend that delegates all persistence operations to Tauri commands, enabling secure native file-system access from the frontend.
The returned backend exposes three operations:
- save — invokes
saveCommandwith{ snapshot, ...args } - load — invokes
loadCommandwith{ ...args }, returns the result ornull - clear — invokes
clearCommand(if provided) with{ ...args }
Rust backend requirements:
saveCommandmust accept asnapshotfield deserializable asserde_json::Value(or a typed struct) and persist it.loadCommandmust returnOption<serde_json::Value>(or the typed equivalent), whereNone/nullmeans "nothing stored yet".clearCommand(optional) must delete the stored data.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The application-specific snapshot data type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
options | TauriFileBackendOptions | Configuration specifying the Tauri commands and invoke function. |
Returns
A StorageBackend backed by Tauri IPC commands.
Example
typescript
import { invoke } from '@tauri-apps/api/core';
import { createTauriFileBackend } from '@statesync/tauri';
// --- Rust side ---
// #[tauri::command]
// fn save_settings(snapshot: serde_json::Value) -> Result<(), String> {
// std::fs::write("settings.json", serde_json::to_string(&snapshot).unwrap())
// .map_err(|e| e.to_string())
// }
//
// #[tauri::command]
// fn load_settings() -> Result<Option<serde_json::Value>, String> {
// match std::fs::read_to_string("settings.json") {
// Ok(data) => Ok(Some(serde_json::from_str(&data).unwrap())),
// Err(_) => Ok(None),
// }
// }
// --- TypeScript side ---
interface Settings {
theme: 'light' | 'dark';
locale: string;
}
const storage = createTauriFileBackend<Settings>({
invoke,
saveCommand: 'save_settings',
loadCommand: 'load_settings',
clearCommand: 'clear_settings',
});
// Persist a snapshot:
await storage.save({ revision: '1', data: { theme: 'dark', locale: 'en' } });
// Restore on next launch:
const cached = await storage.load();
if (cached) {
console.log('Restored revision', cached.revision);
}