{"id":13714801,"url":"https://github.com/Asjas/prisma-redis-middleware","last_synced_at":"2025-05-07T03:30:57.254Z","repository":{"id":37056346,"uuid":"386706271","full_name":"Asjas/prisma-redis-middleware","owner":"Asjas","description":"Prisma Middleware for caching queries in Redis","archived":true,"fork":false,"pushed_at":"2023-12-03T23:12:25.000Z","size":4262,"stargazers_count":362,"open_issues_count":9,"forks_count":38,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-30T19:16:31.461Z","etag":null,"topics":["caching","prisma","prisma-middleware","redis"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Asjas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-07-16T16:51:09.000Z","updated_at":"2025-04-09T03:57:24.000Z","dependencies_parsed_at":"2022-08-08T19:01:32.057Z","dependency_job_id":"ea070e6b-50c7-47b6-949c-c0c904dd217f","html_url":"https://github.com/Asjas/prisma-redis-middleware","commit_stats":{"total_commits":674,"total_committers":13,"mean_commits":51.84615384615385,"dds":0.5712166172106825,"last_synced_commit":"7c34f1716fa72268a7d9453729d5b8b184ffa3f1"},"previous_names":[],"tags_count":61,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Asjas%2Fprisma-redis-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Asjas%2Fprisma-redis-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Asjas%2Fprisma-redis-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Asjas%2Fprisma-redis-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Asjas","download_url":"https://codeload.github.com/Asjas/prisma-redis-middleware/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252806401,"owners_count":21807199,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["caching","prisma","prisma-middleware","redis"],"created_at":"2024-08-03T00:00:50.170Z","updated_at":"2025-05-07T03:30:56.649Z","avatar_url":"https://github.com/Asjas.png","language":"TypeScript","funding_links":[],"categories":[":safety_vest: Community Prisma Tools"],"sub_categories":[],"readme":"# `prisma-redis-middleware`\n\n[![License: Hippocratic 3.0](https://img.shields.io/badge/License-Hippocratic_3.0-lightgrey.svg)](https://firstdonoharm.dev)\n[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)\n[![npm version](https://badge.fury.io/js/prisma-redis-middleware.svg)](https://badge.fury.io/js/prisma-redis-middleware)\n[![codecov](https://codecov.io/gh/Asjas/prisma-redis-middleware/branch/main/graph/badge.svg?token=6F6DDOSRK8)](https://codecov.io/gh/Asjas/prisma-redis-middleware)\n[![Main WorkFlow](https://github.com/Asjas/prisma-redis-middleware/actions/workflows/main.yml/badge.svg)](https://github.com/Asjas/prisma-redis-middleware/actions/workflows/main.yml)\n[![CodeQL WorkFlow](https://github.com/Asjas/prisma-redis-middleware/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/Asjas/prisma-redis-middleware/actions/workflows/codeql-analysis.yml)\n[![Library code size](https://github.com/Asjas/prisma-redis-middleware/actions/workflows/size.yml/badge.svg)](https://github.com/Asjas/prisma-redis-middleware/actions/workflows/size.yml)\n\nThis is a Prisma middleware used for caching and storing of Prisma queries in Redis (uses an in-memory LRU cache as\nfallback storage).\n\nUses [async-cache-dedupe](https://github.com/mcollina/async-cache-dedupe).\n\n## Features\n\n- Cache Invalidation\n- Supports custom cache keys\n- Cache persistance with Redis (uses an in-memory LRU cache as fallback)\n- Caching multiple Prisma models each with a specific cache time\n- Excluding certain Prisma models from being cached\n- Excluding certain Prisma queries from being cached across all models\n\n## Supported Node.js versions\n\nThe latest versions of the following Node.js versions are tested and supported.\n\n- 16\n- 18\n\n## Default Cached Methods\n\nHere is a list of all the Prisma methods that are currently cached by default in `prisma-redis-middleware`.\n\n- findUnique\n- findUniqueOrThrow\n- findFirst\n- findFirstOrThrow\n- findMany\n- count\n- aggregate\n- groupBy\n- findRaw\n- aggregateRaw\n\n`queryRaw` is not cached as it's executed against the Prisma db itself and not a model. This Prisma middleware is used\nfor caching queries based on the models that they are executed against.\n\n## Quick Start\n\nInstall the package using `npm`:\n\n```sh\nnpm i --save-exact prisma-redis-middleware\n```\n\n_You will also need to install and configure an external dependency for `Redis` (for example: `ioredis` or one that uses\na similar API) if you don't already have a Redis Client in your project._\n\n```sh\nnpm i --save-exact ioredis @types/ioredis\n```\n\n## Code Example (ESM / Import)\n\n```mjs\nimport Prisma from \"prisma\";\nimport { PrismaClient } from \"@prisma/client\";\nimport { createPrismaRedisCache } from \"prisma-redis-middleware\";\nimport Redis from \"ioredis\";\n\nconst redis = new Redis(); // Uses default options for Redis connection\n\nconst prisma = new PrismaClient();\n\nconst cacheMiddleware: Prisma.Middleware = createPrismaRedisCache({\n  models: [\n    { model: \"User\", excludeMethods: [\"findMany\"] },\n    { model: \"Post\", cacheTime: 180, cacheKey: \"article\" },\n  ],\n  storage: { type: \"redis\", options: { client: redis, invalidation: { referencesTTL: 300 }, log: console } },\n  cacheTime: 300,\n  excludeModels: [\"Product\", \"Cart\"],\n  excludeMethods: [\"count\", \"groupBy\"],\n  onHit: (key) =\u003e {\n    console.log(\"hit\", key);\n  },\n  onMiss: (key) =\u003e {\n    console.log(\"miss\", key);\n  },\n  onError: (key) =\u003e {\n    console.log(\"error\", key);\n  },\n});\n\nprisma.$use(cacheMiddleware);\n```\n\n## Code Example (Common JS / Require)\n\n```js\nconst Prisma = require(\"prisma\");\nconst { PrismaClient } = require(\"@prisma/client\");\nconst { createPrismaRedisCache } = require(\"prisma-redis-middleware\");\n\nconst prisma = new PrismaClient();\n\nconst cacheMiddleware: Prisma.Middleware = createPrismaRedisCache({\n  models: [\n    { model: \"User\", cacheTime: 60 },\n    { model: \"Post\", cacheTime: 180 },\n  ],\n  storage: { type: \"memory\", options: { invalidation: true, log: console } },\n  cacheTime: 300,\n  onHit: (key) =\u003e {\n    console.log(\"hit\", key);\n  },\n  onMiss: (key) =\u003e {\n    console.log(\"miss\", key);\n  },\n  onError: (key) =\u003e {\n    console.log(\"error\", key);\n  },\n});\n\nprisma.$use(cacheMiddleware);\n```\n\n## API\n\n### `createPrismaRedisCache(opts)`\n\nOptions:\n\n- `onDedupe`: (optional) a function that is called every time a query is deduped.\n- `onError`: (optional) a function that is called every time there is a cache error.\n- `onHit`: (optional) a function that is called every time there is a hit in the cache.\n- `onMiss`: (optional) a function that is called every time the result is not in the cache.\n- `cacheTime`: (optional) (number) the default time (in seconds) to use for models that don't have a `cacheTime` value set.\n  Default is 0.\n- `excludeModels`: (optional) (string) an array of models to exclude from being cached.\n- `excludeMethods`: (optional) (string) an array of Prisma methods to exclude from being cached for all models.\n- `models`: (optional) an array of Prisma models. Models options are:\n\n  - `model`: (required) string.\n  - `cacheKey`: (optional) string. Default is the model value.\n  - `cacheTime`: (optional) number (in seconds).\n  - `excludeMethods`: (optional) (string) an array of Prisma methods to exclude from being cached for this model.\n  - `invalidateRelated`: (optional) (string) an array of Prisma models to invalidate when mutating this model.\n\n    Example:\n\n    ```js\n    createPrismaRedisCache({\n      models: [\n        { model: \"User\", cacheTime: 60, invalidateRelated: [\"Post\"] },\n        { model: \"Post\", cacheKey: \"article\", excludeMethods: [\"findFirst\"] },\n      ],\n    });\n    ```\n\n- `storage`: (optional) the storage options; default is `{ type: \"memory\" }`. Storage options are:\n\n  - `type`: `memory` (default) or `redis`\n  - `options`: by storage type\n\n    - for `memory` type\n\n      - `size`: (optional) maximum number of items to store in the cache. Default is `1024`.\n      - `invalidation`: (optional) enable invalidation. Default is disabled.\n      - `log`: (optional) logger instance `pino` compatible, or `console`, default is disabled.\n\n      Example:\n\n      ```js\n      createPrismaRedisCache({\n        storage: { type: \"memory\", options: { size: 2048 }, log: console },\n      });\n      ```\n\n    - for `redis` type\n\n      - `client`: a redis client instance, mandatory. Should be an `ioredis` client or compatible.\n      - `invalidation`: (optional) enable invalidation. Default is disabled.\n      - `invalidation.referencesTTL`: (optional) references TTL in seconds, it means how long the references are alive;\n        it should be set at the maximum of all the caches ttl.\n      - `log`: (optional) logger instance `pino` compatible, or `console`, default is disabled.\n\n      Example\n\n      ```js\n      const redis = new Redis();\n\n      createPrismaRedisCache({\n        storage: {\n          type: \"redis\",\n          options: { client: redis, invalidation: { referencesTTL: 60 }, log: console },\n        },\n      });\n      ```\n\n- `transformer`: (optional) the transformer to used to serialize and deserialize the cache entries. It must be an object\n  with the following methods:\n\n  - `serialize`: a function that receives the result of the original function and returns a serializable object.\n  - `deserialize`: a function that receives the serialized object and returns the original result.\n\n  - Default is `undefined`, so the default transformer is used.\n\n    Example\n\n    ```js\n    import superjson from \"superjson\";\n\n    createPrismaRedisCache({\n      transformer: {\n        serialize: (result) =\u003e superjson.serialize(result),\n        deserialize: (serialized) =\u003e superjson.deserialize(serialized),\n      },\n    });\n    ```\n\n## Debugging\n\nYou can pass functions for `onMiss`, `onHit`, `onError` and `onDedupe` to `createPrismaRedisCache` which can then be\nused to debug whether a Prisma query is being cached or not.\n\nYou can also pass a custom `log` (pino or console) to the `storage` option and `async-cache-dedupe` will print debug\ninfo as it queries, sets, expires and invalidates the cache. Note that the `log` option can print out very verbose\noutput.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAsjas%2Fprisma-redis-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAsjas%2Fprisma-redis-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAsjas%2Fprisma-redis-middleware/lists"}