flash-store
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

flash-store

Powered by SQLite Powered by LevelDB Powered by RocksDB Powered by TypeScript

NPM Version npm (next) GitHub Action TypeScript Downloads node

FlashStore is Key-Value persistent storage with easy to use ES6 Map-like API(both Async and Sync support), powered by LevelDB and TypeScript.

flash store

Requirements

  1. Node.js v10 or above

Examples

Try the following command

npm install
npm run demo

The basic function as follows:

import { FlashStore } from 'flash-store'

const flashStore = new FlashStore('flashstore.workdir')

await flashStore.set(1, 'a')
console.log(`key: 1, value: ${await flashStore.get(1)}`)
// Output: 'a'

await flashStore.del(1)
console.log(`key: 1, value: ${await flashStore.get(1)}`)
// Output: undefined

Supported Backend

Backend Flash Store Install NPM Command
LevelDB v1.0 npm install flash-store@1
SQLite v0.20 npm install flash-store@0.20
Medea v0.18 npm install flash-store@0.18 (deprecated)
SnapDB v0.16 npm install flash-store@0.16 (deprecated)
RocksDB v0.14 npm install flash-store@0.14
LevelDB v0.12 npm install flash-store@0.12

SnapDB & Medea were all deprecated because of lots of unknown bugs.

API Reference

FlashStore

FlashStore implements the Standard ES6 Map API with Async modification, powered by async-map-like

/**
 * ES6 Map API with Async
 */
export interface AsyncMap<K = any, V = any> {
  [Symbol.asyncIterator]() : AsyncIterableIterator<[K, V]>
  size                     : Promise<number>

  clear   ()                 : Promise<void>
  delete  (key: K)           : Promise<void>
  entries()                  : AsyncIterableIterator<[K, V]>
  get     (key: K)           : Promise<V | undefined>
  has     (key: K)           : Promise<boolean>
  keys    ()                 : AsyncIterableIterator<K>
  set     (key: K, value: V) : Promise<void>
  values  ()                 : AsyncIterableIterator<V>
}

class FlashStore<K, V> implements AsyncMap<K, V> {}

FlashStoreSync

FlashStoreSync implements the Standard ES6 Map API:

class FlashStoreSync<K, V> implements Map<K, V> {}
  1. You get a sync API at the cost of all your data have to be kept in memory.
  2. The data will be async writing back to disk for persistent storage in background.
  3. The performance of FlashStoreSync can be expected high because it's all in memory.

Document

See auto generated docs

See Also

  1. Node.js databases: an embedded database using LevelDB
  2. How to Cook a Graph Database in a Night - LevelGraph
  3. Graph database JS style for Node.js and the Browser. Built upon LevelUp and LevelDB.
  4. 浅析 BigTable 和 LevelDB 的实现

Known Issues

  1. The gte and lte in options do not work property. (#4)

Version History

master

v1.0 (Aug 16, 2021) LevelDB v7.0

Powered by LevelDB

LevelDB v7.0.0 support.

v0.20 Apr 2020 SQLite as Backend

  1. We hardcoded the key type to be string only in this version.
  2. We decide to try better-sqlite3 as it claim is very fast.
  3. The other alternates (would love to try in the future if necessary):
    1. TypeScript: sqlite
    2. WebAssembly: sql.js

v0.18 Feb 2019 - Medea as Backend

Powered by Medea

DEPRECATED: Due to #79 #74 and also it is very unstable in production as my experiences. (e.g. memory leak & block event loop)

  1. Switch from SnapDB to MedeaDown

Medea is a persistent key-value storage library that runs everywhere Node runs.

"It is a pure JS implementation of leveldown and it's almost as fast." — @Raynos link

"The LevelDOWN-compatible wrapper for Medea really opens up the possibility to reuse the modules that have already been created around the LevelUP ecosystem." — @kevinswiber link

Known Issues: FlashStore 会写满磁盘的问题 #155

async function compact (store: FlashStore): Promise<void> {
  await store.size
  const db = (store as any).levelDb.db.db.db
  await new Promise((resolve, reject) => {
    db.compact((err: any) => {
      if (err) {
        return reject(err)
      }
      resolve()
    })
  })
}

v0.16 May 2019 - SnapDB as Backend

Powered by SnapDB

  1. Switch from RocksDB to SnapDB #45
  2. #50 has been fixed. WARN: Do not use this version because it has known issues

v0.14 May 2019 - RocksDB as Backend

  1. Switch from LevelDB to RocksDB #34

v0.12 Jan 2019 - LevelDB as Backend

  1. Use LevelDB as backend to skip the compiling when install.
  2. Using leveldb official typings from @types/

v0.7 Aug 2018 - Nosql-LevelDB as Backend

  1. Use nosql-leveldb as backend to prevent segfault.

v0.6 Jul 2018

  1. Upgrade to TypeScript 3.0

v0.4 Jun 2018

1. Refactor API to implement ES6 Map interface

  1. Update the API to ES6 Map-like, the difference is that FlashStore is all async.

2. Add FlashStoreSync as a in-memory Write-back Cache for Flash-Store

Add a new class FlashStoreSync which is a in-memory full loaded Write-back Cache for Flash-Store:

  1. Writes directly to cache, lazy writes to store.
  2. Reads from cache, never read-miss because cache have the full data of the store which will never expire.
  3. API of FlashStoreSync is the same as the ES6 Map

v0.2 Sep 2017

Init version, API is LevelDB-like.

FAQ

Q: What's the difference between the flash-store and memory-card

Short answer:

  1. flash-store is for save data to local filesystem.
  2. memory-card is for save data to a distributed network storage, it can be serialized/deserialized safely by design.

Long answer:

flash-store and memory-card are all built by @huan, and they are all follow the ES6 Map API.

flash-store is using a no-sql local file database to maximum the performance, it can be used as a local database, or a local cache for whatever you want to cache from other API.

memory-card is using a local file to store data in JSON format by default, however, it supports more distributed methods. Learn more from it's repository at here

Author

Huan LI (李卓桓) zixia@zixia.net

Profile of Huan LI (李卓桓) on StackOverflow

Copyright & License

  • Code & Docs © 2017-now Huan (李卓桓) <zixia@zixia.net>
  • Code released under the Apache-2.0 License
  • Docs released under Creative Commons

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.3.5639next
1.0.655latest

Version History

VersionDownloads (Last 7 Days)Published
1.3.5639
1.3.44
1.3.31
1.1.72
1.1.51
1.1.41
1.1.31
1.1.21
1.1.11
1.0.655
1.0.51
1.0.41
1.0.32
1.0.21
1.0.11
0.12.1111
0.20.718
0.20.61
0.20.51
0.20.41
0.20.31
0.20.21
0.20.11
0.19.21
0.19.11
0.18.321
0.16.201
0.14.54
0.7.105
0.12.72
0.7.91
0.18.211
0.18.201
0.18.181
0.18.171
0.18.161
0.18.151
0.18.141
0.18.121
0.18.111
0.18.102
0.18.81
0.18.71
0.18.61
0.18.41
0.16.141
0.16.132
0.16.111
0.16.61
0.16.51
0.16.41
0.16.31
0.16.21
0.16.11
0.15.71
0.15.61
0.15.51
0.15.31
0.7.21
0.15.11
0.14.11
0.13.41
0.13.31
0.13.21
0.13.11
0.12.11
0.11.91
0.11.81
0.11.71
0.11.61
0.11.51
0.11.41
0.11.31
0.9.21
0.6.61
0.6.51
0.6.41
0.6.31
0.6.21
0.6.11
0.5.91
0.5.81
0.5.71
0.5.41
0.5.01
0.4.51
0.4.41
0.4.31
0.4.21
0.4.11
0.3.81
0.3.71
0.3.61
0.3.51
0.3.21
0.3.11
0.2.11
0.1.91
0.1.81
0.1.72
0.1.51
0.1.41
0.1.31
0.1.21
0.0.191
0.0.181
0.0.171
0.0.161
0.0.151
0.0.141
0.0.131
0.0.121
0.0.111
0.0.101
0.0.91
0.0.81
0.0.71
0.0.62
0.0.51
0.0.41
0.0.31
0.0.21
0.0.11

Package Sidebar

Install

npm i flash-store

Weekly Downloads

751

Version

1.0.6

License

Apache-2.0

Unpacked Size

240 kB

Total Files

42

Last publish

Collaborators

  • zixia