Gathering detailed insights and metrics for @pinian/persistent-state
Gathering detailed insights and metrics for @pinian/persistent-state
Gathering detailed insights and metrics for @pinian/persistent-state
Gathering detailed insights and metrics for @pinian/persistent-state
A collection of essential plugins for Pinia, the official state management library for Vue.js
npm install @pinian/persistent-state
Typescript
Module System
Node Version
NPM Version
54.2
Supply Chain
97.8
Quality
77.1
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
166
Last Day
1
Last Week
4
Last Month
20
Last Year
166
47 Commits
1 Watching
4 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.1.0
Package Id
@pinian/persistent-state@1.1.0
Unpacked Size
29.82 kB
Size
6.19 kB
File Count
6
NPM Version
10.8.2
Node Version
20.17.0
Publised On
05 Nov 2024
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-20%
4
Compared to previous week
Last month
185.7%
20
Compared to previous month
Last year
0%
166
Compared to previous year
1
3
This plugin offers effortless state persistence for Pinia stores with a flexible, easy-to-use API. From simple defaults
to advanced customizations like multi-storage and serializers, it streamlines state management through a single
persistentState
option, making it a perfect fit for any project.
localStorage
, sessionStorage
, or even define your own custom storage
solution to fit your needs.1npm install @pinian/persistent-state
1import { createPinia } from 'pinia'; 2import { createPersistentState } from '@pinian/persistent-state'; 3 4const pinia = createPinia(); 5pinia.use(createPersistentState());
persistentState
option to the store you want to be persisted:1import { defineStore } from 'pinia'; 2 3export const useStore = defineStore('profile', { 4 state: () => ({ 5 user: { 6 name: 'John Doe', 7 }, 8 }), 9 persistentState: true, 10});
Your store will be saved using the default configuration.
The plugin provides flexible configuration options that can be applied in two main ways:
persistentState
option within the store definition.To apply configurations globally, pass them when registering the plugin with Pinia:
1import { createPinia } from 'pinia'; 2import { createPersistentStatePlugin } from '@pinian/persistent-state'; 3 4const pinia = createPinia(); 5pinia.use(createPersistentStatePlugin({ 6 auto: true, 7 key: (id) => `v1.0.0-${id}`, 8 storage: localStorage, 9 serialize: (state) => btoa(JSON.stringify(state)), 10 deserialize: (state) => JSON.parse(atob(state)), 11}));
For specific stores, you can use the persistentState
option to override the global settings or define custom behavior.
There are three ways to configure persistentState
for a store:
1import { defineStore } from 'pinia'; 2 3export const useStore = defineStore('profile', { 4 state: () => ({ 5 user: { 6 name: 'John Doe', 7 }, 8 }), 9 persistentState: true, 10});
1import { defineStore } from 'pinia'; 2 3export const useStore = defineStore('profile', { 4 state: () => ({ 5 user: { 6 name: 'John Doe', 7 password: 'secret', 8 }, 9 settings: { 10 theme: 'dark', 11 }, 12 }), 13 persistentState: { 14 key: (id) => `v1.0.0-${id}`, 15 storage: localStorage, 16 serialize: (state) => btoa(JSON.stringify(state)), 17 deserialize: (state) => JSON.parse(atob(state)), 18 pickPaths: [ 19 'user', 20 'settings', 21 ], 22 omitPaths: [ 23 'user.password', 24 ], 25 }, 26});
1import { defineStore } from 'pinia'; 2 3export const useStore = defineStore('profile', { 4 state: () => ({ 5 user: { 6 name: 'John Doe', 7 password: 'secret', 8 }, 9 settings: { 10 theme: 'dark', 11 }, 12 }), 13 persistentState: [ 14 { 15 key: (id) => `v1.0.0-${id}`, 16 storage: localStorage, 17 serialize: (state) => btoa(JSON.stringify(state)), 18 deserialize: (state) => JSON.parse(atob(state)), 19 pickPaths: [ 20 'user', 21 ], 22 omitPaths: [ 23 'user.password', 24 ], 25 }, 26 { 27 storage: sessionStorage, 28 serialize: (state) => JSON.stringify(state), 29 deserialize: (state) => JSON.parse(state), 30 pickPaths: [ 31 'settings.theme', 32 ] 33 }, 34 ], 35});
boolean
false
Defines whether state persistence should be enabled by default for all stores. This can be useful when you want to save the entire application state to browser storage without explicitly configuring each store.
1import { createPinia } from 'pinia'; 2import { createPersistentStatePlugin } from '@pinian/persistent-state'; 3 4const pinia = createPinia(); 5pinia.use(createPersistentStatePlugin({ 6 auto: true, 7})); 8 9export const useStore = defineStore('profile', { 10 state: () => ({ 11 user: { 12 name: 'John Doe', 13 }, 14 }), 15});
This configuration will automatically enable state persistence for all stores with the specified default settings. This ensures state preservation between page reloads without manual configuration for each store.
(id: string) => string
(id) => id
Defines a custom key to identify the store's state in the selected storage.
1import { defineStore } from 'pinia'; 2 3export const useStore = defineStore('profile', { 4 state: () => ({ 5 user: { 6 name: 'John Doe', 7 }, 8 }), 9 persistentState: { 10 key: (id) => `prefix-${id}`, 11 }, 12});
This store will be saved under the prefix-profile
key in localStorage.
KeyValueStorage
localStorage
Defines which storage mechanism to use. It can be localStorage, sessionStorage, or a custom implementation of the KeyValueStorage interface.
1import { defineStore } from 'pinia'; 2 3export const useStore = defineStore('profile', { 4 state: () => ({ 5 user: { 6 name: 'John Doe', 7 }, 8 }), 9 persistentState: { 10 storage: sessionStorage, 11 }, 12});
This store will be saved using sessionStorage instead of localStorage.
(state: T) => string
JSON.stringify
Defines how to serialize the state into a string before saving it. You can also provide a custom serialization method if needed.
1import { defineStore } from 'pinia'; 2 3export const useStore = defineStore('profile', { 4 state: () => ({ 5 user: { 6 name: 'John Doe', 7 }, 8 }), 9 persistentState: { 10 serialize: (state) => btoa(JSON.stringify(state)), 11 }, 12});
This store will save the state using Base64 encoding.
(state: string) => T
JSON.parse
Defines how to deserialize the string from storage back into the state object. You can also provide a custom deserialization method if needed.
1import { defineStore } from 'pinia'; 2 3export const useStore = defineStore('profile', { 4 state: () => ({ 5 user: { 6 name: 'John Doe', 7 }, 8 }), 9 persistentState: { 10 deserialize: (state) => JSON.parse(atob(state)), 11 }, 12});
This store will load the state from Base64 encoding.
string[]
[]
Defines which paths of the state should be saved. You can specify only the paths you want to store.
1import { defineStore } from 'pinia'; 2 3export const useStore = defineStore('profile', { 4 state: () => ({ 5 user: { 6 name: 'John Doe', 7 password: 'secret', 8 }, 9 settings: { 10 theme: 'dark', 11 }, 12 }), 13 persist: { 14 pickPaths: [ 15 'user.name', 16 'settings.theme', 17 ], 18 }, 19});
This store will only save user.name
and settings.theme
, ignoring other state fields.
string[]
[]
Defines which paths of the state should not be saved. All other paths will be stored.
1import { defineStore } from 'pinia'; 2 3export const useStore = defineStore('main', { 4 state: () => ({ 5 user: { 6 name: 'John Doe', 7 password: 'secret', 8 }, 9 settings: { 10 theme: 'dark', 11 }, 12 }), 13 persist: { 14 omitPaths: [ 15 'user.password', 16 ], 17 }, 18});
This store will save everything except user.password
.
@pinian/persistent-state is released under the MIT License.
No vulnerabilities found.
No security vulnerabilities found.