npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@liwenxiang/indexdb

v1.0.4

Published

一个简易的支持Promise的indexdb工具包

Readme

Index_db_crud

介绍

支持Promise 便捷简单的 Indexdb 增删改查 的工具包 使用简单 减少很多操作 Indexdb 的复杂度 API 即调即用 减少大量重复性代码

安装

npm install --save @liwenxiang/indexdb

npm 引入后使用 ES6模块方式引入

调试方式

IndexDb 在 控制台 F12 后的 Application 面板中 左侧导航找到 IndexDB 即可调试数据

使用

实例化 IndexDb 类得到 db 实例 参数传递一个数据库的名字

内部数据库的版本号 存储在了 localStorage 中 IndexDb 规定每次创建新表或者添加内容更新内容都要打开数据库并且要有更新的版本号.. 但是只有在新增表格的时候版本号才会实际的进行增加 在 本地存储 查看 以表名命名的版本数字ID

const db = new IndexDb("animal"); 

API

db.isSupportIndexDb()

返回值

  • IDBOpenDBRequest 对象

判断是否支持IndexDB 如果支持则顺带顺带创建数据库 内部调用 使用者无需关心

db.createDatabase()

返回值

  • Promise
    • 可以通过 then 和 catch 判断是否创建成功

实例:

db.createDatabase().then(e=>{
   console.log("create success !!!")
});

创建数据库

db.createTable(tableName,[primaryKeyOptions = {keyPath: "id", autoIncrement: false },indexOptions={{options:{unique: false}}}])

参数

  • tableName 表名
  • primaryKeyOptions 主键的配置项
    • keyPath 主键的字段名 默认就是 id 如果不修改 每次添加数据会自动生成一个标识值 添加时此字段不填 如果修改了 在之后的API中需要做对应参数的传入进行区分 后面用到了会讲到
    • indexOptions 创建索引 在创建表的同时给表增加一个索引字段 以后可以通过专属的方法进行数据的查找
      • fieldName 索引名
      • unique 默认是 false 表示是否不重复 false 表示可能会重复

返回值

  • Promise 可以通过 then 和 catch 判断是否创建成功

实例:

db.createTable("pet",{keyPath:"id"},{
   fieldName:"name",
   options:{
       unique:false
   }
}).then(e=>{
   console.log("create pet success !!!")
});

创建表格 如果表已存在则报错

db.add(tableName,data,customPrimaryKey)

参数

  • tableName 表名
  • data 添加的数据 这里的数据添加很自由 就是一个对象
  • customPrimaryKey 是否自定义了主键字段 在创建表的时候指定 默认 id 会自动生成一个随机的标识值 如果自定义了主键这里需要填 true 并且 data 要带上您自定义的主键字段 保证每一次的值是唯一即可

返回值

  • Promise 可以通过 then 和 catch 判断是否创建成功 并拿到添加的数据

实例:

db.add("pet",{name:"Jack",hobby:["piano","basketball","table tennis","violin"]})
.then(success=>{
   console.log(success);
})

添加数据 保持主键唯一

db.commonPart(tableName,isWrite)

内部调用 使用者不用关心此API

公共调用部分

db.selectKey(tableName,primaryKey)

参数

  • tableName 表名
  • primaryKey 主键值

返回值

  • Promise 可以通过 then 和 catch 判断是否创建成功 并拿查询的数据

实例:

db.selectKey("pet","884ceecf8").then(e=>{
   console.log(e)
});

根据主键查询

db.selectIndex(tableName,indexName,indexFieldValue)

参数

  • tableName 表名
  • indexName 索引名
  • indexFieldValue 索引值

返回值

  • Promise 可以通过 then 和 catch 判断是否创建成功 并拿查询的数据

实例:

db.selectIndex("pet","name","Nick").then(e=>{
   console.log(e)
});

根据索引查询数据

db.selectAll(tableName)

参数

  • tableName 表名

返回值

  • Promise 可以通过 then 和 catch 判断是否创建成功 并拿查询的数据

实例:

db.selectAll("pet").then(e=>{
   console.log(e)
});

根据表名查询所有

db.delete(tableName,indexFieldData)

参数

  • tableName 表名
  • indexFieldData 主键字段值

返回值

  • Promise 可以通过 then 和 catch 判断是否创建成功 并拿到删除的这条数据

db.put(tbName,updateData)

参数

  • tabName 表名
  • updateData 要更新的数据 记得把主键给加上进行匹配

实例:

 db.put("pet",{
     id:"884ceecf8",
     name:"Work",
     age:100
 }) .then(r=>{
     console.log(r)
 })

修改数据