Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sequelize-simple-cache

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sequelize-simple-cache

A simple, transparent, client-side, in-memory cache for Sequelize

  • 1.0.0-beta.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
722
decreased by-16.53%
Maintainers
1
Weekly downloads
 
Created
Source

sequelize-simple-cache

This is a simple, transparent, client-side, in-memory cache for Sequelize v4. Cache invalidation is based on time-to-live (ttl). Selectively add your Sequelize models to the cache.

Build Status Coverage Status Dependencies Status Greenkeeper badge Maintainability node code style License Status

This cache might work for you if you have a few database tables that (1) are frequently read but very rarely written and (2) contain only few rows of data.

In a project, we had a couple of database tables that were holding a sort of system configuration. Something like 4 or 5 tables with some 50 rows of data. Nearly every request needed this data, i.e., it was read all the time. But updated only very rarely, once a day maybe. So, pre-fetching or simple caching would work for us.

If that's not matching your scenario, better look for something more sophisticated such as Redis, memcached and alike.

Install

npm install sequelize-simple-cache

Usage

Setup the cache along with loading your Sequelize models like this:

const Sequelize = require('sequelize');
const SequelizeSimpleCache = require('sequelize-simple-cache');

const sequelize = new Sequelize('database', 'username', 'password', { ... });

// initialize cache
const cache = new SequelizeSimpleCache({
  User: { ttl: 5 * 60 }, // 5 minutes
  Page: { }, // default ttl is 1 hour
});

// add your models to the cache
const User = cache.init(sequelize.import('./models/user'));
const Page = cache.init(sequelize.import('./models/page'));
const Balance = sequelize.import('./models/balance'); // no caching for this one

// first time resolved from database, subsequent times from local cache
const fred = User.findOne({ where: { username: 'fred' }});

More Details

Supported methods

Currently, the following methods on a Sequelize model instances are supported for caching: findById, findOne, findAll, findAndCountAll, count, min, max, sum.

Non-cacheable queries / bypass caching

Make sure your queries are cacheable, i.e., do not have dynamic timestamps.

const { Op, fn } = require('sequelize');
// this is not good
Model.findOne({ where: { startDate: { [Op.lte]: new Date() }, } });
// you should do it this way
Model.findOne({ where: { startDate: { [Op.lte]: fn('NOW') }, } });
// if you don't want that to be cached, bypass the cache like this
Model.cacheNo().findOne({ where: { startDate: { [Op.lte]: fn('NOW') }, } });

Clear cache

There are these ways to clear the cache.

// clear all
cache.clear();
// clear all entries of a certain model
cache.clear('User');
// or do the same on the model
User.cacheClear();
User.cacheClearAll();

Bypass caching

Caching can explicitly be bypassed like this:

User.cacheNo().findOne(...);

Debug output

You can activate debug output to console.debug() like this:

const cache = new SequelizeSimpleCache({
  User: { ttl: 5 * 60 },
}, {
  debug: true,
});

Unit testing your models with Sinon et al.

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):

describe('API: GET /consent/sp/{spId}/customer/{lcId}', () => {
  beforeEach(() => {
    User.cacheClearAll();
  });
  // ...

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.

const config = require('config');
//...
if (config.get('disablecache')) {
  cache.disable();
}

Keywords

FAQs

Package last updated on 23 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc