From 132599332faf4444bf6e57d2913232d65cab5128 Mon Sep 17 00:00:00 2001 From: Rowan Trollope Date: Tue, 29 Aug 2023 10:31:34 -0700 Subject: [PATCH] Added MSET command MSET command added. Requires all keys to have the same JSON Path, which might fit most use cases, but is a limitation. Optionally we could make the path an array as well to support all use cases. --- packages/json/lib/commands/MSET.spec.ts | 19 +++++++++++++++++++ packages/json/lib/commands/MSET.ts | 24 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 packages/json/lib/commands/MSET.spec.ts create mode 100644 packages/json/lib/commands/MSET.ts diff --git a/packages/json/lib/commands/MSET.spec.ts b/packages/json/lib/commands/MSET.spec.ts new file mode 100644 index 0000000000..a8ecf9eabd --- /dev/null +++ b/packages/json/lib/commands/MSET.spec.ts @@ -0,0 +1,19 @@ +import { strict as assert } from 'assert'; +import testUtils, { GLOBAL } from '../test-utils'; +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"} '] + ); + }); + + testUtils.testWithClient('client.json.mGet', async client => { + assert.deepEqual( + await client.json.mGet(["1", "2"], "$", [{ a: 1 }, { b: 2 }]), + [null, null] + ); + }, GLOBAL.SERVERS.OPEN); +}); diff --git a/packages/json/lib/commands/MSET.ts b/packages/json/lib/commands/MSET.ts new file mode 100644 index 0000000000..ea553f4e88 --- /dev/null +++ b/packages/json/lib/commands/MSET.ts @@ -0,0 +1,24 @@ +import { RedisJSON, transformRedisJsonArgument } from "."; + +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"); + + let args: Array = ["JSON.SET"]; + + // 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])); + } + + return args; +} + +export declare function transformReply(): "OK" | null;