@kbox/indexed-db

1.0.4 • Public • Published

indexed-db

An indexedDB wrapper for accessing indexedDB as a promise base api implementation.

Features

  • Create structure of database with indexes and version
  • Get model instances and query builder for both indexed columns and non indexed columns

Install

npm install @kbox/indexed-db
yarn add @kbox/indexed-db

Usage

import IndexedDb from "@kbox/indexed-db";

// 数据模型
const infoModel = {
 "name": "info",
 "columns": {
     "id": { "type": "int", "primary": true, "comment": "序号" },
     "name" : { "type": "string", "nullable": false, "comment": "名称" },
     "guid" : { "type": "string", "nullable": false, "unique": true, "default": "uuid", "comment": "唯一编号" },
     "size" : { "type": "int", "nullable": false, "defualt": 0, "comment": "文件大小" },
     "content" : { "type": "string", "nullable": false, "comment":"内容" },
     "is_deleted": { "type": "int", "nullable": false, "default": 0, "comment": "是否删除" },
     "is_disabled": { "type": "int", "nullable": false, "default": 0, "comment": "是否禁用" },
     "create_time" : { "type": "timestamp", "nullable": false, "default": "NOW", "comment": "创建时间" }
 }
}

// 数据集
const data = {
 name: "name",
 size: 10,
 content: "content"
};

// 创建数据库对象并初始化数据库模型
let db = new IndexedDb({
 namespace: "info_db",
 version: 1,
 models: [ infoModel ]
});

// 添加一条记录
await db.collection("info").insertOne(data).catch(err => console.error(err, data));

// 添加多条记录
await db.collection("info").insertMutil([data, data, data]).catch(err => console.error(err, data));

// 获取记录数量
await db.collection("info").count().catch(err => console.error(err, data));

// 查询编号为1的一条记录(没有用到索引,效率较低)
await db.collection("info").findOne({id: 1}).catch(err => console.error(err, data));

// 查询编号为1的一条记录(用到索引,效率较高)
await db.collection("info").findOneByKey(1).catch(err => console.error(err, data));

// 查询名称为“name”的一条记录(没有用到索引,效率较低)
await db.collection("info").findOne({name: "name"}).catch(err => console.error(err, data));

// 查询名称为“name”的所有记录(没有用到索引,效率较低)
await db.collection("info").find({name: "name"}).catch(err => console.error(err, data));

// 查询名称为“name”的所有记录(用到索引,效率较高)
await db.collection("info").findOneByUKey("name", "name").catch(err => console.error(err, data));

// 更新编号为1的记录
let updateData = { id:1, name: "new_name",  size: 100, content: "new_content"}
await db.collection("info").updateOne(updateData).catch(err => console.error(err, updateData));

// 删除编号为1的记录
await db.collection("info").removeOne(1).catch(err => console.error(err, data));

// 清空所有记录
await db.collection("info").truncate().catch(err => console.error(err, data));

// 删除数据库
await db.remove().catch(err => console.error(err));

/****************
 * 
 * 高级查询
 * 
 ****************/

// 集合记录的高级查询方法,用于获取集合的记录。
// 获取全部记录
await db.collection("info").queryVlues().catch(err => console.error(err, data));
// 获取所有符合指定主键或 IDBKeyRange 的记录
await db.collection("info").queryVlues(query).catch(err => console.error(err, data));
// 指定获取记录的数量
await db.collection("info").queryVlues(query, count).catch(err => console.error(err, data));


// 集合记录的高级查询方法,用于获取所有符合条件的主键。
// 获取所有记录的主键
await db.collection("info").queryKeys().catch(err => console.error(err, data));
// 获取所有符合条件的主键
await db.collection("info").queryKeys(query).catch(err => console.error(err, data));
// 指定获取主键的数量
await db.collection("info").queryKeys(query, count).catch(err => console.error(err, data));

Testing

npm run test

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i @kbox/indexed-db

Weekly Downloads

0

Version

1.0.4

License

MIT

Unpacked Size

18.9 kB

Total Files

8

Last publish

Collaborators

  • zhaozhao200295