Skip to content

Commit 305b9bd

Browse files
author
A-J Roos
committed
some more type fixes
1 parent f45535e commit 305b9bd

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

src/index.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,7 @@ import { createCache } from "async-cache-dedupe";
22

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

5-
import type {
6-
CreatePrismaRedisCache,
7-
FetchFromPrisma,
8-
Middleware,
9-
MiddlewareParams,
10-
PrismaAction,
11-
PrismaQueryAction,
12-
PrismaMutationAction,
13-
Result,
14-
} from "./types";
5+
import * as Prisma from "./types";
156

167
const DEFAULT_CACHE_TIME = 0;
178

@@ -25,7 +16,7 @@ export const createPrismaRedisCache = ({
2516
cacheTime = DEFAULT_CACHE_TIME,
2617
excludeModels = [],
2718
excludeMethods = [],
28-
}: CreatePrismaRedisCache) => {
19+
}: Prisma.CreatePrismaRedisCache) => {
2920
// Default options for "async-cache-dedupe"
3021
const cacheOptions = {
3122
onDedupe,
@@ -37,17 +28,17 @@ export const createPrismaRedisCache = ({
3728
};
3829

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

4435
const cache: any = createCache(cacheOptions);
4536

46-
const middleware: Middleware = async (params, next) => {
47-
let result: Result;
37+
const middleware: Prisma.Middleware = async (params, next) => {
38+
let result: Prisma.Result;
4839

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

@@ -60,17 +51,23 @@ export const createPrismaRedisCache = ({
6051
!cache[model] &&
6152
params.model &&
6253
!excludeModels?.includes(params.model) &&
63-
!excludeMethods?.includes(params.action as PrismaQueryAction)
54+
!excludeMethods?.includes(params.action as Prisma.PrismaQueryAction)
6455
) {
6556
cache.define(
6657
model,
6758
{
68-
references: ({ params }: { params: MiddlewareParams }, key: string, result: Result) => {
59+
references: ({ params }: { params: Prisma.MiddlewareParams }, key: string, result: Prisma.Result) => {
6960
return result ? [`${cacheKey || params.model}~${key}`] : null;
7061
},
7162
ttl: cacheTime || cacheOptions.ttl,
7263
},
73-
async function modelsFetch({ cb, params }: { cb: FetchFromPrisma; params: MiddlewareParams }) {
64+
async function modelsFetch({
65+
cb,
66+
params,
67+
}: {
68+
cb: Prisma.FetchFromPrisma;
69+
params: Prisma.MiddlewareParams;
70+
}) {
7471
result = await cb(params);
7572

7673
return result;
@@ -95,16 +92,16 @@ export const createPrismaRedisCache = ({
9592
params.model &&
9693
!cache[params.model] &&
9794
!excludeModels?.includes(params.model) &&
98-
!excludedCacheMethodsInModels?.excludeMethods?.includes(params.action as PrismaQueryAction)
95+
!excludedCacheMethodsInModels?.excludeMethods?.includes(params.action as Prisma.PrismaQueryAction)
9996
) {
10097
cache.define(
10198
params.model,
10299
{
103-
references: ({ params }: { params: MiddlewareParams }, key: string, result: Result) => {
100+
references: ({ params }: { params: Prisma.MiddlewareParams }, key: string, result: Prisma.Result) => {
104101
return result ? [`${params.model}~${key}`] : null;
105102
},
106103
},
107-
async function modelFetch({ cb, params }: { cb: FetchFromPrisma; params: MiddlewareParams }) {
104+
async function modelFetch({ cb, params }: { cb: Prisma.FetchFromPrisma; params: Prisma.MiddlewareParams }) {
108105
result = await cb(params);
109106

110107
return result;
@@ -136,7 +133,7 @@ export const createPrismaRedisCache = ({
136133
result = await fetchFromPrisma(params);
137134

138135
// If we successfully executed the Mutation we clear and invalidate the cache for the Prisma model
139-
if (defaultMutationMethods.includes(params.action as PrismaMutationAction)) {
136+
if (defaultMutationMethods.includes(params.action as Prisma.PrismaMutationAction)) {
140137
await cache.invalidateAll(`${params.model}~*`);
141138
}
142139
}

src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ export type PrismaAction = PrismaQueryAction | PrismaMutationAction;
2929

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

32+
export type ModelName = "ModelName";
33+
3234
/**
3335
* These options are being passed in to the middleware as "params"
3436
* https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#params
3537
*/
3638
export type MiddlewareParams = {
37-
model?: string;
39+
model?: ModelName;
3840
action: PrismaAction;
3941
args: any;
4042
dataPath: string[];

0 commit comments

Comments
 (0)