Skip to content

Commit

Permalink
wip(LiteStorage): 增加 getItem、setItem、removeItem 等方法,提供与 localStorage …
Browse files Browse the repository at this point in the history
…一致的接口用法
  • Loading branch information
renxia committed Dec 23, 2023
1 parent f07c5e9 commit 38566f4
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/node/LiteStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export interface LSOptions<T extends object = Record<string, unknown>> {
* 轻量的本地文件持久性数据存储。主要用于简单的配置信息持久化
*/
export class LiteStorage<T extends object = Record<string, unknown>> {
private static instance: LiteStorage<object>;
private static instance: LiteStorage;
static getInstance<T extends object>(options?: LSOptions) {
if (!LiteStorage.instance) LiteStorage.instance = new LiteStorage<T>(options);
if (!LiteStorage.instance) LiteStorage.instance = new LiteStorage(options);
return LiteStorage.instance as LiteStorage<T>;
}
private get cachePath() {
Expand Down Expand Up @@ -66,6 +66,9 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
},
};
}
public get length() {
return Object.keys(this.get(true)).length;
}
public get config() {
return { ...this.options };
}
Expand Down Expand Up @@ -93,7 +96,6 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
let localCache: LSCache<T>;
if (this.isToml) {
const TOML = await import('@iarna/toml');
// localCache = TOML.default.parse(content, '\n', false) as never;
localCache = JSON.parse(JSON.stringify(TOML.default.parse(content)));
} else {
localCache = JSON.parse(content) as LSCache<T>;
Expand Down Expand Up @@ -121,12 +123,13 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
return this;
}
public get(raw = false) {
const info = this.cache.data[this.options.uuid];
const info = this.cache.data[this.options.uuid] || {};
return raw ? info : { ...info };
}
public getAll() {
return this.cache;
}
/** 移除一项数据 */
public del(key: keyof T) {
const info = this.cache.data[this.options.uuid];
if (key in info) {
Expand All @@ -135,6 +138,26 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
}
return this;
}
public setItem<K extends keyof T>(key: K, value: Partial<T[K]>, mode: 'merge' | 'cover' = 'merge') {
const data = this.get(true);
if (mode === 'cover') data[key] = value as T[K];
else assign(data, { [key]: value });

return this.set(data, 'cover');
}
public getItem(key: keyof T) {
const value = this.get(true)[key];
if (value && typeof value === 'object') return assign({}, value) as T[keyof T];
return value;
}
/** 移除一项数据。同 del 方法 */
public removeItem(key: keyof T) {
return this.del(key);
}
/**
* 清理缓存
* @param isAll 是否清空全部缓存(即移除缓存文件重新初始化)。默认为 false,只清空当前 uuid 约束下的缓存数据
*/
public clear(isAll = false) {
if (isAll) {
if (fs.existsSync(this.cachePath)) fs.rmSync(this.cachePath, { force: true });
Expand Down

0 comments on commit 38566f4

Please sign in to comment.