# @kbox/indexed-db

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

Latest version **1.0.4** (published 2021-11-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @kbox/indexed-db
pnpm add @kbox/indexed-db
yarn add @kbox/indexed-db
bun add @kbox/indexed-db
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2021-11-09 |
| First published | 2021-11-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 18.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | JunzhaoSun |
| Maintainers | zhaozhao200295 |
| Keywords | indexed, db, indexeddb |

## Links

- npm: https://www.npmjs.com/package/@kbox/indexed-db
- Repository: git@code.innev.cn:innev-lab/indexed-db
- Issues: http://code.innev.cn/innev-lab/indexed-db/issues
- npm.io page: https://npm.io/package/@kbox/indexed-db

## Dependencies (4)

- [mocha](https://npm.io/package/mocha.md) ^8.2.1
- [should](https://npm.io/package/should.md) ^13.2.3
- [@babel/register](https://npm.io/package/@babel/register.md) ^7.12.1
- [source-map-support](https://npm.io/package/source-map-support.md) ^0.5.19

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.0.4 (latest) — 2021-11-09
- 1.0.3 — 2021-11-09
- 1.0.2 — 2021-11-09
- 1.0.1 — 2021-11-09

## README

# 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

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

## Usage

```javascript
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](LICENSE)

---
_Source: https://npm.io/package/@kbox/indexed-db · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
