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

add MULTI and EXEC commands to when executing multi command, make client.multi return type innerit the module commands, clean some tests, exclute spec files from coverage report

This commit is contained in:
leibale
2021-05-14 05:03:13 -04:00
parent 5d12315403
commit 61edd4f1b5
2 changed files with 32 additions and 27 deletions

View File

@@ -1,34 +1,27 @@
import { strict as assert } from 'assert';
import RedisMultiCommand, { MultiQueuedCommand } from './multi-command';
import RedisClient from './client';
import RedisMultiCommand from './multi-command';
import RedisCommandsQueue from './commands-queue';
describe('Multi Command', () => {
it('create', async () => {
const multi = RedisMultiCommand.create(async (encodedCommands: Array<MultiQueuedCommand>): Promise<Array<string>> => {
return Object.keys(encodedCommands);
it('simple', async () => {
const multi = RedisMultiCommand.create(queue => {
assert.deepEqual(
queue.map(({encodedCommand}) => encodedCommand),
[
RedisCommandsQueue.encodeCommand(['MULTI']),
RedisCommandsQueue.encodeCommand(['PING']),
RedisCommandsQueue.encodeCommand(['EXEC']),
]
);
return Promise.resolve(['PONG']);
});
multi.ping();
multi.set('a', 'b');
// console.log(
await multi.exec()
// );
});
it('client.multi', async () => {
const client = RedisClient.create();
await client.connect();
assert.deepEqual(
await client
.multi()
.ping()
.set('key', 'value')
.exec(),
['PONG', 'OK']
await multi.exec(),
['PONG']
);
await client.disconnect();
});
});

View File

@@ -19,7 +19,7 @@ export interface MultiQueuedCommand {
transformReply?: RedisCommand['transformReply'];
}
export type RedisMultiExecutor = (encodedCommands: Array<MultiQueuedCommand>, chainId: Symbol) => Promise<Array<RedisReply>>;
export type RedisMultiExecutor = (queue: Array<MultiQueuedCommand>, chainId: Symbol) => Promise<Array<RedisReply>>;
export default class RedisMultiCommand {
static defineCommand(on: any, name: string, command: RedisCommand) {
@@ -61,9 +61,21 @@ export default class RedisMultiCommand {
}
async exec(): Promise<Array<unknown>> {
const results = await this.#executor(this.#queue, Symbol('[RedisMultiCommand] Chain ID'));
return this.#queue.map(({transformReply}, i) => {
const reply = results[i];
if (!this.#queue.length) {
return [];
}
const queue = this.#queue.splice(0);
queue.unshift({
encodedCommand: RedisCommandsQueue.encodeCommand(['MULTI'])
});
queue.push({
encodedCommand: RedisCommandsQueue.encodeCommand(['EXEC'])
});
const rawReplies = await this.#executor(queue, Symbol('[RedisMultiCommand] Chain ID'));
return rawReplies.map((reply, i) => {
const { transformReply } = queue[i + 1];
return transformReply ? transformReply(reply) : reply;
});
};