tauri-plugin-pinia 0.1.10

Persistent Pinia stores for Tauri
Documentation

tauri-plugin-pinia

Persistent Pinia stores for Tauri.

Features

  • Save your Pinia stores to disk on app exit (or manually, if needed).
  • Synchronize your stores across multiple windows.
  • Debounce store updates.

Install

Install the Rust crate by adding the following to your Cargo.toml file:

[dependencies]
tauri-plugin-pinia = 0.1

Install the JavaScript package with your preferred package manager:

pnpm add tauri-plugin-pinia
# or
npm add tauri-plugin-pinia
# or
yarn add tauri-plugin-pinia

Usage

For a working example, see the playground.

  1. Enable the required permissions in your capabilities file:
{
  "permissions": ["event:allow-listen", "event:allow-unlisten", "pinia:default"]
}
  1. Register the plugin with Tauri:

src-tauri/src/main.rs

fn main() {
  tauri::Builder::default()
    .plugin(tauri_plugin_pinia::init())
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}
  1. Enable the plugin for Pinia:

src/index.ts

import { createApp } from 'vue';
import { createPinia } from 'pinia';
import { createPlugin } from 'tauri-plugin-pinia';

const app = createApp(App);

const pinia = createPinia();
pinia.use(createPlugin());

app.use(pinia);
app.mount('#app');
  1. Create your Pinia store:

src/stores/counter.ts

import { ref } from 'vue';
import { defineStore } from 'pinia';

export const useCounterStore = defineStore('counter', () => {
  const counter = ref(0);

  function increment() {
    counter.value++;
  }

  return {
    counter,
    increment
  };
});
  1. Start the plugin!
import { useCounterStore } from './stores/counter';

const counterStore = useCounterStore();
counterStore.$tauri.start();