npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prisma-extension-caching

v1.0.9

Published

Caching layer over prisma client

Downloads

62

Readme

Prisma Client Extension :: Prisma Native Caching

This extension adds the ability to cache complex queries using client-side prisms. The prism model is used as storage, so you can use any database that prisma support.

This extension uses modern databases json query features. Query arguments will be used as json caching key. You can use Prisma.JsonFilter in purge function to get maximum flexibility.

Usage:

  // Query arguments will be used as flexible caching key.
  const cachedPosts = await cache.post.findMany({ orderBy: { id: "desc" }, take: 10 })  
      .then(console.log)

  await cache.post.count({ where: { id: { gt: 100 } } })

  await cache.post.purge({ orderBy: { id: "desc" }, take: 10 }) // query exact match
  await cache.post.purge({ path: ['where', 'visible'], not: Prisma.DbNull }) // query JsonFilter match 
  await cache.post.purge({ hours: 3 }) // all model cache, older than 3 hours
  await cache.post.purge({ seconds: 60 }) // all model cache, older than 1 minute
  await cache.post.purge({ seconds: 10 }, "findMany") // purge findMany method cache, older than 10 seconds 
  await cache.post.purge({ orderBy: { id: "desc" }, take: 10 }, "findMany") // query cache for method
  await cache.post.purge("findMany") // all model cache for method
  await cache.post.purge() // all model cache data

Get started

Installation: Add this model to your prisma schema.

model cache {
  model String
  operation String
  key   Json
  value Json

  created DateTime @default(now())
  updated DateTime @default(now())

  @@id([model, operation, key])
}

Create caching client:

import { caching } from "prisma-extension-caching" 

const prisma = new PrismaClient()
const cache = prisma.$extends(caching()) 

That's all. Now you can use it as second client.

Supported methods

findMany
groupBy
aggregate
count*
findUnique*
findFirst*

*findUnique,findFirst,count - if original query return null, result won't be saved in cache. Any other method will produce standart database query without caching.

FAQ:

How it work? - First time query executed in database, rest will be served from cache until being purged manually, by calling purge() method. Can i disable cache for specific method/model? - No. Use main or second client instead. Can i purge cache for all models by calling one root method? - No. Only by calling purge() for every model.

More Examples:

  // Purge by JsonFilter is good, when you have dynamic query param cache
  const currentUser = 1; // getCurrentUser()
  const userPosts = await cache.post.findMany({
    where: {
      author: currentUser
    }
  })
  // will purge all cache by query route match, ignoring currentUser difference
  await cache.post.purge({ path: ['where', 'author'], not: Prisma.DbNull })

  /* You can use findUnique as materialised view */
  // const materialisedView = await cache.post.findUnique({
  //   include: {
  //     comments: true,
  //     author: true,
  //     links: true,
  //     metadata: true,
  //     ...
  //   }
  // })

  /** If you have complex query and you want to make key more readable,
   *  you can export query as separate object, and tag it explicit  */
  // Named key concept 
  const cacheKey = { select: { title: true } } as const
  const posts = await cache.post.findMany(cacheKey)
  // purge with key
  await cache.post.purge(cacheKey)

  // Typed key concept 
  type cacheKey = { select: {} }
  const postsCatalogView: cacheKey = { select: { title: true } }
  const data = await cache.post.findMany(postsCatalogView)
  // purge with key
  await cache.post.purge(postsCatalogView)

  // Tip: if you have same query params in model, you should purge cache apart
  await cache.post.count({ where: { id: { gt: 100 } } })
  await cache.post.findMany({ where: { id: { gt: 100 } } })
  await cache.post.purge({ where: { id: { gt: 100 } } }) // will purge both caches, because query params are match
  await cache.post.purge({ where: { id: { gt: 100 } } }, 'count') // will purge cache only for count mehtod

  // query without caching
  // ex: const prisma = new PrismaClient()
  // const originalPosts = await prisma.post.findMany({ orderBy: { id: "desc" }, take: 10 })

The example app:

cd example
npm install
npx prisma db push

Test the extension in the example app:

npm run dev