Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/gabriel-434/prisma-redis-cache
- Owner: Gabriel-434
- License: mit
- Created: 2024-03-30T03:57:41.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-06-26T01:07:26.000Z (5 months ago)
- Last Synced: 2024-11-02T05:48:26.303Z (13 days ago)
- Topics: cache, extension, nodejs, orm, prisma, redis, typesafe, typesafety, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/prisma-redis-cache
- Size: 46.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Redis cache for Prisma
Streamlines caching Prisma operations using Redis with strong type-safety
## 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`
}
})
```