@adiwajshing/keyed-db

中文文档
0.2.4发布于 2020/12/24MITTypeScriptCommonJS零依赖近 30 天下载 24万

Lightweight library to store an in-memory DB

原始使用文档
文档目录5 节
  1. Install
  2. Running Tests
  3. Functions
  4. Usage
  5. Time Complexity

Keyed DB

A light-weight node library to manage a sorted & indexed collection with pagination support. All done using Binary Search. Based off my swift code for Queenfisher

Install

npm i github:adiwajshing/keyed-db

Running Tests

npm test

Functions


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)

Usage

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()) 

Time Complexity

OperationTime Complexity
db.insert()O(logN)
db.delete()O(logN)
db.get()O(1)
db.updateKey()O(logN)

版本与发布记录

@adiwajshing/keyed-db 共发布 14 个版本,首次发布于 2020/07/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.4latest2020/12/245 年前
0.2.32020/12/185 年前
0.2.22020/12/185 年前
0.2.12020/12/185 年前
0.2.02020/12/075 年前
0.1.82020/10/046 年前

关于 @adiwajshing/keyed-db 的常见问题

@adiwajshing/keyed-db 是什么?

Lightweight library to store an in-memory 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 和提交记录判断。