Prisma (2) Client middleware.
Pass an LRU Cache to cache Prisma query results.
Only caches these actions
findOne
findMany
queryRaw
aggregate
Middlewares are an experimental feature. Read more about them here
Install the package using yarn
:
yarn add prisma-lrucache-middleware
Middlewares need to be enabled with the feature flag middlewares like so:
generator client {
provider = "prisma-client-js"
previewFeatures = ["middlewares"]
}
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 }));