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

@unicef-polymer/etools-dexie-caching

v1.1.0

Published

Handles IndexedDb caching

Downloads

19

Readme

<etools-dexie-caching>

  • Handles caching in IndexedDb.
  • To use you have to define your Dexie db(s) and endpoints in your app.
  • Ability to cache in your app's specific db: window.EtoolsRequestCacheDb
  • Ability to cache in a db that is shared between etools apps and holds the data that is common to all these apps: window.EtoolsSharedDb.

Data caching requirement

Example of defining the window.EtoolsRequestCacheDb:

  var appDexieDb = new Dexie('[insert name]');
  appDexieDb.version(1).stores({
    countries: "id, name"
    listsExpireMapTable: "&name, expire",
    ajaxDefaultDataTable: "&cacheKey, data, expire"
  });

  window.EtoolsRequestCacheDb = appDexieDb;

Example of defining the window.EtoolsSharedDb:

  var sharedDexieDb = new Dexie('EtoolsSharedDb');
  sharedDexieDb.version(1).stores({
    collections: "&cacheKey, data, expire"
  });

  window.EtoolsSharedDb = sharedDexieDb;

In your app you will configure your cacheable endpoints:

const endpoints = {
  {
    url: 'your/api/route',
    exp: 300000, // if exp = 0 no caching will be made
    cachingKey: 'stringIdentifier'
  },
   {
    url: 'your/api/route',
    exp: 300000, // if exp is missing no caching will be made
    sharedDbCachingKey: 'stringIdentifier'
  },
   {
    url: 'your/api/route',
    exp: 300000, // milliseconds expected
    cacheTableName: 'stringIdentifier'
  }
};

To mark an endpoint as cacheable you have to set the exp property and one of cachingKey, cacheTableName or sharedDbCachingKey. If just exp property is provided, cachingKey will automatically be set to the url of the endpoint.

Set the cachingKey property if you want to cache the endpoint response in the default table ajaxDefaultDataTable and 'cachingKey' will be the row identifier used to retrieve the data. The cached data will have the following format:

{
  // cacheKey can have request params stringified in the end if params were provided in sendRequest options
  cacheKey: '[provided cachingKey value]',
  // Date.now() + endpoint.exp
  expire: 1491306589975,
  // request response data
  data: [endpoint response]
}

Set the cacheTableName property if you do not want to cache in the ajaxDefaultDataTable table, but in a separate table with the provided name. This is recommended if you need to do queries on this table later, like showing a list with pagination and filtering only on frontend side. The expiration of the data in these tables is stored in the listsExpireMapTable table, under the following format, with name column being the row identifier:

{
  name: '[provided cacheTableName value]',
  // Date.now() + endpoint.exp
  expire: 1491306589975
}

Set the sharedDbCachingKey if you want to cache the data in the EtoolsSharedDb, in the default table called collections and 'sharedDbCachingKey' will be the row identifier used to retrieve the data.

For info about Dexie.js databases check the documentation.

Disable caching

Just set this in your app: window.EtoolsRequestCacheDisabled = true

Install

$ npm i --save @unicef-polymer/etools-dexie-caching

Usage example

if (requestIsCacheable(method, endpoint)) {
    return getFromCache(endpoint)
      .catch(() => { // Data not found in cache or is expired
        return `do http request...`
          .then(response => cacheEndpointResponse(response, endpoint));
      });
  }

  return `do http request...`; // When request is not cacheable, just do the http request

Demo

See etools-ajax component (https://github.com/unicef-polymer/etools-ajax) for an example.