@e280/kv

中文文档
0.2.6发布于 2026/11/03MITTypeScriptES Module构建来源已验证近 30 天下载 2409

tiny run-anywhere key-value database

原始使用文档
文档目录4 节
  1. 🪇 kv is easy.
  2. 🪇 plug in your favorite kv magazine.
  3. 🪇 kv scopes.
  4. 🪇 more kv methods.

🪇 kv

tiny key-value storage library.
typescript. node or web. memory, leveldb, localstorage, or indexeddb. scoped namespaces and atomic write batches. pass scoped and typed kv instances around to your app components, they don't need to worry about where the data actually lives.

npm install @e280/kv
import {Kv} from "@e280/kv"

🪇 kv is easy.

  • make your kv.
    const kv = new Kv()
    
  • set and get stuff.
    await kv.set("penguins", 123)
      // setting undefined is the same as delete
    
    await kv.get("penguins")
      // 123
    
  • keys are strings. values can be any structured data.
    await kv.set("hello", {alpha: 123, bravo: ["bingus"]})
    
  • commit batches of ops, atomically.
    await kv.commit([
      kv.op.set("pangolins", 100),
      kv.op.delete("bingus"),
    ])
    

🪇 plug in your favorite kv magazine.

  • MemoryMagazine (default), ephemeral in-memory storage.

    memory magazine is slow and non-atomic. it's meant for testing.

    import {Kv, MemoryMagazine} from "@e280/kv"
    
    const kv = new Kv(new MemoryMagazine())
    
  • LevelMagazine, nodejs on-disk leveldb.
    import {Level} from "level"
    import {Kv, LevelMagazine} from "@e280/kv"
    
    const level = new Level("./kv")
    const kv = new Kv(new LevelMagazine(level))
    
  • IdbMagazine, in-browser indexedDB storage.
    import {Kv, IdbMagazine, idbOpen} from "@e280/kv"
    
    const idb = await idbOpen("kv")
    const kv = new Kv(new IdbMagazine(idb))
    
  • StorageMagazine, in-browser localStorage/sessionStorage.

    storage magazine is slow and non-atomic. it's meant for small amounts of data.

    import {Kv, StorageMagazine} from "@e280/kv"
    
    const kv = new Kv(new StorageMagazine(localStorage))
    
  • write your own magazine, you won't believe how easy it is.
    import {Magazine, Op, Scan, Value} from "@e280/kv"
    
    // three methods and you're done!
    export class MyMagazine implements Magazine {
      async commit(ops: Op<Value>[]): Promise<void> {/*...*/}
      async getMany(keys: string[]): Promise<(unknown | undefined)[]> {/*...*/}
      async* entries(scan?: Scan): AsyncIterable<[key: string, value: unknown]> {/*...*/}
    }
    
    see magazines/memory.ts for inspiration.

🪇 kv scopes.

  • scope makes namespaced Kv instances.
    const users = kv.scope("users")
    const messages = kv.scope("messages")
    
    await users.set("111", "chase")
    await messages.set("222", ["111", "yo"])
    
    await kv.get("111") // undefined
      // 👮 parent is blind to child entries
    
    await users.get("222") // undefined
      // 👮 child is blind to sibling and parent entries
    
  • scopes are nestable, and it's turtles all the way down.
    kv.scope("animals").scope("turtles")
    
    // 🥸 equivalent
    kv.scope("animals", "turtles")
    
    // 🙅 illegal: empty strings, reserved characters "." and ":"
    kv.scope("", "e280.org", "e280:org")
    
  • all kv operations are isolated to their own scope.

    the root kv is a scope like any other.

    const animals = kv.scope("animals")
    const turtles = animals.scope("turtles")
    const squirrels = animals.scope("squirrels")
    
    await animals.clear()
      // 👮 turtles and squirrels are safe
    
    await squirrels.clear()
      // 👮 turtles are safe
    
  • ☣️ subtree is dangerous, it allows a parent scope to hurt its children.

    it returns a special Subtree instance that only has count and clear methods.

    await animals.subtree.count()
      // count includes animals, turtles, and squirrels
    
    await animals.subtree.clear()
      // 💀 wipes out all animals, turtles, and squirrels. i'm sorry.
    
  • don't forget you can set strict types, on both Kv and scopes.
    const kv = new Kv<unknown>()
    const users = kv.scope<string>("users")
    const messages = kv.scope<[author: string, text: string]>("messages")
    
  • 🍋‍🟩 commits can be cross-scoped, don't miss this!
    await kv.commit([
      users.op.set("345", "bingus"),
      messages.op.set("456", ["345", "don't let the raccoons know"]),
    ])
    

🪇 more kv methods.

  • delete a pair by its key.
    await kv.delete("hello")
    
    you can also pass multiple keys.
    await kv.delete("123", "234", "345")
    
  • has checks whether a key exists.
    await kv.has("hello")
      // true
    
  • setMany sets many key-value pairs at once.
    await kv.setMany([["1", "alpha"], ["2", "bravo"]])
    
  • getMany retrieves many values at once.
    const values = await kv.getMany(["alpha", "bravo"])
      // [123, undefined]
    
  • need retrieves a value, or throws if the value is missing/nullish.
    const value = await kv.need("hello")
      // "world" (or throws error)
    
  • needMany retrieves many values, or throws on missing/nullish values.
    const values = await kv.needMany(["alpha", "bravo"])
      // [123, 234] (or throws error)
    
  • entries loops over key-value pairs.
    for await (const [key, value] of kv.entries())
      console.log(key, value)
    
    it's aliased to Symbol.asyncIterator, so you can do this:
    for await (const [key, value] of kv)
      console.log(key, value)
    
    the entries method accepts scan options.
    for await (const [key, value] of kv.entries({
        limit: 100,
        reverse: false,
        start: "alpha", // inclusive
        end: "omega", // exclusive
      }))
      console.log(key, value)
    
    💡 you can use collect helper from @e280/stz to get entries/keys/values as an array:
    import {collect} from "@e280/stz"
    
    const entries = await collect(kv)
      // [["123", "alpha"], ["234", "bravo"]]
    
    const keys = await collect(kv.keys())
      // ["123", "234"]
    
  • keys and values. (accepts scan options)
    for await (const key of kv.keys()) console.log(key)
    for await (const value of kv.values()) console.log(value)
    
  • count the number of entries in this scope. (accepts scan options)
    await kv.count()
      // 123
    
  • clear deletes everything in this scope. (accepts scan options)
    await kv.clear()
    
  • cell makes a little cubby, for storing a single value.

    (it implements @e280/stz's Cubby type)

    const muffins = kv.cell<number>("muffins")
    
    await muffins.set(99)
    
    await muffins.has() // true or false
    await muffins.get() // number or undefined
    await muffins.need() // number or throws error
    
    await muffins.delete()
    
    you can pass typed Cell<X> instances all around your app.
    import {Cell} from "@e280/kv"
    
    class MuffinCaptain {
      constructor(public muffins: Cell<number>) {}
    }
    



https://e280.org/

版本与发布记录

@e280/kv 共发布 35 个版本,首次发布于 2025/04/23;过去 12 个月发布了 20 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.6latest2026/11/0310 天前
0.2.52026/11/0211 天前
0.2.42026/10/1727 天前
0.2.32026/10/131 个月前
0.2.22026/10/101 个月前
0.2.12026/09/052 个月前

关于 @e280/kv 的常见问题

@e280/kv 是什么?

tiny run-anywhere key-value database

如何安装 @e280/kv?

在项目目录运行 npm install @e280/kv,或使用 pnpm add / yarn add / bun add。

@e280/kv 支持 TypeScript 吗?

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

@e280/kv 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@e280/kv 可以商用吗?

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

@e280/kv 的下载量有多少?

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

@e280/kv 还在维护吗?

最新版本 0.2.6 发布于 2026/11/03(10 天前);至今共发布 35 个版本,其中过去 12 个月发布了 20 个;首次发布于 2025/04/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。