Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-kv-storage

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-kv-storage

Wrapper around node-persist that conforms to the std:kv-storage interface

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

Node KV Storage

Wrapper around node-persist that implements the std:kv-storage interface.

Why

  • Standardized API
  • Easier code sharing with frontend
  • No await init()

How

const storage = require('node-kv-storage')

;(async () => {
  await storage.set('test', { a: 3 })
  console.log(await storage.get('test'))

  // Compatible with node-persist for string and binary keys
  await require('node-persist').init()
  await require('node-persist').setItem('foo', { b: 4 })
  console.log(await storage.get('foo'))

  await require('node-persist').setItem(Buffer.from([0x1, 0x2]), { d: 6 })
  console.log(await storage.get(Buffer.from([0x1, 0x2])))
  
  // Also allows Number keys
  await storage.set(3, { f: 8 })
  console.log(await storage.get(3))

  // Also allows Date keys
  const d = new Date(0)
  await storage.set(d, { c: 5 })
  console.log(await storage.get(d))

  // Use multiple storage areas
  const { StorageArea } = storage
  const other = new StorageArea('other_area')
  await other.set('test', { i: 11 })
  console.log(await other.get('test'))
})()

API

Limitations

  • Since there is no IndexedDB in node, backingStore is not implemented.
  • Using typed arrays as keys works, but when using keys() or entries() they will be returned in the form JSON.parse(JSON.stringify(yourTypedArray)). Typically this will look like { '0': 255, '1': 255, ... }.
  • Storage areas with the name default and storage will both map to the directory storage.

Questions

How do I change the storage directory?

node-persist provides the dir option to specify the directory in which to write files. The KV Storage proposal intended for browsers has no such option, and for compatibility this library doesn't provide this option either. In my opinion, providing the file path as configuration option is bad idea anyway, instead node-kv-storage takes inspiration form the Twelve Factor App takes the base directory from the environment variable NODE_KV_STORAGE_DIR.

How do I change the serialization / deserialization ?

Currently not implemented

FAQs

Package last updated on 08 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc