feathers-offline-log

0.0.1 • Public • Published

feathers-offline-log

Greenkeeper badge

Build Status Code Climate Test Coverage Dependency Status Download Status

| Performant, persistent client-side transaction log.

Browser client logs are kept in (preferred order) IndexedDB, WebSQL or localStorage. Log entries are added in an efficient manner, whether the log is small or very large. JavaScript objects are also handled in an efficient manner.

ProTip: Used for feathers-offline's optimistic mutation log, which is updated while the client is disconnected.

The cache stringifies an added object just once, eliminating the multiple stringifications of alternative designs. The cache bundles logs into chunks, whose maximum size you can specify (0.5 meg default). This avoids both the repetitive persisting of large blobs and the inefficiencies of writing individual log items.

Installation

npm install feathers-offline-log --save

Documentation

  • cache = new Cache(storageHandler, options) - Configure a new cache.
    • storageHandler (required, module) - Module responsible for persistence.
      • /browserStorage - Browser support. See example below.
    • options (optional) - Options for the cache.
      • chunkMaxLen (optional, number, default 500 000) - Maximum chunk size in chars.
      • sep (optional, char, default ',') - Separator char between log entries. Objects are always separated with ','.
  • cache.config(options) - Configure the storage handler for the cache. The options depend on the storage handler.
    • browserStorage - See options for localforage
      • name (optional, string, default 'offline') - Namespace for storage. Typically the app name.
      • collection (optional, string, default 'log') - Namespace within name. Typically the name of the collection or table name.
  • cache.add(str) - Add str to the logs.
  • cache.addObj(obj) - Stringifyy obj and add it to the logs.
  • cache.getOldestChunk() - Returns the oldest chunk of logs. If it consists solely of added objects, then JSON.stringify('[' + chunk + ']') will return an array of POJO.
  • cache.removeOldestChunk() - Remove the oldest chunk from the log.
  • cache.clear() - Remove all chunks.

Example

const browserStorage = require('feathers-offline-log/browser-storage');
const Cache = require('feathers-offline-log');
 
const cache = new Cache(browserStorage, { chunkMaxLen: 500000 }); // logs stored in 0.5 meg chunks
cache.config({ name: 'myApp', collection: 'people' })
  .then(() => cache.addObj(obj1))
  .then(() => cache.addObj(obj2))
  .then(() => cache.addObj(obj3))
 
  .then(() => cache.getOldestChunk()) 
  .then(chunk => console.log(JSON.parse(`[${chunk}]`))) 
  .then(() => cache.removeOldestChunk()) 

The included browser storage abstraction for localforage may be used standalone.

const browserStorage = require('feathers-offline-log/browser-storage');
 
browserStorage.config({ name: 'myApp', collection: 'people' })
  .then(() => browserStorage.setItem('john', ' ... ')) // full key is myApp_people/john
  .then(() => browserStorage.getItem('john'))
  .then(str => browserStorage.removeItem('john'))
  .then(() => browserStorage.clear())
  .then(() => browserStorage.length())
  .then(numb => browserStorage.keys())
  .then(array => browserStorage.iterate((value, key, iterationNumber) => { ... }));

License

Copyright (c) 2017

Licensed under the MIT license.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.0.1
    1
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.0.1
    1

Package Sidebar

Install

npm i feathers-offline-log

Weekly Downloads

1

Version

0.0.1

License

MIT

Last publish

Collaborators

  • eddystop