@@ -21,10 +21,10 @@ export function cache(opts: CacheOptions) {
2121 * @param operation The operation to perform
2222 * @returns The base result.
2323 */
24- const callBaseMethod = async < Type , Operation extends SupportedOperation > (
24+ const callBaseMethod = async < Type , Operation extends SupportedOperation , Args > (
2525 model : string ,
2626 operation : Operation ,
27- args : Prisma . Args < Type , Operation > | undefined
27+ args ? : Prisma . Exact < Args , Prisma . Args < Type , Operation > >
2828 ) => {
2929 // eslint-disable-next-line @typescript-eslint/no-explicit-any
3030 return ( client [ model as any ] as any ) [ operation ] ( args ) as Prisma . Result <
@@ -39,15 +39,14 @@ export function cache(opts: CacheOptions) {
3939 * @param operation The operation to perform
4040 * @returns The overwritten method.
4141 */
42- const overrideMethod = <
43- Type ,
44- Operation extends SupportedOperation ,
45- Args extends Prisma . Args < Type , Operation > & CacheQueryArgs
46- > (
42+ const overrideMethod = < Type , Operation extends SupportedOperation , Args > (
4743 operation : Operation
4844 ) : ClientMethodWithCache < Type , Operation , Args > => {
49- return async function ( this : Type , args ?: Args ) {
50- type BaseResult = Prisma . Result < Type , Args , Operation > ;
45+ return async function (
46+ this : Type ,
47+ args ?: Prisma . Exact < Args , Prisma . Args < Type , Operation > & Partial < CacheQueryArgs > >
48+ ) {
49+ type BaseResult = Prisma . Result < Type , Omit < Args , 'cache' > , Operation > ;
5150 type CachedResult = { result : BaseResult } & CacheMetadata ;
5251
5352 const context = Prisma . getExtensionContext ( this ) ;
@@ -57,15 +56,18 @@ export function cache(opts: CacheOptions) {
5756 throw new Error ( 'context.$name is undefined' ) ;
5857 }
5958
60- args ??= { } as Required < Args > ;
59+ let queryCacheArgs : CacheConfig | undefined = undefined ;
6160
62- const cacheConfig =
63- ( args ?. cache as CacheConfig ) ??
64- opts . models ?. [ model ] ??
65- opts . default ??
66- DefaultCacheConfig ;
61+ if ( typeof args === 'object' && args !== null && ! Array . isArray ( args ) ) {
62+ // If we don't do this TypeScript complains about wrong types
63+ // Maybe there is a better way.
64+ const _args = args as Record < string , unknown > ;
65+ queryCacheArgs = ( _args . cache as CacheConfig ) ?? { } ;
66+ delete _args . cache ;
67+ }
6768
68- delete args . cache ;
69+ const cacheConfig =
70+ queryCacheArgs ?? opts . models ?. [ model ] ?? opts . default ?? DefaultCacheConfig ;
6971
7072 if ( cacheConfig . get ) {
7173 const cached = await db . get < CachedResult > ( { model, operation, args } ) ;
@@ -83,7 +85,7 @@ export function cache(opts: CacheOptions) {
8385 }
8486 }
8587
86- const base = await callBaseMethod < Type , Operation > ( model , operation , args ) ;
88+ const base = await callBaseMethod < Type , Operation , Args > ( model , operation , args ) ;
8789
8890 if ( cacheConfig . set ) {
8991 const cached_at = Date . now ( ) ;
@@ -101,55 +103,49 @@ export function cache(opts: CacheOptions) {
101103 name : 'prisma-cache' ,
102104 model : {
103105 $allModels : {
104- async findUnique < T , A extends Prisma . Args < T , 'findUnique' > & Partial < CacheQueryArgs > > (
106+ async findUnique < T , A > (
105107 this : T ,
106108 args ?: Prisma . Exact < A , Prisma . Args < T , 'findUnique' > & Partial < CacheQueryArgs > >
107109 ) {
108110 return overrideMethod < T , 'findUnique' , A > ( 'findUnique' ) . call ( this , args ) ;
109111 } ,
110- async findUniqueOrThrow <
111- T ,
112- A extends Prisma . Args < T , 'findUnique' > & Partial < CacheQueryArgs >
113- > (
112+ async findUniqueOrThrow < T , A > (
114113 this : T ,
115114 args ?: Prisma . Exact < A , Prisma . Args < T , 'findUniqueOrThrow' > & Partial < CacheQueryArgs > >
116115 ) {
117116 return overrideMethod < T , 'findUniqueOrThrow' , A > ( 'findUniqueOrThrow' ) . call ( this , args ) ;
118117 } ,
119- async findFirst < T , A extends Prisma . Args < T , 'findFirst' > & Partial < CacheQueryArgs > > (
118+ async findFirst < T , A > (
120119 this : T ,
121120 args ?: Prisma . Exact < A , Prisma . Args < T , 'findFirst' > & Partial < CacheQueryArgs > >
122121 ) {
123122 return overrideMethod < T , 'findFirst' , A > ( 'findFirst' ) . call ( this , args ) ;
124123 } ,
125- async findFirstOrThrow <
126- T ,
127- A extends Prisma . Args < T , 'findFirst' > & Partial < CacheQueryArgs >
128- > (
124+ async findFirstOrThrow < T , A > (
129125 this : T ,
130126 args ?: Prisma . Exact < A , Prisma . Args < T , 'findFirstOrThrow' > & Partial < CacheQueryArgs > >
131127 ) {
132128 return overrideMethod < T , 'findFirstOrThrow' , A > ( 'findFirstOrThrow' ) . call ( this , args ) ;
133129 } ,
134- async findMany < T , A extends Prisma . Args < T , 'findMany' > & Partial < CacheQueryArgs > > (
130+ async findMany < T , A > (
135131 this : T ,
136132 args ?: Prisma . Exact < A , Prisma . Args < T , 'findMany' > & Partial < CacheQueryArgs > >
137133 ) {
138134 return overrideMethod < T , 'findMany' , A > ( 'findMany' ) . call ( this , args ) ;
139135 } ,
140- async count < T , A extends Prisma . Args < T , 'count' > & Partial < CacheQueryArgs > > (
136+ async count < T , A > (
141137 this : T ,
142138 args ?: Prisma . Exact < A , Prisma . Args < T , 'count' > & Partial < CacheQueryArgs > >
143139 ) {
144140 return overrideMethod < T , 'count' , A > ( 'count' ) . call ( this , args ) ;
145141 } ,
146- async aggregate < T , A extends Prisma . Args < T , 'aggregate' > & Partial < CacheQueryArgs > > (
142+ async aggregate < T , A > (
147143 this : T ,
148144 args ?: Prisma . Exact < A , Prisma . Args < T , 'aggregate' > & Partial < CacheQueryArgs > >
149145 ) {
150146 return overrideMethod < T , 'aggregate' , A > ( 'aggregate' ) . call ( this , args ) ;
151147 } ,
152- async groupBy < T , A extends Prisma . Args < T , 'groupBy' > & Partial < CacheQueryArgs > > (
148+ async groupBy < T , A > (
153149 this : T ,
154150 args ?: Prisma . Exact < A , Prisma . Args < T , 'groupBy' > & Partial < CacheQueryArgs > >
155151 ) {
0 commit comments