0.2.4 • Published 9 days ago

prisma-redis-cache v0.2.4

Weekly downloads
-
License
MIT
Repository
github
Last release
9 days ago

Redis cache for Prisma

Quick setup

Install the Prisma Extension:

npm install prisma-redis-cache

Configure the extension:

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:

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

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

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

await prisma.user.delete({
    where: {
        username: "user"
    },
    cache: {
        key: `user`
    }
})
0.2.4

9 days ago

0.2.3

28 days ago

0.2.1

29 days ago

0.2.0

29 days ago

0.1.4

1 month ago

0.2.2

29 days ago

0.1.3

1 month ago

0.1.2

1 month ago

0.1.1

1 month ago

0.1.0

1 month ago