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 🙏

© 2024 – Pkg Stats / Ryan Hefner

zl-indexdb

v1.2.3

Published

indexDB操作库---提高indexDb的使用效率

Downloads

1

Readme

zl-indexdb

indexDB操作库---提高indexDb的使用效率

安装引入

script方式


    <script src="https://cdn.jsdelivr.net/npm/[email protected]/index.js"></script>
    let IndexDBOpt=window["zl-indexdb"];

import方式

   import IndexDBOpt from "zl-indexdb"

创建或升级indexDB数据库


//------------- 定义数据库信息 -----------------
// indexdb数据库名字
let dbName='myTestdb';

// indexdb数据库版本
let dbVersion=1;

//indexdb数据库对象仓库结构(类似mysql中的数据表集合),objectStores为数组结构,每个数组元素都代表一个对象仓库(类似于mysql中的table)
let objectStores = [ 
{
    objectStoreName: "notes",//表名
    type: 1, //0:表示把已存在的删除,然后重新创建。 1:表示如果不存在才重新创建,2:表示只删除,不再进行重新创建
    keyMode:  { },//为空对象表示:设置主键模式为手动指定(即操作数据时需要手动指定主键)
    indexs: [ // 创建索引信息
        { indexName: "keyIndex", fieldName: "key", only: { unique: false } },//索引名,字段名,索引属性值是否唯一
        { indexName: "emailIndex", fieldName: "email", only: { unique: false } },//索引名,字段名,索引属性值是否唯一
        { indexName: "doc_typeIndex", fieldName: "doc_type", only: { unique: false } },//索引名,字段名,索引属性值是否唯一
    ]
}, 
{
    objectStoreName: "User",//表名
    type: 1,//0:表示把已存在的删除,然后重新创建。 1:表示如果不存在才重新创建,2:表示只删除,不再进行重新创建
    keyMode: { autoIncrement: true,keyPath: 'userId' },//设置主键为 userId ,且自动递增
    indexs: [ // 创建索引信息
        { indexName: "nameIndex", fieldName: "name", only: { unique: false } },//索引名,字段名,索引属性值是否唯一
        { indexName: "ageIndex", fieldName: "age", only: { unique: false } },//索引名,字段名,索引属性值是否唯一
        { indexName: "userIdIndex", fieldName: "userId", only: { unique: true } },//索引名,字段名,索引属性值是否唯一
    ]
}
]
//------------- 开始创建或升级数据库 -----------------
window.DB = new IndexDBOpt(dbName, dbVersion, objectStores); //提示:如果数据库版本需要升级,请刷新下页面在执行升级操作

操作indexDB数据库

(async function () {
//添加数据
//==============创建数据仓库时指定了keypath的模式==============
//添加一条数据(添加的数据中包含了key,或者key会自动生成并自动递增)
let res = await DB.addData(["User"], { name: "zs", age: 18 });

//添加多条数据(添加的数据中包含了key,或者key会自动生成并自动递增)
let res2 = await DB.addData(["User"], [
{ name: "zss1", age: 18 }, 
{ name: "zsd2", age: 18 }, 
{ name: "zs3", age: 18 }, 
{ name: "zsf4", age: 20 }
]);

//==============创建数据仓库时没有指定keypath的模式==============
//添加一条数据(需要手动传入指定的key:thekeyName)
let res = await DB.addData(["User"], { name: "zs", age: 18, thekeyName:"id" });

//添加多条数据(需要手动传入指定的key:thekeyName)
let res2 = await DB.addData(["User"], [
{ name: "zss1", age: 18, thekeyName:"id1"  }, 
{ name: "zsd2", age: 18, thekeyName:"id2" }, 
{ name: "zs3", age: 18 , thekeyName:"id3" }, 
{ name: "zsf4", age: 20 , thekeyName:"id4" }
]);







//修改数据
//======================主键本身就是数据元素中的一个字段:userId===========
//修改单条数据
let res3 = await DB.updateData(["User"], { name: "111", age: 111, userId: 1 });

//批量修改数据
let res4 = await DB.updateData(["User"], [
{ name: "zss111111", age: 180, userId: 1 }, 
{ name: "zss1222222", age: 180, userId: 2 }, 
{ name: "zss1333333", age: 180, userId: 3 }
]);

//======================主键为手动指定的字段thekeyName,不存在于数据仓库结构中===========
//修改单条数据(存在就修改)
 let res3 = await DB.updateData(["User"], { name: "222", age: 222, userId: 2 , thekeyName:"id" } );

//修改单条数据(没有时会创建)
 let res3 = await DB.updateData(["User"], { name: "111", age: 111, userId: 1 , thekeyName:1 } );

//批量修改数据
let res4 = await DB.updateData(["User"], [
{ name: "zss111111", age: 180, userId: 4 , thekeyName:"id1" }, 
{ name: "zss1222222", age: 180, userId: 5 , thekeyName:"id2" }, 
{ name: "zss1333333", age: 180, userId: 6 , thekeyName:"id3" }
]);







//查询数据
// ===========通过keypath向对象仓库(表)查询数据,无参数则查询所有==========
//从User表中查询主键为5的单条数据
let res9 = await DB.queryBykeypath(['User'],5)
console.log("==res9===",res9);
//从User表中查询所有数据
let res10 = await DB.queryBykeypath(['User'])
console.log("==res10===",res10);
//=======通过索引index向对象仓库(表)查询数据,不传查询参数就查询所有数据========
//从User表中查询nameIndex为zs3的单条数据
let res11 = await DB.queryByIndex(['User'],{name:"nameIndex",value:"zs3",uni:true})
console.log("==res11===",res11);

//从User表中查询ageIndex为18的多条数据
let res12 = await DB.queryByIndex(['User'],{name:"ageIndex",value:18})
console.log("==res12===",res12);

//从User表中查询ageIndex为18的多条数据,然后传入where进一步查询
let res13 = await DB.queryByIndex(['User'],{name:"ageIndex",value:18},[{key:"name",value:'zsd2',opt:"=="}])
console.log("==res13===",res13);








// 删除数据
//删除主键为3的数据
let res5 = await DB.deleteData(["User"], [3]);
//删除表的所有数据
let res6 = await DB.deleteData(["User"]);







//关闭数据库链接
//当数据库的链接关闭后,对他的操作就不再有效
let res7 = await DB.close();






//删除数据库
//如果没传参就删除本身数据库,否则删除指定数据库
let res8 = await DB.deleteDataBase();//删除后,可能要刷新下才能看

}())


注意事项说明

  1. 当开始创建或者打开indexDB数据库时,objectStores 会在升级函数里面执行

  2. keypath值示例:{autoIncrement: true,keyPath: 'userId'}

    • autoIncrement: true, 表示设置主键自动递增(默认创建主键字段为key)
    • keyPath: 'userId' 显式的指定主键字段为userId
    • keypath为空对象表示:设置主键模式为每次操作数据时都要手动指定(会生成类似于map结构的键值对数据)
    • 两则字段可以都写上,也可以写一个参数,都写上时表示以指定字段为主键,且如果没传入时对其进行自动递增。
  3. 手动指定行外主键适合某些一个名字代码一些信息的情况如:config:{...} , 手动指定config为主键,他的值为一个配置对象

  4. 自动生成或者指定行内主键则适合类似于mysql那种二维表的那种情况