You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-09 00:22:08 +03:00
ref #1653 - better types
This commit is contained in:
@@ -21,7 +21,8 @@ type WithScripts<M extends RedisModules, S extends RedisLuaScripts> = {
|
||||
[P in keyof S]: RedisMultiCommandSignature<S[P], M, S>
|
||||
};
|
||||
|
||||
export type RedisMultiCommandType<M extends RedisModules, S extends RedisLuaScripts> = RedisMultiCommand & WithCommands<M, S> & WithModules<M, S> & WithScripts<M, S>;
|
||||
export type RedisMultiCommandType<M extends RedisModules = {}, S extends RedisLuaScripts = {}> =
|
||||
RedisMultiCommand<M, S> & WithCommands<M, S> & WithModules<M, S> & WithScripts<M, S>;
|
||||
|
||||
export interface MultiQueuedCommand {
|
||||
args: TransformArgumentsReply;
|
||||
@@ -31,60 +32,20 @@ export interface MultiQueuedCommand {
|
||||
|
||||
export type RedisMultiExecutor = (queue: Array<MultiQueuedCommand>, chainId?: symbol) => Promise<Array<RedisReply>>;
|
||||
|
||||
export default class RedisMultiCommand<M extends RedisModules = RedisModules, S extends RedisLuaScripts = RedisLuaScripts> {
|
||||
static commandsExecutor(this: RedisMultiCommand, command: RedisCommand, args: Array<unknown>): RedisMultiCommand {
|
||||
return this.addCommand(
|
||||
command.transformArguments(...args),
|
||||
command.transformReply
|
||||
);
|
||||
}
|
||||
|
||||
static #scriptsExecutor(
|
||||
this: RedisMultiCommand,
|
||||
script: RedisLuaScript,
|
||||
args: Array<unknown>
|
||||
): RedisMultiCommand {
|
||||
const transformedArguments: TransformArgumentsReply = [];
|
||||
if (this.#scriptsInUse.has(script.SHA1)) {
|
||||
transformedArguments.push(
|
||||
'EVALSHA',
|
||||
script.SHA1
|
||||
);
|
||||
} else {
|
||||
this.#scriptsInUse.add(script.SHA1);
|
||||
transformedArguments.push(
|
||||
'EVAL',
|
||||
script.SCRIPT
|
||||
);
|
||||
}
|
||||
|
||||
transformedArguments.push(script.NUMBER_OF_KEYS.toString());
|
||||
|
||||
const scriptArguments = script.transformArguments(...args);
|
||||
transformedArguments.push(...scriptArguments);
|
||||
if (scriptArguments.preserve) {
|
||||
transformedArguments.preserve = scriptArguments.preserve;
|
||||
}
|
||||
|
||||
return this.addCommand(
|
||||
transformedArguments,
|
||||
script.transformReply
|
||||
);
|
||||
}
|
||||
|
||||
export default class RedisMultiCommand<M extends RedisModules, S extends RedisLuaScripts> {
|
||||
static extend<M extends RedisModules, S extends RedisLuaScripts>(
|
||||
clientOptions?: RedisClientOptions<M, S>
|
||||
): new (...args: ConstructorParameters<typeof RedisMultiCommand>) => RedisMultiCommandType<M, S> {
|
||||
return <any>extendWithModulesAndScripts({
|
||||
BaseClass: RedisMultiCommand,
|
||||
modules: clientOptions?.modules,
|
||||
modulesCommandsExecutor: RedisMultiCommand.commandsExecutor,
|
||||
modulesCommandsExecutor: RedisMultiCommand.prototype.commandsExecutor,
|
||||
scripts: clientOptions?.scripts,
|
||||
scriptsExecutor: RedisMultiCommand.#scriptsExecutor
|
||||
scriptsExecutor: RedisMultiCommand.prototype.scriptsExecutor
|
||||
});
|
||||
}
|
||||
|
||||
static create<M extends RedisModules, S extends RedisLuaScripts>(
|
||||
static create<M extends RedisModules = {}, S extends RedisLuaScripts = {}>(
|
||||
executor: RedisMultiExecutor,
|
||||
clientOptions?: RedisClientOptions<M, S>
|
||||
): RedisMultiCommandType<M, S> {
|
||||
@@ -153,6 +114,42 @@ export default class RedisMultiCommand<M extends RedisModules = RedisModules, S
|
||||
(this as any)[name] = (...args: Array<unknown>): void => (this as any).addCommand(name, args);
|
||||
}
|
||||
|
||||
commandsExecutor(command: RedisCommand, args: Array<unknown>): this {
|
||||
return this.addCommand(
|
||||
command.transformArguments(...args),
|
||||
command.transformReply
|
||||
);
|
||||
}
|
||||
|
||||
scriptsExecutor(script: RedisLuaScript, args: Array<unknown>): this {
|
||||
const transformedArguments: TransformArgumentsReply = [];
|
||||
if (this.#scriptsInUse.has(script.SHA1)) {
|
||||
transformedArguments.push(
|
||||
'EVALSHA',
|
||||
script.SHA1
|
||||
);
|
||||
} else {
|
||||
this.#scriptsInUse.add(script.SHA1);
|
||||
transformedArguments.push(
|
||||
'EVAL',
|
||||
script.SCRIPT
|
||||
);
|
||||
}
|
||||
|
||||
transformedArguments.push(script.NUMBER_OF_KEYS.toString());
|
||||
|
||||
const scriptArguments = script.transformArguments(...args);
|
||||
transformedArguments.push(...scriptArguments);
|
||||
if (scriptArguments.preserve) {
|
||||
transformedArguments.preserve = scriptArguments.preserve;
|
||||
}
|
||||
|
||||
return this.addCommand(
|
||||
transformedArguments,
|
||||
script.transformReply
|
||||
);
|
||||
}
|
||||
|
||||
addCommand(args: TransformArgumentsReply, transformReply?: RedisCommand['transformReply']): this {
|
||||
this.#queue.push({
|
||||
args,
|
||||
@@ -205,4 +202,4 @@ export default class RedisMultiCommand<M extends RedisModules = RedisModules, S
|
||||
}
|
||||
}
|
||||
|
||||
extendWithDefaultCommands(RedisMultiCommand, RedisMultiCommand.commandsExecutor);
|
||||
extendWithDefaultCommands(RedisMultiCommand, RedisMultiCommand.prototype.commandsExecutor);
|
||||
|
Reference in New Issue
Block a user