# prisma-redis-cache

> Streamlines caching Prisma operations using Redis with strong type-safety.

Latest version **1.0.0** (published 2024-06-26) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install prisma-redis-cache
pnpm add prisma-redis-cache
yarn add prisma-redis-cache
bun add prisma-redis-cache
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2024-06-26 |
| First published | 2024-04-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 1 |
| Unpacked size | 13.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 0 |
| Author | Gabriel434 |
| Keywords | prisma, extension, redis, cache |

## Links

- npm: https://www.npmjs.com/package/prisma-redis-cache
- Repository: https://github.com/Gabriel-434/prisma-redis-cache
- Homepage: https://github.com/Gabriel-434/prisma-redis-cache#readme
- Issues: https://github.com/Gabriel-434/prisma-redis-cache/issues
- npm.io page: https://npm.io/package/prisma-redis-cache

## Dependencies (1)

- [superjson](https://npm.io/package/superjson.md) ^2.2.1

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2024-06-26
- 0.2.4 — 2024-04-26
- 0.2.3 — 2024-04-07
- 0.2.2 — 2024-04-06
- 0.2.1 — 2024-04-06
- 0.2.0 — 2024-04-06

## README

# Redis cache for Prisma
<p align="center">
    <img src="./assets/SocialBanner.svg" width="600" title="Redis cache for Prisma banner">
</p>
<h3 align="center">Streamlines caching Prisma operations using Redis with strong type-safety</h3>

<p align="center">
    <a href="https://opensource.org/license/mit">
        <img alt="NPM License" src="https://img.shields.io/npm/l/prisma-redis-cache?logo=prisma">
    </a>
    <a href="https://www.npmjs.com/package/prisma-redis-cache">
        <img alt="NPM Version" src="https://img.shields.io/npm/v/prisma-redis-cache?logo=npm&label=version">
    </a>
    <a href="https://github.com/Gabriel-434/prisma-redis-cache/actions/workflows/CI.yml">
        <img src="https://github.com/Gabriel-434/prisma-redis-cache/actions/workflows/CI.yml/badge.svg" alt="Run Continuous Integration"/>
    </a>
    <img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/Gabriel-434/prisma-redis-cache">
</p>

## Quick setup
Install the [Prisma Extension](https://www.npmjs.com/package/prisma-redis-cache):
```sh
npm install prisma-redis-cache
```

Configure the extension:
```ts
import {PrismaClient} from "@prisma/client"
import {env} from "node:process"
import configureCache from "prisma-redis-cache"
import {createClient} from "redis"

const redis = await createClient({
    url: env.REDIS_CONNECTION_URL
}).connect()

const prisma = new PrismaClient().$extends(
    configureCache({
        redisClient: redis
    })
)
```

Example usage:
```ts
const user = await prisma.user.findUnique({
    where: {
        username: "Gabriel"
    },
    select: {
        username: true,
        email: true,
        password: true
    },
    // ˅˅ - The per-operation caching options
    cache: {
        // | A unique key for the cache.
        // ˅ The key is automatically prefixed with the model name to avoid overlaps.
        key: `Gabriel`,
        // ˅ The time-to-live of the cache, in seconds **/
        ttl: 60 * 15
    }
})
```

That's it! You can start caching your Prisma operations _right away_ 🎉

> [!NOTE]
> The extension has **JSDoc documentation** and supports **IDE auto-completion**.
All Prisma operations except raw queries can interact with the cache.

## Flexible & easy-to-use cache interaction

### Create a new row and cache the selected data
```ts
await prisma.user.create({
    data: {
        username: "user",
        email: "user@example.com",
        password: "TheHashedUserPassword"
    },
    select: {
        username: true,
        email: true,
        password: true
    },
    cache: {
        key: `user`,
        ttl: 60 * 60
    }
})
```

### Update a row and cache the selected data
```ts
await prisma.user.update({
    where: {
        username: "user"
    },
    data: {
        email: "veryOriginalEmail@example.com"
    },
    select: {
        username: true,
        email: true,
        password: true
    },
    cache: {
        key: `user`,
        // ˅ Whether to update or evict the cache
        update: true,
        ttl: 60 * 60
    }
})
```

## Delete a row and evict the cache
```ts
await prisma.user.delete({
    where: {
        username: "user"
    },
    cache: {
        key: `user`
    }
})
```

---
_Source: https://npm.io/package/prisma-redis-cache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
