2.0.1 • Published 2 years ago

@via-profit-services/dataloader v2.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Via Profit / dataloader

npm (scoped) NPM npm bundle size (scoped)

Table of Contents

Overview

This module is a fork of the package GraphQL DataLoader with some changes to allow the use of Redis cache with the ability to specify the expiration time of each of its elements.

Installation

We used IO Redis package as Redis client. You must install this dependency to.

# yarn
yarn add @via-profit-services/dataloader ioredis

# npm
npm i @via-profit-services/dataloader ioredis

Getting Started

Setup Reids using ioredis and pass it to constructor.

import DataLoader from '@via-profit-services/dataloader';
import Redis from 'ioredis';

const redis = new Redis();
const loader = new DataLoader({
  // ioredis instance
  redis,

  // Each dataloader creates its own cache in Redis.
  // Therefore, the `cacheName` must be a unique key in Redis.
  // It is better to use a meaningful name for the `cacheName`
  cacheName: 'books',

  // Default value of expiration time of each value placed in the cache
  // It will be used in cases of calling 'load` without the expires argument.
  // Format: digit + entity
  defaultExpiration: '12 hours',
});

// Now you can start the load your data
loader.load('fde7fcf7-984d-44b8-8504-d6347e105f56');
loader.load('34bcab6b-7207-4883-a442-92d21e53d31d');
loader.loadMany(['e3fd7057-858b-43f9-b276-0c631e0de1af', 'ba995e24-0d05-4de4-a12c-756c009f3620']);

API

Use TypeScript for more data abot API and arguments

NameArgumentsReturn typeDescription
loadkey: string, expires: string \| numberPromise<T\|null>Loads one entity by key
reloadkey: string, expires: string \| numberPromise<T\|null>Loads one entity by key and ignore cache, but put loaded data into the cache
reloadManykeys: string[], expires: string \| numberPromise<T[]>Loads one or more entities by keys and ignore cache, but put loaded data into the cache
loadManykeys: string[], expires: string \| numberPromise<T[]>Loads one ore more entities
clearkey: stringPromise<this>Clear one entity by key
clearManykeys: string[]Promise<this>Clear one ore more entities
clearAll-Promise<this>Clear all entities
primevalue: T, expires: string \| numberPromise<this>Put data to dataloader cache
primeManyvalues: T[], expires: string \| numberPromise<this>Put more then one data to dataloader cache
2.0.1

2 years ago

2.0.0

2 years ago