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

prisma-redis-middleware

Package Overview
Dependencies
Maintainers
1
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prisma-redis-middleware

Prisma Middleware for caching results of queries in Redis

  • 1.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.9K
decreased by-18.16%
Maintainers
1
Weekly downloads
 
Created
Source

Prisma Redis Middleware

Prisma Middleware for caching queries in Redis.

Based on the work done by @abhiaiyer91 on prisma-lrucache-middleware.

Features

  • Basic Cache Invalidation
  • Caching multiple Prisma models
  • Excluding certain Prisma queries from being cached

Supported Node.js versions

The latest versions of the following Node.js versions are tested and supported.

  • 12
  • 14
  • 16

Cached queries

Here is a list of all the query methods that are currently cached by default in prisma-redis-middleware.

  • findUnique
  • findFirst
  • findMany
  • queryRaw
  • aggregate
  • count
  • groupBy

Quick Start

Install the package using npm:

npm i --save-exact prisma-redis-middleware

or yarn:

yarn add prisma-redis-middleware

You will also need to install and configure an external dependency for Redis (the examples show ioredis) if you don't already have a Redis Client in your project.

npm i --save-exact ioredis

Code Example

import Prisma from "@prisma/client";
import { createPrismaRedisCache } from "prisma-redis-middleware";
import Redis from "ioredis";

const redis = new Redis(); // Uses default options for Redis connection

const prismaClient = new Prisma.PrismaClient();

prismaClient.$use(
  createPrismaRedisCache({
    models: ["User", "Post"],
    cacheTime: 300, // five minutes
    redis,
    excludeCacheMethods: ["findMany"],
  }),
);

Options

The prisma-redis-middleware function takes 4 arguments, models, cacheTime and redis are required arguments. excludeCacheMethods is an optional argument.

createPrismaMiddleware({ models, cacheTime, redis, excludeCacheMethods });
Cache
  • models: An array of Prisma models (for example User, Post, Comment) (required)
  • cacheTime: number (milliseconds) (required)
  • redis: A Redis instance (required)
  • excludeCacheMethods: An array of Prisma Methods that should be excluded from being cached. (optional)

Debugging

Default: false

You may turn on logging to help debug why certain Prisma queries are not being cached as you may expect.

If you are using the Node.js binary directly you can do this.

"scripts": {
  "debug:dev": "DEBUG=prisma-redis-middleware node index.js",
}

If you are using a library such as nodemon then you can do this.

"scripts": {
  "debug:dev": "DEBUG=prisma-redis-middleware nodemon index.js",
}

[DEBUG] Query found in cache

This is what the debug output should look like when a query is found in the cache.

[prisma:redis:middleware][DEBUG] findUnique on User was found in the cache with key User:findUnique:{"where":{"id":1}}.

[DEBUG] Query not found in cache

This is what the debug output should look like when a query isn't found in the cache.

[prisma:redis:middleware][DEBUG] findUnique on User with key User:findUnique:{"where":{"id":1}} was not found in the cache.
[prisma:redis:middleware][DEBUG] Manually fetching query findUnique on User from the Prisma database.
[prisma:redis:middleware][DEBUG] Caching action findUnique on User with key User:findUnique:{"where":{"id":1}}.

[DEBUG] Query skipped

This is what the debug output should look like when an action is skipped (ie. Upsert, Delete, etc.)

[prisma:redis:middleware][DEBUG] upsert on User is skipped.
[prisma:redis:middleware][DEBUG] deleteMany on Post is skipped.

[DEBUG] Invalidated cache

This is what the debug output should look like when an action caused the cache to be invalidated.

[prisma:redis:middleware][DEBUG] create on User caused 1 keys to be deleted from cache.
[prisma:redis:middleware][DEBUG] deleteMany on Post caused 7 keys to be deleted from cache.

Keywords

FAQs

Package last updated on 11 Feb 2022

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