Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gabriel-434/prisma-redis-cache

Streamlines caching Prisma operations using Redis with strong type-safety.
https://github.com/gabriel-434/prisma-redis-cache

cache extension nodejs orm prisma redis typesafe typesafety typescript

Last synced: 5 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# Redis cache for Prisma




Streamlines caching Prisma operations using Redis with strong type-safety



NPM License


NPM Version


Run Continuous Integration

GitHub Repo stars

## 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: "[email protected]",
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: "[email protected]"
},
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`
}
})
```