diff --git a/packages/client/lib/cluster/commands.ts b/packages/client/lib/cluster/commands.ts index 84a37862772..c81475bcb1e 100644 --- a/packages/client/lib/cluster/commands.ts +++ b/packages/client/lib/cluster/commands.ts @@ -64,6 +64,7 @@ import * as HRANDFIELD_COUNT_WITHVALUES from '../commands/HRANDFIELD_COUNT_WITHV import * as HRANDFIELD_COUNT from '../commands/HRANDFIELD_COUNT'; import * as HRANDFIELD from '../commands/HRANDFIELD'; import * as HSCAN from '../commands/HSCAN'; +import * as HSCAN_VALUES from '../commands/HSCAN_VALUES'; import * as HSET from '../commands/HSET'; import * as HSETNX from '../commands/HSETNX'; import * as HSTRLEN from '../commands/HSTRLEN'; @@ -343,6 +344,8 @@ export default { hRandField: HRANDFIELD, HSCAN, hScan: HSCAN, + HSCAN_VALUES, + hScanValues: HSCAN_VALUES, HSET, hSet: HSET, HSETNX, diff --git a/packages/client/lib/commands/HSCAN_VALUES.spec.ts b/packages/client/lib/commands/HSCAN_VALUES.spec.ts new file mode 100644 index 00000000000..773c8701661 --- /dev/null +++ b/packages/client/lib/commands/HSCAN_VALUES.spec.ts @@ -0,0 +1,76 @@ +import { strict as assert } from 'assert'; +import testUtils, { GLOBAL } from '../test-utils'; +import { transformArguments, transformReply } from './HSCAN_VALUES'; + +describe('HSCAN_VALUES', () => { + describe('transformArguments', () => { + it('cusror only', () => { + assert.deepEqual( + transformArguments('key', 0), + ['HSCAN', 'key', '0', 'VALUES'] + ); + }); + + it('with MATCH', () => { + assert.deepEqual( + transformArguments('key', 0, { + MATCH: 'pattern' + }), + ['HSCAN', 'key', '0', 'MATCH', 'pattern', 'VALUES'] + ); + }); + + it('with COUNT', () => { + assert.deepEqual( + transformArguments('key', 0, { + COUNT: 1 + }), + ['HSCAN', 'key', '0', 'COUNT', '1', 'VALUES'] + ); + }); + + it('with MATCH & COUNT', () => { + assert.deepEqual( + transformArguments('key', 0, { + MATCH: 'pattern', + COUNT: 1 + }), + ['HSCAN', 'key', '0', 'MATCH', 'pattern', 'COUNT', '1', 'VALUES'] + ); + }); + }); + + describe('transformReply', () => { + it('without tuples', () => { + assert.deepEqual( + transformReply(['0', []]), + { + cursor: 0, + fields: [] + } + ); + }); + + it('with tuples', () => { + assert.deepEqual( + transformReply(['0', ['field']]), + { + cursor: 0, + fields: [ + 'field', + ] + } + ); + }); + }); + + testUtils.testWithClient('client.hScan', async client => { + assert.deepEqual( + await client.hScanValues('key', 0), + { + cursor: 0, + fields: [] + } + ); + }, GLOBAL.SERVERS.OPEN); +}); diff --git a/packages/client/lib/commands/HSCAN_VALUES.ts b/packages/client/lib/commands/HSCAN_VALUES.ts new file mode 100644 index 00000000000..5729f58fa6b --- /dev/null +++ b/packages/client/lib/commands/HSCAN_VALUES.ts @@ -0,0 +1,39 @@ +import { RedisCommandArgument, RedisCommandArguments } from '.'; +import { ScanOptions, pushScanArguments } from './generic-transformers'; + +export const FIRST_KEY_INDEX = 1; + +export const IS_READ_ONLY = true; + +export function transformArguments( + key: RedisCommandArgument, + cursor: number, + options?: ScanOptions +): RedisCommandArguments { + const args = pushScanArguments([ + 'HSCAN', + key + ], cursor, options); + args.push('VALUES'); + + return args; +} + +type HScanRawReply = [RedisCommandArgument, Array]; + +export interface HScanTuple { + field: RedisCommandArgument; + value: RedisCommandArgument; +} + +interface HScanValueReply { + cursor: number; + fields: Array; +} + +export function transformReply([cursor, fields]: HScanRawReply): HScanValueReply { + return { + cursor: Number(cursor), + fields: fields + }; +}