diff --git a/lib/client.ts b/lib/client.ts index 5dda3cdf37..b061bd5101 100644 --- a/lib/client.ts +++ b/lib/client.ts @@ -14,7 +14,7 @@ export interface RedisClientOptions { modules?: M; scripts?: S; commandsQueueMaxLength?: number; - readOnly?: boolean; + readonly?: boolean; legacyMode?: boolean; } @@ -102,8 +102,8 @@ export default class RedisClient, promises: Array>): RedisClient { + #initiateClientForNode(node: RedisClusterMasterNode | RedisClusterReplicaNode, readonly: boolean, clientsInUse: Set, promises: Array>): RedisClient { clientsInUse.add(node.url); let client = this.#clientByKey.get(node.url); @@ -101,7 +101,7 @@ export default class RedisClusterSlots { host: node.host, port: node.port }, - readOnly + readonly }); promises.push(client.connect()); this.#clientByKey.set(node.url, client); @@ -158,13 +158,13 @@ export default class RedisClusterSlots { return value; } - getClient(firstKey?: string, isReadOnly?: boolean): RedisClient { + getClient(firstKey?: string, isReadonly?: boolean): RedisClient { if (!firstKey) { return this.#getRandomClient(); } const slot = calculateSlot(firstKey); - if (!isReadOnly || !this.#options.useReplicas) { + if (!isReadonly || !this.#options.useReplicas) { return this.#getSlotMaster(slot); } diff --git a/lib/cluster.ts b/lib/cluster.ts index f4f973780a..8b4c9bbc78 100644 --- a/lib/cluster.ts +++ b/lib/cluster.ts @@ -51,9 +51,9 @@ export default class RedisCluster { return this.#slots.connect(); } - async sendCommand(args: Array, firstKeyIndex?: number, isReadOnly?: boolean, redirections: number = 0): Promise { + async sendCommand(args: Array, firstKeyIndex?: number, isReadonly?: boolean, redirections: number = 0): Promise { const firstKey = firstKeyIndex ? args[firstKeyIndex] : undefined, - client = this.#slots.getClient(firstKey, isReadOnly); + client = this.#slots.getClient(firstKey, isReadonly); try { return await client.sendCommand(args); @@ -64,7 +64,7 @@ export default class RedisCluster { await this.#slots.discover(); if (redirections < (this.#options.maxCommandRedirections ?? 16)) { - return this.sendCommand(args, firstKeyIndex, isReadOnly, redirections + 1); + return this.sendCommand(args, firstKeyIndex, isReadonly, redirections + 1); } } diff --git a/lib/commands/HDEL.spec.ts b/lib/commands/HDEL.spec.ts new file mode 100644 index 0000000000..04191f51ad --- /dev/null +++ b/lib/commands/HDEL.spec.ts @@ -0,0 +1,28 @@ +import { strict as assert } from 'assert'; +import { TestRedisServers, itWithClient } from '../test-utils'; +import { transformArguments } from './HDEL'; + +describe('HDEL', () => { + describe('transformArguments', () => { + it('string', () => { + assert.deepEqual( + transformArguments('key', 'field'), + ['HDEL', 'key', 'field'] + ); + }); + + it('array', () => { + assert.deepEqual( + transformArguments('key', ['1', '2']), + ['HDEL', 'key', '1', '2'] + ); + }); + }); + + itWithClient(TestRedisServers.OPEN, 'client.hDel', async client => { + assert.equal( + await client.hDel('key', 'field'), + 0 + ); + }); +}); diff --git a/lib/commands/HDEL.ts b/lib/commands/HDEL.ts index f12fcd7f28..911c849712 100644 --- a/lib/commands/HDEL.ts +++ b/lib/commands/HDEL.ts @@ -2,12 +2,16 @@ import { transformReplyNumber } from './generic-transformers'; export const FIRST_KEY_INDEX = 1; -export function transformArguments(key: string, ...fields: Array): Array { - return [ - 'HDEL', - key, - ...fields - ]; +export function transformArguments(key: string, field: string | Array): Array { + const args = ['HDEL', key]; + + if (typeof field === 'string') { + args.push(field); + } else { + args.push(...field); + } + + return args; } export const transformReply = transformReplyNumber; diff --git a/lib/commands/HEXISTS.spec.ts b/lib/commands/HEXISTS.spec.ts new file mode 100644 index 0000000000..26c411c432 --- /dev/null +++ b/lib/commands/HEXISTS.spec.ts @@ -0,0 +1,19 @@ +import { strict as assert } from 'assert'; +import { TestRedisServers, itWithClient } from '../test-utils'; +import { transformArguments } from './HEXISTS'; + +describe('HEXISTS', () => { + it('transformArguments', () => { + assert.deepEqual( + transformArguments('key', 'field'), + ['HEXISTS', 'key', 'field'] + ); + }); + + itWithClient(TestRedisServers.OPEN, 'client.hExists', async client => { + assert.equal( + await client.hExists('key', 'field'), + false + ); + }); +}); diff --git a/lib/commands/HGET.spec.ts b/lib/commands/HGET.spec.ts new file mode 100644 index 0000000000..c78550c517 --- /dev/null +++ b/lib/commands/HGET.spec.ts @@ -0,0 +1,19 @@ +import { strict as assert } from 'assert'; +import { TestRedisServers, itWithClient } from '../test-utils'; +import { transformArguments } from './HGET'; + +describe('HGET', () => { + it('transformArguments', () => { + assert.deepEqual( + transformArguments('key', 'field'), + ['HGET', 'key', 'field'] + ); + }); + + itWithClient(TestRedisServers.OPEN, 'client.hGet', async client => { + assert.equal( + await client.hGet('key', 'field'), + null + ); + }); +}); diff --git a/lib/commands/HGETALL.spec.ts b/lib/commands/HGETALL.spec.ts index a0ac1b2674..68b51a2902 100644 --- a/lib/commands/HGETALL.spec.ts +++ b/lib/commands/HGETALL.spec.ts @@ -1,4 +1,5 @@ import { strict as assert } from 'assert'; +import { TestRedisServers, itWithClient } from '../test-utils'; import { transformReply } from './HGETALL'; describe('HGETALL', () => { @@ -30,4 +31,11 @@ describe('HGETALL', () => { ); }); }); + + itWithClient(TestRedisServers.OPEN, 'client.hGetAll', async client => { + assert.deepEqual( + await client.hGetAll('key'), + Object.create(null) + ); + }); }); diff --git a/lib/commands/HINCRBY.spec.ts b/lib/commands/HINCRBY.spec.ts new file mode 100644 index 0000000000..898dfd1172 --- /dev/null +++ b/lib/commands/HINCRBY.spec.ts @@ -0,0 +1,19 @@ +import { strict as assert } from 'assert'; +import { TestRedisServers, itWithClient } from '../test-utils'; +import { transformArguments } from './HINCRBY'; + +describe('HINCRBY', () => { + it('transformArguments', () => { + assert.deepEqual( + transformArguments('key', 'field', 1), + ['HINCRBY', 'key', 'field', '1'] + ); + }); + + itWithClient(TestRedisServers.OPEN, 'client.hIncrBy', async client => { + assert.equal( + await client.hIncrBy('key', 'field', 1), + 1 + ); + }); +}); diff --git a/lib/commands/HINCRBYFLOAT.spec.ts b/lib/commands/HINCRBYFLOAT.spec.ts new file mode 100644 index 0000000000..83e87538c5 --- /dev/null +++ b/lib/commands/HINCRBYFLOAT.spec.ts @@ -0,0 +1,19 @@ +import { strict as assert } from 'assert'; +import { TestRedisServers, itWithClient } from '../test-utils'; +import { transformArguments } from './HINCRBYFLOAT'; + +describe('HINCRBYFLOAT', () => { + it('transformArguments', () => { + assert.deepEqual( + transformArguments('key', 'field', 1.5), + ['HINCRBYFLOAT', 'key', 'field', '1.5'] + ); + }); + + itWithClient(TestRedisServers.OPEN, 'client.hIncrByFloat', async client => { + assert.equal( + await client.hIncrByFloat('key', 'field', 1.5), + '1.5' + ); + }); +}); diff --git a/lib/commands/HKEYS.spec.ts b/lib/commands/HKEYS.spec.ts new file mode 100644 index 0000000000..12190668b0 --- /dev/null +++ b/lib/commands/HKEYS.spec.ts @@ -0,0 +1,19 @@ +import { strict as assert } from 'assert'; +import { TestRedisServers, itWithClient } from '../test-utils'; +import { transformArguments } from './HKEYS'; + +describe('HKEYS', () => { + it('transformArguments', () => { + assert.deepEqual( + transformArguments('key'), + ['HKEYS', 'key'] + ); + }); + + itWithClient(TestRedisServers.OPEN, 'client.hKeys', async client => { + assert.deepEqual( + await client.hKeys('key'), + [] + ); + }); +}); diff --git a/lib/commands/HLEN.spec.ts b/lib/commands/HLEN.spec.ts new file mode 100644 index 0000000000..e9aaa64e6e --- /dev/null +++ b/lib/commands/HLEN.spec.ts @@ -0,0 +1,19 @@ +import { strict as assert } from 'assert'; +import { TestRedisServers, itWithClient } from '../test-utils'; +import { transformArguments } from './HLEN'; + +describe('HLEN', () => { + it('transformArguments', () => { + assert.deepEqual( + transformArguments('key'), + ['HLEN', 'key'] + ); + }); + + itWithClient(TestRedisServers.OPEN, 'client.hLen', async client => { + assert.equal( + await client.hLen('key'), + 0 + ); + }); +}); diff --git a/lib/commands/INCR.spec.ts b/lib/commands/INCR.spec.ts new file mode 100644 index 0000000000..d64c3696af --- /dev/null +++ b/lib/commands/INCR.spec.ts @@ -0,0 +1,19 @@ +import { strict as assert } from 'assert'; +import { TestRedisServers, itWithClient } from '../test-utils'; +import { transformArguments } from './INCR'; + +describe('INCR', () => { + it('transformArguments', () => { + assert.deepEqual( + transformArguments('key'), + ['INCR', 'key'] + ); + }); + + itWithClient(TestRedisServers.OPEN, 'client.incr', async client => { + assert.equal( + await client.incr('key'), + 1 + ); + }); +}); diff --git a/lib/commands/INCRBY.spec.ts b/lib/commands/INCRBY.spec.ts new file mode 100644 index 0000000000..875277570c --- /dev/null +++ b/lib/commands/INCRBY.spec.ts @@ -0,0 +1,19 @@ +import { strict as assert } from 'assert'; +import { TestRedisServers, itWithClient } from '../test-utils'; +import { transformArguments } from './INCRBY'; + +describe('INCR', () => { + it('transformArguments', () => { + assert.deepEqual( + transformArguments('key', 1), + ['INCRBY', 'key', '1'] + ); + }); + + itWithClient(TestRedisServers.OPEN, 'client.incrBy', async client => { + assert.equal( + await client.incrBy('key', 1), + 1 + ); + }); +}); diff --git a/lib/commands/INCRBYFLOAT.spec.ts b/lib/commands/INCRBYFLOAT.spec.ts new file mode 100644 index 0000000000..fe062b6290 --- /dev/null +++ b/lib/commands/INCRBYFLOAT.spec.ts @@ -0,0 +1,19 @@ +import { strict as assert } from 'assert'; +import { TestRedisServers, itWithClient } from '../test-utils'; +import { transformArguments } from './INCRBYFLOAT'; + +describe('INCRBYFLOAT', () => { + it('transformArguments', () => { + assert.deepEqual( + transformArguments('key', 1.5), + ['INCRBYFLOAT', 'key', '1.5'] + ); + }); + + itWithClient(TestRedisServers.OPEN, 'client.incrByFloat', async client => { + assert.equal( + await client.incrByFloat('key', 1.5), + '1.5' + ); + }); +}); diff --git a/lib/commands/INCRBYFLOAT.ts b/lib/commands/INCRBYFLOAT.ts index 1de9f08dd2..38912cbdc9 100644 --- a/lib/commands/INCRBYFLOAT.ts +++ b/lib/commands/INCRBYFLOAT.ts @@ -1,4 +1,4 @@ -import { transformReplyNumber } from './generic-transformers'; +import { transformReplyString } from './generic-transformers'; export const FIRST_KEY_INDEX = 1; @@ -6,4 +6,4 @@ export function transformArguments(key: string, increment: number): Array { + describe('transformArguments', () => { + it('string', () => { + assert.deepEqual( + transformArguments('key', 'field'), + ['LPUSH', 'key', 'field'] + ); + }); + + it('array', () => { + assert.deepEqual( + transformArguments('key', ['1', '2']), + ['LPUSH', 'key', '1', '2'] + ); + }); + }); + + itWithClient(TestRedisServers.OPEN, 'client.lPush', async client => { + assert.equal( + await client.lPush('key', 'field'), + 1 + ); + }); +}); diff --git a/lib/commands/READONLY.spec.ts b/lib/commands/READONLY.spec.ts new file mode 100644 index 0000000000..bf5ecfc139 --- /dev/null +++ b/lib/commands/READONLY.spec.ts @@ -0,0 +1,12 @@ +import { strict as assert } from 'assert'; +import { TestRedisServers, itWithClient } from '../test-utils'; +import { transformArguments } from './READONLY'; + +describe('READONLY', () => { + it('transformArguments', () => { + assert.deepEqual( + transformArguments(), + ['READONLY'] + ); + }); +}); diff --git a/lib/commands/index.ts b/lib/commands/index.ts index 196e4a2027..288965d1c8 100644 --- a/lib/commands/index.ts +++ b/lib/commands/index.ts @@ -166,7 +166,7 @@ export default { RANDOMKEY, randomKey: RANDOMKEY, READONLY, - readOnly: READONLY, + readonly: READONLY, RENAME, rename: RENAME, RENAMENX,