{"id":41284027,"url":"https://github.com/funny-bytes/sequelize-simple-cache","last_synced_at":"2026-01-23T02:58:35.811Z","repository":{"id":40935872,"uuid":"158856475","full_name":"funny-bytes/sequelize-simple-cache","owner":"funny-bytes","description":"A simple, transparent, client-side, in-memory cache for Sequelize","archived":false,"fork":false,"pushed_at":"2024-02-26T12:22:47.000Z","size":1347,"stargazers_count":17,"open_issues_count":11,"forks_count":8,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-09-21T04:38:09.162Z","etag":null,"topics":["cache","client-side","in-memory","sequelize","transparent"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/funny-bytes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-11-23T16:36:01.000Z","updated_at":"2025-02-28T19:08:07.000Z","dependencies_parsed_at":"2024-02-26T12:31:36.576Z","dependency_job_id":null,"html_url":"https://github.com/funny-bytes/sequelize-simple-cache","commit_stats":{"total_commits":101,"total_committers":3,"mean_commits":"33.666666666666664","dds":"0.28712871287128716","last_synced_commit":"753d4a3119c944e46041d8bcbc0a4bd3fefe31cc"},"previous_names":["frankthelen/sequelize-simple-cache"],"tags_count":32,"template":false,"template_full_name":null,"purl":"pkg:github/funny-bytes/sequelize-simple-cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/funny-bytes%2Fsequelize-simple-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/funny-bytes%2Fsequelize-simple-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/funny-bytes%2Fsequelize-simple-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/funny-bytes%2Fsequelize-simple-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/funny-bytes","download_url":"https://codeload.github.com/funny-bytes/sequelize-simple-cache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/funny-bytes%2Fsequelize-simple-cache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28679139,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T01:00:35.747Z","status":"online","status_checked_at":"2026-01-23T02:00:08.296Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","client-side","in-memory","sequelize","transparent"],"created_at":"2026-01-23T02:58:35.128Z","updated_at":"2026-01-23T02:58:35.796Z","avatar_url":"https://github.com/funny-bytes.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sequelize-simple-cache\n\nThis is a simple, transparent, client-side, in-memory cache for [Sequelize](https://github.com/sequelize/sequelize).\nCache invalidation is based on time-to-live (ttl).\nSelectively add your Sequelize models to the cache.\nWorks with all storage engines supported by Sequelize.\n\n![main workflow](https://github.com/funny-bytes/sequelize-simple-cache/actions/workflows/main.yml/badge.svg)\n[![Coverage Status](https://coveralls.io/repos/github/funny-bytes/sequelize-simple-cache/badge.svg?branch=master)](https://coveralls.io/github/funny-bytes/sequelize-simple-cache?branch=master)\n[![Dependencies Status](https://david-dm.org/funny-bytes/sequelize-simple-cache.svg)](https://david-dm.org/funny-bytes/sequelize-simple-cache)\n[![Maintainability](https://api.codeclimate.com/v1/badges/c8bdb1fc29ef12070cac/maintainability)](https://codeclimate.com/github/funny-bytes/sequelize-simple-cache/maintainability)\n[![node](https://img.shields.io/node/v/sequelize-simple-cache.svg)]()\n[![code style](https://img.shields.io/badge/code_style-airbnb-brightgreen.svg)](https://github.com/airbnb/javascript)\n[![Types](https://img.shields.io/npm/types/sequelize-simple-cache.svg)](https://www.npmjs.com/package/sequelize-simple-cache)\n[![License Status](http://img.shields.io/npm/l/sequelize-simple-cache.svg)]()\n\nThis cache might work for you if you have database tables that\n(1) are frequently read but very rarely written and\n(2) contain only few rows of data.\n\nIn a project, we had a couple of database tables with a sort of configuration.\nSomething like 4 or 5 tables with some 10 rows of data.\nNearly every request needed this data, i.e., it was read all the time.\nBut updated only very rarely, e.g, once a day.\nSo, pre-fetching or simple in-memory caching would work for us.\n\nIf that's not matching your scenario,\nbetter look for something more sophisticated such as Redis or Memcached.\n\nTested with\n\n* Sequelize 6, Node 12/14/15, integration tested with Postgres 11/12 (via pg 8) and sqlite3 v5 (memory)\n* Sequelize 5, Node 10/12/13, integration tested with Postgres 10/11 (via pg 7) and sqlite3 v4 (memory)\n\n## Install\n\n```bash\nnpm install sequelize-simple-cache\n```\n\n## Usage\n\nSetup the cache along with loading your Sequelize models like this:\n\n```javascript\nconst Sequelize = require('sequelize');\nconst SequelizeSimpleCache = require('sequelize-simple-cache');\n\n// create db connection\nconst sequelize = new Sequelize('database', 'username', 'password', { ... });\n\n// create cache -- referring to Sequelize models by name, e.g., `User`\nconst cache = new SequelizeSimpleCache({\n  User: { ttl: 5 * 60 }, // 5 minutes\n  Page: { }, // default ttl is 1 hour\n});\n\n// assuming you have your models in separate files with \"model definers\"\n// -- e.g, see below or https://github.com/sequelize/express-example --\n// add your models to the cache like this\nconst User = cache.init(require('./models/user')(sequelize));\nconst Page = cache.init(require('./models/page')(sequelize));\n\n// no caching for this one (because it's not configured to be cached)\n// will only add dummy decorators to the model for a homogeneous interface to all models\nconst Order = cache.init(require('./models/order')(sequelize));\n\n// the Sequelize model API is fully transparent, no need to change anything.\n// first time resolved from database, subsequent times from local cache.\nconst fred = await User.findOne({ where: { name: 'fred' }});\n```\n\n`./models/user.js` might look like this:\n\n```javascript\nconst { Model } = require('sequelize');\nclass User extends Model {}\nmodule.exports = (sequelize) =\u003e User.init({ /* attributes */ }, { sequelize });\n```\n\nPlease note that `SequelizeSimpleCache` refers to Sequelize **models by name**.\nThe model name is usually equals the class name (e.g., `class User extends Model {}` \u0026#8594; `User`).\nUnless it is specified differently in the model options' `modelName` property\n(e.g., `User.init({ /* attributes */ }, { sequelize, modelName: 'Foo' })` \u0026#8594; `Foo`).\nThe same is true if you are using `sequelize.define()` to define your models.\n\n## More Details\n\n### Supported methods\n\nThe following methods on Sequelize model instances are supported for caching:\n`findOne`, `findAndCountAll`, `findByPk`, `findAll`, `count`, `min`, `max`, `sum`.\nIn addition, for Sequelize v4: `find`, `findAndCount`, `findById`, `findByPrimary`, `all`.\n\n### Non-cacheable queries / bypass caching\n\nYou need to avoid non-cacheable queries, e.g., queries containing dynamic timestamps.\n\n```javascript\nconst { Op, fn } = require('sequelize');\n// this is not good\nModel.findAll({ where: { startDate: { [Op.lte]: new Date() }, } });\n// you should do it this way\nModel.findAll({ where: { startDate: { [Op.lte]: fn('NOW') }, } });\n// if you don't want a query to be cached, you may explicitly bypass the cache like this\nModel.noCache().findAll(/* ... */);\n// transactions enforce bypassing the cache, e.g.:\nModel.findOne({ where: { name: 'foo' }, transaction: t, lock: true });\n```\n\n### Time-to-live (ttl)\n\nEach model has its individual time-to-live (ttl), i.e.,\nall database requests on a model are cached for a particular number of seconds.\nDefault is one hour.\nFor eternal caching, i.e., no automatic cache invalidation, simply set the model's `ttl` to `false` (or any number less or equals `0`).\n\n```javascript\nconst cache = new SequelizeSimpleCache({\n  User: { ttl: 5 * 60 }, // 5 minutes\n  Page: { }, // default ttl is 1 hour\n  Foo: { ttl: false } // cache forever\n});\n```\n\n### Clear cache\n\nThere are these ways to clear the cache.\n\n```javascript\nconst cache = new SequelizeSimpleCache({ /* ... */ });\n// clear all\ncache.clear();\n// clear all entries of specific models\ncache.clear('User', 'Page');\n// or do the same on any model\nModel.clearCache(); // only model\nModel.clearCacheAll(); // entire cache\n```\n\nBy default, the model's cache is automatically cleared if these methods are called:\n`update`, `create`, `upsert`, `destroy`, `findOrBuild`.\nIn addition, for Sequelize v4: `insertOrUpdate`, `findOrInitialize`, `updateAttributes`.\n\nYou can change this default behavior like this:\n\n```javascript\nconst cache = new SequelizeSimpleCache({\n  User: { }, // default clearOnUpdate is true\n  Page: { clearOnUpdate: false },\n});\n```\n\nIf you run multiple instances (clients or containers or PODs or alike),\nbe aware that cache invalidation is more complex that the above simple approach.\n\n### Bypass caching\n\nCaching can explicitly be bypassed like this:\n\n```javascript\nModel.noCache().findOne(/* ... */);\n```\n\n### Limit\n\nThis cache is meant as a simple in-memory cache for a very limited amount of data.\nSo, you should be able to control the size of the cache.\n\n```javascript\nconst cache = new SequelizeSimpleCache({\n  User: { }, // default limit is 50\n  Page: { limit: 30 },\n});\n```\n\n### Logging\n\nThere is \"debug\" and \"ops\" logging -- both are off by default.\nLogging goes to `console.debug()` unless you set `delegate` to log somewhere else.\n`event` is one of: `init`, `hit`, `miss`, `load`, `purge` or `ops`.\n\n```javascript\nconst cache = new SequelizeSimpleCache({\n  // ...\n}, {\n  debug: true,\n  ops: 60, // seconds\n  delegate: (event, details) =\u003e { ... },\n});\n```\n\n### Unit testing\n\nIf you are mocking your Sequelize models in unit tests with [Sinon](https://sinonjs.org/) et al.,\ncaching might be somewhat counterproductive.\nSo, either clear the cache as needed in your unit tests. For example (using [mocha](https://mochajs.org/)):\n\n```javascript\ndescribe('My Test Suite', () =\u003e {\n  beforeEach(() =\u003e {\n    Model.clearCacheAll(); // on any model with the same effect\n  });\n  // ...\n```\n\nOr disable the cache right from the beginning.\nA quick idea... have a specific config value in your project's `/config/default.js`\nand `/config/test.js` to enable or disable the cache respectively.\nAnd start your unit tests with setting `NODE_ENV=test` before.\nThis is actually the way I am doing it; plus a few extra unit tests for caching.\n\n```javascript\nconst config = require('config');\nconst useCache = config.get('database.cache');\n// initializing the cache\nconst cache = useCache ? new SequelizeSimpleCache({/* ... */}) : undefined;\n// loading the models\nconst model = require('./models/model')(sequelize);\nconst Model = useCache ? cache.init(model) : model;\n```\n\n## TypeScript Support\n\n`SequelizeSimpleCache` includes type definitions for TypeScript.\nThey are based on the [Sequelize types](https://sequelize.org/master/manual/typescript.html).\n\nFor this module to work, your **TypeScript compiler options** must include\n`\"target\": \"ES2015\"` (or later), `\"moduleResolution\": \"node\"`, and\n`\"esModuleInterop\": true`.\n\nA quick example:\n\n```typescript\nimport { Sequelize, Model, DataTypes } from \"sequelize\";\nimport SequelizeSimpleCache from \"sequelize-simple-cache\";\n\ninterface UserAttributes {\n  id: number;\n  name: string;\n}\n\nclass User extends Model\u003cUserAttributes\u003e implements UserAttributes {\n  public id!: number;\n  public name!: string;\n}\n\n// create db connection\nconst sequelize = new Sequelize(/* ... */);\n\n// initialize models\nUser.init({ /* attributes */ }, { sequelize, tableName: 'users' });\n\n// create cache -- referring to Sequelize models by name, e.g., `User`\nconst cache = new SequelizeSimpleCache({\n  [User.name]: { ttl: 5 * 60 }, // 5 minutes\n  'Foo': {}, // default ttl is 1 hour\n});\n\n// add User model to the cache\nconst UserCached = cache.init\u003cUser\u003e(User);\n\n// the Sequelize model API is fully transparent, no need to change anything.\n// first time resolved from database, subsequent times from local cache.\nconst fred = await UserCached.findOne({ where: { name: 'fred' }});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffunny-bytes%2Fsequelize-simple-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffunny-bytes%2Fsequelize-simple-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffunny-bytes%2Fsequelize-simple-cache/lists"}