https://github.com/labd/dataloader-cache-wrapper
https://github.com/labd/dataloader-cache-wrapper
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/labd/dataloader-cache-wrapper
- Owner: labd
- License: mit
- Created: 2023-05-15T07:02:50.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-30T18:49:18.000Z (about 1 year ago)
- Last Synced: 2025-12-02T09:32:15.921Z (7 months ago)
- Language: TypeScript
- Size: 209 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# node-dataloader-cache-wrapper
## Usage
```ts
import { DataLoaderCache } from "@labdigital/dataloader-cache-wrapper"
const loader = new DataLoaderCache(ProductDataLoader, {
cache: {
storeFn: () => new Keyv(),
ttl: 3600,
},
cacheKeyFn: (ref: ProdProductReferenceuctRef) => {
const key = `${ref.store}-${ref.locale}-${ref.currency}`;
return `some-data:${key}:id:${ref.slug}`
},
maxBatchSize: 50,
});
```
Or use the older API with the `dataloaderCache` function:
```ts
import { dataloaderCache } from "@labdigital/dataloader-cache-wrapper"
export const createProductBySlugLoader = () => {
return new DataLoader(ProductDataLoader, {
maxBatchSize: 50,
});
};
export const ProductDataLoader = async (keys: readonly any[]): Promise<(Product | null)[]> => {
return dataloaderCache(_uncachedProductDataLoader, keys, {
store: new Keyv(),
ttl: 3600,
cacheKeysFn: (ref: ProductReference) => {
const key = `${ref.store}-${ref.locale}-${ref.currency}`;
return [`some-data:${key}:id:${ref.slug}`];
},
})
}
```