Skip to content

Commit bf80c16

Browse files
authored
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'"
1 parent 7196b90 commit bf80c16

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

packages/client/lib/client/index.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,20 @@ describe('Client', () => {
468468
['PONG']
469469
);
470470
}, GLOBAL.SERVERS.OPEN);
471+
472+
testUtils.testWithClient('should remember selected db', async client => {
473+
await client.multi()
474+
.select(1)
475+
.exec();
476+
await killClient(client);
477+
assert.equal(
478+
(await client.clientInfo()).db,
479+
1
480+
);
481+
}, {
482+
...GLOBAL.SERVERS.OPEN,
483+
minimumDockerVersion: [6, 2] // CLIENT INFO
484+
});
471485
});
472486

473487
testUtils.testWithClient('scripts', async client => {

packages/client/lib/client/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -606,18 +606,26 @@ export default class RedisClient<
606606
);
607607
}
608608

609-
multiExecutor(commands: Array<RedisMultiQueuedCommand>, chainId?: symbol): Promise<Array<RedisCommandRawReply>> {
609+
async multiExecutor(
610+
commands: Array<RedisMultiQueuedCommand>,
611+
selectedDB?: number,
612+
chainId?: symbol
613+
): Promise<Array<RedisCommandRawReply>> {
610614
const promise = Promise.all(
611615
commands.map(({ args }) => {
612-
return this.#queue.addCommand(args, RedisClient.commandOptions({
613-
chainId
614-
}));
616+
return this.#queue.addCommand(args, { chainId });
615617
})
616618
);
617619

618620
this.#tick();
619621

620-
return promise;
622+
const results = await promise;
623+
624+
if (selectedDB !== undefined) {
625+
this.#selectedDB = selectedDB;
626+
}
627+
628+
return results;
621629
}
622630

623631
async* scanIterator(options?: ScanCommandOptions): AsyncIterable<string> {

packages/client/lib/client/multi-command.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ type InstantiableRedisMultiCommand<
5858
S extends RedisScripts
5959
> = new (...args: ConstructorParameters<typeof RedisClientMultiCommand>) => RedisClientMultiCommandType<M, F, S>;
6060

61-
62-
export type RedisClientMultiExecutor = (queue: Array<RedisMultiQueuedCommand>, chainId?: symbol) => Promise<Array<RedisCommandRawReply>>;
61+
export type RedisClientMultiExecutor = (
62+
queue: Array<RedisMultiQueuedCommand>,
63+
selectedDB?: number,
64+
chainId?: symbol
65+
) => Promise<Array<RedisCommandRawReply>>;
6366

6467
export default class RedisClientMultiCommand {
65-
readonly #multi = new RedisMultiCommand();
66-
readonly #executor: RedisClientMultiExecutor;
67-
6868
static extend<
6969
M extends RedisModules,
7070
F extends RedisFunctions,
@@ -81,7 +81,10 @@ export default class RedisClientMultiCommand {
8181
});
8282
}
8383

84+
readonly #multi = new RedisMultiCommand();
85+
readonly #executor: RedisClientMultiExecutor;
8486
readonly v4: Record<string, any> = {};
87+
#selectedDB?: number;
8588

8689
constructor(executor: RedisClientMultiExecutor, legacyMode = false) {
8790
this.#executor = executor;
@@ -136,6 +139,13 @@ export default class RedisClientMultiCommand {
136139
);
137140
}
138141

142+
SELECT(db: number, transformReply?: RedisCommand['transformReply']): this {
143+
this.#selectedDB = db;
144+
return this.addCommand(['SELECT', db.toString()], transformReply);
145+
}
146+
147+
select = this.SELECT;
148+
139149
addCommand(args: RedisCommandArguments, transformReply?: RedisCommand['transformReply']): this {
140150
this.#multi.addCommand(args, transformReply);
141151
return this;
@@ -160,15 +170,22 @@ export default class RedisClientMultiCommand {
160170
if (!commands) return [];
161171

162172
return this.#multi.handleExecReplies(
163-
await this.#executor(commands, RedisMultiCommand.generateChainId())
173+
await this.#executor(
174+
commands,
175+
this.#selectedDB,
176+
RedisMultiCommand.generateChainId()
177+
)
164178
);
165179
}
166180

167181
EXEC = this.exec;
168182

169183
async execAsPipeline(): Promise<Array<RedisCommandRawReply>> {
170184
return this.#multi.transformReplies(
171-
await this.#executor(this.#multi.queue)
185+
await this.#executor(
186+
this.#multi.queue,
187+
this.#selectedDB
188+
)
172189
);
173190
}
174191
}

packages/client/lib/cluster/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export default class RedisCluster<
230230
return this.#execute(
231231
firstKey,
232232
false,
233-
client => client.multiExecutor(commands, chainId)
233+
client => client.multiExecutor(commands, undefined, chainId)
234234
);
235235
},
236236
routing

0 commit comments

Comments
 (0)