0.6.0 • Published 3 months ago

extra-disk-store v0.6.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

extra-disk-store

A disk-based persistent store.

Install

npm install --save extra-disk-store
# or
yarn add extra-disk-store

Limitations

This library uses lmdb as the storage layer for performance reasons. Unfortunately, lmdb only supports keys of limited size (1978 bytes), so make sure you only use this library for scenarios with limited key size.

Usage

import { DiskStore } from 'extra-disk-store'

const store = new DiskStore('/tmp/store')
await store.set('key', Buffer.from('value'))
const value = store.get('key')

API

DiskStore

class DiskStore {
  constructor(dirname: string)

  close(): Promise<void>

  has(key: string): boolean
  get(key: string): Buffer | undefined
  set(key: string, value: Buffer): Promise<void>
  delete(key: string): Promise<void>
  clear(): Promise<void>

  keys(): IterableIterator<string>
}

DiskStoreWithCache

interface ICache {
  set(key: string, value: Buffer | false): void
  get(key: string): Buffer | false | undefined
  delete(key: string): void
  clear(): void
}

class DiskStoreWithCache {
  constructor(
    store: DiskStore
  , cache: ICache
  )

  close(): Promise<void>

  has(key: string): boolean
  get(key: string): Buffer | undefined
  set(key: string, value: Buffer): Promise<void>
  delete(key: string): Promise<void>
  clear(): Promise<void>

  keys(): IterableIterator<string>
}

DiskStoreView

interface IKeyConverter<T> {
  toString: (value: T) => Awaitable<string>
  fromString: (value: string) => Awaitable<T | undefined>
}

interface IValueConverter<T> {
  toBuffer: (value: T) => Awaitable<Buffer>
  fromBuffer: (value: Buffer) => Awaitable<T>
}

class DiskStoreView<K, V> {
  constructor(
    store: DiskStore | DiskStoreWithCache
  , keyConverter: IKeyConverter<K>
  , valueConverter: IValueConverter<V>
  ) {}

  has(key: K): Promise<boolean>
  get(key: K): Promise<V | undefined>
  set(key: K, value: V): Promise<void>
  delete(key: K): Promise<void>
  clear(): Promise<void>

  keys(): IterableIterator<K>
}

Converters

IndexKeyConverter

class IndexKeyConverter implements IKeyConverter<number>

JSONKeyConverter

class JSONKeyConverter<T> implements IKeyConverter<T>

JSONValueConverter

class JSONValueConverter<T> implements IValueConverter<T> {
  constructor(encoding: BufferEncoding = 'utf-8')
}

PassthroughKeyConverter

class PassthroughKeyConverter implements IKeyConverter<string>

PassthroughValueConverter

class PassthroughValueConverter implements IValueConverter<Buffer>

PrefixKeyConverter

class PrefixKeyConverter<T> implements IKeyConverter<T> {
  constructor(keyConverter: IKeyConverter<T>, prefix: string)
}

LZ4ValueConverter

class LZ4ValueConverter<T> implements IValueConverter<T> {
  constructor(valueConverter: IValueConverter<T>)
}

ZstandardValueConverter

class ZstandardValueConverter<T> implements IValueConverter<T> {
  constructor(
    valueConverter: IValueConverter<T>
  , level: number
  )
}
0.6.0

3 months ago

0.5.1

11 months ago

0.5.0

1 year ago

0.4.0

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago