redis-om-node icon indicating copy to clipboard operation
redis-om-node copied to clipboard

Adapter API for third party clients i.e. ioredis

Open macintoshhelper opened this issue 2 years ago • 4 comments

Hi, it would be great to have some kind of adapter API for third party Redis clients such as ioredis. This would eliminate the need to mix ioredis and redis usage in an existing codebase which uses ioredis.

I’ve hacked together support for ioredis (not thoroughly tested yet) by doing this:

ioredis-client-adapter.js

import { Redis } from 'ioredis';

export const createClient = (redis: Redis) => {
  redis.hGetAll = redis.hgetall;
  redis.hSet = redis.hset;
  redis.executeIsolated = redis.exec;
  redis.__client = 'ioredis'

  return redis;
};

Patched redis-om Client file

/* <PATCH-START> */
  async jsonget(key) {
    this.validateRedisOpen();
    const json = this.redis.__client && this.redis.__client === 'ioredis'
      ? await this.redis.call('JSON.GET', [key, "."])
      : await this.redis.sendCommand<string>(['JSON.GET', key, '.'])
    return JSON.parse(json);
  }
  async jsonset(key, data) {
    this.validateRedisOpen();
    const json = JSON.stringify(data);
    if (this.redis.__client && this.redis.__client === 'ioredis') {
      await this.redis.call('JSON.SET', [key, ".", json]);
    } else {
      await this.redis.sendCommand<string>(['JSON.SET', key, '.', json]);
    }
  }
/* <PATCH-END> */

macintoshhelper avatar Oct 02 '22 15:10 macintoshhelper

Not at the top of my list at the moment but I think this is a good idea. I'll mark it as an enhancement and see about including it in 1.0.

guyroyse avatar Oct 03 '22 13:10 guyroyse

Not at the top of my list at the moment but I think this is a good idea. I'll mark it as an enhancement and see about including it in 1.0.

Thanks! Would be great. I’d be happy to have a go at implementing this myself and opening a PR.

macintoshhelper avatar Oct 03 '22 17:10 macintoshhelper

any progress on this ? @guyroyse

vamshi9666 avatar Feb 24 '23 04:02 vamshi9666

Any update? How do we use this library in conjugation with ioredis?

tomerh2001 avatar Apr 26 '24 17:04 tomerh2001