Enhances Pinia stores with advanced features such as persistence, encryption, and store extension. Simplifies state management and ensures data security for Vue.js and Nuxt applications.
npm install eppsepps - Extends and Persist Pinia Storeepps is a plugin for the Pinia state management library in Vue.js. It extends Pinia stores with advanced features such as persistence, encryption, and store extension. It simplifies state and action management while ensuring sensitive data is protected and enabling seamless integration of parent-child store relationships.
epps plugin, you can create more powerful and flexible Pinia stores while ensuring data security and simplifying state management in your Vue.js or Nuxt application.
epps plugin:
sh
npm install epps
or
yarn add epps
`
$3
To use the plugin, simply import it and add it to your Pinia instance.
All createPlugin function parameters are optional.
In the examples below, the environment variable CRYPT_KEY must be replaced with string. It's used for encrypting data persisted in the browser.
#### Vue
`javascript
// ./main.js
import { createPinia } from 'pinia'
import { createPlugin } from 'epps'
const pinia = createPinia()
const epps = createPlugin(
'localStorage', // define another database name to use IndexedDB
import.meta.env.CRYPT_KEY
)
pinia.use(epps)
`
#### Nuxt
`typeScript
// ./plugins/epps-plugin.client.ts
import type { Pinia, PiniaPlugin, PiniaPluginContext } from "pinia"
import { createPlugin } from 'epps'
export default defineNuxtPlugin({
name: 'eppsPlugin',
async setup({ $pinia }) {
($pinia as Pinia).use(
createPlugin(
'localStorage', // define another database name to use IndexedDB
useRuntimeConfig().public.cryptKey
)
)
}
})
`
Usage
$3
The useListsStore store demonstrates how to create a collection-based store using the useCollectionStore store, which is integrated into the Epps plugin. This allows you to manage collections of items in your project seamlessly.
#### Version >= 0.4.0
`typeScript
import { ref } from "vue";
import { defineEppsStore, getExtendedStore, useCollectionStore } from 'epps';
import type { List, ListsState, ListsStoreMethods } from "../types/list";
export const useListsStore = (id?: string) => defineEppsStore(
id ?? 'listsStore',
() => ({
newList: (list: List) => {
const store = getExtendedStore()
store.addItem({ ...list, id: store.lists.length + 1})
}
}),
{
actionsToRename: { getItems: 'getLists' },
parentsStores: [new ParentStore('listsCollection', useCollectionStore)],
persist: { watchMutation: true },
propertiesToRename: { items: 'lists' }
}
)()
`
#### Version 0.3.X
`typeScript
...
const epps = new Epps({
parentsStores: [ new ParentStore('listsCollection', useCollectionStore) ],
persist: true // Store persisted manually. Use “watchMutation” to persist each time the State is modified.
})
export const useListsStore = (id?: string) => defineEppsStore>(
id ?? 'listsStore',
() => ({
getLists: () => {
const collectionStore = epps.getStore>(0, id ?? defaultStoreId)
return collectionStore()?.getItems()
}
}),
epps
)();
`
In this example:
- useCollectionStore is a utility provided by the Epps plugin to manage collections of items.
- The useListsStore store extends useCollectionStore to manage a collection of lists with persistence enabled.
$3
To use the useListsStore store in a Vue component, you can import and use it as follows:
`vue
Lists
-
{{ list.name }} (Type: {{ list.type }})
`
This example shows how to add a new list and retrieve all lists from the useListsStore store. The useCollectionStore` integration simplifies collection management in your project.