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

https://github.com/ackeecz/kesha

Caching utils based on ioredis and dataloaders
https://github.com/ackeecz/kesha

backend

Last synced: 6 months ago
JSON representation

Caching utils based on ioredis and dataloaders

Awesome Lists containing this project

README

          

![](logo.png)

# Kesha

Caching utils based on [ioredis](https://www.npmjs.com/package/ioredis)
and [dataloaders](https://github.com/graphql/dataloader)

## Quick start ๐Ÿš€

- Initialize [ioredis](https://www.npmjs.com/package/ioredis) client

```typescript
import Redis from 'ioredis';

const redisClient = new Redis({
// Host, DB,...
})
```

- Use exported utils directly f.e. [rateLimiter](src/lib/rateLimiter.ts)

```typescript
import { rateLimiter } from 'kesha';

const rateLimitedFn = rateLimiter(redisClient, (number: number) => {
console.log('Called')
}, '', 1000)
```

- or create set of utils for client with [createRedisRepository](src/lib/redisRepository.ts)

```typescript
import { createRedisRepository } from 'kesha';

const repo = createRedisRepository(client)

const rateLimitedFn = repo.rateLimiter((number: number) => {
console.log('Called')
}, '', 1000)
```

- Provide typehints for `setJSON` and `getJSON` methods

```typescript
// Per key
const repoPerKey = createRedisRepository<{ 'test:key': string }>(client)

repoPerKey.setJSON('test:key', {}) // TS Error: {} is not assignable to string

// Using template litarals to define key prefixes (From TS 4.4+)
const repo = createRedisRepository<{ [K: `test:key:${number}`]: string }>(client)

repo.setJSON('test:key:1', {}) // TS Error: {} is not assignable to string
repo.getJSON('test:key:1') // TS: Returns string
repo.getJSON('test:key:a') // TS: Returns any

```

## Tests ๐Ÿงช

- Prepare docker container with Redis ๐Ÿณ

```bash
sudo docker-compose -f docker-compose/docker-compose.yml up
```

- Run the tests

```bash
npm run test
```