prisma-lrucache-middleware
v1.0.1
Published
[Prisma](https://www.prisma.io/) (2) Client middleware.
Downloads
86
Readme
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 }));
const PostCache = new LRU({
max: 500,
maxAge: 1000 * 60 * 60,
});
db.use(createLRUCacheMiddleware({ model: `Post`, cache: PostCache }));