0.3.0 • Published 7 months ago

prisma-extension-cache-manager v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

prisma-extension-cache-manager

A caching extension for Prisma, compatible with cache-manager.

Features

  • cache-manager compatibility
  • Only model queries can be cacheable (no $query or $queryRaw)
  • Uses object-code as default key generator, but you can pass a custom one
  • In-memory cache is recommended, since types like Date or Prisma.Decimal would be lost if using JSON serialization (maybe will try to use some binary serialization in the future)

Installation

Install:

npm i prisma-extension-cache-manager

Usage

import { PrismaClient } from '@prisma/client';
import * as cm from 'cache-manager';
import cacheExtension from 'prisma-extension-cache-manager';

async function main() {
  const cache = await cm.caching('memory', {
    ttl: 10000,
    max: 200,
  });
  const prisma = new PrismaClient().$extends(cacheExtension({ cache }));
  await prisma.user.findUniqueOrThrow({
    where: {
      email: user.email,
    },
    cache: true, // using cache default settings
  });
  await prisma.user.findMany({
    cache: 5000, // setting ttl in milliseconds
  });
  await prisma.user.count({
    cache: {
      ttl: 2000,
      key: 'user_count', // custom cache key
    },
  });
}

main().catch(console.error);

Learn more

0.3.0

7 months ago

0.2.2

8 months ago

0.2.1

8 months ago

0.2.0

8 months ago

0.1.0

8 months ago