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

Update doctest client with latest v4 release (#2844)

This commit is contained in:
Shaya Potter
2024-09-29 13:19:06 +03:00
committed by GitHub
parent fd7b10be6c
commit 49fdb79897
167 changed files with 8227 additions and 9599 deletions

View File

@@ -1,6 +1,6 @@
# @redis/json
This package provides support for the [RedisJSON](https://redisjson.io) module, which adds JSON as a native data type to Redis. It extends the [Node Redis client](https://github.com/redis/node-redis) to include functions for each of the RedisJSON commands.
This package provides support for the [RedisJSON](https://redis.io/docs/stack/json/) module, which adds JSON as a native data type to Redis. It extends the [Node Redis client](https://github.com/redis/node-redis) to include functions for each of the RedisJSON commands.
To use these extra commands, your Redis server must have the RedisJSON module installed.
@@ -10,7 +10,7 @@ For a complete example, see [`managing-json.js`](https://github.com/redis/node-r
### Storing JSON Documents in Redis
The [`JSON.SET`](https://oss.redis.com/redisjson/commands/#jsonset) command stores a JSON value at a given JSON Path in a Redis key.
The [`JSON.SET`](https://redis.io/commands/json.set/) command stores a JSON value at a given JSON Path in a Redis key.
Here, we'll store a JSON document in the root of the Redis key "`mydoc`":
@@ -37,11 +37,11 @@ await client.json.set('noderedis:jsondata', '$', {
});
```
For more information about RedisJSON's path syntax, [check out the documentation](https://oss.redis.com/redisjson/path/).
For more information about RedisJSON's path syntax, [check out the documentation](https://redis.io/docs/stack/json/path/).
### Retrieving JSON Documents from Redis
With RedisJSON, we can retrieve all or part(s) of a JSON document using the [`JSON.GET`]() command and one or more JSON Paths. Let's get the name and age of one of the pets:
With RedisJSON, we can retrieve all or part(s) of a JSON document using the [`JSON.GET`](https://redis.io/commands/json.get/) command and one or more JSON Paths. Let's get the name and age of one of the pets:
```javascript
const results = await client.json.get('noderedis:jsondata', {
@@ -62,19 +62,19 @@ const results = await client.json.get('noderedis:jsondata', {
RedisJSON includes commands that can atomically update values in a JSON document, in place in Redis without having to first retrieve the entire document.
Using the [`JSON.NUMINCRBY`](https://oss.redis.com/redisjson/commands/#jsonnumincrby) command, we can update the age of one of the pets like this:
Using the [`JSON.NUMINCRBY`](https://redis.io/commands/json.numincrby/) command, we can update the age of one of the pets like this:
```javascript
await client.json.numIncrBy('noderedis:jsondata', '.pets[1].age', 1);
```
And we can add a new object to the pets array with the [`JSON.ARRAPPEND`](https://oss.redis.com/redisjson/commands/#jsonarrappend) command:
And we can add a new object to the pets array with the [`JSON.ARRAPPEND`](https://redis.io/commands/json.arrappend/) command:
```javascript
await client.json.arrAppend('noderedis:jsondata', '.pets', {
name: 'Robin',
species: 'bird',
age: 1,
isMammal: false
});
await client.json.arrAppend('noderedis:jsondata', '.pets', {
name: 'Robin',
species: 'bird',
age: 1,
isMammal: false
});
```

View File

@@ -0,0 +1,21 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './MERGE';
describe('MERGE', () => {
testUtils.isVersionGreaterThanHook([2, 6]);
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', '$', 1),
['JSON.MERGE', 'key', '$', '1']
);
});
testUtils.testWithClient('client.json.merge', async client => {
assert.equal(
await client.json.merge('key', '$', 'json'),
'OK'
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -0,0 +1,9 @@
import { RedisJSON, transformRedisJsonArgument } from '.';
export const FIRST_KEY_INDEX = 1;
export function transformArguments(key: string, path: string, json: RedisJSON): Array<string> {
return ['JSON.MERGE', key, path, transformRedisJsonArgument(json)];
}
export declare function transformReply(): 'OK';

View File

@@ -2,6 +2,8 @@ import { RedisJSON, transformRedisJsonNullReply } from '.';
export const FIRST_KEY_INDEX = 1;
export const IS_READ_ONLY = true;
export function transformArguments(keys: Array<string>, path: string): Array<string> {
return [
'JSON.MGET',

View File

@@ -0,0 +1,35 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './MSET';
describe('MSET', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments([{
key: '1',
path: '$',
value: 1
}, {
key: '2',
path: '$',
value: '2'
}]),
['JSON.MSET', '1', '$', '1', '2', '$', '"2"']
);
});
testUtils.testWithClient('client.json.mSet', async client => {
assert.deepEqual(
await client.json.mSet([{
key: '1',
path: '$',
value: 1
}, {
key: '2',
path: '$',
value: '2'
}]),
'OK'
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -0,0 +1,28 @@
import { RedisJSON, transformRedisJsonArgument } from '.';
import { RedisCommandArgument } from '@redis/client/dist/lib/commands';
export const FIRST_KEY_INDEX = 1;
interface JsonMSetItem {
key: RedisCommandArgument;
path: RedisCommandArgument;
value: RedisJSON;
}
export function transformArguments(items: Array<JsonMSetItem>): Array<string> {
const args = new Array(1 + items.length * 3);
args[0] = 'JSON.MSET';
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.value);
}
return args;
}
export declare function transformReply(): 'OK';

View File

@@ -8,7 +8,9 @@ import * as DEBUG_MEMORY from './DEBUG_MEMORY';
import * as DEL from './DEL';
import * as FORGET from './FORGET';
import * as GET from './GET';
import * as MERGE from './MERGE';
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';
@@ -40,8 +42,12 @@ export default {
forget: FORGET,
GET,
get: GET,
MERGE,
merge: MERGE,
MGET,
mGet: MGET,
MSET,
mSet: MSET,
NUMINCRBY,
numIncrBy: NUMINCRBY,
NUMMULTBY,

View File

@@ -4,7 +4,7 @@ import RedisJSON from '.';
export default new TestUtils({
dockerImageName: 'redislabs/rejson',
dockerImageVersionArgument: 'rejson-version',
defaultDockerVersion: '2.0.9'
defaultDockerVersion: '2.6.9'
});
export const GLOBAL = {

View File

@@ -1,6 +1,6 @@
{
"name": "@redis/json",
"version": "1.0.4",
"version": "1.0.7",
"license": "MIT",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
@@ -18,12 +18,24 @@
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@redis/test-utils": "*",
"@types/node": "^18.11.6",
"@types/node": "^20.6.2",
"nyc": "^15.1.0",
"release-it": "^15.5.0",
"release-it": "^16.1.5",
"source-map-support": "^0.5.21",
"ts-node": "^10.9.1",
"typedoc": "^0.23.18",
"typescript": "^4.8.4"
}
"typedoc": "^0.25.1",
"typescript": "^5.2.2"
},
"repository": {
"type": "git",
"url": "git://github.com/redis/node-redis.git"
},
"bugs": {
"url": "https://github.com/redis/node-redis/issues"
},
"homepage": "https://github.com/redis/node-redis/tree/master/packages/json",
"keywords": [
"redis",
"RedisJSON"
]
}