Skip to content

Commit 8fff94a

Browse files
committed
First Commit ⚡🚀
0 parents  commit 8fff94a

File tree

6 files changed

+259
-0
lines changed

6 files changed

+259
-0
lines changed

.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
.DS_Store
12+
dist-ssr
13+
coverage
14+
*.local
15+
16+
/cypress/videos/
17+
/cypress/screenshots/
18+
19+
# Editor directories and files
20+
.vscode/*
21+
!.vscode/extensions.json
22+
.idea
23+
*.suo
24+
*.ntvs*
25+
*.njsproj
26+
*.sln
27+
*.sw?
28+
29+
demo
30+
test
31+
/index.html
32+
jsconfig.json

.npmignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
.DS_Store
12+
dist-ssr
13+
coverage
14+
*.local
15+
16+
/cypress/videos/
17+
/cypress/screenshots/
18+
19+
# Editor directories and files
20+
.vscode/*
21+
!.vscode/extensions.json
22+
.idea
23+
*.suo
24+
*.ntvs*
25+
*.njsproj
26+
*.sln
27+
*.sw?
28+
29+
demo
30+
dist
31+
test
32+
index.html
33+
jsconfig.json

README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Pinia Storage
2+
3+
![npm license](https://img.shields.io/npm/l/@ctechhindi/Pinia-Storage.svg)
4+
![npm download](https://img.shields.io/npm/dm/@ctechhindi/Pinia-Storage.svg)
5+
![GitHub top language](https://img.shields.io/github/languages/top/ctechhindi/Pinia-Storage.svg)
6+
![GitHub issues](https://img.shields.io/github/issues/ctechhindi/Pinia-Storage.svg)
7+
[![npm package](https://img.shields.io/npm/v/@ctechhindi/Pinia-Storage.svg)](https://www.npmjs.com/package/@ctechhindi/Pinia-Storage)
8+
9+
> A infinite progress bar for vue 3
10+
11+
- [GitHub](https://github.com/ctechhindi/Pinia-Storage)
12+
13+
## Requirements
14+
15+
- Vue.js `^3.0.0`
16+
17+
## Installation
18+
19+
### npm
20+
21+
```
22+
npm i @ctechhindi/Pinia-Storage
23+
```
24+
25+
## Usage
26+
27+
**`main.js`**
28+
29+
```js
30+
import piniaStorage from "@ctechhindi/pinia-storage";
31+
const pinia = createPinia();
32+
pinia.use(piniaStorage);
33+
```
34+
35+
**`\stores\{sites}-store.js`**
36+
37+
```js
38+
import { defineStore } from "pinia";
39+
40+
export const useUsersStore = defineStore({
41+
id: "users-store",
42+
state: () => ({
43+
name: {
44+
first: "",
45+
last: "",
46+
},
47+
}),
48+
conserve: {
49+
enabled: true,
50+
strategies: [
51+
{ storage: localStorage, states: ["name"] }, // Save custom state
52+
// { storage: localStorage, }, // Save all state
53+
// { storage: sessionStorage, states: ['name'] },
54+
],
55+
},
56+
getters: {},
57+
actions: {},
58+
});
59+
```
60+
61+
### Custom LocalStorage
62+
63+
**`\stores\{sites}-store.js`**
64+
65+
```js
66+
import { defineStore } from "pinia";
67+
68+
// Custom
69+
const secureStorage = {
70+
setItem(key, state) {
71+
localStorage.setItem(key, state)
72+
},
73+
getItem(key) {
74+
localStorage.getItem(key, state)
75+
},
76+
}
77+
78+
export const useUsersStore = defineStore({
79+
id: "users-store",
80+
state: () => ({
81+
name: {
82+
first: "",
83+
last: "",
84+
},
85+
}),
86+
conserve: {
87+
enabled: true,
88+
strategies: [
89+
{ storage: secureStorage, states: ["name"] }, // Save custom state
90+
],
91+
},
92+
getters: {},
93+
actions: {},
94+
});
95+
```
96+
97+
# License
98+
99+
[MIT License](https://opensource.org/licenses/MIT)

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "@ctechhindi/pinia-storage",
3+
"version": "1.0.0",
4+
"description": "Store Vue 3 Store data in the browser LocalStorage",
5+
"homepage": "https://github.com/ctechhindi/Pinia-Storage",
6+
"bugs": "https://github.com/ctechhindi/Pinia-Storage/issues",
7+
"email": "ctechhindi@gmail.com",
8+
"main": "src/index.js",
9+
"module": "src/index.js",
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/ctechhindi/Pinia-Storage.git"
13+
},
14+
"scripts": {
15+
"dev": "vite",
16+
"build": "vite build",
17+
"preview": "vite preview --port 4173",
18+
"publish": "npm publish --access public"
19+
},
20+
"keywords": [
21+
"vue3",
22+
"pinia",
23+
"pinia-storage",
24+
"storage",
25+
"vue",
26+
"ctechhindi"
27+
],
28+
"author": "Jeevan Lal",
29+
"license": "MIT",
30+
"devDependencies": {
31+
"@vitejs/plugin-vue": "^3.0.3",
32+
"vite": "^3.0.9",
33+
"vue": "^3.2.38",
34+
"vue-router": "^4.1.5"
35+
}
36+
}

src/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Update Storage
3+
* @param {Object} conserve
4+
* @param {Object} store
5+
*/
6+
export const updateStorage = (conserve, store) => {
7+
const storage = conserve.storage || sessionStorage
8+
const storeKey = conserve.key || store.$id
9+
10+
if (conserve.states) {
11+
const partialState = conserve.states.reduce((finalObj, key) => {
12+
finalObj[key] = store.$state[key];
13+
return finalObj;
14+
}, {})
15+
16+
storage.setItem(storeKey, JSON.stringify(partialState))
17+
} else {
18+
storage.setItem(storeKey, JSON.stringify(store.$state))
19+
}
20+
}
21+
22+
// Default
23+
function piniaStorage({ options, store }) {
24+
if (options.conserve && options.conserve.enabled && options.conserve.strategies && options.conserve.strategies.length > 0) {
25+
26+
// Get Storage and Set in the Store
27+
options.conserve.strategies.forEach((s) => {
28+
const storage = s.storage || sessionStorage
29+
const storeKey = s.key || store.$id
30+
const storageResult = storage.getItem(storeKey)
31+
32+
if (storageResult) {
33+
store.$patch(JSON.parse(storageResult))
34+
updateStorage(s, store)
35+
}
36+
});
37+
38+
store.$subscribe((mutations, state) => {
39+
options.conserve.strategies.forEach((s) => {
40+
updateStorage(s, store)
41+
})
42+
}, { detached: true });
43+
}
44+
}
45+
46+
export default piniaStorage;

vite.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { fileURLToPath, URL } from 'url'
2+
import { defineConfig } from 'vite'
3+
import vue from '@vitejs/plugin-vue'
4+
5+
// https://vitejs.dev/config/
6+
export default defineConfig({
7+
plugins: [vue()],
8+
resolve: {
9+
alias: {
10+
'@': fileURLToPath(new URL('./test', import.meta.url))
11+
}
12+
}
13+
})

0 commit comments

Comments
 (0)