Skip to content

abhiaiyer91/prisma-lrucache-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

prisma-lrucache-middleware

Prisma (2) Client middleware.

Pass an LRU Cache to cache Prisma query results.

Only caches these actions

  • findOne
  • findMany
  • queryRaw
  • aggregate

Required Reading

Middlewares are an experimental feature. Read more about them here

Quick Start

Install the package using yarn:

yarn add prisma-lrucache-middleware

Feature flag

Middlewares need to be enabled with the feature flag middlewares like so:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["middlewares"]
}

Code

import { PrismaClient } from "@prisma/client";
import { createLRUCacheMiddleware } from "prisma-lrucache-middleware";
import * as LRU from "lru-cache";

const db = new PrismaClient();

const UserCache = new LRU(50);

db.use(createLRUCacheMiddleware({ model: `User`, cache: UserCache }));

// The first time
let user = await db.user.findOne({ where: { id: "1111" } });

// From cache
user = await db.user.findOne({ where: { id: "1111" } });

const PostCache = new LRU({
  max: 500,
  maxAge: 1000 * 60 * 60,
});

db.use(createLRUCacheMiddleware({ model: `Post`, cache: PostCache }));

About

Prisma Middleware for caching results of prisma queries

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published