Vue Store Adapter
Storagefy's Vue adapter provides seamless integration with Vue's reactive state management systems, particularly focusing on Pinia stores. This adapter synchronizes your Vue store state with your chosen storage backend (localStorage, sessionStorage, or IndexedDB) with minimal configuration.
1. Basic Usage
The Vue adapter works primarily with Pinia, Vue's recommended state management solution, allowing you to persist your store state across page reloads and browser sessions.
import { startStoragefy, getPiniaAdapter } from "storagefy";
// Initialize the storage backend
startStoragefy({
dbName: "my-vue-app",
adapter: "indexedDB", // or "localStorage" or "sessionStorage"
encrypt: true,
channelName: "vue-sync", // the channel the tabs talk on…
enableSyncTabs: true // …and this storage tells them about its writes
});
// Get the Pinia adapter
const { setInStorage, getFromStorage } = getPiniaAdapter();
// Connect with your store
const userStore = useUserStore();
await getFromStorage(userStore, "user"); // Always call this before setInStorage
await setInStorage(userStore, "user", {
timeout: 3600000, // 1 hour expiration
ignoreKeys: ["tempData", "user.password"],
syncTabs: true
});
2. Using the Helper Function
For a more concise approach, use the setPiniaStorage helper function:
import { startStoragefy, setPiniaStorage } from "storagefy";
// Initialize storage backend
startStoragefy({
dbName: "my-vue-app",
adapter: "localStorage"
});
// Use the helper function
const userStore = useUserStore();
await setPiniaStorage(userStore, "user", {
timeout: 86400000, // 24 hours
ignoreKeys: ["sessionToken", "user.password"],
syncTabs: true
});
3. Test It
4. Advanced Configuration
You can directly instantiate the PiniaAdapter class for more advanced control:
import { PiniaAdapter, IndexedDBAdapter } from "storagefy";
// Create storage adapter instance
const storageAdapter = new IndexedDBAdapter({
dbName: "my-app",
encrypt: true
});
// Create Pinia adapter with the storage adapter
const piniaAdapter = new PiniaAdapter(storageAdapter);
// Connect with your store
const settingsStore = useSettingsStore();
await piniaAdapter.getFromStorage(settingsStore, "app-settings");
await piniaAdapter.setInStorage(settingsStore, "app-settings", {
ignoreKeys: ["tempSettings", "user.password"],
timeout: 604800000 // 1 week
});
5. Configuration Options
The setInStorage method accepts the following options:
6. Handling Store Changes
The Vue adapter automatically subscribes to store changes using Pinia's $subscribe method and persists updates to your chosen storage. When data changes in other tabs (with syncTabs: true), the adapter will update the local store without triggering additional storage operations.
// Store changes are automatically persisted
userStore.$patch({
name: "New Username",
preferences: { theme: "dark" }
});
// The above change is automatically synchronized to storage
// and to other tabs if syncTabs is enabled
7. API Reference
getPiniaAdapter([options])
Returns a configured Pinia adapter instance. Options include:
adapterParams: Configuration for the underlying storage adapter
Returns: { setInStorage, getFromStorage, flush, destroy, destroyAll } — the adapter's methods, bound to it (they may be destructured)
setPiniaStorage(store, key, [options])
Helper function that internally calls getFromStorage followed by setInStorage.
store: Pinia store instancekey: Storage key stringoptions: Configuration options (ignoreKeys, timeout, syncTabs, onError)
Returns: Promise resolving to true on success
PiniaAdapter Class Methods
setInStorage(store, key, options): Persists store state and sets up subscriptiongetFromStorage(store, key): Loads persisted state into store —{ onError }tells a stored state that cannot be readflush([key]): Makes sure the state is in storage — what the store holds now is written, unless it is stored already, and the promise resolves once its writes are done:truestored,falsea write was not saved (onErrorsays why). It never rejects for a write. Withoutkey, every store of the adapter; a key no store is kept under rejects. Call it before something that must not lose a change, such as signing outdestroy(key): Stops keeping the store ofkey— no more writes, no more changes from the other tabs. What is stored staysdestroyAll(): Stops keeping every store of the adapter. The storage adapter goes on- A Pinia state is an object: a stored value of another kind is not loaded (
onErrorsays why)
8. Best Practices
- Always call
getFromStoragebeforesetInStorageto hydrate your store with existing data - Before signing out (or anything that must not lose a change), clear the store and
await flush(key): once it resolves, the cleared state is in storage and the page may close - Use
ignoreKeysfor sensitive data like authentication tokens or large temporary objects - Set appropriate
timeoutvalues for different types of data (e.g., longer for preferences, shorter for session data) - Enable
syncTabsfor shared state across multiple browser tabs - Use encryption (
encrypt: true) to keep values out of plain sight — it does not protect a secret: leave tokens and passwords out of storage withignoreKeys