# @adiwajshing/keyed-db 中文文档

规范地址：https://npmcn.com/package/%40adiwajshing/keyed-db/

版本：0\.2\.4
npm 元数据更新时间：2022-04-04T11:31:02.341Z
中文译文更新时间：2026-11-05T17:34:36.947Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40adiwajshing/keyed-db
原文对照：https://npmcn.com/package/%40adiwajshing/keyed-db/?lang=original
项目主页：https://github.com/adiwajshing/keyed-db
源码仓库：https://github.com/adiwajshing/keyed-db

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：17.7 kB，9 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2020-07-31T14:31:43.814Z
- 最新版本发布：2020-12-24T15:14:52.314Z
- 近 30 天下载量：239957（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：14（近 12 个月 0 个）

## 中文简介

存储内存 DB 的轻量级库

## 安装

```sh
npm install @adiwajshing/keyed-db
```

也可以使用：`pnpm add @adiwajshing/keyed-db`、`yarn add @adiwajshing/keyed-db`、`bun add @adiwajshing/keyed-db`

## README

# 已键入 DB

一个轻量级节点库,用来管理一个排序并索引的集合,并配有页码支持. 全部使用二进制搜索完成 。 根据我的快速密码 [皇后渔夫](https://github.com/adiwajshing/Queenfisher)

## 安装

`npm i github:adiwajshing/keyed-db`

## 运行测试

`npm test`

## 职能

```ts

db = new KeyedDB<T> (t => t.uniqueNumberKeyProperty, t => t.optionalUniqueIDProperty)
// compare with a custom function
db = new KeyedDB<T> ({  
    key: t => t.someProperty,
    compare: (t1, t2) => someComputation(t1, t2) // return -1 if t1 < t2, 0 if t1=t2 & 1 if t1 > t2
}, t => t.optionalUniqueIDProperty)

db.insert (value) // insert value in DB
db.upsert (value) // upserts value
db.insertIfAbsent (value) // only inserts if not already present in DB
db.delete (value) // delete value
db.deleteById (value.optionalUniqueIDProperty) // delete value by referencing the ID
// update the key of a value, 
// will automatically place object after key change
db.updateKey (value, value => value.uniqueKeyProperty = newValue) 
db.paginated (someCursor, 20) // get X results after the given cursor (null for the first X results)

```

## 使用量

```ts
import KeyedDB from '@adiwajshing/keyed-db'

// Let's use the db to sort & maintain a list of chats
// Chats must be accessed quickly via the chatID (the person you're chatting with)
// Chats must be sorted by recency
type Chat = {
    timestamp: Date
    chatID: string
}

// first argument -- sorting property, second argument -- ID property
const db = new KeyedDB<Chat>(value => value.timestamp.getTime()*-1, value => value.chatID)

for (let i = 0; i < 1000;i++) {
    // insert data
    db.insert (
        {
            timestamp: new Date( new Date().getTime() - Math.random()*10000 ), 
            chatID: `person ${i}`
        }
    )
}
console.log (db.all()) // return internal sorted array
console.log (db.paginated(null, 20)) // return first 20 chats
console.log (db.paginated(null, 20, null, 'before')) // return last 20 chats
console.log (db.paginated(null, 20, chat => chat.chatID.includes('something'))) // return first 20 chats where the chatID contains 'something'

const someDate = new Date().getTime()
const cursorPaginated = db.paginated(someDate, 20)
console.log (cursorPaginated) // return 20 chats after the specified date

db.delete (cursorPaginated[0]) // delete paginated chats 

// update chat timestamp
db.updateKey(cursorPaginated[1], value => value.timestamp = new Date().getTime()) 

```

## 时间复杂度

| 运行 | 时间复杂度 |
| --- | --- |
| db. 插入( ) | O( logN) |
| db.delete () (中文(简体) ) | O( logN) |
| db.get () (中文(简体) ) | ○(1) |
| db.updateKey () (中文(简体) ) | O( logN) |

## 常见问题

### @adiwajshing/keyed-db 是什么？

存储内存 DB 的轻量级库

### 如何安装 @adiwajshing/keyed-db？

在项目目录运行 npm install @adiwajshing/keyed-db，或使用 pnpm add / yarn add / bun add。

### @adiwajshing/keyed-db 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @adiwajshing/keyed-db 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @adiwajshing/keyed-db 可以商用吗？

@adiwajshing/keyed-db 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @adiwajshing/keyed-db 的下载量有多少？

根据 npm 官方下载统计，@adiwajshing/keyed-db 在 2026/10/05 – 2026/11/03 这 30 天内被下载 239,957 次，日均约 7,999 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @adiwajshing/keyed-db 还在维护吗？

最新版本 0\.2\.4 发布于 2020/12/24（5 年前）；至今共发布 14 个版本，其中过去 12 个月发布了 0 个；首次发布于 2020/07/31。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

