Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
some more type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Asjas committed Jul 7, 2022
1 parent f45535e commit 305b9bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
41 changes: 19 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@ import { createCache } from "async-cache-dedupe";

import { defaultCacheMethods, defaultMutationMethods } from "./cacheMethods";

import type {
CreatePrismaRedisCache,
FetchFromPrisma,
Middleware,
MiddlewareParams,
PrismaAction,
PrismaQueryAction,
PrismaMutationAction,
Result,
} from "./types";
import * as Prisma from "./types";

const DEFAULT_CACHE_TIME = 0;

Expand All @@ -25,7 +16,7 @@ export const createPrismaRedisCache = ({
cacheTime = DEFAULT_CACHE_TIME,
excludeModels = [],
excludeMethods = [],
}: CreatePrismaRedisCache) => {
}: Prisma.CreatePrismaRedisCache) => {
// Default options for "async-cache-dedupe"
const cacheOptions = {
onDedupe,
Expand All @@ -37,17 +28,17 @@ export const createPrismaRedisCache = ({
};

// Do not cache any Prisma method specified in the defaultExcludeCacheMethods option
const excludedCacheMethods: PrismaAction[] = defaultCacheMethods.filter((cacheMethod) => {
const excludedCacheMethods: Prisma.PrismaAction[] = defaultCacheMethods.filter((cacheMethod) => {
return !excludeMethods.includes(cacheMethod);
});

const cache: any = createCache(cacheOptions);

const middleware: Middleware = async (params, next) => {
let result: Result;
const middleware: Prisma.Middleware = async (params, next) => {
let result: Prisma.Result;

// This function is used by `async-cache-dedupe` to fetch data when the cache is empty
const fetchFromPrisma: FetchFromPrisma = async (params) => {
const fetchFromPrisma: Prisma.FetchFromPrisma = async (params) => {
return next(params);
};

Expand All @@ -60,17 +51,23 @@ export const createPrismaRedisCache = ({
!cache[model] &&
params.model &&
!excludeModels?.includes(params.model) &&
!excludeMethods?.includes(params.action as PrismaQueryAction)
!excludeMethods?.includes(params.action as Prisma.PrismaQueryAction)
) {
cache.define(
model,
{
references: ({ params }: { params: MiddlewareParams }, key: string, result: Result) => {
references: ({ params }: { params: Prisma.MiddlewareParams }, key: string, result: Prisma.Result) => {
return result ? [`${cacheKey || params.model}~${key}`] : null;
},
ttl: cacheTime || cacheOptions.ttl,
},
async function modelsFetch({ cb, params }: { cb: FetchFromPrisma; params: MiddlewareParams }) {
async function modelsFetch({
cb,
params,
}: {
cb: Prisma.FetchFromPrisma;
params: Prisma.MiddlewareParams;
}) {
result = await cb(params);

return result;
Expand All @@ -95,16 +92,16 @@ export const createPrismaRedisCache = ({
params.model &&
!cache[params.model] &&
!excludeModels?.includes(params.model) &&
!excludedCacheMethodsInModels?.excludeMethods?.includes(params.action as PrismaQueryAction)
!excludedCacheMethodsInModels?.excludeMethods?.includes(params.action as Prisma.PrismaQueryAction)
) {
cache.define(
params.model,
{
references: ({ params }: { params: MiddlewareParams }, key: string, result: Result) => {
references: ({ params }: { params: Prisma.MiddlewareParams }, key: string, result: Prisma.Result) => {
return result ? [`${params.model}~${key}`] : null;
},
},
async function modelFetch({ cb, params }: { cb: FetchFromPrisma; params: MiddlewareParams }) {
async function modelFetch({ cb, params }: { cb: Prisma.FetchFromPrisma; params: Prisma.MiddlewareParams }) {
result = await cb(params);

return result;
Expand Down Expand Up @@ -136,7 +133,7 @@ export const createPrismaRedisCache = ({
result = await fetchFromPrisma(params);

// If we successfully executed the Mutation we clear and invalidate the cache for the Prisma model
if (defaultMutationMethods.includes(params.action as PrismaMutationAction)) {
if (defaultMutationMethods.includes(params.action as Prisma.PrismaMutationAction)) {
await cache.invalidateAll(`${params.model}~*`);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ export type PrismaAction = PrismaQueryAction | PrismaMutationAction;

export type Result = Record<string, unknown> | Record<string, unknown>[];

export type ModelName = "ModelName";

/**
* These options are being passed in to the middleware as "params"
* https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#params
*/
export type MiddlewareParams = {
model?: string;
model?: ModelName;
action: PrismaAction;
args: any;
dataPath: string[];
Expand Down

0 comments on commit 305b9bd

Please sign in to comment.