An open API service indexing awesome lists of open source software.

https://github.com/labd/dataloader-cache-wrapper


https://github.com/labd/dataloader-cache-wrapper

Last synced: 4 months ago
JSON representation

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}`];
},
})
}
```