0.1.4 • Published 2 years ago

prisma-cache-middleware v0.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Prisma cache middleware

Prisma is one of the greatest ORMs for Node.js with lot of features, with this middleware you can cache your database queries into the Redis (one of the fastest in-memory databases for caching) and reduce your database queries. You need to have pre-installed redis server in order to work with this module.

Install

npm i prisma-cache-middleware

Basic Example

import { PrismaClient } from '@prisma/client'
import prismaCacheMiddleware from 'prisma-cache-middleware';

const prisma = new PrismaClient();

prisma.$use(prismaCacheMiddleware({
    redisOptions: {                    
        host: 'localhost',             
        port: 6379                     
    },
    instances: [{                      
        model: 'Users',                
        action: 'findFirst',           
        ttl: 20,                       
        keyPrefix: 'myCache'             
    },{                                
        model: 'Users',
        action: 'findMany',
    }]
}));

export default prisma;

Prisma cache middleware options

OptionDescription
redisOptionsThis is your redis server configuration (same as ioredis options)
instancesCache instances and the way you want to cache them

If you pass {} to redisOptions it will connect to your local running redis server with default port. otherwise you can specified your server settings, for example {host: '127.0.0.1', port: 6379}.

Instances

OptionDescriptionOptionalExample
modelThe Prisma database model name you define in your schema.prismafalseUsers
actionQuery action name you want to cache for this model.falsefindFirst
ttlThe TTL or expire time of the cache in secondstrue10
keyPrefixKey prefix for caches of this instancetruemyCache

You can defind cache instances as much as you want and pass array of instances to instances option.

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago