You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-10 11:43:01 +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:
@@ -1,34 +1,27 @@
|
|||||||
import { strict as assert } from 'assert';
|
import { strict as assert } from 'assert';
|
||||||
import RedisMultiCommand, { MultiQueuedCommand } from './multi-command';
|
import RedisMultiCommand from './multi-command';
|
||||||
import RedisClient from './client';
|
import RedisCommandsQueue from './commands-queue';
|
||||||
|
|
||||||
describe('Multi Command', () => {
|
describe('Multi Command', () => {
|
||||||
it('create', async () => {
|
it('simple', async () => {
|
||||||
const multi = RedisMultiCommand.create(async (encodedCommands: Array<MultiQueuedCommand>): Promise<Array<string>> => {
|
const multi = RedisMultiCommand.create(queue => {
|
||||||
return Object.keys(encodedCommands);
|
assert.deepEqual(
|
||||||
|
queue.map(({encodedCommand}) => encodedCommand),
|
||||||
|
[
|
||||||
|
RedisCommandsQueue.encodeCommand(['MULTI']),
|
||||||
|
RedisCommandsQueue.encodeCommand(['PING']),
|
||||||
|
RedisCommandsQueue.encodeCommand(['EXEC']),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
return Promise.resolve(['PONG']);
|
||||||
});
|
});
|
||||||
|
|
||||||
multi.ping();
|
multi.ping();
|
||||||
multi.set('a', 'b');
|
|
||||||
// console.log(
|
|
||||||
await multi.exec()
|
|
||||||
// );
|
|
||||||
});
|
|
||||||
|
|
||||||
it('client.multi', async () => {
|
|
||||||
const client = RedisClient.create();
|
|
||||||
|
|
||||||
await client.connect();
|
|
||||||
|
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
await client
|
await multi.exec(),
|
||||||
.multi()
|
['PONG']
|
||||||
.ping()
|
|
||||||
.set('key', 'value')
|
|
||||||
.exec(),
|
|
||||||
['PONG', 'OK']
|
|
||||||
);
|
);
|
||||||
|
|
||||||
await client.disconnect();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -19,7 +19,7 @@ export interface MultiQueuedCommand {
|
|||||||
transformReply?: RedisCommand['transformReply'];
|
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 {
|
export default class RedisMultiCommand {
|
||||||
static defineCommand(on: any, name: string, command: RedisCommand) {
|
static defineCommand(on: any, name: string, command: RedisCommand) {
|
||||||
@@ -61,9 +61,21 @@ export default class RedisMultiCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async exec(): Promise<Array<unknown>> {
|
async exec(): Promise<Array<unknown>> {
|
||||||
const results = await this.#executor(this.#queue, Symbol('[RedisMultiCommand] Chain ID'));
|
if (!this.#queue.length) {
|
||||||
return this.#queue.map(({transformReply}, i) => {
|
return [];
|
||||||
const reply = results[i];
|
}
|
||||||
|
|
||||||
|
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;
|
return transformReply ? transformReply(reply) : reply;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user