1.0.1 • Published 1 year ago

webindexeddb v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

webindexeddb

展示图

  • 持久化模拟数据库对象存储,可配合mockjs实现大数据量的数据模拟,可以解决storage可用存储空间小的问题,可以保存图片数据
  • 当前版本:v1.0.0

安装方式

  1. npm/yarn方式安装
npm install webindexeddb --save
or  
yarn add webindexeddb  
  1. cdn方式
<script src="https://cdn.jsdelivr.net/gh/yinyangshibolange/webindexeddb/lib/bundle.umd.min.js"></script>  

demo

demo仓库地址:https://github.com/yinyangshibolange/webindexeddb

在线预览地址:https://yinyangshibolange.github.io/webindexeddb/demo.html

IndexdbStore配置项

keytype(类型)Description(描述)Default(默认)
nameString数据库名称'indexeddbname'
versionNumber数据库版本1
storesArray数据库表初始化配置[]

stores表配置

keytype(类型)Description(描述)Default(默认)
storeNameString表名''
mainKeyString主键'id'
indexsArray索引配置[]

indexs索引配置

keytype(类型)Description(描述)Default(默认)
nameString索引名''
keyPathString索引字段,多个字段用","分割''
paramsObject索引配置,可设置索引类型,例如unique{}

使用方法

  1. 在html原生js使用方式
<script src="https://cdn.jsdelivr.net/gh/yinyangshibolange/webindexeddb/lib/bundle.umd.min.js"></script>
<body>
<div id="ctx_menu"></div>
</body>
<script>
    var indexedStore = new window.IndexdbStore({
        name: "myindexdb", // 你的indexdb数据库名称
        version: 7, // 如果修改了options里的stores参数,那么必须修改version版本号,不然stores的修改不会生效
        stores: [
            { // 类似数据库表
                storeName: "demoStore",
                // mainKey: 'id', // 主键,默认为id
                // indexs: []
            },
            { // 类似数据库表
                storeName: "imageList",
                // mainKey: 'id', // 主键,默认为id
                indexs: [{
                    name: "parentid", // 索引名称
                    keyPath: "parentid", // 索引字段
                    params: {
                        unique: false,
                    }
                }]
            }
        ]
    })

</script> 
  1. 模块化导入方式
import IndexdbStore from "webindexeddb"
const indexedStore = new IndexdbStore({
    name: "myindexdb", // 你的indexdb数据库名称
    version: 7, // 如果修改了options里的stores参数,那么必须修改version版本号,不然stores的修改不会生效
    stores: [
        { // 类似数据库表
            storeName: "demoStore",
            // mainKey: 'id', // 主键,默认为id
            // indexs: []
        },
        { // 类似数据库表
            storeName: "imageList",
            // mainKey: 'id', // 主键,默认为id
            indexs: [{
                name: "parentid", // 索引名称
                keyPath: "parentid", // 索引字段
                params: {
                    unique: false,
                }
            }]
        }
    ]
})

IndexdbStore对象包含4个属性

  • name
  • version
  • stores

上面3个属性已在上方列出

  • db (IndexedDB对象,无需使用)

IndexdbStore对象包含10个数据库操作函数

functionNamedesciption(描述)params(参数)
addItem新增数据,自动添加addtime和addtimeformat字段,会自动添加[mainKey](主键)字段storeName, data
addBatch批量新增数据,自动添加addtime和addtimeformat字段,会自动添加[mainKey](主键)字段storeName, list
updateItem修改数据,会自动添加updatetime和updatetimeformatstoreName, data
updateBatch批量修改数据,会自动添加updatetime和updatetimeformatstoreName, list
delItem删除数据,id是主键值,不一定是idstoreName, id'
delBatch批量删除数据,id是主键值,不一定是idstoreName, ids
getAll查询表中所有数据storeName, sortParams = []
queryAll条件查询数据storeName, params, sortParams = []
queryPage条件查询分页数据storeName, params, sortParams, page = 1, pagesize = 10
rebuild数据库动态升级重构,当name,version,stores修改后调用,options同构造函数参数,但可为空,默认使用当前参数options

代码打包工具rollup

1.0.1

1 year ago

1.0.0

1 year ago