Remote API model, cached using Redis
The library is driven by needs of query remote API, but caching it locally. It
How to install
$ npm i -S cached-remote
Usage
import CachedRemote from 'cached-remote'
import redis from 'redis'
const modelName = 'clubs'
const host = `http://some-remote-api.io`
const basePath = '/api'
// Or use global client/pool
const cacheClient = redis.createClient()
class Clubs extends CachedRemote {
constructor () {
super({ modelName, host, basePath, cacheClient })
}
// Custom endpoints (non REST)
findByHostname (hostname) {
const cacheIdentifier = `hostname:${hostname}`
return this.readFromCache(cacheIdentifier)
.then(cachedData => {
if (cachedData) { return cachedData }
const query = { hostname }
const extraPath = '/by-hostname'
return this.searchFromRemote({ query, extraPath })
.then(remoteData => {
this.writeInCache(cacheIdentifier, remoteData)
return remoteData
})
})
}
}
export default new Clubs()
Then in your code you can use it:
import Clubs from 'models'
Clubs.find(5).then(club => console.log(club))
Clubs.findByHostname('mylovelyhostname.com').then(club => console.log(club))
Development instructions
- Create branch using:
$ git checkout -b feature-branch
- Run the linter and tests (tests are requiring Redis server running on port 6379) using:
$ npm run lint
$ npm run tests
-
Create PR after you are finished.
-
Then publish it to NPM registry using:
$ npm run release