Skip to content

PersistOptions

Overview

PersistOptions is the configuration object passed at the store level when using this plugin. It allows you to customize how each store’s state is persisted.

Properties

PropertyTypeDescription
keystring | Record<keyof S, string>Key(s) used to store data in storage. Defaults to the store’s ID.
debugbooleanEnables logging for debugging purposes. Defaults to false.
overwritebooleanWhen true, replaces the entire store state with the persisted state during $restore. For object keys, unmapped properties are replaced, and mapped properties are patched individually, effectively acting as an overwrite for those keys. Defaults to false.
clientOnlybooleanDetermines if storage operations should be restricted to the client environment only. Defaults to false.
storageStorage | AsyncStorageStorage mechanism for persisting data. Supports synchronous options like localStorage and cookies, asynchronous options such as localForage, or fully custom storage implementations (e.g., fetching from a remote API).
filter(mutation, state) => booleanFilters which mutations trigger persistence.
serialize(state) => stringCustom function for serializing the state.
deserialize(state: string) => Partial<S>Custom function for deserializing the state.
deepCopybooleanEnsure a deep copy of the state by serializing and deserializing. Store the state as an object while avoiding issues with unsupported values like functions or circular references.
includestring | string[]Specifies a store property or an array of properties to include for persistence. Dot notation is supported to target nested properties (e.g., "user.settings"). If provided, only these properties will be persisted.
excludestring | string[]Specifies a store property or an array of properties to exclude from persistence. Dot notation is supported to target nested properties (e.g., "user.password"). If provided, all properties except these will be persisted.

Default Values

If no options are provided at the store level, the plugin uses the following defaults:

ts
import { 
defineStore
} from 'pinia'
export const
useStore
=
defineStore
('exampleStore', {
persist
: {
key
: 'exampleStore', // Defaults to the store's ID
debug
: false, // Debugging disabled
overwrite
: false, // Do not overwrite existing state
filter
: () => true, // Always allow persistence
clientOnly
: false, // Run on both server and client
storage
:
localStorage
, // Use localStorage by default
serialize
:
JSON
.
stringify
, // Default serialization
deserialize
:
JSON
.
parse
, // Default deserialization
deepCopy
: false, // Default deepCopy
include
:
undefined
, // Include all properties by default
exclude
:
undefined
, // Exclude no properties by default
}, })