Skip to content

Commit f476c38

Browse files
authored
change JSON.MSET signature, add to json command object, fix tests
1 parent 1325993 commit f476c38

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

packages/json/lib/commands/MSET.spec.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,31 @@ import { transformArguments } from './MSET';
55
describe('MSET', () => {
66
it('transformArguments', () => {
77
assert.deepEqual(
8-
transformArguments(['1', '2'], '$', [{ a: 1 }, { b: 2 }]),
9-
['JSON.MSET', '1', '$', '{ "a":"1" } ', '2', '$', '{ "b":"2"} ']
8+
transformArguments([{
9+
key: '1',
10+
path: '$',
11+
value: 1
12+
}, {
13+
key: '2',
14+
path: '$',
15+
value: '2'
16+
}]),
17+
['JSON.MSET', '1', '$', '1', '2', '$', '"2"']
1018
);
1119
});
1220

13-
testUtils.testWithClient('client.json.mGet', async client => {
21+
testUtils.testWithClient('client.json.mSet', async client => {
1422
assert.deepEqual(
15-
await client.json.mGet(["1", "2"], "$", [{ a: 1 }, { b: 2 }]),
16-
[null, null]
23+
await client.json.mSet([{
24+
key: '1',
25+
path: '$',
26+
value: 1
27+
}, {
28+
key: '2',
29+
path: '$',
30+
value: '2'
31+
}]),
32+
'OK'
1733
);
1834
}, GLOBAL.SERVERS.OPEN);
1935
});

packages/json/lib/commands/MSET.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
import { RedisJSON, transformRedisJsonArgument } from ".";
1+
import { RedisJSON, transformRedisJsonArgument } from '.';
2+
import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands';
23

34
export const FIRST_KEY_INDEX = 1;
45

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");
6+
interface JsonMSetItem {
7+
key: RedisCommandArgument;
8+
path: RedisCommandArgument;
9+
value: RedisJSON;
10+
}
1311

14-
let args: Array<string> = ["JSON.SET"];
12+
export function transformArguments(items: Array<JsonMSetItem>): Array<string> {
13+
const args = new Array(1 + items.length * 3);
14+
args[0] = 'JSON.MSET';
1515

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]));
16+
let argsIndex = 1;
17+
for (let i = 0; i < items.length; i++) {
18+
const item = items[i];
19+
args[argsIndex++] = item.key;
20+
args[argsIndex++] = item.path;
21+
args[argsIndex++] = transformRedisJsonArgument(item.json);
1922
}
2023

2124
return args;
2225
}
2326

24-
export declare function transformReply(): "OK" | null;
27+
export declare function transformReply(): 'OK';

packages/json/lib/commands/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as DEL from './DEL';
99
import * as FORGET from './FORGET';
1010
import * as GET from './GET';
1111
import * as MGET from './MGET';
12+
import * as MSET from './MSET';
1213
import * as NUMINCRBY from './NUMINCRBY';
1314
import * as NUMMULTBY from './NUMMULTBY';
1415
import * as OBJKEYS from './OBJKEYS';
@@ -42,6 +43,8 @@ export default {
4243
get: GET,
4344
MGET,
4445
mGet: MGET,
46+
MSET,
47+
mSet: MSET,
4548
NUMINCRBY,
4649
numIncrBy: NUMINCRBY,
4750
NUMMULTBY,

0 commit comments

Comments
 (0)