Skip to content

Commit 207909c

Browse files
committed
fix: set DataLoaderCache implements DataLoader
1 parent a7af10f commit 207909c

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

.changeset/shaky-coats-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@labdigital/dataloader-cache-wrapper': patch
3+
---
4+
5+
Fix type mismastch with DataLoader

src/wrapper.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type Options<K, V, C = K> = {
1010
};
1111
cacheKeyFn: (key: K) => string;
1212
maxBatchSize?: number;
13-
name?: string;
13+
name?: string | null;
1414
};
1515

1616
/**
@@ -19,19 +19,21 @@ type Options<K, V, C = K> = {
1919
* cacheKeyFn to generate the cache key (which needs to return a string).
2020
*
2121
*/
22-
export class DataLoaderCache<K extends NotUndefined, V> {
22+
export class DataLoaderCache<K extends NotUndefined, V>
23+
implements DataLoader<K, V | null>
24+
{
2325
_dataloader: DataLoader<K, V | null>;
2426
_cacheStore?: Keyv<V | null>;
2527
_cacheKeyFn: (key: K) => string;
2628

27-
name: string | undefined;
29+
name: string | null;
2830

2931
constructor(
3032
batchLoadFn: BatchLoadFn<K, V | null>,
3133
options: Options<K, V, string>,
3234
) {
3335
this._cacheKeyFn = options.cacheKeyFn;
34-
this.name = options.name;
36+
this.name = options.name ?? null;
3537

3638
let wrappedBatchLoadFn: BatchLoadFn<K, V | null>;
3739
if (options.cache) {
@@ -61,8 +63,19 @@ export class DataLoaderCache<K extends NotUndefined, V> {
6163
});
6264
}
6365

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;
6679
}
6780

6881
async load(key: K): Promise<V | null> {
@@ -73,14 +86,14 @@ export class DataLoaderCache<K extends NotUndefined, V> {
7386
return this._dataloader.loadMany(keys);
7487
}
7588

76-
async clear(key: K) {
89+
async clearCache(key: K) {
7790
if (this._cacheStore) {
7891
await this._cacheStore.delete(this._cacheKeyFn(key));
7992
}
8093
return this._dataloader.clear(key);
8194
}
8295

83-
async clearAll() {
96+
async clearCacheAll() {
8497
if (this._cacheStore) {
8598
await this._cacheStore.clear();
8699
}

0 commit comments

Comments
 (0)