From f476c389711d34889bcbc90897085ee7e80a62f0 Mon Sep 17 00:00:00 2001 From: Leibale Eidelman Date: Tue, 29 Aug 2023 11:21:34 -0700 Subject: [PATCH] change JSON.MSET signature, add to json command object, fix tests --- packages/json/lib/commands/MSET.spec.ts | 26 +++++++++++++++++---- packages/json/lib/commands/MSET.ts | 31 ++++++++++++++----------- packages/json/lib/commands/index.ts | 3 +++ 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/packages/json/lib/commands/MSET.spec.ts b/packages/json/lib/commands/MSET.spec.ts index a8ecf9eabd..53d4d82250 100644 --- a/packages/json/lib/commands/MSET.spec.ts +++ b/packages/json/lib/commands/MSET.spec.ts @@ -5,15 +5,31 @@ import { transformArguments } from './MSET'; describe('MSET', () => { it('transformArguments', () => { assert.deepEqual( - transformArguments(['1', '2'], '$', [{ a: 1 }, { b: 2 }]), - ['JSON.MSET', '1', '$', '{ "a":"1" } ', '2', '$', '{ "b":"2"} '] + transformArguments([{ + key: '1', + path: '$', + value: 1 + }, { + key: '2', + path: '$', + value: '2' + }]), + ['JSON.MSET', '1', '$', '1', '2', '$', '"2"'] ); }); - testUtils.testWithClient('client.json.mGet', async client => { + testUtils.testWithClient('client.json.mSet', async client => { assert.deepEqual( - await client.json.mGet(["1", "2"], "$", [{ a: 1 }, { b: 2 }]), - [null, null] + await client.json.mSet([{ + key: '1', + path: '$', + value: 1 + }, { + key: '2', + path: '$', + value: '2' + }]), + 'OK' ); }, GLOBAL.SERVERS.OPEN); }); diff --git a/packages/json/lib/commands/MSET.ts b/packages/json/lib/commands/MSET.ts index ea553f4e88..79af2b3c0f 100644 --- a/packages/json/lib/commands/MSET.ts +++ b/packages/json/lib/commands/MSET.ts @@ -1,24 +1,27 @@ -import { RedisJSON, transformRedisJsonArgument } from "."; +import { RedisJSON, transformRedisJsonArgument } from '.'; +import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands'; export const FIRST_KEY_INDEX = 1; -export function transformArguments( - keys: Array, - path: string, - json: Array -): Array { - - if (keys.length != json.length) - throw new Error("Number of keys and json objects must be equal"); +interface JsonMSetItem { + key: RedisCommandArgument; + path: RedisCommandArgument; + value: RedisJSON; +} - let args: Array = ["JSON.SET"]; +export function transformArguments(items: Array): Array { + const args = new Array(1 + items.length * 3); + args[0] = 'JSON.MSET'; - // walk through the key array, adding the key, the path and the json objects, calling transformRedisJsonArgument for each - for (let i = 0; i < keys.length; i++) { - args.push(keys[i], path, transformRedisJsonArgument(json[i])); + let argsIndex = 1; + for (let i = 0; i < items.length; i++) { + const item = items[i]; + args[argsIndex++] = item.key; + args[argsIndex++] = item.path; + args[argsIndex++] = transformRedisJsonArgument(item.json); } return args; } -export declare function transformReply(): "OK" | null; +export declare function transformReply(): 'OK'; diff --git a/packages/json/lib/commands/index.ts b/packages/json/lib/commands/index.ts index efcf156b84..a42ece5f27 100644 --- a/packages/json/lib/commands/index.ts +++ b/packages/json/lib/commands/index.ts @@ -9,6 +9,7 @@ import * as DEL from './DEL'; import * as FORGET from './FORGET'; import * as GET from './GET'; import * as MGET from './MGET'; +import * as MSET from './MSET'; import * as NUMINCRBY from './NUMINCRBY'; import * as NUMMULTBY from './NUMMULTBY'; import * as OBJKEYS from './OBJKEYS'; @@ -42,6 +43,8 @@ export default { get: GET, MGET, mGet: MGET, + MSET, + mSet: MSET, NUMINCRBY, numIncrBy: NUMINCRBY, NUMMULTBY,