You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-11 22:42:42 +03:00
replace "modern" with "v4"
This commit is contained in:
@@ -4,7 +4,7 @@ const cronometro = require('cronometro'),
|
|||||||
|
|
||||||
let client;
|
let client;
|
||||||
cronometro({
|
cronometro({
|
||||||
'New Client - Modern Mode': {
|
'New Client': {
|
||||||
async before() {
|
async before() {
|
||||||
client = newRedis.createClient();
|
client = newRedis.createClient();
|
||||||
await client.connect();
|
await client.connect();
|
||||||
|
@@ -41,7 +41,7 @@ describe('Client', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
before(() => client.connect());
|
before(() => client.connect());
|
||||||
afterEach(() => client.modern.flushAll());
|
afterEach(() => client.v4.flushAll());
|
||||||
after(() => client.disconnect());
|
after(() => client.disconnect());
|
||||||
|
|
||||||
it('client.sendCommand should call the callback', done => {
|
it('client.sendCommand should call the callback', done => {
|
||||||
@@ -61,12 +61,12 @@ describe('Client', () => {
|
|||||||
|
|
||||||
it('client.sendCommand should work without callback', async () => {
|
it('client.sendCommand should work without callback', async () => {
|
||||||
(client as any).sendCommand('PING');
|
(client as any).sendCommand('PING');
|
||||||
await client.modern.ping(); // make sure the first command was replied
|
await client.v4.ping(); // make sure the first command was replied
|
||||||
});
|
});
|
||||||
|
|
||||||
it('client.modern.sendCommand should return a promise', async () => {
|
it('client.v4.sendCommand should return a promise', async () => {
|
||||||
assert.equal(
|
assert.equal(
|
||||||
await client.modern.sendCommand(['PING']),
|
await client.v4.sendCommand(['PING']),
|
||||||
'PONG'
|
'PONG'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -137,12 +137,12 @@ describe('Client', () => {
|
|||||||
(client as any).multi()
|
(client as any).multi()
|
||||||
.ping()
|
.ping()
|
||||||
.exec();
|
.exec();
|
||||||
await client.modern.ping(); // make sure the first command was replied
|
await client.v4.ping(); // make sure the first command was replied
|
||||||
});
|
});
|
||||||
|
|
||||||
it('client.modern.exec should return a promise', async () => {
|
it('client.v4.exec should return a promise', async () => {
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
await ((client as any).multi().modern
|
await ((client as any).multi().v4
|
||||||
.ping()
|
.ping()
|
||||||
.exec()),
|
.exec()),
|
||||||
['PONG']
|
['PONG']
|
||||||
|
@@ -68,7 +68,7 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
|
|||||||
readonly #socket: RedisSocket;
|
readonly #socket: RedisSocket;
|
||||||
readonly #queue: RedisCommandsQueue;
|
readonly #queue: RedisCommandsQueue;
|
||||||
readonly #Multi: typeof RedisMultiCommand & { new(): RedisMultiCommandType<M, S> };
|
readonly #Multi: typeof RedisMultiCommand & { new(): RedisMultiCommandType<M, S> };
|
||||||
readonly #modern: Record<string, Function> = {};
|
readonly #v4: Record<string, Function> = {};
|
||||||
#selectedDB = 0;
|
#selectedDB = 0;
|
||||||
|
|
||||||
get options(): RedisClientOptions<M> | null | undefined {
|
get options(): RedisClientOptions<M> | null | undefined {
|
||||||
@@ -79,12 +79,12 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
|
|||||||
return this.#socket.isOpen;
|
return this.#socket.isOpen;
|
||||||
}
|
}
|
||||||
|
|
||||||
get modern(): Record<string, Function> {
|
get v4(): Record<string, Function> {
|
||||||
if (!this.#options?.legacyMode) {
|
if (!this.#options?.legacyMode) {
|
||||||
throw new Error('the client is not in "legacy mode"');
|
throw new Error('the client is not in "legacy mode"');
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.#modern;
|
return this.#v4;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(options?: RedisClientOptions<M, S>) {
|
constructor(options?: RedisClientOptions<M, S>) {
|
||||||
@@ -218,13 +218,13 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
|
|||||||
#legacyMode(): void {
|
#legacyMode(): void {
|
||||||
if (!this.#options?.legacyMode) return;
|
if (!this.#options?.legacyMode) return;
|
||||||
|
|
||||||
this.#modern.sendCommand = this.sendCommand.bind(this);
|
this.#v4.sendCommand = this.sendCommand.bind(this);
|
||||||
|
|
||||||
(this as any).sendCommand = (...args: Array<unknown>): void => {
|
(this as any).sendCommand = (...args: Array<unknown>): void => {
|
||||||
const options = isCommandOptions(args[0]) && args.shift(),
|
const options = isCommandOptions(args[0]) && args.shift(),
|
||||||
callback = typeof args[args.length - 1] === 'function' && (args.pop() as Function);
|
callback = typeof args[args.length - 1] === 'function' && (args.pop() as Function);
|
||||||
|
|
||||||
this.#modern.sendCommand(args.flat(), options)
|
this.#v4.sendCommand(args.flat(), options)
|
||||||
.then((reply: unknown) => {
|
.then((reply: unknown) => {
|
||||||
if (!callback) return;
|
if (!callback) return;
|
||||||
|
|
||||||
@@ -274,7 +274,7 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
|
|||||||
}
|
}
|
||||||
|
|
||||||
#defineLegacyCommand(name: string): void {
|
#defineLegacyCommand(name: string): void {
|
||||||
this.#modern[name] = (this as any)[name];
|
this.#v4[name] = (this as any)[name];
|
||||||
(this as any)[name] = function (...args: Array<unknown>): void {
|
(this as any)[name] = function (...args: Array<unknown>): void {
|
||||||
this.sendCommand(name, ...args);
|
this.sendCommand(name, ...args);
|
||||||
};
|
};
|
||||||
|
@@ -48,14 +48,14 @@ export default class RedisMultiCommand<M extends RedisModules = RedisModules, S
|
|||||||
|
|
||||||
readonly #scriptsInUse = new Set<string>();
|
readonly #scriptsInUse = new Set<string>();
|
||||||
|
|
||||||
readonly #modern: Record<string, Function> = {};
|
readonly #v4: Record<string, Function> = {};
|
||||||
|
|
||||||
get modern(): Record<string, Function> {
|
get v4(): Record<string, Function> {
|
||||||
if (!this.#clientOptions?.legacyMode) {
|
if (!this.#clientOptions?.legacyMode) {
|
||||||
throw new Error('client is not in "legacy mode"');
|
throw new Error('client is not in "legacy mode"');
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.#modern;
|
return this.#v4;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(executor: RedisMultiExecutor, clientOptions?: RedisClientOptions<M, S>) {
|
constructor(executor: RedisMultiExecutor, clientOptions?: RedisClientOptions<M, S>) {
|
||||||
@@ -110,12 +110,12 @@ export default class RedisMultiCommand<M extends RedisModules = RedisModules, S
|
|||||||
#legacyMode(): Record<string, Function> | undefined {
|
#legacyMode(): Record<string, Function> | undefined {
|
||||||
if (!this.#clientOptions?.legacyMode) return;
|
if (!this.#clientOptions?.legacyMode) return;
|
||||||
|
|
||||||
this.#modern.exec = this.exec.bind(this);
|
this.#v4.exec = this.exec.bind(this);
|
||||||
this.#modern.addCommand = this.addCommand.bind(this);
|
this.#v4.addCommand = this.addCommand.bind(this);
|
||||||
|
|
||||||
(this as any).exec = function (...args: Array<unknown>): void {
|
(this as any).exec = function (...args: Array<unknown>): void {
|
||||||
const callback = typeof args[args.length - 1] === 'function' && args.pop() as Function;
|
const callback = typeof args[args.length - 1] === 'function' && args.pop() as Function;
|
||||||
this.#modern.exec()
|
this.#v4.exec()
|
||||||
.then((reply: unknown) => {
|
.then((reply: unknown) => {
|
||||||
if (!callback) return;
|
if (!callback) return;
|
||||||
|
|
||||||
@@ -152,7 +152,7 @@ export default class RedisMultiCommand<M extends RedisModules = RedisModules, S
|
|||||||
}
|
}
|
||||||
|
|
||||||
#defineLegacyCommand(name: string): void {
|
#defineLegacyCommand(name: string): void {
|
||||||
this.#modern[name] = (this as any)[name];
|
this.#v4[name] = (this as any)[name];
|
||||||
|
|
||||||
// TODO: https://github.com/NodeRedis/node-redis#commands:~:text=minimal%20parsing
|
// TODO: https://github.com/NodeRedis/node-redis#commands:~:text=minimal%20parsing
|
||||||
(this as any)[name] = function (...args: Array<unknown>) {
|
(this as any)[name] = function (...args: Array<unknown>) {
|
||||||
|
Reference in New Issue
Block a user