Skip to content

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

@macintoshhelper

Description

@macintoshhelper

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> */

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions