1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

change JSON.MSET signature, add to json command object, fix tests

This commit is contained in:
Leibale Eidelman
2023-08-29 11:21:34 -07:00
committed by GitHub
parent 132599332f
commit f476c38971
3 changed files with 41 additions and 19 deletions

View File

@ -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);
});

View File

@ -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<string>,
path: string,
json: Array<RedisJSON>
): Array<string> {
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<string> = ["JSON.SET"];
export function transformArguments(items: Array<JsonMSetItem>): Array<string> {
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';

View File

@ -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,