diff --git a/packages/time-series/lib/commands/GET.spec.ts b/packages/time-series/lib/commands/GET.spec.ts index 29634cd775..3af57f7510 100644 --- a/packages/time-series/lib/commands/GET.spec.ts +++ b/packages/time-series/lib/commands/GET.spec.ts @@ -1,46 +1,46 @@ import { strict as assert } from 'assert'; import testUtils, { GLOBAL } from '../test-utils'; -import { transformArguments } from './GET'; +import GET from './GET'; describe('GET', () => { - describe('transformArguments', () => { - it('without options', () => { - assert.deepEqual( - transformArguments('key'), - ['TS.GET', 'key'] - ); - }); - - it('with LATEST', () => { - assert.deepEqual( - transformArguments('key', { - LATEST: true - }), - ['TS.GET', 'key', 'LATEST'] - ); - }); + describe('transformArguments', () => { + it('without options', () => { + assert.deepEqual( + GET.transformArguments('key'), + ['TS.GET', 'key'] + ); }); - describe('client.ts.get', () => { - testUtils.testWithClient('null', async client => { - await client.ts.create('key'); - - assert.equal( - await client.ts.get('key'), - null - ); - }, GLOBAL.SERVERS.OPEN); - - testUtils.testWithClient('with samples', async client => { - await client.ts.add('key', 0, 1); - - assert.deepEqual( - await client.ts.get('key'), - { - timestamp: 0, - value: 1 - } - ); - }, GLOBAL.SERVERS.OPEN); + it('with LATEST', () => { + assert.deepEqual( + GET.transformArguments('key', { + LATEST: true + }), + ['TS.GET', 'key', 'LATEST'] + ); }); + }); + + describe('client.ts.get', () => { + testUtils.testWithClient('null', async client => { + await client.ts.create('key'); + + assert.equal( + await client.ts.get('key'), + null + ); + }, GLOBAL.SERVERS.OPEN); + + testUtils.testWithClient('with samples', async client => { + await client.ts.add('key', 0, 1); + + assert.deepEqual( + await client.ts.get('key'), + { + timestamp: 0, + value: 1 + } + ); + }, GLOBAL.SERVERS.OPEN); + }); }); diff --git a/packages/time-series/lib/commands/GET.ts b/packages/time-series/lib/commands/GET.ts index 6d74f97c9c..e2de752a0d 100644 --- a/packages/time-series/lib/commands/GET.ts +++ b/packages/time-series/lib/commands/GET.ts @@ -1,20 +1,30 @@ -import { RedisCommandArguments } from '@redis/client/dist/lib/commands'; -import { pushLatestArgument, SampleRawReply, SampleReply, transformSampleReply } from '.'; +import { RedisArgument, TuplesReply, NumberReply, DoubleReply, Resp2Reply, Command } from '@redis/client/dist/lib/RESP/types'; +import { pushLatestArgument } from '.'; -export const FIRST_KEY_INDEX = 1; - -export const IS_READ_ONLY = true; - -interface GetOptions { - LATEST?: boolean; +export interface TsGetOptions { + LATEST?: boolean; } -export function transformArguments(key: string, options?: GetOptions): RedisCommandArguments { +export type TsGetReply = TuplesReply<[]> | TuplesReply<[NumberReply, DoubleReply]>; + +export default { + FIRST_KEY_INDEX: 1, + IS_READ_ONLY: true, + transformArguments(key: RedisArgument, options?: TsGetOptions) { return pushLatestArgument(['TS.GET', key], options?.LATEST); -} - -export function transformReply(reply: [] | SampleRawReply): null | SampleReply { - if (reply.length === 0) return null; - - return transformSampleReply(reply); -} + }, + transformReply: { + 2(reply: Resp2Reply) { + return reply.length === 0 ? null : { + timestamp: reply[0], + value: Number(reply[1]) + }; + }, + 3(reply: TsGetReply) { + return reply.length === 0 ? null : { + timestamp: reply[0], + value: reply[1] + }; + } + } +} as const satisfies Command;