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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@lepauloricardo/sequelize-simple-cache

v0.0.3

Published

A simple, transparent, client-side, in-memory cache for Sequelize (Fork de funny-bytes/sequelize-simple-cache)

Downloads

135

Readme

@lepauloricardo/sequelize-simple-cache

Overview

This is a fork of sequelize-simple-cache

In this fork, I have added support for associations when clearing the cache. This works in my specific configuration, and no additional tests were added. This library is not actively maintained, so use it at your own risk.

main workflow

Installation

npm install @lepauloricardo/sequelize-simple-cache

Usage

Set up the cache and load your Sequelize models like this:

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

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

// create cache -- referring to Sequelize models by name, e.g., `User`
const cache = new SequelizeSimpleCache({
  User: { ttl: 5 * 60 }, // 5 minutes
  Page: { }, // default ttl is 1 hour
});

// assuming you have your models in separate files with "model definers"
// add your models to the cache like this
const User = cache.init(require('./models/user')(sequelize));
const Page = cache.init(require('./models/page')(sequelize));

// no caching for this one (because it's not configured to be cached)
const Order = cache.init(require('./models/order')(sequelize));

// Sequelize model API is fully transparent, no need to change anything.
// first time resolved from the database, subsequent times from the cache.
const fred = await User.findOne({ where: { name: 'fred' }});

Example model definition:

const { Model } = require('sequelize');
class User extends Model {}
module.exports = (sequelize) => User.init({ /* attributes */ }, { sequelize });

Note: SequelizeSimpleCache refers to Sequelize models by name. By default, the model name is equal to the class name (e.g., User for class User extends Model {}).

Key Features

Supported methods

The following methods are supported for caching:

  • findOne, findAndCountAll, findByPk, findAll, count, min, max, sum.
  • For Sequelize v4: find, findAndCount, findById, findByPrimary, all.

Bypass caching

To bypass the cache for a specific query:

Model.noCache().findOne(/* ... */);

Time-to-live (TTL)

Each model has an individual TTL, i.e., all database requests for a model are cached for a specific number of seconds. The default TTL is one hour.

For eternal caching (no TTL), set the model's ttl to false or any number less than or equal to 0.

const cache = new SequelizeSimpleCache({
  User: { ttl: 5 * 60 }, // 5 minutes
  Page: { ttl: false }, // cache forever
});

Clear Cache

You can clear the cache in several ways:

const cache = new SequelizeSimpleCache({ /* ... */ });
// clear all caches
cache.clear();
// clear specific models
cache.clear('User', 'Page');
// clear a single model's cache
Model.clearCache();
// clear all model caches
Model.clearCacheAll();

By default, the cache is cleared automatically after the following actions:

  • update, create, upsert, destroy, findOrBuild.

You can customize this behavior:

const cache = new SequelizeSimpleCache({
  User: { }, // default clearOnUpdate is true
  Page: { clearOnUpdate: false },
});

Limit Cache Size

This cache is designed for small datasets. You can set limits for the cache size.

const cache = new SequelizeSimpleCache({
  User: { }, // default limit is 50
  Page: { limit: 30 },
});

Logging

You can enable logging for cache events:

const cache = new SequelizeSimpleCache({
  // ...
}, {
  debug: true,
  ops: 60, // seconds
  delegate: (event, details) => { ... },
});

Unit Testing

If you mock your Sequelize models in unit tests, you can clear the cache as needed:

describe('My Test Suite', () => {
  beforeEach(() => {
    Model.clearCacheAll();
  });
  // ...
});

Or disable caching entirely during tests:

const config = require('config');
const useCache = config.get('database.cache');
const cache = useCache ? new SequelizeSimpleCache({/* ... */}) : undefined;

TypeScript Support

SequelizeSimpleCache includes TypeScript definitions.

To use this module with TypeScript, ensure your compiler options include:

  • "target": "ES2015" (or later),
  • "moduleResolution": "node",
  • "esModuleInterop": true.