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

V5 bringing RESP3, Sentinel and TypeMapping to node-redis

RESP3 Support
   - Some commands responses in RESP3 aren't stable yet and therefore return an "untyped" ReplyUnion.
 
Sentinel

TypeMapping

Correctly types Multi commands

Note: some API changes to be further documented in v4-to-v5.md
This commit is contained in:
Shaya Potter
2024-10-15 17:46:52 +03:00
committed by GitHub
parent 2fc79bdfb3
commit b2d35c5286
1174 changed files with 45931 additions and 36274 deletions

View File

@@ -1,30 +1,30 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './ARRAPPEND';
import ARRAPPEND from './ARRAPPEND';
describe('ARRAPPEND', () => {
describe('transformArguments', () => {
it('single JSON', () => {
assert.deepEqual(
transformArguments('key', '$', 1),
['JSON.ARRAPPEND', 'key', '$', '1']
);
});
it('multiple JSONs', () => {
assert.deepEqual(
transformArguments('key', '$', 1, 2),
['JSON.ARRAPPEND', 'key', '$', '1', '2']
);
});
describe('JSON.ARRAPPEND', () => {
describe('transformArguments', () => {
it('single element', () => {
assert.deepEqual(
ARRAPPEND.transformArguments('key', '$', 'value'),
['JSON.ARRAPPEND', 'key', '$', '"value"']
);
});
testUtils.testWithClient('client.json.arrAppend', async client => {
await client.json.set('key', '$', []);
it('multiple elements', () => {
assert.deepEqual(
ARRAPPEND.transformArguments('key', '$', 1, 2),
['JSON.ARRAPPEND', 'key', '$', '1', '2']
);
});
});
assert.deepEqual(
await client.json.arrAppend('key', '$', 1),
[1]
);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('client.json.arrAppend', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '$', []),
client.json.arrAppend('key', '$', 'value')
]);
assert.deepEqual(reply, [1]);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,15 +1,27 @@
import { RedisJSON, transformRedisJsonArgument } from '.';
import { RedisArgument, NumberReply, ArrayReply, NullReply, Command } from '@redis/client/dist/lib/RESP/types';
export const FIRST_KEY_INDEX = 1;
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(
key: RedisArgument,
path: RedisArgument,
json: RedisJSON,
...jsons: Array<RedisJSON>
) {
const args = new Array<RedisArgument>(4 + jsons.length);
args[0] = 'JSON.ARRAPPEND';
args[1] = key;
args[2] = path;
args[3] = transformRedisJsonArgument(json);
export function transformArguments(key: string, path: string, ...jsons: Array<RedisJSON>): Array<string> {
const args = ['JSON.ARRAPPEND', key, path];
for (const json of jsons) {
args.push(transformRedisJsonArgument(json));
let argsIndex = 4;
for (let i = 0; i < jsons.length; i++) {
args[argsIndex++] = transformRedisJsonArgument(jsons[i]);
}
return args;
}
export declare function transformReply(): number | Array<number>;
},
transformReply: undefined as unknown as () => NumberReply | ArrayReply<NumberReply | NullReply>
} as const satisfies Command;

View File

@@ -1,37 +1,48 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './ARRINDEX';
import ARRINDEX from './ARRINDEX';
describe('ARRINDEX', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments('key', '$', 'json'),
['JSON.ARRINDEX', 'key', '$', '"json"']
);
});
it('with start', () => {
assert.deepEqual(
transformArguments('key', '$', 'json', 1),
['JSON.ARRINDEX', 'key', '$', '"json"', '1']
);
});
it('with start, end', () => {
assert.deepEqual(
transformArguments('key', '$', 'json', 1, 2),
['JSON.ARRINDEX', 'key', '$', '"json"', '1', '2']
);
});
describe('JSON.ARRINDEX', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
ARRINDEX.transformArguments('key', '$', 'value'),
['JSON.ARRINDEX', 'key', '$', '"value"']
);
});
testUtils.testWithClient('client.json.arrIndex', async client => {
await client.json.set('key', '$', []);
describe('with range', () => {
it('start only', () => {
assert.deepEqual(
await client.json.arrIndex('key', '$', 'json'),
[-1]
ARRINDEX.transformArguments('key', '$', 'value', {
range: {
start: 0
}
}),
['JSON.ARRINDEX', 'key', '$', '"value"', '0']
);
}, GLOBAL.SERVERS.OPEN);
});
it('with start and stop', () => {
assert.deepEqual(
ARRINDEX.transformArguments('key', '$', 'value', {
range: {
start: 0,
stop: 1
}
}),
['JSON.ARRINDEX', 'key', '$', '"value"', '0', '1']
);
});
});
});
testUtils.testWithClient('client.json.arrIndex', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '$', []),
client.json.arrIndex('key', '$', 'value')
]);
assert.deepEqual(reply, [-1]);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,21 +1,33 @@
import { RedisArgument, NumberReply, ArrayReply, NullReply, Command } from '@redis/client/dist/lib/RESP/types';
import { RedisJSON, transformRedisJsonArgument } from '.';
export const FIRST_KEY_INDEX = 1;
export interface JsonArrIndexOptions {
range?: {
start: number;
stop?: number;
};
}
export const IS_READ_ONLY = true;
export function transformArguments(key: string, path: string, json: RedisJSON, start?: number, stop?: number): Array<string> {
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(
key: RedisArgument,
path: RedisArgument,
json: RedisJSON,
options?: JsonArrIndexOptions
) {
const args = ['JSON.ARRINDEX', key, path, transformRedisJsonArgument(json)];
if (start !== undefined && start !== null) {
args.push(start.toString());
if (options?.range) {
args.push(options.range.start.toString());
if (stop !== undefined && stop !== null) {
args.push(stop.toString());
}
if (options.range.stop !== undefined) {
args.push(options.range.stop.toString());
}
}
return args;
}
export declare function transformReply(): number | Array<number>;
},
transformReply: undefined as unknown as () => NumberReply | ArrayReply<NumberReply | NullReply>
} as const satisfies Command;

View File

@@ -1,30 +1,30 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './ARRINSERT';
import ARRINSERT from './ARRINSERT';
describe('ARRINSERT', () => {
describe('transformArguments', () => {
it('single JSON', () => {
assert.deepEqual(
transformArguments('key', '$', 0, 'json'),
['JSON.ARRINSERT', 'key', '$', '0', '"json"']
);
});
it('multiple JSONs', () => {
assert.deepEqual(
transformArguments('key', '$', 0, '1', '2'),
['JSON.ARRINSERT', 'key', '$', '0', '"1"', '"2"']
);
});
describe('JSON.ARRINSERT', () => {
describe('transformArguments', () => {
it('single element', () => {
assert.deepEqual(
ARRINSERT.transformArguments('key', '$', 0, 'value'),
['JSON.ARRINSERT', 'key', '$', '0', '"value"']
);
});
testUtils.testWithClient('client.json.arrInsert', async client => {
await client.json.set('key', '$', []);
it('multiple elements', () => {
assert.deepEqual(
ARRINSERT.transformArguments('key', '$', 0, '1', '2'),
['JSON.ARRINSERT', 'key', '$', '0', '"1"', '"2"']
);
});
});
assert.deepEqual(
await client.json.arrInsert('key', '$', 0, 'json'),
[1]
);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('client.json.arrInsert', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '$', []),
client.json.arrInsert('key', '$', 0, 'value')
]);
assert.deepEqual(reply, [1]);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,15 +1,29 @@
import { RedisArgument, NumberReply, ArrayReply, NullReply, Command } from '@redis/client/dist/lib/RESP/types';
import { RedisJSON, transformRedisJsonArgument } from '.';
export const FIRST_KEY_INDEX = 1;
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(
key: RedisArgument,
path: RedisArgument,
index: number,
json: RedisJSON,
...jsons: Array<RedisJSON>
) {
const args = new Array<RedisArgument>(4 + jsons.length);
args[0] = 'JSON.ARRINSERT';
args[1] = key;
args[2] = path;
args[3] = index.toString();
args[4] = transformRedisJsonArgument(json);
export function transformArguments(key: string, path: string, index: number, ...jsons: Array<RedisJSON>): Array<string> {
const args = ['JSON.ARRINSERT', key, path, index.toString()];
for (const json of jsons) {
args.push(transformRedisJsonArgument(json));
let argsIndex = 5;
for (let i = 0; i < jsons.length; i++) {
args[argsIndex++] = transformRedisJsonArgument(jsons[i]);
}
return args;
}
export declare function transformReply(): number | Array<number>;
},
transformReply: undefined as unknown as () => NumberReply | ArrayReply<NumberReply | NullReply>
} as const satisfies Command;

View File

@@ -1,30 +1,32 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './ARRLEN';
import ARRLEN from './ARRLEN';
describe('ARRLEN', () => {
describe('transformArguments', () => {
it('without path', () => {
assert.deepEqual(
transformArguments('key'),
['JSON.ARRLEN', 'key']
);
});
it('with path', () => {
assert.deepEqual(
transformArguments('key', '$'),
['JSON.ARRLEN', 'key', '$']
);
});
describe('JSON.ARRLEN', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
ARRLEN.transformArguments('key'),
['JSON.ARRLEN', 'key']
);
});
testUtils.testWithClient('client.json.arrLen', async client => {
await client.json.set('key', '$', []);
it('with path', () => {
assert.deepEqual(
ARRLEN.transformArguments('key', {
path: '$'
}),
['JSON.ARRLEN', 'key', '$']
);
});
});
assert.deepEqual(
await client.json.arrLen('key', '$'),
[0]
);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('client.json.arrLen', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '$', []),
client.json.arrLen('key')
]);
assert.equal(reply, 0);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,15 +1,20 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, ArrayReply, NumberReply, NullReply, Command } from '@redis/client/dist/lib/RESP/types';
export const IS_READ_ONLY = true;
export interface JsonArrLenOptions {
path?: RedisArgument;
}
export function transformArguments(key: string, path?: string): Array<string> {
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, options?: JsonArrLenOptions) {
const args = ['JSON.ARRLEN', key];
if (path) {
args.push(path);
if (options?.path !== undefined) {
args.push(options.path);
}
return args;
}
export declare function transformReply(): number | Array<number>;
},
transformReply: undefined as unknown as () => NumberReply | ArrayReply<NumberReply | NullReply>
} as const satisfies Command;

View File

@@ -1,57 +1,66 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './ARRPOP';
import ARRPOP from './ARRPOP';
describe('ARRPOP', () => {
describe('transformArguments', () => {
it('key', () => {
assert.deepEqual(
transformArguments('key'),
['JSON.ARRPOP', 'key']
);
});
it('key, path', () => {
assert.deepEqual(
transformArguments('key', '$'),
['JSON.ARRPOP', 'key', '$']
);
});
it('key, path, index', () => {
assert.deepEqual(
transformArguments('key', '$', 0),
['JSON.ARRPOP', 'key', '$', '0']
);
});
describe('JSON.ARRPOP', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
ARRPOP.transformArguments('key'),
['JSON.ARRPOP', 'key']
);
});
describe('client.json.arrPop', () => {
testUtils.testWithClient('null', async client => {
await client.json.set('key', '.', []);
assert.equal(
await client.json.arrPop('key', '.'),
null
);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('with value', async client => {
await client.json.set('key', '.', ['value']);
assert.equal(
await client.json.arrPop('key', '.'),
'value'
);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('array', async client => {
await client.json.set('key', '$', ['value']);
assert.deepEqual(
await client.json.arrPop('key', '$'),
['value']
);
}, GLOBAL.SERVERS.OPEN);
it('with path', () => {
assert.deepEqual(
ARRPOP.transformArguments('key', {
path: '$'
}),
['JSON.ARRPOP', 'key', '$']
);
});
it('with path and index', () => {
assert.deepEqual(
ARRPOP.transformArguments('key', {
path: '$',
index: 0
}),
['JSON.ARRPOP', 'key', '$', '0']
);
});
});
describe('client.json.arrPop', () => {
testUtils.testWithClient('without path and value', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '$', []),
client.json.arrPop('key')
]);
assert.equal(reply, null);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('. path with value', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '.', ['value']),
client.json.arrPop('key', {
path: '.'
})
]);
assert.equal(reply, 'value');
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('$ path with value', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '$', ['value']),
client.json.arrPop('key', {
path: '$'
})
]);
assert.deepEqual(reply, ['value']);
}, GLOBAL.SERVERS.OPEN);
});
});

View File

@@ -1,27 +1,32 @@
import { RedisJSON, transformRedisJsonNullReply } from '.';
import { RedisArgument, ArrayReply, NullReply, BlobStringReply, Command, UnwrapReply } from '@redis/client/dist/lib/RESP/types';
import { isArrayReply } from '@redis/client/dist/lib/commands/generic-transformers';
import { transformRedisJsonNullReply } from '.';
export const FIRST_KEY_INDEX = 1;
export interface RedisArrPopOptions {
path: RedisArgument;
index?: number;
}
export function transformArguments(key: string, path?: string, index?: number): Array<string> {
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, options?: RedisArrPopOptions) {
const args = ['JSON.ARRPOP', key];
if (path) {
args.push(path);
if (options) {
args.push(options.path);
if (index !== undefined && index !== null) {
args.push(index.toString());
}
if (options.index !== undefined) {
args.push(options.index.toString());
}
}
return args;
}
},
transformReply(reply: NullReply | BlobStringReply | ArrayReply<NullReply | BlobStringReply>) {
return isArrayReply(reply) ?
(reply as unknown as UnwrapReply<typeof reply>).map(item => transformRedisJsonNullReply(item)) :
transformRedisJsonNullReply(reply);
}
} as const satisfies Command;
export function transformReply(reply: null | string | Array<null | string>): null | RedisJSON | Array<RedisJSON> {
if (reply === null) return null;
if (Array.isArray(reply)) {
return reply.map(transformRedisJsonNullReply);
}
return transformRedisJsonNullReply(reply);
}

View File

@@ -1,21 +1,21 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './ARRTRIM';
import ARRTRIM from './ARRTRIM';
describe('ARRTRIM', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', '$', 0, 1),
['JSON.ARRTRIM', 'key', '$', '0', '1']
);
});
describe('JSON.ARRTRIM', () => {
it('transformArguments', () => {
assert.deepEqual(
ARRTRIM.transformArguments('key', '$', 0, 1),
['JSON.ARRTRIM', 'key', '$', '0', '1']
);
});
testUtils.testWithClient('client.json.arrTrim', async client => {
await client.json.set('key', '$', []);
testUtils.testWithClient('client.json.arrTrim', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '$', []),
client.json.arrTrim('key', '$', 0, 1)
]);
assert.deepEqual(
await client.json.arrTrim('key', '$', 0, 1),
[0]
);
}, GLOBAL.SERVERS.OPEN);
assert.deepEqual(reply, [0]);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,7 +1,10 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, ArrayReply, NumberReply, NullReply, Command } from '@redis/client/dist/lib/RESP/types';
export function transformArguments(key: string, path: string, start: number, stop: number): Array<string> {
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, path: RedisArgument, start: number, stop: number) {
return ['JSON.ARRTRIM', key, path, start.toString(), stop.toString()];
}
export declare function transformReply(): number | Array<number>;
},
transformReply: undefined as unknown as () => NumberReply | ArrayReply<NumberReply | NullReply>
} as const satisfies Command;

View File

@@ -0,0 +1,32 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import CLEAR from './CLEAR';
describe('JSON.CLEAR', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
CLEAR.transformArguments('key'),
['JSON.CLEAR', 'key']
);
});
it('with path', () => {
assert.deepEqual(
CLEAR.transformArguments('key', {
path: '$'
}),
['JSON.CLEAR', 'key', '$']
);
});
});
testUtils.testWithClient('client.json.clear', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '$', null),
client.json.clear('key')
]);
assert.equal(reply, 0);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -0,0 +1,20 @@
import { RedisArgument, NumberReply, Command } from '@redis/client/dist/lib/RESP/types';
export interface JsonClearOptions {
path?: RedisArgument;
}
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, options?: JsonClearOptions) {
const args = ['JSON.CLEAR', key];
if (options?.path !== undefined) {
args.push(options.path);
}
return args;
},
transformReply: undefined as unknown as () => NumberReply
} as const satisfies Command;

View File

@@ -1,28 +1,30 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './DEBUG_MEMORY';
import DEBUG_MEMORY from './DEBUG_MEMORY';
describe('DEBUG MEMORY', () => {
describe('transformArguments', () => {
it('without path', () => {
assert.deepEqual(
transformArguments('key'),
['JSON.DEBUG', 'MEMORY', 'key']
);
});
it('with path', () => {
assert.deepEqual(
transformArguments('key', '$'),
['JSON.DEBUG', 'MEMORY', 'key', '$']
);
});
describe('JSON.DEBUG MEMORY', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
DEBUG_MEMORY.transformArguments('key'),
['JSON.DEBUG', 'MEMORY', 'key']
);
});
testUtils.testWithClient('client.json.arrTrim', async client => {
assert.deepEqual(
await client.json.debugMemory('key', '$'),
[]
);
}, GLOBAL.SERVERS.OPEN);
it('with path', () => {
assert.deepEqual(
DEBUG_MEMORY.transformArguments('key', {
path: '$'
}),
['JSON.DEBUG', 'MEMORY', 'key', '$']
);
});
});
testUtils.testWithClient('client.json.debugMemory', async client => {
assert.equal(
await client.json.debugMemory('key'),
0
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,13 +1,20 @@
export const FIRST_KEY_INDEX = 2;
import { RedisArgument, NumberReply, Command } from '@redis/client/dist/lib/RESP/types';
export function transformArguments(key: string, path?: string): Array<string> {
export interface JsonDebugMemoryOptions {
path?: RedisArgument;
}
export default {
FIRST_KEY_INDEX: 2,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, options?: JsonDebugMemoryOptions) {
const args = ['JSON.DEBUG', 'MEMORY', key];
if (path) {
args.push(path);
if (options?.path !== undefined) {
args.push(options.path);
}
return args;
}
export declare function transformReply(): number;
},
transformReply: undefined as unknown as () => NumberReply
} as const satisfies Command;

View File

@@ -1,28 +1,31 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './DEL';
import DEL from './DEL';
describe('DEL', () => {
describe('transformArguments', () => {
it('key', () => {
assert.deepEqual(
transformArguments('key'),
['JSON.DEL', 'key']
);
});
it('key, path', () => {
assert.deepEqual(
transformArguments('key', '$.path'),
['JSON.DEL', 'key', '$.path']
);
});
describe('JSON.DEL', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
DEL.transformArguments('key'),
['JSON.DEL', 'key']
);
});
testUtils.testWithClient('client.json.del', async client => {
assert.deepEqual(
await client.json.del('key'),
0
);
}, GLOBAL.SERVERS.OPEN);
it('with path', () => {
assert.deepEqual(
DEL.transformArguments('key', {
path: '$.path'
}),
['JSON.DEL', 'key', '$.path']
);
});
});
testUtils.testWithClient('client.json.del', async client => {
assert.equal(
await client.json.del('key'),
0
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,13 +1,20 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, NumberReply, Command } from '@redis/client/dist/lib/RESP/types';
export function transformArguments(key: string, path?: string): Array<string> {
export interface JsonDelOptions {
path?: RedisArgument
}
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, options?: JsonDelOptions) {
const args = ['JSON.DEL', key];
if (path) {
args.push(path);
if (options?.path !== undefined) {
args.push(options.path);
}
return args;
}
export declare function transformReply(): number;
},
transformReply: undefined as unknown as () => NumberReply
} as const satisfies Command;

View File

@@ -1,28 +1,30 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './FORGET';
import FORGET from './FORGET';
describe('FORGET', () => {
describe('transformArguments', () => {
it('key', () => {
assert.deepEqual(
transformArguments('key'),
['JSON.FORGET', 'key']
);
});
it('key, path', () => {
assert.deepEqual(
transformArguments('key', '$.path'),
['JSON.FORGET', 'key', '$.path']
);
});
describe('JSON.FORGET', () => {
describe('transformArguments', () => {
it('key', () => {
assert.deepEqual(
FORGET.transformArguments('key'),
['JSON.FORGET', 'key']
);
});
testUtils.testWithClient('client.json.forget', async client => {
assert.deepEqual(
await client.json.forget('key'),
0
);
}, GLOBAL.SERVERS.OPEN);
it('key, path', () => {
assert.deepEqual(
FORGET.transformArguments('key', {
path: '$.path'
}),
['JSON.FORGET', 'key', '$.path']
);
});
});
testUtils.testWithClient('client.json.forget', async client => {
assert.equal(
await client.json.forget('key'),
0
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,13 +1,20 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, NumberReply, Command } from '@redis/client/dist/lib/RESP/types';
export function transformArguments(key: string, path?: string): Array<string> {
export interface JsonForgetOptions {
path?: RedisArgument;
}
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, options?: JsonForgetOptions) {
const args = ['JSON.FORGET', key];
if (path) {
args.push(path);
if (options?.path !== undefined) {
args.push(options.path);
}
return args;
}
export declare function transformReply(): number;
},
transformReply: undefined as unknown as () => NumberReply
} as const satisfies Command;

View File

@@ -1,78 +1,37 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './GET';
import GET from './GET';
describe('GET', () => {
describe('transformArguments', () => {
describe('path', () => {
it('string', () => {
assert.deepEqual(
transformArguments('key', { path: '$' }),
['JSON.GET', 'key', '$']
);
});
it('array', () => {
assert.deepEqual(
transformArguments('key', { path: ['$.1', '$.2'] }),
['JSON.GET', 'key', '$.1', '$.2']
);
});
});
it('key', () => {
assert.deepEqual(
transformArguments('key'),
['JSON.GET', 'key']
);
});
it('INDENT', () => {
assert.deepEqual(
transformArguments('key', { INDENT: 'indent' }),
['JSON.GET', 'key', 'INDENT', 'indent']
);
});
it('NEWLINE', () => {
assert.deepEqual(
transformArguments('key', { NEWLINE: 'newline' }),
['JSON.GET', 'key', 'NEWLINE', 'newline']
);
});
it('SPACE', () => {
assert.deepEqual(
transformArguments('key', { SPACE: 'space' }),
['JSON.GET', 'key', 'SPACE', 'space']
);
});
it('NOESCAPE', () => {
assert.deepEqual(
transformArguments('key', { NOESCAPE: true }),
['JSON.GET', 'key', 'NOESCAPE']
);
});
it('INDENT, NEWLINE, SPACE, NOESCAPE, path', () => {
assert.deepEqual(
transformArguments('key', {
path: '$.path',
INDENT: 'indent',
NEWLINE: 'newline',
SPACE: 'space',
NOESCAPE: true
}),
['JSON.GET', 'key', '$.path', 'INDENT', 'indent', 'NEWLINE', 'newline', 'SPACE', 'space', 'NOESCAPE']
);
});
describe('JSON.GET', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
GET.transformArguments('key'),
['JSON.GET', 'key']
);
});
testUtils.testWithClient('client.json.get', async client => {
assert.equal(
await client.json.get('key'),
null
describe('with path', () => {
it('string', () => {
assert.deepEqual(
GET.transformArguments('key', { path: '$' }),
['JSON.GET', 'key', '$']
);
}, GLOBAL.SERVERS.OPEN);
});
it('array', () => {
assert.deepEqual(
GET.transformArguments('key', { path: ['$.1', '$.2'] }),
['JSON.GET', 'key', '$.1', '$.2']
);
});
});
});
testUtils.testWithClient('client.json.get', async client => {
assert.equal(
await client.json.get('key'),
null
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,42 +1,22 @@
import { pushVerdictArguments } from '@redis/client/dist/lib/commands/generic-transformers';
import { RedisCommandArguments } from '@redis/client/dist/lib/commands';
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
import { RedisVariadicArgument, pushVariadicArguments } from '@redis/client/dist/lib/commands/generic-transformers';
import { transformRedisJsonNullReply } from '.';
export const FIRST_KEY_INDEX = 1;
export const IS_READ_ONLY = true;
interface GetOptions {
path?: string | Array<string>;
INDENT?: string;
NEWLINE?: string;
SPACE?: string;
NOESCAPE?: true;
export interface JsonGetOptions {
path?: RedisVariadicArgument;
}
export function transformArguments(key: string, options?: GetOptions): RedisCommandArguments {
let args: RedisCommandArguments = ['JSON.GET', key];
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, options?: JsonGetOptions) {
let args = ['JSON.GET', key];
if (options?.path) {
args = pushVerdictArguments(args, options.path);
}
if (options?.INDENT) {
args.push('INDENT', options.INDENT);
}
if (options?.NEWLINE) {
args.push('NEWLINE', options.NEWLINE);
}
if (options?.SPACE) {
args.push('SPACE', options.SPACE);
}
if (options?.NOESCAPE) {
args.push('NOESCAPE');
if (options?.path !== undefined) {
args = pushVariadicArguments(args, options.path);
}
return args;
}
export { transformRedisJsonNullReply as transformReply } from '.';
},
transformReply: transformRedisJsonNullReply
} as const satisfies Command;

View File

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

View File

@@ -1,9 +1,16 @@
import { SimpleStringReply, Command, RedisArgument } from '@redis/client/dist/lib/RESP/types';
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';
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, path: RedisArgument, value: RedisJSON) {
return [
'JSON.MERGE',
key,
path,
transformRedisJsonArgument(value)
];
},
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
} as const satisfies Command;

View File

@@ -1,19 +1,19 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './MGET';
import MGET from './MGET';
describe('MGET', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments(['1', '2'], '$'),
['JSON.MGET', '1', '2', '$']
);
});
describe('JSON.MGET', () => {
it('transformArguments', () => {
assert.deepEqual(
MGET.transformArguments(['1', '2'], '$'),
['JSON.MGET', '1', '2', '$']
);
});
testUtils.testWithClient('client.json.mGet', async client => {
assert.deepEqual(
await client.json.mGet(['1', '2'], '$'),
[null, null]
);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('client.json.mGet', async client => {
assert.deepEqual(
await client.json.mGet(['1', '2'], '$'),
[null, null]
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,17 +1,17 @@
import { RedisJSON, transformRedisJsonNullReply } from '.';
import { RedisArgument, UnwrapReply, ArrayReply, NullReply, BlobStringReply, Command } from '@redis/client/dist/lib/RESP/types';
import { transformRedisJsonNullReply } from '.';
export const FIRST_KEY_INDEX = 1;
export const IS_READ_ONLY = true;
export function transformArguments(keys: Array<string>, path: string): Array<string> {
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(keys: Array<RedisArgument>, path: RedisArgument) {
return [
'JSON.MGET',
...keys,
path
'JSON.MGET',
...keys,
path
];
}
export function transformReply(reply: Array<string | null>): Array<RedisJSON | null> {
return reply.map(transformRedisJsonNullReply);
}
},
transformReply(reply: UnwrapReply<ArrayReply<NullReply | BlobStringReply>>) {
return reply.map(json => transformRedisJsonNullReply(json))
}
} as const satisfies Command;

View File

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

View File

@@ -1,28 +1,28 @@
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/dist/lib/RESP/types';
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 interface JsonMSetItem {
key: RedisArgument;
path: RedisArgument;
value: RedisJSON;
}
export function transformArguments(items: Array<JsonMSetItem>): Array<string> {
const args = new Array(1 + items.length * 3);
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(items: Array<JsonMSetItem>) {
const args = new Array<RedisArgument>(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);
const item = items[i];
args[argsIndex++] = item.key;
args[argsIndex++] = item.path;
args[argsIndex++] = transformRedisJsonArgument(item.value);
}
return args;
}
export declare function transformReply(): 'OK';
},
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
} as const satisfies Command;

View File

@@ -1,21 +1,21 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './NUMINCRBY';
import NUMINCRBY from './NUMINCRBY';
describe('NUMINCRBY', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', '$', 1),
['JSON.NUMINCRBY', 'key', '$', '1']
);
});
describe('JSON.NUMINCRBY', () => {
it('transformArguments', () => {
assert.deepEqual(
NUMINCRBY.transformArguments('key', '$', 1),
['JSON.NUMINCRBY', 'key', '$', '1']
);
});
testUtils.testWithClient('client.json.numIncrBy', async client => {
await client.json.set('key', '$', 0);
testUtils.testWithClient('client.json.numIncrBy', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '$', 0),
client.json.numIncrBy('key', '$', 1)
]);
assert.deepEqual(
await client.json.numIncrBy('key', '$', 1),
[1]
);
}, GLOBAL.SERVERS.OPEN);
assert.deepEqual(reply, [1]);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,7 +1,15 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, ArrayReply, NumberReply, DoubleReply, NullReply, BlobStringReply, UnwrapReply, Command } from '@redis/client/dist/lib/RESP/types';
export function transformArguments(key: string, path: string, by: number): Array<string> {
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, path: RedisArgument, by: number) {
return ['JSON.NUMINCRBY', key, path, by.toString()];
}
export { transformNumbersReply as transformReply } from '.';
},
transformReply: {
2: (reply: UnwrapReply<BlobStringReply>) => {
return JSON.parse(reply.toString()) as number | Array<null | number>;
},
3: undefined as unknown as () => ArrayReply<NumberReply | DoubleReply | NullReply>
}
} as const satisfies Command;

View File

@@ -1,21 +1,21 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './NUMMULTBY';
import NUMMULTBY from './NUMMULTBY';
describe('NUMMULTBY', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', '$', 2),
['JSON.NUMMULTBY', 'key', '$', '2']
);
});
describe('JSON.NUMMULTBY', () => {
it('transformArguments', () => {
assert.deepEqual(
NUMMULTBY.transformArguments('key', '$', 2),
['JSON.NUMMULTBY', 'key', '$', '2']
);
});
testUtils.testWithClient('client.json.numMultBy', async client => {
await client.json.set('key', '$', 1);
testUtils.testWithClient('client.json.numMultBy', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '$', 1),
client.json.numMultBy('key', '$', 2)
]);
assert.deepEqual(
await client.json.numMultBy('key', '$', 2),
[2]
);
}, GLOBAL.SERVERS.OPEN);
assert.deepEqual(reply, [2]);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,7 +1,11 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
import NUMINCRBY from './NUMINCRBY';
export function transformArguments(key: string, path: string, by: number): Array<string> {
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, path: RedisArgument, by: number) {
return ['JSON.NUMMULTBY', key, path, by.toString()];
}
export { transformNumbersReply as transformReply } from '.';
},
transformReply: NUMINCRBY.transformReply
} as const satisfies Command;

View File

@@ -1,28 +1,30 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './OBJKEYS';
import OBJKEYS from './OBJKEYS';
describe('OBJKEYS', () => {
describe('transformArguments', () => {
it('without path', () => {
assert.deepEqual(
transformArguments('key'),
['JSON.OBJKEYS', 'key']
);
});
it('with path', () => {
assert.deepEqual(
transformArguments('key', '$'),
['JSON.OBJKEYS', 'key', '$']
);
});
describe('JSON.OBJKEYS', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
OBJKEYS.transformArguments('key'),
['JSON.OBJKEYS', 'key']
);
});
// testUtils.testWithClient('client.json.objKeys', async client => {
// assert.deepEqual(
// await client.json.objKeys('key', '$'),
// [null]
// );
// }, GLOBAL.SERVERS.OPEN);
it('with path', () => {
assert.deepEqual(
OBJKEYS.transformArguments('key', {
path: '$'
}),
['JSON.OBJKEYS', 'key', '$']
);
});
});
testUtils.testWithClient('client.json.objKeys', async client => {
assert.equal(
await client.json.objKeys('key'),
null
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,13 +1,20 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, ArrayReply, BlobStringReply, NullReply, Command } from '@redis/client/dist/lib/RESP/types';
export function transformArguments(key: string, path?: string): Array<string> {
export interface JsonObjKeysOptions {
path?: RedisArgument;
}
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, options?: JsonObjKeysOptions) {
const args = ['JSON.OBJKEYS', key];
if (path) {
args.push(path);
if (options?.path !== undefined) {
args.push(options.path);
}
return args;
}
export declare function transformReply(): Array<string> | null | Array<Array<string> | null>;
},
transformReply: undefined as unknown as () => ArrayReply<BlobStringReply> | ArrayReply<ArrayReply<BlobStringReply> | NullReply>
} as const satisfies Command;

View File

@@ -1,28 +1,30 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './OBJLEN';
import OBJLEN from './OBJLEN';
describe('OBJLEN', () => {
describe('transformArguments', () => {
it('without path', () => {
assert.deepEqual(
transformArguments('key'),
['JSON.OBJLEN', 'key']
);
});
it('with path', () => {
assert.deepEqual(
transformArguments('key', '$'),
['JSON.OBJLEN', 'key', '$']
);
});
describe('JSON.OBJLEN', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
OBJLEN.transformArguments('key'),
['JSON.OBJLEN', 'key']
);
});
// testUtils.testWithClient('client.json.objLen', async client => {
// assert.equal(
// await client.json.objLen('key', '$'),
// [null]
// );
// }, GLOBAL.SERVERS.OPEN);
it('with path', () => {
assert.deepEqual(
OBJLEN.transformArguments('key', {
path: '$'
}),
['JSON.OBJLEN', 'key', '$']
);
});
});
testUtils.testWithClient('client.json.objLen', async client => {
assert.equal(
await client.json.objLen('key'),
null
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,13 +1,20 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, NumberReply, ArrayReply, NullReply, Command } from '@redis/client/dist/lib/RESP/types';
export function transformArguments(key: string, path?: string): Array<string> {
export interface JsonObjLenOptions {
path?: RedisArgument;
}
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, options?: JsonObjLenOptions) {
const args = ['JSON.OBJLEN', key];
if (path) {
args.push(path);
if (options?.path !== undefined) {
args.push(options.path);
}
return args;
}
export declare function transformReply(): number | null | Array<number | null>;
},
transformReply: undefined as unknown as () => NumberReply | ArrayReply<NumberReply | NullReply>
} as const satisfies Command;

View File

@@ -1,4 +1,4 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './RESP';

View File

@@ -1,35 +1,35 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './SET';
import SET from './SET';
describe('SET', () => {
describe('transformArguments', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', '$', 'json'),
['JSON.SET', 'key', '$', '"json"']
);
});
it('NX', () => {
assert.deepEqual(
transformArguments('key', '$', 'json', { NX: true }),
['JSON.SET', 'key', '$', '"json"', 'NX']
);
});
it('XX', () => {
assert.deepEqual(
transformArguments('key', '$', 'json', { XX: true }),
['JSON.SET', 'key', '$', '"json"', 'XX']
);
});
describe('JSON.SET', () => {
describe('transformArguments', () => {
it('transformArguments', () => {
assert.deepEqual(
SET.transformArguments('key', '$', 'json'),
['JSON.SET', 'key', '$', '"json"']
);
});
testUtils.testWithClient('client.json.mGet', async client => {
assert.equal(
await client.json.set('key', '$', 'json'),
'OK'
);
}, GLOBAL.SERVERS.OPEN);
it('NX', () => {
assert.deepEqual(
SET.transformArguments('key', '$', 'json', { NX: true }),
['JSON.SET', 'key', '$', '"json"', 'NX']
);
});
it('XX', () => {
assert.deepEqual(
SET.transformArguments('key', '$', 'json', { XX: true }),
['JSON.SET', 'key', '$', '"json"', 'XX']
);
});
});
testUtils.testWithClient('client.json.set', async client => {
assert.equal(
await client.json.set('key', '$', 'json'),
'OK'
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,25 +1,38 @@
import { RedisArgument, SimpleStringReply, NullReply, Command } from '@redis/client/dist/lib/RESP/types';
import { RedisJSON, transformRedisJsonArgument } from '.';
export const FIRST_KEY_INDEX = 1;
interface NX {
NX: true;
export interface JsonSetOptions {
condition?: 'NX' | 'XX';
/**
* @deprecated Use `{ condition: 'NX' }` instead.
*/
NX?: boolean;
/**
* @deprecated Use `{ condition: 'XX' }` instead.
*/
XX?: boolean;
}
interface XX {
XX: true;
}
export function transformArguments(key: string, path: string, json: RedisJSON, options?: NX | XX): Array<string> {
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(
key: RedisArgument,
path: RedisArgument,
json: RedisJSON,
options?: JsonSetOptions
) {
const args = ['JSON.SET', key, path, transformRedisJsonArgument(json)];
if ((<NX>options)?.NX) {
args.push('NX');
} else if ((<XX>options)?.XX) {
args.push('XX');
if (options?.condition) {
args.push(options?.condition);
} else if (options?.NX) {
args.push('NX');
} else if (options?.XX) {
args.push('XX');
}
return args;
}
export declare function transformReply(): 'OK' | null;
},
transformReply: undefined as unknown as () => SimpleStringReply<'OK'> | NullReply
} as const satisfies Command;

View File

@@ -1,30 +1,32 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './STRAPPEND';
import STRAPPEND from './STRAPPEND';
describe('STRAPPEND', () => {
describe('transformArguments', () => {
it('without path', () => {
assert.deepEqual(
transformArguments('key', 'append'),
['JSON.STRAPPEND', 'key', '"append"']
);
});
it('with path', () => {
assert.deepEqual(
transformArguments('key', '$', 'append'),
['JSON.STRAPPEND', 'key', '$', '"append"']
);
});
describe('JSON.STRAPPEND', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
STRAPPEND.transformArguments('key', 'append'),
['JSON.STRAPPEND', 'key', '"append"']
);
});
testUtils.testWithClient('client.json.strAppend', async client => {
await client.json.set('key', '$', '');
it('with path', () => {
assert.deepEqual(
STRAPPEND.transformArguments('key', 'append', {
path: '$'
}),
['JSON.STRAPPEND', 'key', '$', '"append"']
);
});
});
assert.deepEqual(
await client.json.strAppend('key', '$', 'append'),
[6]
);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('client.json.strAppend', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '$', ''),
client.json.strAppend('key', 'append')
]);
assert.deepEqual(reply, 6);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,21 +1,22 @@
import { RedisArgument, Command, NullReply, NumberReply, ArrayReply } from '@redis/client/dist/lib/RESP/types';
import { transformRedisJsonArgument } from '.';
export const FIRST_KEY_INDEX = 1;
type AppendArguments = [key: string, append: string];
type AppendWithPathArguments = [key: string, path: string, append: string];
export function transformArguments(...[key, pathOrAppend, append]: AppendArguments | AppendWithPathArguments): Array<string> {
const args = ['JSON.STRAPPEND', key];
if (append !== undefined && append !== null) {
args.push(pathOrAppend, transformRedisJsonArgument(append));
} else {
args.push(transformRedisJsonArgument(pathOrAppend));
}
return args;
export interface JsonStrAppendOptions {
path?: RedisArgument;
}
export declare function transformReply(): number | Array<number>;
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, append: string, options?: JsonStrAppendOptions) {
const args = ['JSON.STRAPPEND', key];
if (options?.path !== undefined) {
args.push(options.path);
}
args.push(transformRedisJsonArgument(append));
return args;
},
transformReply: undefined as unknown as () => NumberReply | ArrayReply<NullReply | NumberReply>
} as const satisfies Command;

View File

@@ -1,30 +1,32 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './STRLEN';
import STRLEN from './STRLEN';
describe('STRLEN', () => {
describe('transformArguments', () => {
it('without path', () => {
assert.deepEqual(
transformArguments('key'),
['JSON.STRLEN', 'key']
);
});
it('with path', () => {
assert.deepEqual(
transformArguments('key', '$'),
['JSON.STRLEN', 'key', '$']
);
});
describe('JSON.STRLEN', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
STRLEN.transformArguments('key'),
['JSON.STRLEN', 'key']
);
});
testUtils.testWithClient('client.json.strLen', async client => {
await client.json.set('key', '$', '');
it('with path', () => {
assert.deepEqual(
STRLEN.transformArguments('key', {
path: '$'
}),
['JSON.STRLEN', 'key', '$']
);
});
});
assert.deepEqual(
await client.json.strLen('key', '$'),
[0]
);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('client.json.strLen', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '$', ''),
client.json.strLen('key')
]);
assert.deepEqual(reply, 0);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,15 +1,20 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, ArrayReply, NumberReply, NullReply, Command } from '@redis/client/dist/lib/RESP/types';
export const IS_READ_ONLY = true;
export interface JsonStrLenOptions {
path?: RedisArgument;
}
export function transformArguments(key: string, path?: string): Array<string> {
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, options?: JsonStrLenOptions) {
const args = ['JSON.STRLEN', key];
if (path) {
args.push(path);
if (options?.path) {
args.push(options.path);
}
return args;
}
export declare function transformReply(): number;
},
transformReply: undefined as unknown as () => NumberReply | ArrayReply<NumberReply | NullReply>
} as const satisfies Command;

View File

@@ -0,0 +1,21 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import TOGGLE from './TOGGLE';
describe('JSON.TOGGLE', () => {
it('transformArguments', () => {
assert.deepEqual(
TOGGLE.transformArguments('key', '$'),
['JSON.TOGGLE', 'key', '$']
);
});
testUtils.testWithClient('client.json.toggle', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '$', true),
client.json.toggle('key', '$')
]);
assert.deepEqual(reply, [0]);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -0,0 +1,10 @@
import { RedisArgument, ArrayReply, NumberReply, NullReply, Command, } from '@redis/client/dist/lib/RESP/types';
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, path: RedisArgument) {
return ['JSON.TOGGLE', key, path];
},
transformReply: undefined as unknown as () => NumberReply | NullReply | ArrayReply<NumberReply | NullReply>
} as const satisfies Command;

View File

@@ -1,28 +1,30 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './TYPE';
import TYPE from './TYPE';
describe('TYPE', () => {
describe('transformArguments', () => {
it('without path', () => {
assert.deepEqual(
transformArguments('key'),
['JSON.TYPE', 'key']
);
});
it('with path', () => {
assert.deepEqual(
transformArguments('key', '$'),
['JSON.TYPE', 'key', '$']
);
});
describe('JSON.TYPE', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
TYPE.transformArguments('key'),
['JSON.TYPE', 'key']
);
});
// testUtils.testWithClient('client.json.type', async client => {
// assert.deepEqual(
// await client.json.type('key', '$'),
// [null]
// );
// }, GLOBAL.SERVERS.OPEN);
it('with path', () => {
assert.deepEqual(
TYPE.transformArguments('key', {
path: '$'
}),
['JSON.TYPE', 'key', '$']
);
});
});
testUtils.testWithClient('client.json.type', async client => {
assert.equal(
await client.json.type('key'),
null
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,13 +1,27 @@
export const FIRST_KEY_INDEX = 1;
import { NullReply, BlobStringReply, ArrayReply, Command, RedisArgument, UnwrapReply } from '@redis/client/dist/lib/RESP/types';
export function transformArguments(key: string, path?: string): Array<string> {
export interface JsonTypeOptions {
path?: RedisArgument;
}
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, options?: JsonTypeOptions) {
const args = ['JSON.TYPE', key];
if (path) {
args.push(path);
if (options?.path) {
args.push(options.path);
}
return args;
}
},
transformReply: {
2: undefined as unknown as () => NullReply | BlobStringReply | ArrayReply<BlobStringReply | NullReply>,
// TODO: RESP3 wraps the response in another array, but only returns 1
3: (reply: UnwrapReply<ArrayReply<NullReply | BlobStringReply | ArrayReply<BlobStringReply | NullReply>>>) => {
return reply[0];
}
},
} as const satisfies Command;
export declare function transformReply(): string | null | Array<string | null>;

View File

@@ -1,96 +1,100 @@
import * as ARRAPPEND from './ARRAPPEND';
import * as ARRINDEX from './ARRINDEX';
import * as ARRINSERT from './ARRINSERT';
import * as ARRLEN from './ARRLEN';
import * as ARRPOP from './ARRPOP';
import * as ARRTRIM from './ARRTRIM';
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';
import * as OBJLEN from './OBJLEN';
import * as RESP from './RESP';
import * as SET from './SET';
import * as STRAPPEND from './STRAPPEND';
import * as STRLEN from './STRLEN';
import * as TYPE from './TYPE';
import { BlobStringReply, NullReply, UnwrapReply } from '@redis/client/dist/lib/RESP/types';
import ARRAPPEND from './ARRAPPEND';
import ARRINDEX from './ARRINDEX';
import ARRINSERT from './ARRINSERT';
import ARRLEN from './ARRLEN';
import ARRPOP from './ARRPOP';
import ARRTRIM from './ARRTRIM';
import CLEAR from './CLEAR';
import DEBUG_MEMORY from './DEBUG_MEMORY';
import DEL from './DEL';
import FORGET from './FORGET';
import GET from './GET';
import MERGE from './MERGE';
import MGET from './MGET';
import MSET from './MSET';
import NUMINCRBY from './NUMINCRBY';
import NUMMULTBY from './NUMMULTBY';
import OBJKEYS from './OBJKEYS';
import OBJLEN from './OBJLEN';
// import RESP from './RESP';
import SET from './SET';
import STRAPPEND from './STRAPPEND';
import STRLEN from './STRLEN';
import TOGGLE from './TOGGLE';
import TYPE from './TYPE';
import { isNullReply } from '@redis/client/dist/lib/commands/generic-transformers';
export default {
ARRAPPEND,
arrAppend: ARRAPPEND,
ARRINDEX,
arrIndex: ARRINDEX,
ARRINSERT,
arrInsert: ARRINSERT,
ARRLEN,
arrLen: ARRLEN,
ARRPOP,
arrPop: ARRPOP,
ARRTRIM,
arrTrim: ARRTRIM,
DEBUG_MEMORY,
debugMemory: DEBUG_MEMORY,
DEL,
del: DEL,
FORGET,
forget: FORGET,
GET,
get: GET,
MERGE,
merge: MERGE,
MGET,
mGet: MGET,
MSET,
mSet: MSET,
NUMINCRBY,
numIncrBy: NUMINCRBY,
NUMMULTBY,
numMultBy: NUMMULTBY,
OBJKEYS,
objKeys: OBJKEYS,
OBJLEN,
objLen: OBJLEN,
RESP,
resp: RESP,
SET,
set: SET,
STRAPPEND,
strAppend: STRAPPEND,
STRLEN,
strLen: STRLEN,
TYPE,
type: TYPE
ARRAPPEND,
arrAppend: ARRAPPEND,
ARRINDEX,
arrIndex: ARRINDEX,
ARRINSERT,
arrInsert: ARRINSERT,
ARRLEN,
arrLen: ARRLEN,
ARRPOP,
arrPop: ARRPOP,
ARRTRIM,
arrTrim: ARRTRIM,
CLEAR,
clear: CLEAR,
DEBUG_MEMORY,
debugMemory: DEBUG_MEMORY,
DEL,
del: DEL,
FORGET,
forget: FORGET,
GET,
get: GET,
MERGE,
merge: MERGE,
MGET,
mGet: MGET,
MSET,
mSet: MSET,
NUMINCRBY,
numIncrBy: NUMINCRBY,
/**
* @deprecated since JSON version 2.0
*/
NUMMULTBY,
/**
* @deprecated since JSON version 2.0
*/
numMultBy: NUMMULTBY,
OBJKEYS,
objKeys: OBJKEYS,
OBJLEN,
objLen: OBJLEN,
// RESP,
// resp: RESP,
SET,
set: SET,
STRAPPEND,
strAppend: STRAPPEND,
STRLEN,
strLen: STRLEN,
TOGGLE,
toggle: TOGGLE,
TYPE,
type: TYPE
};
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface RedisJSONArray extends Array<RedisJSON> {}
interface RedisJSONObject {
[key: string]: RedisJSON;
[key: number]: RedisJSON;
}
export type RedisJSON = null | boolean | number | string | Date | RedisJSONArray | RedisJSONObject;
export type RedisJSON = null | boolean | number | string | Date | Array<RedisJSON> | {
[key: string]: RedisJSON;
[key: number]: RedisJSON;
};
export function transformRedisJsonArgument(json: RedisJSON): string {
return JSON.stringify(json);
return JSON.stringify(json);
}
export function transformRedisJsonReply(json: string): RedisJSON {
return JSON.parse(json);
export function transformRedisJsonReply(json: BlobStringReply): RedisJSON {
return JSON.parse((json as unknown as UnwrapReply<typeof json>).toString());
}
export function transformRedisJsonNullReply(json: string | null): RedisJSON | null {
if (json === null) return null;
return transformRedisJsonReply(json);
}
export function transformNumbersReply(reply: string): number | Array<number> {
return JSON.parse(reply);
export function transformRedisJsonNullReply(json: NullReply | BlobStringReply): NullReply | RedisJSON {
return isNullReply(json) ? json : transformRedisJsonReply(json);
}