Skip to content

Commit e661f60

Browse files
authored
feat(storage): add new storage (pure-admin#48)
* feat(storage): add new storage,新增 database 和 cookie 存储 * fix(storage): 区分用户,获取当前存储路径时,应该获取当前用户,用来区分用户 * fix: ts alias mapping * chore: remove lodash * fix(storage): replace lodash with lodash-es * fix(storage): initialization,Initialization failed, unable to write
1 parent 6d81431 commit e661f60

File tree

9 files changed

+175
-12
lines changed

9 files changed

+175
-12
lines changed

.env

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# port
22
VITE_PORT = 8848
3-
3+
# title
4+
VITE_TITLE = vue-pure-admin
5+
# version
6+
VITE_VERSION = 1.0.0
47
# open
58
VITE_OPEN = false
69

.env.development

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# port
22
VITE_PORT = 8848
3-
3+
# title
4+
VITE_TITLE = vue-pure-admin
5+
# version
6+
VITE_VERSION = 1.0.0
47
# open
58
VITE_OPEN = false
69

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
"axios": "^0.21.1",
2828
"cropperjs": "^1.5.11",
2929
"dayjs": "^1.10.6",
30-
"dotenv": "^8.2.0",
3130
"echarts": "^5.1.2",
3231
"element-plus": "^1.1.0-beta.16",
3332
"element-resize-detector": "^1.2.3",
3433
"font-awesome": "^4.7.0",
3534
"lodash-es": "^4.17.21",
35+
"lowdb": "^3.0.0",
3636
"mitt": "^2.1.0",
3737
"mockjs": "^1.1.0",
3838
"nprogress": "^0.2.0",
@@ -42,6 +42,7 @@
4242
"resize-observer-polyfill": "^1.5.1",
4343
"responsive-storage": "^1.0.10",
4444
"sortablejs": "1.13.0",
45+
"typescript-cookie": "^1.0.0",
4546
"v-contextmenu": "^3.0.0",
4647
"vue": "^3.2.19",
4748
"vue-i18n": "^9.2.0-beta.3",

src/utils/storage/cookie.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { loadEnv } from "@build/utils";
2+
import { merge } from "lodash-es";
3+
import tsCookies from "typescript-cookie/dist/src/compat";
4+
5+
class Cookies {
6+
private static env = loadEnv();
7+
constructor() {}
8+
/**
9+
* 存储 cookie 值
10+
* @param name
11+
* @param value
12+
* @param cookieSetting
13+
*/
14+
set(name = "default", value = "", cookieSetting = {}) {
15+
const currentCookieSetting = {
16+
expires: 1
17+
};
18+
merge(currentCookieSetting, cookieSetting);
19+
tsCookies.set(
20+
`${Cookies.env.VITE_TITLE}-${Cookies.env.VITE_VERSION}-${name}`,
21+
value,
22+
currentCookieSetting
23+
);
24+
}
25+
/**
26+
* 拿到 cookie 值
27+
* @param name
28+
* @returns
29+
*/
30+
get(name = "default"): any {
31+
return tsCookies.get(
32+
`${Cookies.env.VITE_TITLE}-${Cookies.env.VITE_VERSION}-${name}`
33+
);
34+
}
35+
/**
36+
* 拿到 cookie 全部的值
37+
* @returns
38+
*/
39+
getAll(): any {
40+
return tsCookies.get();
41+
}
42+
/**
43+
* 删除 cookie
44+
* @param name
45+
*/
46+
remove(name = "default") {
47+
tsCookies.remove(
48+
`${Cookies.env.VITE_TITLE}-${Cookies.env.VITE_VERSION}-${name}`
49+
);
50+
}
51+
}
52+
53+
export const cookies = new Cookies();

src/utils/storage/db.ts

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { loadEnv } from "@build/utils";
2+
import { LocalStorage, LowSync } from "lowdb";
3+
import { chain, cloneDeep } from "lodash-es";
4+
import { storageLocal } from ".";
5+
import { cookies } from "./cookie";
6+
type Data = {
7+
database: {};
8+
sys: {};
9+
};
10+
/**
11+
* db 数据存储,采用 LocalStorage存储
12+
*/
13+
class DB {
14+
private db: LowSync<Data>;
15+
private static env = loadEnv();
16+
constructor() {
17+
this.db = new LowSync<Data>(
18+
new LocalStorage<Data>(`${DB.env.VITE_TITLE}-${DB.env.VITE_VERSION}`)
19+
);
20+
this.initialization();
21+
this.db.chain = chain(this.db.data);
22+
}
23+
private initialization() {
24+
this.db.data = storageLocal.getItem(
25+
`${DB.env.VITE_TITLE}-${DB.env.VITE_VERSION}`
26+
) || { database: {}, sys: {} };
27+
this.db.write();
28+
}
29+
/**
30+
* 检查路径是否存在 不存在的话初始化
31+
* @param param0
32+
* @returns path
33+
*/
34+
pathInit({
35+
dbName = "database",
36+
path = "",
37+
user = true,
38+
validator = () => true,
39+
defaultValue = ""
40+
}): string {
41+
const uuid = cookies.get("uuid") || "ghost-uuid";
42+
const currentPath = `${dbName}.${user ? `user.${uuid}` : "public"}${
43+
path ? `.${path}` : ""
44+
}`;
45+
const value = this.db.chain.get(currentPath).value();
46+
if (!(value !== undefined && validator(value))) {
47+
this.db.chain.set(currentPath, defaultValue).value();
48+
this.db.write();
49+
}
50+
return currentPath;
51+
}
52+
/**
53+
*将数据存储到指定位置 | 路径不存在会自动初始化
54+
*
55+
* 效果类似于取值 dbName.path = value
56+
* @param param0
57+
*/
58+
dbSet({ dbName = "database", path = "", value = "", user = false }): void {
59+
const currentPath = this.pathInit({
60+
dbName,
61+
path,
62+
user
63+
});
64+
this.db.chain.set(currentPath, value).value();
65+
this.db.write();
66+
}
67+
/**
68+
* 获取数据
69+
*
70+
* 效果类似于取值 dbName.path || defaultValue
71+
* @param param0
72+
* @returns
73+
*/
74+
dbGet({
75+
dbName = "database",
76+
path = "",
77+
defaultValue = "",
78+
user = false
79+
}): any {
80+
const values = this.db.chain
81+
.get(this.pathInit({ dbName, path, user, defaultValue }))
82+
.value();
83+
return cloneDeep(values);
84+
}
85+
}
86+
87+
export const db = new DB();

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"incremental": true,
2222
"paths": {
2323
"/@/*": ["src/*"],
24+
"@build/*": ["build/*"],
2425
"/#/*": ["types/*"]
2526
},
2627
"types": ["node", "vite/client"],

types/global.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ declare global {
6565

6666
declare interface ViteEnv {
6767
VITE_PORT: number;
68+
VITE_TITLE: string;
69+
VITE_VERSION: string;
6870
VITE_USE_MOCK: boolean;
6971
VITE_USE_PWA: boolean;
7072
VITE_PUBLIC_PATH: string;

vite.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const pathResolve = (dir: string): any => {
1515

1616
const alias: Record<string, string> = {
1717
"/@": pathResolve("src"),
18+
"@build": pathResolve("build"),
1819
//解决开发环境下的警告 You are running the esm-bundler build of vue-i18n. It is recommended to configure your bundler to explicitly replace feature flag globals with boolean literals to get proper tree-shaking in the final bundle.
1920
"vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js"
2021
};

yarn.lock

+21-9
Original file line numberDiff line numberDiff line change
@@ -1732,11 +1732,6 @@ dot-prop@^5.1.0:
17321732
dependencies:
17331733
is-obj "^2.0.0"
17341734

1735-
dotenv@^8.2.0:
1736-
version "8.6.0"
1737-
resolved "https://registry.npmjs.org/dotenv/-/dotenv-8.6.0.tgz#061af664d19f7f4d8fc6e4ff9b584ce237adcb8b"
1738-
integrity sha512-IrPdXQsk2BbzvCBGBOTmmSH5SodmqZNt4ERAZDmW4CT+tL8VtvinqywuANaFu4bOMWki16nqf0e4oC0QIaDr/g==
1739-
17401735
17411736
version "1.4.7"
17421737
resolved "https://registry.npmjs.org/downloadjs/-/downloadjs-1.4.7.tgz#f69f96f940e0d0553dac291139865a3cd0101e3c"
@@ -2872,8 +2867,8 @@ locate-path@^6.0.0:
28722867

28732868
lodash-es@^4.17.21:
28742869
version "4.17.21"
2875-
resolved "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz#43e626c46e6591b7750beb2b50117390c609e3ee"
2876-
integrity sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==
2870+
resolved "https://registry.npm.taobao.org/lodash-es/download/lodash-es-4.17.21.tgz?cache=0&sync_timestamp=1613836838613&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Flodash-es%2Fdownload%2Flodash-es-4.17.21.tgz#43e626c46e6591b7750beb2b50117390c609e3ee"
2871+
integrity sha1-Q+YmxG5lkbd1C+srUBFzkMYJ4+4=
28772872

28782873
lodash.camelcase@^4.3.0:
28792874
version "4.3.0"
@@ -2897,8 +2892,8 @@ lodash.truncate@^4.4.2:
28972892

28982893
lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.21:
28992894
version "4.17.21"
2900-
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
2901-
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
2895+
resolved "https://registry.npm.taobao.org/lodash/download/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
2896+
integrity sha1-Z5WRxWTDv/quhFTPCz3zcMPWkRw=
29022897

29032898
log-symbols@^4.1.0:
29042899
version "4.1.0"
@@ -2923,6 +2918,13 @@ longest-streak@^2.0.0:
29232918
resolved "https://registry.npmjs.org/longest-streak/-/longest-streak-2.0.4.tgz#b8599957da5b5dab64dee3fe316fa774597d90e4"
29242919
integrity sha512-vM6rUVCVUJJt33bnmHiZEvr7wPT78ztX7rojL+LW51bHtLh6HTjx84LA5W4+oa6aKEJA7jJu5LR6vQRBpA5DVg==
29252920

2921+
lowdb@^3.0.0:
2922+
version "3.0.0"
2923+
resolved "https://registry.nlark.com/lowdb/download/lowdb-3.0.0.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.nlark.com%2Flowdb%2Fdownload%2Flowdb-3.0.0.tgz#c10ab4e7eb86f1cbe255e35e60ffb0c6f42049e0"
2924+
integrity sha1-wQq05+uG8cviVeNeYP+wxvQgSeA=
2925+
dependencies:
2926+
steno "^2.1.0"
2927+
29262928
lower-case@^2.0.2:
29272929
version "2.0.2"
29282930
resolved "https://registry.npmjs.org/lower-case/-/lower-case-2.0.2.tgz#6fa237c63dbdc4a82ca0fd882e4722dc5e634e28"
@@ -4027,6 +4029,11 @@ statuses@~1.5.0:
40274029
resolved "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"
40284030
integrity sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=
40294031

4032+
steno@^2.1.0:
4033+
version "2.1.0"
4034+
resolved "https://registry.nlark.com/steno/download/steno-2.1.0.tgz#05a9c378ce42ed04f642cda6fcb41787a10e4e33"
4035+
integrity sha1-BanDeM5C7QT2Qs2m/LQXh6EOTjM=
4036+
40304037
40314038
version "0.3.1"
40324039
resolved "https://registry.npmjs.org/string-argv/-/string-argv-0.3.1.tgz#95e2fbec0427ae19184935f816d74aaa4c5c19da"
@@ -4344,6 +4351,11 @@ typedarray-to-buffer@^3.1.5:
43444351
dependencies:
43454352
is-typedarray "^1.0.0"
43464353

4354+
typescript-cookie@^1.0.0:
4355+
version "1.0.0"
4356+
resolved "https://registry.npmmirror.com/typescript-cookie/download/typescript-cookie-1.0.0.tgz#5354bf5d827b98dfe40807bf19b143cdd595a772"
4357+
integrity sha1-U1S/XYJ7mN/kCAe/GbFDzdWVp3I=
4358+
43474359
typescript@^4.4.2:
43484360
version "4.4.3"
43494361
resolved "https://registry.npmjs.org/typescript/-/typescript-4.4.3.tgz#bdc5407caa2b109efd4f82fe130656f977a29324"

0 commit comments

Comments
 (0)