0.1.0 • Published 5 years ago

@dzangolab/vuejs-cache v0.1.0

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

Vuejs cache plugin

Installation

via npm

npm install @dzangolab/vuejs-cache

via yarn

yarn add @dzangolab/vuejs-cache

Requirements

This package need following packages installed in your app

  • pouchdb-browser@^7.0.0
  • vue@^2.5.16
  • vuex@^3.0.1

Api requirements

Your api needs to attach a custom header for db version, the cache data is updated based on this version.

Custom header have json key and value pair. key is based on cacheKey and value is db version.

{"cacheKey1": 898999, "cacheKey2": 48383}

Usage

Install Vuejs cache plugin as follows:

# main.js

import cachePlugin from '@dzangolab/vuejs-cache'

Vue.use(cachePlugin, {options})

Options

OptionDescriptionTypeRequiredDefault
storeMain store to which the cache module is attached toObjecttrue
axiosThe instance of axios used by appObjecttrue
userIdCurrently logged in user's idInteger/Stringfalsenull
databaseNameName of the cache dbStringfalse_cache
storeModuleNamestore module nameStringfalsecache
dbVersionsHeaderKeyCustom response header key for db versionStringtrue
cacheConfigsarray value to handle route, db header key, namearraytrue
ignoreRoutesarray of route to ignore for cache fetcharrayfalse[]

cacheConfigs example


[{cacheKey: 'cacheKey_name', cacheRoute: 'cache_route', isCacheRoutePublic: false}]

Once you have registered the plugin. It attaches a cache or the {storeModuleName} module to the store, The cached data received from the cacheRoute can be accessed as . All store and getters created based on cacheKey

using store

assuming you have not updated the storeModuleName

store.cache.<cacheKey>

Or

store.cache[<cacheKey>]

use getters

# File.vue

// ..
// ..
this.$store.getters['cache/cacheKey_name']

Changing user id

The store has a action to change the user id. Most probably after a login you need to change the user id for the cache

// ..
// ..

this.$store.dispatch('cache/updateCachedUserId', id)

Displaying message to users when updating cache

The cache module has a getter updatingCache that is true when the data is being fetched from the server.

you can use the getter to add visual clues about ongoing update.

Resetting cache

The store has a action reset the cache

// ..
// ..

this.$store.dispatch('cache/reset')

Making sure you app is mounted after the cache is available

// main.js

// ..
// ..

Vue._cacheInitialized
  .then(() => {
    new Vue({
      el: '#app',
      store
    })
  })