@chonglou_/storage
TypeScript icon, indicating that this package has built-in type declarations

1.0.8-beta.0 • Public • Published

@variety/storage

开箱即用的浏览器本地持久化数据缓存工具,并且可开启数据加密,支持 sessionStorage, localStorage, indexedDB 等本地存储

特性

    1. 支持 sessionStorage, localStorage,indexedDB 三个位置存储,并且提供数据加密
    1. ts 开发支持智能的类型语法提示
    1. hooks 思维,只提供便捷的钩子函数
    1. 基于 pnpm

安装

# 安装工具
pnpm add @variety/storage -S

API

useEncrypt

AES 非对称数据加密

参数 类型 必填 说明
data any 需要加密的数据
//加密数据
let encrypt =  useEncrypt({ name: 'xiaoming' })

useDecrypt

AES 非对称数据解密

参数 类型 必填 说明
encryptString string 已加密过的字符串数据
//加密数据
let encrypt = useEncrypt({ name: 'xiaoming' })
let object = useDecrypt(encrypt)

useBase64Encrypt

BASE64 数据加密

参数 类型 必填 说明
data any 需要加密的数据
//加密数据
let encrypt =  useBase64Encrypt({ name: 'xiaoming' })

useBase64Decrypt

BASE64 数据解密

参数 类型 必填 说明
encryptString string 已加密过的字符串数据
//加密数据
let encrypt = useEncrypt({ name: 'xiaoming' })
let object = useDecrypt(encrypt)

useSetStorage

设置本地缓存数据

参数 类型 必填 说明
options object 配置参数

options

参数 类型 必填 说明
isEncrypt boolean 是否加密数据
storage enum localStorage,sessionStorage 默认 localStorage
data { key , value , expire } 存储的数据对象, expire 为过期时间,单位为 s ,默认为 0,则不设置过期时间
//存储数据
useSetStorage({
  isEncrypt: true,
  storage: 'localStorage',
  data: {
    key: '__TEST_DATA__',
    value: { name: 'xiaoming' , expire: 32}
  }
})

useGetStorage

获取缓存数据(过期数据删除本地缓存,并返回 null)

参数 类型 必填 说明
options object 配置参数

options

参数 类型 必填 说明
isEncrypt boolean 是否加密数据,和 useSetStorage 的存储加密方式需要一一对应
storage enum localStorage,sessionStorage 默认 localStorage
key string 本地存储的 key
//存储数据
useSetStorage({
  isEncrypt: true,
  storage: 'localStorage',
  data: {
    key: '__TEST_DATA__',
    value: { name: 'xiaoming' , expire: 32}
  }
})

//获取本地数据
let state = useGetStorage({
  isEncrypt: true,
  storage: 'localStorage',
  key: '__TEST_DATA__'
})

useRemoveStorage

删除本地缓存数据

参数 类型 必填 说明
options object 配置参数

options

参数 类型 必填 说明
storage enum localStorage,sessionStorage 默认 localStorage
key string 本地存储的 key
//删除本地数据
let state = useRemoveStorage({
  storage: 'localStorage',
  key: '__TEST_DATA__'
})

useClearStorage

清空本地缓存

参数 类型 必填 说明
storage enum localStorage,sessionStorage 默认 localStorage
//删除本地数据
let state = useClearStorage('localStorage')

useOpenDataBase

打开数据库,并返回 Promise,数据库对象 db

参数 类型 必填 说明
options object 配置参数

options

参数 类型 必填 说明
dbName string 数据库名称
version number 数据库版本,正整数,默认 1.0
storeName string 首次建表的表名称
注: 由于indexedDB的表建立是在数据库升级的时候才允许创建,也就是versionchange的事件。所以新建表的逻辑,也是更
新数据库的版本号
//打开数据库,并新建variety-table表
useOpenDataBase({
  version: 1.0,
  dbName: 'variety-indexdb',
  storeName: 'variety-table'
}).then(db => {
 console.log('数据库对象', db)
})

useCloseDataBase

关闭数据库

参数 类型 必填 说明
db IDBDatabase 数据库对象实例
//打开数据库,并新建variety-table表
let db = await useOpenDataBase({
  version: 1.0,
  dbName: 'variety-indexdb',
  storeName: 'variety-table'
})

//关闭数据库
useCloseDataBase(db)

useDeleteDataBase

删除数据库

参数 类型 必填 说明
dbName string 数据库名称
//打开数据库,并新建variety-table表
let db = await useOpenDataBase({
  version: 1.0,
  dbName: 'variety-indexdb',
  storeName: 'variety-table'
})

//关闭数据库
useDeleteDataBase(‘variety-indexdb’)

useSetDataStore

添加数据到数据表,返回 Promise,是 boolean

参数 类型 必填 说明
options object 配置参数

options

参数 类型 必填 说明
dbName string 数据库名称
version number 数据库版本,正整数,默认 1.0
storeName string 首次建表的表名称
isEncrypt boolean 是否数据加密
data { id:'必传', ... } 存储的数据对象
//添加数据到数据库,只针对新增的数据
useSetDataStore({
  isEncrypt: true,
  dbName: 'variety-indexdb',
  storeName: 'variety-table',
  data: { id: '3243124132432', value: 'xiamiung' }
})

useUpdateDataStore

更新数据到数据表 ,返回 Promise,是 boolean

参数 类型 必填 说明
options object 配置参数

options

参数 类型 必填 说明
dbName string 数据库名称
version number 数据库版本,正整数,默认 1.0
storeName string 首次建表的表名称
isEncrypt boolean 是否数据加密
data { id:'必传', ... } 存储的数据对象
//更新或者添加新的数据到数据库
useUpdateDataStore({
  isEncrypt: true,
  dbName: 'variety-indexdb',
  storeName: 'variety-table',
  data: { id: '3243124132432', value: 'xiamiung' }
})

useGetDataStore

根据主键 id 获取数据,返回 Promise, result 为存储的数据对象

参数 类型 必填 说明
options object 配置参数

options

参数 类型 必填 说明
dbName string 数据库名称
version number 数据库版本,正整数,默认 1.0
storeName string 首次建表的表名称
isEncrypt boolean 是否数据加密
id string 或者 number 主键 id
//更新或者添加新的数据到数据库
useGetDataStore({
  isEncrypt: true,
  dbName: 'variety-indexdb',
  storeName: 'variety-table',
  id: '3243124132432'
}).then(data => {
  console.log('数据库存储的数据', data)
})

useRemoveDataStore

根据主键 id 获取数据,返回 Promise,是 boolean

参数 类型 必填 说明
options object 配置参数

options

参数 类型 必填 说明
dbName string 数据库名称
storeName string 首次建表的表名称
id string 或者 number 主键 id
//更新或者添加新的数据到数据库
useRemoveDataStore({
  dbName: 'variety-indexdb',
  storeName: 'variety-table',
  id: '3243124132432'
})

/@chonglou_/storage/

    Package Sidebar

    Install

    npm i @chonglou_/storage

    Weekly Downloads

    3

    Version

    1.0.8-beta.0

    License

    ISC

    Unpacked Size

    26 kB

    Total Files

    7

    Last publish

    Collaborators

    • chonglou_