@@ -10,7 +10,7 @@ type Options<K, V, C = K> = {
10
10
} ;
11
11
cacheKeyFn : ( key : K ) => string ;
12
12
maxBatchSize ?: number ;
13
- name ?: string ;
13
+ name ?: string | null ;
14
14
} ;
15
15
16
16
/**
@@ -19,19 +19,21 @@ type Options<K, V, C = K> = {
19
19
* cacheKeyFn to generate the cache key (which needs to return a string).
20
20
*
21
21
*/
22
- export class DataLoaderCache < K extends NotUndefined , V > {
22
+ export class DataLoaderCache < K extends NotUndefined , V >
23
+ implements DataLoader < K , V | null >
24
+ {
23
25
_dataloader : DataLoader < K , V | null > ;
24
26
_cacheStore ?: Keyv < V | null > ;
25
27
_cacheKeyFn : ( key : K ) => string ;
26
28
27
- name : string | undefined ;
29
+ name : string | null ;
28
30
29
31
constructor (
30
32
batchLoadFn : BatchLoadFn < K , V | null > ,
31
33
options : Options < K , V , string > ,
32
34
) {
33
35
this . _cacheKeyFn = options . cacheKeyFn ;
34
- this . name = options . name ;
36
+ this . name = options . name ?? null ;
35
37
36
38
let wrappedBatchLoadFn : BatchLoadFn < K , V | null > ;
37
39
if ( options . cache ) {
@@ -61,8 +63,19 @@ export class DataLoaderCache<K extends NotUndefined, V> {
61
63
} ) ;
62
64
}
63
65
64
- prime ( key : K , value : V | null ) {
65
- return this . _dataloader . prime ( key , value ) ;
66
+ prime ( key : K , value : V | PromiseLike < V > | Error ) {
67
+ this . _dataloader . prime ( key , value ) ;
68
+ return this ;
69
+ }
70
+
71
+ clear ( key : K ) {
72
+ this . _dataloader . clear ( key ) ;
73
+ return this ;
74
+ }
75
+
76
+ clearAll ( ) {
77
+ this . _dataloader . clearAll ( ) ;
78
+ return this ;
66
79
}
67
80
68
81
async load ( key : K ) : Promise < V | null > {
@@ -73,14 +86,14 @@ export class DataLoaderCache<K extends NotUndefined, V> {
73
86
return this . _dataloader . loadMany ( keys ) ;
74
87
}
75
88
76
- async clear ( key : K ) {
89
+ async clearCache ( key : K ) {
77
90
if ( this . _cacheStore ) {
78
91
await this . _cacheStore . delete ( this . _cacheKeyFn ( key ) ) ;
79
92
}
80
93
return this . _dataloader . clear ( key ) ;
81
94
}
82
95
83
- async clearAll ( ) {
96
+ async clearCacheAll ( ) {
84
97
if ( this . _cacheStore ) {
85
98
await this . _cacheStore . clear ( ) ;
86
99
}
0 commit comments