redis / redis-om-node

Object mapping, and more, for Redis and Node.js. Written in TypeScript.
MIT License
1.18k stars 80 forks source link

Adapter API for third party clients i.e. ioredis #136

Open macintoshhelper opened 2 years ago

macintoshhelper commented 2 years ago

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> */
guyroyse commented 2 years ago

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.

macintoshhelper commented 2 years ago

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.

vamshi9666 commented 1 year ago

any progress on this ? @guyroyse

tomerh2001 commented 6 months ago

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