Skip to content
This repository was archived by the owner on Aug 31, 2025. It is now read-only.

Commit c07a772

Browse files
authored
✨ add global key factory option (#137)
* ✨ add factoryOptions key option * 📝 add global key docs
1 parent d80af4c commit c07a772

File tree

6 files changed

+219
-100
lines changed

6 files changed

+219
-100
lines changed

docs/guide/advanced.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,36 @@ Available global options include:
3131
Any option passed to a store's `persist` configuration will override its counterpart declared in the global options.
3232
:::
3333

34+
## Global key option
35+
36+
On top of other global persistence options, the `createPersistedState` factory function takes one more option: `key`. The global `key` option accepts a function that gets passed the store key and should return a string to become the new store key.
37+
38+
```ts
39+
import { createPinia } from 'pinia'
40+
import { createPersistedState } from 'pinia-plugin-persistedstate'
41+
42+
const pinia = createPinia()
43+
44+
pinia.use(createPersistedState({
45+
key: id => `__persisted__${id}`,
46+
}))
47+
```
48+
49+
```ts
50+
import { defineStore } from 'pinia'
51+
52+
defineStore('store', {
53+
state: () => ({ saved : '' }),
54+
persist: true,
55+
})
56+
```
57+
58+
In this example, the store will be persisted under `__persisted__store` key instead of `store`.
59+
60+
:::info
61+
This is option should be considered when you need to prefix/postfix all store keys globally.
62+
:::
63+
3464
## Multiple persistences per-store
3565

3666
There may be specific use cases where you need to persist data from a single store to different storages. The `persist` option also accepts an array of configurations.

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,25 @@
7171
},
7272
"devDependencies": {
7373
"@prazdevs/eslint-config-typescript": "^3.2.1",
74-
"@types/node": "^18.11.4",
75-
"@vitest/coverage-c8": "^0.24.3",
76-
"@vitest/ui": "^0.24.3",
74+
"@types/node": "^18.11.9",
75+
"@vitest/coverage-c8": "^0.24.5",
76+
"@vitest/ui": "^0.24.5",
7777
"bumpp": "^8.2.1",
7878
"eslint": "8.26.0",
7979
"eslint-config-prettier": "^8.5.0",
8080
"eslint-plugin-prettier": "^4.2.1",
81-
"happy-dom": "^7.6.0",
81+
"happy-dom": "^7.6.6",
8282
"husky": "^8.0.1",
8383
"lint-staged": "^13.0.3",
8484
"nuxt": "3.0.0-rc.12",
8585
"pinia": "^2.0.23",
8686
"prettier": "^2.7.1",
87-
"quasar": "^2.10.0",
87+
"quasar": "^2.10.1",
8888
"tsup": "^6.3.0",
8989
"typescript": "^4.8.4",
90-
"vite": "^3.1.8",
91-
"vitepress": "1.0.0-alpha.22",
92-
"vitest": "^0.24.3",
90+
"vite": "^3.2.2",
91+
"vitepress": "1.0.0-alpha.26",
92+
"vitest": "^0.24.5",
9393
"vue": "^3.2.41",
9494
"vue-demi": "^0.13.11",
9595
"vue2": "npm:vue@^2.7.5"

0 commit comments

Comments
 (0)