prisma-lrucache-middleware

1.0.1 • Public • Published

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 }));

Readme

Keywords

none

Package Sidebar

Install

npm i prisma-lrucache-middleware

Weekly Downloads

32

Version

1.0.1

License

MIT

Unpacked Size

5.99 kB

Total Files

4

Last publish

Collaborators

  • abhiaiyer