1.0.12 • Published 10 months ago

storage-h v1.0.12

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

Localstore模块化管理器

一、功能介绍

日常开发中,我们本地中的localstore非常难以管理,维护,很多时候不知道项目中存储了哪些内容。或者是说命名规范、多项目协同命名空间问题,因此,此库主要解决了如下几点:
  • 模块化管理方式,根据业务来进行建立模块,如果为微前端应用则可以根据应用来建模块;
  • 命名空间,每个模块有独立的命名空间,不存在命名冲突问题;
  • 配置中需要配置模块下所使用的键,也就是defineProps,目的是为了更直观的看当前模块使用了哪些字段的缓存;
  • store中key风格统一,不管是你传递了大驼峰、小驼峰、烤串,都会统一转换成下划线,同时拼接命名空间,也就是如:"user_xxxx_xxx";

二、如何使用

1、安装依赖

// npm
npm install storage-h -S;
// yarn
yarn add storeage-h -S

2、在项目中新建一个管理/定义store中文件;

// use-storage.ts
import { createStorage } from "storage-h"; // 引入存储库

// 创建一个系统store
const systemStore = createStorage({
    // store唯一id
    id: "system",

    // localstore存储前缀
    prefix: "system",

    // 所使用的字段,如果不传递则不限制
    defineProps: ["userName", "userId", "nickName", "token"]
});

// 菜单store
const menuStore = createStorage({
    // store唯一id
    id: "menu",

    // localstore存储前缀
    prefix: "menu",

    // 所使用的字段,如果不传递则不限制
    defineProps: ["menuList"]
});

// 建议按需导出,不使用默认更方便管理
export { systemStore, menuStore };

3、业务中使用

import { systemStore, menuStore } from "./lib/use-storage.ts";

/**
 * 每个模块的常用方法
 * 取值
 * get(key) 获取单个值
 * get(key1, key2, ...key) 获取多个值,返回一个object
 * getOnce(key) 获取值,获取了后会自动删除
 * 
 * 设置值
 * set(key, value) 设置单个值
 * set({key: value}) 设置多个值
 * set(key, value, time) 设置超时时间
 * 
 * 删除值
 * delete(key) 单个删除
 * delete(key1, key2, ...key) 多个删除
 * 
 * 设置过期时间
 * setExpire(key, expire) 设置过期时间
 * getKeys() 获取当前存储中的所有键, 并非定义数量
 * getSize() 获取当前模块中存储的数量,并非定义数量
 */

// 设置用户名
systemStore.set("userName", "John");

// 设置其他未定义的
systemStore.set("userSex", "John"); // 直接会错误警告

// 批量设置
systemStore.set({
    userName: "zhangsan",
    nickName: "就是张三",
    userId: "123456"
});

// 批量删除
systemStore.delete("userName", "nickName", "userId");

三、API 说明

方法参数参数说明
getget(...keys: string[]): Object | any 单个返回值,多个则返回对象
setset( key: string | object, value?: any, expireTime?: number,): { key: string: IContent }
deletedelete(...keys: string[]): Storage
setExpiresetExpire(key: string, expireTime?: number): object | number | null
getKeysgetKeys(): string[]
getSizegetSize(): number
1.0.12

10 months ago

1.0.11

10 months ago

1.0.10

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago