项目作者: msiviero

项目描述 :
Simple on disk key vale db
高级语言: TypeScript
项目地址: git://github.com/msiviero/kv-storage.git
创建时间: 2020-04-27T07:11:42Z
项目社区:https://github.com/msiviero/kv-storage

开源协议:Apache License 2.0

下载


KV Storage

> A simple on disk key value storage for nodejs

Description

The implementation is done using an append only log where every value is stored in binary format and the keys are pointers to disk segments,
similar to the bitcask engine, so the keys should fit in memory.

Installation

  1. npm i @msiviero/kv-storage

Usage

Basic usage

  1. import { FileSystemStorage } from "@msiviero/kv-storage";
  2. interface User {
  3. email: string;
  4. id: number;
  5. }
  6. // create a store. The directory will be created if doesn't exist
  7. const storage = await FileSystemStorage.create<User>("directory to store data in");
  8. await storage.put("mykey", {
  9. id: 1,
  10. email: "user@example.com",
  11. });
  12. /*
  13. The result contains the stored data in the .data field, and a
  14. metadata object containing:
  15. - checksum: an md5 checksum of the stored buffer
  16. - timestamp: timestamp of insertion
  17. - key: the original key
  18. */
  19. const result = await storage.get("mykey");
  20. const user = result?.data;

Log compaction

The log compaction logic is left to the user, as it’s different based on the application. To compact the log call the compact() method

  1. await storage.compact();

Iterate records

  1. // Using an async generator
  2. for await (const it of storage.iterator()) {
  3. // do something with the record
  4. console.log(it);
  5. }
  6. // using a stream
  7. storage
  8. .stream()
  9. .on("data", (result: Readonly<Result<User>>) => console.log(result))
  10. .on("end", () => console.log("finished"));