1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-07 13:22:56 +03:00

fix #2046 - add support for multi in select (#2133)

* fix #2046 - add support for multi in select

* fix "Argument of type 'symbol | undefined' is not assignable to parameter of type 'number | undefined'"
This commit is contained in:
Leibale Eidelman
2022-05-26 09:55:47 -04:00
committed by GitHub
parent 7196b907e5
commit bf80c163b1
4 changed files with 52 additions and 13 deletions

View File

@@ -468,6 +468,20 @@ describe('Client', () => {
['PONG']
);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('should remember selected db', async client => {
await client.multi()
.select(1)
.exec();
await killClient(client);
assert.equal(
(await client.clientInfo()).db,
1
);
}, {
...GLOBAL.SERVERS.OPEN,
minimumDockerVersion: [6, 2] // CLIENT INFO
});
});
testUtils.testWithClient('scripts', async client => {

View File

@@ -606,18 +606,26 @@ export default class RedisClient<
);
}
multiExecutor(commands: Array<RedisMultiQueuedCommand>, chainId?: symbol): Promise<Array<RedisCommandRawReply>> {
async multiExecutor(
commands: Array<RedisMultiQueuedCommand>,
selectedDB?: number,
chainId?: symbol
): Promise<Array<RedisCommandRawReply>> {
const promise = Promise.all(
commands.map(({ args }) => {
return this.#queue.addCommand(args, RedisClient.commandOptions({
chainId
}));
return this.#queue.addCommand(args, { chainId });
})
);
this.#tick();
return promise;
const results = await promise;
if (selectedDB !== undefined) {
this.#selectedDB = selectedDB;
}
return results;
}
async* scanIterator(options?: ScanCommandOptions): AsyncIterable<string> {

View File

@@ -58,13 +58,13 @@ type InstantiableRedisMultiCommand<
S extends RedisScripts
> = new (...args: ConstructorParameters<typeof RedisClientMultiCommand>) => RedisClientMultiCommandType<M, F, S>;
export type RedisClientMultiExecutor = (queue: Array<RedisMultiQueuedCommand>, chainId?: symbol) => Promise<Array<RedisCommandRawReply>>;
export type RedisClientMultiExecutor = (
queue: Array<RedisMultiQueuedCommand>,
selectedDB?: number,
chainId?: symbol
) => Promise<Array<RedisCommandRawReply>>;
export default class RedisClientMultiCommand {
readonly #multi = new RedisMultiCommand();
readonly #executor: RedisClientMultiExecutor;
static extend<
M extends RedisModules,
F extends RedisFunctions,
@@ -81,7 +81,10 @@ export default class RedisClientMultiCommand {
});
}
readonly #multi = new RedisMultiCommand();
readonly #executor: RedisClientMultiExecutor;
readonly v4: Record<string, any> = {};
#selectedDB?: number;
constructor(executor: RedisClientMultiExecutor, legacyMode = false) {
this.#executor = executor;
@@ -136,6 +139,13 @@ export default class RedisClientMultiCommand {
);
}
SELECT(db: number, transformReply?: RedisCommand['transformReply']): this {
this.#selectedDB = db;
return this.addCommand(['SELECT', db.toString()], transformReply);
}
select = this.SELECT;
addCommand(args: RedisCommandArguments, transformReply?: RedisCommand['transformReply']): this {
this.#multi.addCommand(args, transformReply);
return this;
@@ -160,7 +170,11 @@ export default class RedisClientMultiCommand {
if (!commands) return [];
return this.#multi.handleExecReplies(
await this.#executor(commands, RedisMultiCommand.generateChainId())
await this.#executor(
commands,
this.#selectedDB,
RedisMultiCommand.generateChainId()
)
);
}
@@ -168,7 +182,10 @@ export default class RedisClientMultiCommand {
async execAsPipeline(): Promise<Array<RedisCommandRawReply>> {
return this.#multi.transformReplies(
await this.#executor(this.#multi.queue)
await this.#executor(
this.#multi.queue,
this.#selectedDB
)
);
}
}

View File

@@ -230,7 +230,7 @@ export default class RedisCluster<
return this.#execute(
firstKey,
false,
client => client.multiExecutor(commands, chainId)
client => client.multiExecutor(commands, undefined, chainId)
);
},
routing