Skip to content
forked from fed135/ha-store

Efficient data fetching for your Javascript Applications

License

Notifications You must be signed in to change notification settings

mats852/ha-store

 
 

Repository files navigation

High-Availability store

Efficient data fetching



ha-store Node Build Status Dependencies Status


HA-store is a generic wrapper for your data queries, it features:

  • Smart TLRU cache for 'hot' information
  • Request coalescing and batching (solves the Thundering Herd problem)
  • Insightful stats and events
  • Lightweight, configurable, battle-tested

Learn how you can improve your app's performance, design and resiliancy here!

Installing

npm install ha-store

Usage

// Create your store
const store = require('ha-store');
const itemStore = store({
  resolver: getItems,
  uniqueParams: ['language']
});

// Define your resolver
function getItem(ids, params, contexts) {
  // Ids will be a list of all the unique requested items
  // Params will be the parameters for the request, which must be declared in the `uniqueParams` config of the store
  // Contexts will be the list of originating context information

  // Now perform some exensive network call or database lookup...

  // Then, respond with your data formatted into one of these two formats:
    // a) [ { id: '123', language: 'fr', name: 'fred' } ]
    // b) { '123': { language: 'fr', name: 'fred' } }
}

// Now to use your store
itemStore.get('123', { language: 'fr' }, { some: 'context' })
  .then(item => /* The item you requested */);

// You can even ask for more than one item at a time
itemStore.get(['123', '456'], { language: 'en' }, { another: 'context' })
  .then(items => /* All the items you requested */);

Options

Name Required Default Description
resolver true - The method to wrap, and how to interpret the returned data. Uses the format <function(ids, params)>
responseParser false (system) The method that format the results from the resolver into an indexed collection. Accepts indexed collections or arrays of objects with an id property. Uses the format <function(response, requestedIds, params)>
uniqueParams false [] The list of parameters that, when passed, generate unique results. Ex: 'language', 'view', 'fields', 'country'. These will generate different combinations of cache keys.
store false null A custom store for the data, like ha-store-redis.
cache false
{
  limit: 5000,
  ttl: 300000
}
Caching options for the data - limit - the maximum number of records, and ttl - time to live for a record.
batch false
{
  tick: 50,
  max: 100
}
Batching options for the requests

*All options are in (ms)

Monitoring and events

HA-store emits events to track cache hits, miss and outbound requests.

Event Description
cacheHit When the requested item is present in the microcache, or is already being fetched. Prevents another request from being created.
cacheMiss When the requested item not cached or coalesced and must be fetched.
coalescedHit When a record query successfully hooks to the promise of the same record in transit.
query When a batch of requests is about to be sent.
queryFailed Indicates that the batch has failed. Retry policy will dictate if it should be re-attempted.
querySuccess Indicates that the batch request was successful.

You may also want to track the amount of contexts and records stored via the size method.

Testing

npm test

Benchmarks

Read instructions here

npm run bench

Contribute

Please do! This is an open source project - if you see something that you want, open an issue or file a pull request.

I am always looking for more maintainers, as well.

License

Apache 2.0 (c) 2019 Frederic Charette

About

Efficient data fetching for your Javascript Applications

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%