npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@maynoothuniversity/caching-json-databridge

v0.0.6

Published

A caching data bridge for NodeJS

Downloads

4

Readme

@maynoothuniversity/caching-json-databridge

A NodeJS module designed to act as caching middle-ware between apps or scripts that need access to data that can be represented as a JSON string, and sources of that data.

Each data source must provide one or more data fetching functions which return a promise of data that can be serialised to a JSON file for caching.

The module provides control over the cache TTL, and the ability to bypass the cache when truly live data is needed.

Module Architecture

A databridge is an object that manages multiple datasources, each of which contain one or more data fetcher functions which accept arbitarily many parameters, and can be arranged into nested namespaces if desired.

Data returned from data fetchers that accept parameters is split into named data streams for caching. The module does its best to handle this automatically, but when you need to pass values that can't be converted to a JSON string as parameters to a data fetcher you'll need to provide a custom stream name generaterator function. When a data fetcher is called with no parameters the data stream name main is used.

As a practical example, a data fetcher that retrieves all movies released in a given year would need one parameter - the year. A separate cache needs to be maintained for each year, so each year value should result in a different stream name. The default stream name generator would convert the single parameter value 1980 into the stream name n_1980. So, if you fetch the movies for 1980, and then 1982, two separate caches will be created, one for the stream name n_1980 and one for the stream name n_1982.

Databridges define a default cache TTL, but each datasource can specify a custom TTL.

To facilitate this, the following four classes are exported by the module:

  1. Databridge - representing databridges (your app/script will probably instantiate just one object of this type)
  2. Datasource - representing datasources (your app/script will instantiate an object of this type for each source of data it needs access to)
  3. FetchRequest - representing a request to a databridge for a stream of data from the cache or a datasource.
  4. FetchResponse - representing a promise of data returned by a databridge. The data could have origintated from the cache or a datasource.

Example

// import the module
const cjdb = require('@maynoothuniversity/caching-json-databridge');

// instantiate a databridge with a custom cache location and TTL
let myDB = new cjdb.Databridge({ cacheDir: './cache', defaultCacheTTL: 3600 });

// create a simple datasource that always returns the days of the week
let dowDS = new cjdb.Datasource('daysOfTheWeek', function(){
    return ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ];
});

// register the datasource into the bridge
myDB.register(dowDS);

// fetch a promise ofthe data by datasource name
myDB.daysOfTheWeek().then(function(data){
    console.log(data);
});

API Documentation

  • https://bbusschots-mu.github.io/caching-json-databridge/