redis-om-node
redis-om-node copied to clipboard
Adapter API for third party clients i.e. ioredis
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> */
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.
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.
any progress on this ? @guyroyse
Any update? How do we use this library in conjugation with ioredis
?