2.4.3 • Published 5 years ago
quick-storage v2.4.3
QuickStorage
Simple key/value storage module with persistency on file system.
This module used to work in the browser with IndexedDB, more info can be found here https://github.com/BonneVoyager/quick-storage/tree/v1.4.0.
Installation
npm install --save quick-storageUsage
QuickStorage expects an argument - a path to store data in the file system (otherwise, it will throw).
import QuickStorage from 'quick-storage'
const myStorage = new QuickStorage(`${__dirname}/data`)This will create ${__dirname}/data directory and store data in it. Each data key will be stored in separate file (key myKey will be store in file ${__dirname}/data/myKey). File content is stringified on write, and parsed on read.
API
myStorage.set('myKey', { foo: 'bar' })myStorage.has('myKey') // truemyStorage.get('myKey') // { "foo": "bar" }myStorage.keys() // [ "foo" ]myStorage.delete('myKey')myStorage.isReady // false before onReady callback, and true afterwardsmyStorage.onReady((fn) => {
  console.info('All previous data was read and I am ready for some work!')
})myStorage.onError((err) => {
  console.error('My storage error:', err)
})const obj = { foo: "bar" }
myStorage.proxy(obj, {
  preventExtensions: true, // whether to invoke Object.preventExtensions(obj)
  persistProps: [ 'foo' ]  // props which should keep the persistency
})Few tips
- please keep in mind that this module is intended to be used with small chunks of data (up to dozens of megabytes). All the data is stored in memory with Mapcache object.
- data is parsed between string and json. That means that this module works only with JSON objects.
- when you create QuickStorage(__dirname + '/data'), the script will try to find and read data associated with__dirname + '/data'(read FS on the server). After it's read, storage object will callonReadyfunction.
- you can use a callback as a second argument for getfunction to force read the data from the FS, instead ofMapcache object.
Test
npm run test