npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

pinia-plugin-persist-state

v0.0.1

Published

Pinia plugin for persisting store state

Downloads

37

Readme

Persist Pinia State Plugin

A small Pinia plugin that adds persistence and optional encryption to your Pinia stores. It integrates with browser storage (localStorage or sessionStorage) or IndexedDB and augments stores with convenient methods to persist, restore and control watching behavior.


🔧 Features

  • Persist store state to LocalStorage, SessionStorage or IndexedDB
  • Selective encryption for specific properties using Web Crypto (AES-GCM)
  • Per-store options via storeOptions when defining a store
  • Augmented store API: persistState, remember, removePersistedState, watch, stopWatch
  • Simple initialization through createPersistStatePlugin(dbName?, cryptKey?)

⚙️ Installation

Install the package (example):

npm install --save persist-pinia-state

Then register the plugin with Pinia in your app entry (see src/main.ts):

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import { createPlugin } from 'pinia-plugin-subscription'
import { createPersistStatePlugin } from 'persist-pinia-state'

const app = createApp(App)
const pinia = createPinia()

// Pass `dbName` (e.g. 'localStorage', 'sessionStorage' or a DB name for IndexedDB)
// and an optional `cryptKey` to enable encryption support
pinia.use(createPlugin([
  createPersistStatePlugin('localStorage', 'my-secret-key')
], true))

app.use(pinia)

📚 Usage

When defining a store you can pass storeOptions (type: PersistedStoreOptions) as part of the defineStore options. Example in src/stores/test.ts:

// -- using defineAStore

export const useTestStore = defineAStore('testStore', () => {
  const myString = ref('Hello World')
  const myStringEncrypted = ref('Sensitive Data')

  return { myString, myStringEncrypted }
}, {
  persistedPropertiesToEncrypt: ['myStringEncrypted'],
  watchMutation: true
})

// -- or using defineStore

const storeOptions = {
  persistedPropertiesToEncrypt: ['myStringEncrypted'],
  watchMutation: true
}

export const useTestStore = defineStore('testStore', () => {
  const myString = ref('Hello World')
  const myStringEncrypted = ref('Sensitive Data')

  return { myString, myStringEncrypted }
}, { storeOptions })

If persist or watchMutation are true the plugin will attempt to persist the store state using the configured persister: localStorage, sessionStorage or IndexedDB.


🔣 PersistedStoreOptions

Fields available when setting storeOptions:

  • dbName?: string — (Optional) Name of the database/storage to use to persist the store state (use only if different from the one defined in the plugin). Use 'localStorage' or 'sessionStorage' for window storage, or any other name to use IndexedDB.
  • excludedKeys?: string[] — List of state properties that should NOT be persisted.
  • persist?: boolean — Enable or disable persistence for the store (default: false).
  • persistedPropertiesToEncrypt?: string[] — List of property names to be encrypted when persisted.
  • watchMutation?: boolean — When true, plugin watches store mutations and automatically persists changes.

🧰 Augmented Store API

When the plugin is active stores gain the following methods (see PersistedStore interface):

  • persistState(): Promise<void> — Immediately persist the current store state (ignores empty values and excluded keys).
  • remember(): Promise<void> — Load persisted state and apply it to the store (used on plugin init).
  • removePersistedState(): void — Delete the persisted entry for this store.
  • watch(): void — Start watching for mutations (sets watchMutation = true).
  • stopWatch(): void — Stop auto-persisting on mutations (sets watchMutation = false).

Note: encrypted properties are automatically decrypted when remembered (if a crypto key was provided during plugin creation).


🔐 Encryption

Optionally supply a cryptKey when creating the plugin, e.g. createPersistStatePlugin(undefined, 'my-secret'). The plugin uses the Web Crypto API (PBKDF2 + AES-GCM) to encrypt properties listed in persistedPropertiesToEncrypt on each store. Only the specified properties will be encrypted.


💡 Notes

  • The plugin augments Pinia store definitions using the pinia-plugin-subscription helper. It adds storeOptions to Pinia's DefineStoreOptionsBase type through declaration merging.
  • The $reset method is available for stores augmented by the plugin (also setup store 😁).
  • When using IndexedDB, the persister stores objects with a storeName key path.

📜 License

MIT