sequelize-simple-cache
Advanced tools
Comparing version 1.0.0-beta.3 to 1.0.0-beta.4
{ | ||
"name": "sequelize-simple-cache", | ||
"version": "1.0.0-beta.3", | ||
"version": "1.0.0-beta.4", | ||
"description": "A simple, transparent, client-side, in-memory cache for Sequelize", | ||
@@ -14,4 +14,6 @@ "main": "src/SequelizeSimpleCache.js", | ||
"Sequelize", | ||
"transparent", | ||
"cache", | ||
"transparent" | ||
"client-side", | ||
"in-memory" | ||
], | ||
@@ -18,0 +20,0 @@ "scripts": { |
@@ -6,3 +6,3 @@ # sequelize-simple-cache | ||
Selectively add your Sequelize models to the cache. | ||
Works with a all storage engines supported by Sequelize. | ||
Works with all storage engines supported by Sequelize. | ||
@@ -28,3 +28,4 @@ [![Build Status](https://travis-ci.org/frankthelen/sequelize-simple-cache.svg?branch=master)](https://travis-ci.org/frankthelen/sequelize-simple-cache) | ||
If that's not matching your scenario, better look for something more sophisticated such as Redis, memcached and alike. | ||
If that's not matching your scenario, | ||
better look for something more sophisticated such as Redis, memcached and alike. | ||
@@ -52,3 +53,3 @@ ## Install | ||
// add your models to the cache | ||
// add your models to the cache like this | ||
const User = cache.init(sequelize.import('./models/user')); | ||
@@ -58,3 +59,4 @@ const Page = cache.init(sequelize.import('./models/page')); | ||
// first time resolved from database, subsequent times from local cache | ||
// the model API is fully transparent, no need to change the interface. | ||
// first time resolved from database, subsequent times from local cache. | ||
const fred = User.findOne({ where: { username: 'fred' }}); | ||
@@ -72,3 +74,3 @@ ``` | ||
Make sure your queries are cacheable, i.e., do not have dynamic timestamps. | ||
You need to avoid non-cacheable queries, e.g., queries containing dynamic timestamps. | ||
```javascript | ||
@@ -80,3 +82,3 @@ const { Op, fn } = require('sequelize'); | ||
Model.findOne({ where: { startDate: { [Op.lte]: fn('NOW') }, } }); | ||
// if you don't want that to be cached, bypass the cache like this | ||
// if you don't want a query to be cached, bypass the cache like this | ||
Model.cacheBypass().findOne({ where: { startDate: { [Op.lte]: fn('NOW') }, } }); | ||
@@ -89,9 +91,10 @@ ``` | ||
```javascript | ||
const cache = new SequelizeSimpleCache({...}); | ||
// clear all | ||
cache.clear(); | ||
// clear all entries of a certain model | ||
// clear all entries of one specific model | ||
cache.clear('User'); | ||
// or do the same on the model | ||
User.cacheClear(); | ||
User.cacheClearAll(); | ||
// or do the same on any model | ||
Model.cacheClear(); | ||
Model.cacheClearAll(); | ||
``` | ||
@@ -103,3 +106,3 @@ | ||
```javascript | ||
User.cacheBypass().findOne(...); | ||
Model.cacheBypass().findOne(...); | ||
``` | ||
@@ -112,3 +115,3 @@ | ||
const cache = new SequelizeSimpleCache({ | ||
User: { ttl: 5 * 60 }, | ||
// ... | ||
}, { | ||
@@ -119,10 +122,11 @@ debug: true, | ||
### Unit testing your models with Sinon et al. | ||
### Unit testing your models | ||
If you run unit tests against your Sequelize models, caching might be somewhat counterproductive. | ||
So, either clear the cache as needed in your unit tests. For example (using Mocha): | ||
If you are unit testing your Sequelize models using [Sinon](https://sinonjs.org/) et al., | ||
caching might be somewhat counterproductive. | ||
So, either clear the cache as needed in your unit tests. For example (using [mocha](https://mochajs.org/)): | ||
```javascript | ||
describe('API: GET /consent/sp/{spId}/customer/{lcId}', () => { | ||
describe('My Test Suite', () => { | ||
beforeEach(() => { | ||
User.cacheClearAll(); | ||
Model.cacheClearAll(); // on any model with the same effect | ||
}); | ||
@@ -133,10 +137,14 @@ // ... | ||
Or disable the cache right from the beginning. | ||
A quick idea... have a config value to be set in your project's `/config/test.js` | ||
and start your unit tests with setting `NODE_ENV=test` before. | ||
A quick idea... have a specific config value in your project's `/config/default.js` | ||
and `/config/test.js` to enable or disable the cache respectively. | ||
And start your unit tests with setting `NODE_ENV=test` before. | ||
This is actually the way I am doing it; plus a few extra unit tests for explicitly testing cache. | ||
```javascript | ||
const config = require('config'); | ||
//... | ||
if (config.get('disablecache')) { | ||
cache.disable(); | ||
} | ||
const useCache = config.get('database.cache'); | ||
// initializing the cache | ||
const cache = useCache ? new SequelizeSimpleCache({...}) : undefined; | ||
// loading the models | ||
const model = sequelize.import('./models/model'); | ||
const Model = useCache ? cache.init(model) : model; | ||
``` |
@@ -20,3 +20,2 @@ const Promise = require('bluebird'); | ||
this.cache = new Map(); | ||
this.disabled = new Set(); | ||
} | ||
@@ -55,6 +54,2 @@ | ||
model.cacheClearAll = () => this.clear(); | ||
model.cacheDisable = () => this.disable(name); | ||
model.cacheDisableAll = () => this.disable(); | ||
model.cacheEnable = () => this.enable(name); | ||
model.cacheEnableAll = () => this.enable(); | ||
/* eslint-enable no-param-reassign */ | ||
@@ -69,3 +64,3 @@ // setup caching for this model | ||
get: (target, prop) => { | ||
if (this.disabled.has(name) || !methods.includes(prop)) { | ||
if (!methods.includes(prop)) { | ||
return target[prop]; | ||
@@ -117,14 +112,4 @@ } | ||
} | ||
disable(...modelnames) { | ||
const names = modelnames.length ? modelnames : Object.keys(this.config); | ||
names.forEach(name => this.disabled.add(name)); | ||
} | ||
enable(...modelnames) { | ||
const names = modelnames.length ? modelnames : Object.keys(this.config); | ||
names.forEach(name => this.disabled.delete(name)); | ||
} | ||
} | ||
module.exports = SequelizeSimpleCache; |
@@ -80,6 +80,2 @@ const chai = require('chai'); | ||
expect(User).to.have.property('cacheClearAll').which.is.a('function'); | ||
expect(User).to.have.property('cacheDisable').which.is.a('function'); | ||
expect(User).to.have.property('cacheDisableAll').which.is.a('function'); | ||
expect(User).to.have.property('cacheEnable').which.is.a('function'); | ||
expect(User).to.have.property('cacheEnableAll').which.is.a('function'); | ||
}); | ||
@@ -86,0 +82,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
140
24967
436