Skip to content

Commit 1325993

Browse files
committed
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.
1 parent 294cbf8 commit 1325993

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { strict as assert } from 'assert';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import { transformArguments } from './MSET';
4+
5+
describe('MSET', () => {
6+
it('transformArguments', () => {
7+
assert.deepEqual(
8+
transformArguments(['1', '2'], '$', [{ a: 1 }, { b: 2 }]),
9+
['JSON.MSET', '1', '$', '{ "a":"1" } ', '2', '$', '{ "b":"2"} ']
10+
);
11+
});
12+
13+
testUtils.testWithClient('client.json.mGet', async client => {
14+
assert.deepEqual(
15+
await client.json.mGet(["1", "2"], "$", [{ a: 1 }, { b: 2 }]),
16+
[null, null]
17+
);
18+
}, GLOBAL.SERVERS.OPEN);
19+
});

packages/json/lib/commands/MSET.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { RedisJSON, transformRedisJsonArgument } from ".";
2+
3+
export const FIRST_KEY_INDEX = 1;
4+
5+
export function transformArguments(
6+
keys: Array<string>,
7+
path: string,
8+
json: Array<RedisJSON>
9+
): Array<string> {
10+
11+
if (keys.length != json.length)
12+
throw new Error("Number of keys and json objects must be equal");
13+
14+
let args: Array<string> = ["JSON.SET"];
15+
16+
// walk through the key array, adding the key, the path and the json objects, calling transformRedisJsonArgument for each
17+
for (let i = 0; i < keys.length; i++) {
18+
args.push(keys[i], path, transformRedisJsonArgument(json[i]));
19+
}
20+
21+
return args;
22+
}
23+
24+
export declare function transformReply(): "OK" | null;

0 commit comments

Comments
 (0)