ioredis icon indicating copy to clipboard operation
ioredis copied to clipboard

`addBuiltinCommand` cannot customize function name for prefixed commands

Open eddyw opened this issue 3 years ago • 0 comments

Hi,

Currently there is only one way to extend ioredis using addBuiltinCommand. However, it doesn't allow setting a custom function name. I'm currently trying to extend ioredis to support commands from RedisGears, RedisTimeSeries, and RedisSearch. Commands from these Redis modules are prefixed like so RG.[Command], TS.[Command], and FT.[Command], so using for example:

redis.createBuiltinCommand("ft._list")

generates redis["ft._list"] method which is kinda weird.

Would you consider adding a second param to createBuiltinCommand to customize the method/function name? This would look like:

redis.createBuiltinCommand("ft._list", /* Optional */ "ftList")
// (...)
redis.ftList(...) // instead of redis["ft._list"](...)

The current ugly workaround I have is this:

export function createBuiltinCommand(redis: any, functionName: string, commandName: string) {
	/**
	 * @see https://github.com/luin/ioredis/blob/72f31b265085c40e496813ff68c15ad14e512739/lib/commander.ts#L66-L81
	 */
	redis.addBuiltinCommand(commandName);

	if (functionName !== commandName) {
		const stringCmd = redis[commandName];
		const bufferCmd = redis[commandName + "Buffer"];

		delete redis[commandName];
		delete redis[commandName + "Buffer"];

		redis[functionName] = stringCmd;
		redis[functionName + "Buffer"] = bufferCmd;

		/**
		 * @see https://github.com/luin/ioredis/blob/72f31b265085c40e496813ff68c15ad14e512739/lib/pipeline.ts#L50-L53
		 */
		redis.addedBuiltinSet.delete(commandName);
		redis.addedBuiltinSet.add(functionName);
	}
}

If the behavior is not intentional and you'd be willing to accept this change, I guess I could even try opening a PR. There are some open questions though, like what to do if a command is already defined (current implementation just overwrite)

eddyw avatar Nov 20 '21 18:11 eddyw