diff --git a/docs/v4-to-v5.md b/docs/v4-to-v5.md index e1d67a9d0f..1ad3aab8da 100644 --- a/docs/v4-to-v5.md +++ b/docs/v4-to-v5.md @@ -178,6 +178,7 @@ Some command arguments/replies have changed to align more closely to data types - `TOPK.QUERY`: `Array` -> `Array` - `GRAPH.SLOWLOG`: `timestamp` has been changed from `Date` to `number` - `JSON.ARRINDEX`: `start` and `end` arguments moved to `{ range: { start: number; end: number; }; }` [^future-proofing] +- `JSON.ARRPOP`: `path` and `index` arguments moved to `{ path: string; index: number; }` [^future-proofing] - `JSON.ARRLEN`, `JSON.CLEAR`, `JSON.DEBUG MEMORY`, `JSON.DEL`, `JSON.FORGET`, `JSON.OBJKEYS`, `JSON.OBJLEN`, `JSON.STRAPPEND`, `JSON.STRLEN`, `JSON.TYPE`: `path` argument moved to `{ path: string; }` [^future-proofing] - : `path` argument moved to `{ path: string; }` [^future-proofing] - `TS.[M][REV]RANGE`: `enum TimeSeriesBucketTimestamp` -> `const TIME_SERIES_BUCKET_TIMESTAMP` [^enum-to-constants], `enum TimeSeriesReducers` -> `const TIME_SERIES_REDUCERS` [^enum-to-constants], the `ALIGN` argument has been moved into `AGGREGRATION` diff --git a/packages/client/lib/commands/generic-transformers.ts b/packages/client/lib/commands/generic-transformers.ts index 789464fddb..5b8aa12d7e 100644 --- a/packages/client/lib/commands/generic-transformers.ts +++ b/packages/client/lib/commands/generic-transformers.ts @@ -4,6 +4,10 @@ export function isNullReply(reply: unknown): reply is NullReply { return reply === null; } +export function isArrayReply(reply: unknown): reply is ArrayReply { + return Array.isArray(reply); +} + export const transformBooleanReply = { 2: (reply: NumberReply<0 | 1>) => reply as unknown as UnwrapReply === 1, 3: undefined as unknown as () => BooleanReply diff --git a/packages/json/lib/commands/ARRAPPEND.spec.ts b/packages/json/lib/commands/ARRAPPEND.spec.ts index 991185dd92..c5bc636f95 100644 --- a/packages/json/lib/commands/ARRAPPEND.spec.ts +++ b/packages/json/lib/commands/ARRAPPEND.spec.ts @@ -4,14 +4,14 @@ import ARRAPPEND from './ARRAPPEND'; describe('ARRAPPEND', () => { describe('transformArguments', () => { - it('single JSON', () => { + it('single element', () => { assert.deepEqual( - ARRAPPEND.transformArguments('key', '$', 1), - ['JSON.ARRAPPEND', 'key', '$', '1'] + ARRAPPEND.transformArguments('key', '$', 'value'), + ['JSON.ARRAPPEND', 'key', '$', '"value"'] ); }); - it('multiple JSONs', () => { + it('multiple elements', () => { assert.deepEqual( ARRAPPEND.transformArguments('key', '$', 1, 2), ['JSON.ARRAPPEND', 'key', '$', '1', '2'] @@ -22,7 +22,7 @@ describe('ARRAPPEND', () => { testUtils.testWithClient('client.json.arrAppend', async client => { const [, reply] = await Promise.all([ client.json.set('key', '$', []), - client.json.arrAppend('key', '$', 1) + client.json.arrAppend('key', '$', 'value') ]); assert.deepEqual(reply, [1]); diff --git a/packages/json/lib/commands/ARRAPPEND.ts b/packages/json/lib/commands/ARRAPPEND.ts index 7f5b49eead..6f486a301d 100644 --- a/packages/json/lib/commands/ARRAPPEND.ts +++ b/packages/json/lib/commands/ARRAPPEND.ts @@ -4,11 +4,21 @@ import { RedisArgument, NumberReply, ArrayReply, NullReply, Command } from '@red export default { FIRST_KEY_INDEX: 1, IS_READ_ONLY: false, - transformArguments(key: RedisArgument, path: RedisArgument, ...jsons: Array) { - const args = ['JSON.ARRAPPEND', key, path]; + transformArguments( + key: RedisArgument, + path: RedisArgument, + json: RedisJSON, + ...jsons: Array + ) { + const args = new Array(4 + jsons.length); + args[0] = 'JSON.ARRAPPEND'; + args[1] = key; + args[2] = path; + args[3] = transformRedisJsonArgument(json); - 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; diff --git a/packages/json/lib/commands/ARRINDEX.spec.ts b/packages/json/lib/commands/ARRINDEX.spec.ts index fc65dbdf0f..42d98c1532 100644 --- a/packages/json/lib/commands/ARRINDEX.spec.ts +++ b/packages/json/lib/commands/ARRINDEX.spec.ts @@ -6,33 +6,32 @@ describe('JSON.ARRINDEX', () => { describe('transformArguments', () => { it('simple', () => { assert.deepEqual( - ARRINDEX.transformArguments('key', '$', 'json'), - ['JSON.ARRINDEX', 'key', '$', '"json"'] + ARRINDEX.transformArguments('key', '$', 'value'), + ['JSON.ARRINDEX', 'key', '$', '"value"'] ); }); - describe('with range', () => { it('start only', () => { assert.deepEqual( - ARRINDEX.transformArguments('key', '$', 'json', { + ARRINDEX.transformArguments('key', '$', 'value', { range: { start: 0 } }), - ['JSON.ARRINDEX', 'key', '$', '"json"', '0'] + ['JSON.ARRINDEX', 'key', '$', '"value"', '0'] ); }); it('with start and stop', () => { assert.deepEqual( - ARRINDEX.transformArguments('key', '$', 'json', { + ARRINDEX.transformArguments('key', '$', 'value', { range: { start: 0, stop: 1 } }), - ['JSON.ARRINDEX', 'key', '$', '"json"', '0', '1'] + ['JSON.ARRINDEX', 'key', '$', '"value"', '0', '1'] ); }); }); @@ -41,7 +40,7 @@ describe('JSON.ARRINDEX', () => { testUtils.testWithClient('client.json.arrIndex', async client => { const [, reply] = await Promise.all([ client.json.set('key', '$', []), - client.json.arrIndex('key', '$', 'json') + client.json.arrIndex('key', '$', 'value') ]); assert.deepEqual(reply, [-1]); diff --git a/packages/json/lib/commands/ARRINSERT.spec.ts b/packages/json/lib/commands/ARRINSERT.spec.ts index e081593539..c9f4c36a1e 100644 --- a/packages/json/lib/commands/ARRINSERT.spec.ts +++ b/packages/json/lib/commands/ARRINSERT.spec.ts @@ -4,14 +4,14 @@ import ARRINSERT from './ARRINSERT'; describe('JSON.ARRINSERT', () => { describe('transformArguments', () => { - it('single JSON', () => { + it('single element', () => { assert.deepEqual( - ARRINSERT.transformArguments('key', '$', 0, 'json'), - ['JSON.ARRINSERT', 'key', '$', '0', '"json"'] + ARRINSERT.transformArguments('key', '$', 0, 'value'), + ['JSON.ARRINSERT', 'key', '$', '0', '"value"'] ); }); - it('multiple JSONs', () => { + it('multiple elements', () => { assert.deepEqual( ARRINSERT.transformArguments('key', '$', 0, '1', '2'), ['JSON.ARRINSERT', 'key', '$', '0', '"1"', '"2"'] @@ -22,7 +22,7 @@ describe('JSON.ARRINSERT', () => { testUtils.testWithClient('client.json.arrInsert', async client => { const [, reply] = await Promise.all([ client.json.set('key', '$', []), - client.json.arrInsert('key', '$', 0, 'json') + client.json.arrInsert('key', '$', 0, 'value') ]); assert.deepEqual(reply, [1]); diff --git a/packages/json/lib/commands/ARRINSERT.ts b/packages/json/lib/commands/ARRINSERT.ts index da2a3ed440..c089188472 100644 --- a/packages/json/lib/commands/ARRINSERT.ts +++ b/packages/json/lib/commands/ARRINSERT.ts @@ -11,16 +11,16 @@ export default { json: RedisJSON, ...jsons: Array ) { - const args = [ - 'JSON.ARRINSERT', - key, - path, - index.toString(), - transformRedisJsonArgument(json) - ]; + const args = new Array(4 + jsons.length); + args[0] = 'JSON.ARRINSERT'; + args[1] = key; + args[2] = path; + args[3] = index.toString(); + args[4] = transformRedisJsonArgument(json); - 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; diff --git a/packages/json/lib/commands/ARRLEN.spec.ts b/packages/json/lib/commands/ARRLEN.spec.ts index ec2e0e46f5..743c6fa0bc 100644 --- a/packages/json/lib/commands/ARRLEN.spec.ts +++ b/packages/json/lib/commands/ARRLEN.spec.ts @@ -27,6 +27,6 @@ describe('JSON.ARRLEN', () => { client.json.arrLen('key') ]); - assert.deepEqual(reply, 0); + assert.equal(reply, 0); }, GLOBAL.SERVERS.OPEN); }); diff --git a/packages/json/lib/commands/ARRLEN.ts b/packages/json/lib/commands/ARRLEN.ts index 5763b893fa..d30032c7d8 100644 --- a/packages/json/lib/commands/ARRLEN.ts +++ b/packages/json/lib/commands/ARRLEN.ts @@ -10,7 +10,7 @@ export default { transformArguments(key: RedisArgument, options?: JsonArrLenOptions) { const args = ['JSON.ARRLEN', key]; - if (options?.path) { + if (options?.path !== undefined) { args.push(options.path); } diff --git a/packages/json/lib/commands/ARRPOP.spec.ts b/packages/json/lib/commands/ARRPOP.spec.ts index 7c2ec365eb..91d8c96281 100644 --- a/packages/json/lib/commands/ARRPOP.spec.ts +++ b/packages/json/lib/commands/ARRPOP.spec.ts @@ -1,57 +1,66 @@ import { strict as assert } from '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); + }); }); diff --git a/packages/json/lib/commands/ARRPOP.ts b/packages/json/lib/commands/ARRPOP.ts index 18830c0d31..4eafe9fdde 100644 --- a/packages/json/lib/commands/ARRPOP.ts +++ b/packages/json/lib/commands/ARRPOP.ts @@ -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 { +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) { + return isArrayReply(reply) ? + (reply as unknown as UnwrapReply).map(item => transformRedisJsonNullReply(item)) : + transformRedisJsonNullReply(reply); + } +} as const satisfies Command; -export function transformReply(reply: null | string | Array): null | RedisJSON | Array { - if (reply === null) return null; - - if (Array.isArray(reply)) { - return reply.map(transformRedisJsonNullReply); - } - - return transformRedisJsonNullReply(reply); -} diff --git a/packages/json/lib/commands/CLEAR.spec.ts b/packages/json/lib/commands/CLEAR.spec.ts index e9ada575a8..a58e16868e 100644 --- a/packages/json/lib/commands/CLEAR.spec.ts +++ b/packages/json/lib/commands/CLEAR.spec.ts @@ -22,9 +22,11 @@ describe('JSON.CLEAR', () => { }); testUtils.testWithClient('client.json.clear', async client => { - assert.deepEqual( - await client.json.clear('key'), - 0 - ); + const [, reply] = await Promise.all([ + client.json.set('key', '$', null), + client.json.clear('key') + ]); + + assert.equal(reply, 0); }, GLOBAL.SERVERS.OPEN); }); diff --git a/packages/json/lib/commands/CLEAR.ts b/packages/json/lib/commands/CLEAR.ts index 186ce296bc..23e86d900e 100644 --- a/packages/json/lib/commands/CLEAR.ts +++ b/packages/json/lib/commands/CLEAR.ts @@ -10,7 +10,7 @@ export default { transformArguments(key: RedisArgument, options?: JsonClearOptions) { const args = ['JSON.CLEAR', key]; - if (options?.path) { + if (options?.path !== undefined) { args.push(options.path); } diff --git a/packages/json/lib/commands/DEBUG_MEMORY.spec.ts b/packages/json/lib/commands/DEBUG_MEMORY.spec.ts index 8cdd2bdef9..88e59e1329 100644 --- a/packages/json/lib/commands/DEBUG_MEMORY.spec.ts +++ b/packages/json/lib/commands/DEBUG_MEMORY.spec.ts @@ -22,7 +22,7 @@ describe('JSON.DEBUG MEMORY', () => { }); testUtils.testWithClient('client.json.debugMemory', async client => { - assert.deepEqual( + assert.equal( await client.json.debugMemory('key'), 0 ); diff --git a/packages/json/lib/commands/DEBUG_MEMORY.ts b/packages/json/lib/commands/DEBUG_MEMORY.ts index 17c202ff40..a96f05b806 100644 --- a/packages/json/lib/commands/DEBUG_MEMORY.ts +++ b/packages/json/lib/commands/DEBUG_MEMORY.ts @@ -10,7 +10,7 @@ export default { transformArguments(key: RedisArgument, options?: JsonDebugMemoryOptions) { const args = ['JSON.DEBUG', 'MEMORY', key]; - if (options?.path) { + if (options?.path !== undefined) { args.push(options.path); } diff --git a/packages/json/lib/commands/DEL.spec.ts b/packages/json/lib/commands/DEL.spec.ts index 590f70d788..11e817af18 100644 --- a/packages/json/lib/commands/DEL.spec.ts +++ b/packages/json/lib/commands/DEL.spec.ts @@ -22,7 +22,7 @@ describe('JSON.DEL', () => { }); testUtils.testWithClient('client.json.del', async client => { - assert.deepEqual( + assert.equal( await client.json.del('key'), 0 ); diff --git a/packages/json/lib/commands/DEL.ts b/packages/json/lib/commands/DEL.ts index 13632840c6..f6952a8dc6 100644 --- a/packages/json/lib/commands/DEL.ts +++ b/packages/json/lib/commands/DEL.ts @@ -10,7 +10,7 @@ export default { transformArguments(key: RedisArgument, options?: JsonDelOptions) { const args = ['JSON.DEL', key]; - if (options?.path) { + if (options?.path !== undefined) { args.push(options.path); } diff --git a/packages/json/lib/commands/FORGET.spec.ts b/packages/json/lib/commands/FORGET.spec.ts index ec9f2221eb..e60e5355ad 100644 --- a/packages/json/lib/commands/FORGET.spec.ts +++ b/packages/json/lib/commands/FORGET.spec.ts @@ -22,7 +22,7 @@ describe('JSON.FORGET', () => { }); testUtils.testWithClient('client.json.forget', async client => { - assert.deepEqual( + assert.equal( await client.json.forget('key'), 0 ); diff --git a/packages/json/lib/commands/FORGET.ts b/packages/json/lib/commands/FORGET.ts index 1820f8f202..68335ee92e 100644 --- a/packages/json/lib/commands/FORGET.ts +++ b/packages/json/lib/commands/FORGET.ts @@ -10,7 +10,7 @@ export default { transformArguments(key: RedisArgument, options?: JsonForgetOptions) { const args = ['JSON.FORGET', key]; - if (options?.path) { + if (options?.path !== undefined) { args.push(options.path); } diff --git a/packages/json/lib/commands/GET.spec.ts b/packages/json/lib/commands/GET.spec.ts index ed831689a9..dec66dbb36 100644 --- a/packages/json/lib/commands/GET.spec.ts +++ b/packages/json/lib/commands/GET.spec.ts @@ -1,78 +1,37 @@ import { strict as assert } from '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); }); diff --git a/packages/json/lib/commands/GET.ts b/packages/json/lib/commands/GET.ts index 445f0b3eae..b7bcc52e3c 100644 --- a/packages/json/lib/commands/GET.ts +++ b/packages/json/lib/commands/GET.ts @@ -1,42 +1,22 @@ -// import { pushVariadicArguments } 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 interface JsonGetOptions { + path?: RedisVariadicArgument; +} -// export const IS_READ_ONLY = true; +export default { + FIRST_KEY_INDEX: 1, + IS_READ_ONLY: false, + transformArguments(key: RedisArgument, options?: JsonGetOptions) { + let args = ['JSON.GET', key]; -// interface GetOptions { -// path?: string | Array; -// INDENT?: string; -// NEWLINE?: string; -// SPACE?: string; -// NOESCAPE?: true; -// } + if (options?.path !== undefined) { + args = pushVariadicArguments(args, options.path); + } -// export function transformArguments(key: string, options?: GetOptions): RedisCommandArguments { -// let args: RedisCommandArguments = ['JSON.GET', key]; - -// if (options?.path) { -// args = pushVariadicArguments(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'); -// } - -// return args; -// } - -// export { transformRedisJsonNullReply as transformReply } from '.'; + return args; + }, + transformReply: transformRedisJsonNullReply +} as const satisfies Command; diff --git a/packages/json/lib/commands/MERGE.spec.ts b/packages/json/lib/commands/MERGE.spec.ts index fbb84aaaeb..a2f880176f 100644 --- a/packages/json/lib/commands/MERGE.spec.ts +++ b/packages/json/lib/commands/MERGE.spec.ts @@ -6,7 +6,7 @@ describe('MERGE', () => { it('transformArguments', () => { assert.deepEqual( MERGE.transformArguments('key', '$', 'value'), - ['JSON.MERGE', 'key', '$', 'value'] + ['JSON.MERGE', 'key', '$', '"value"'] ); }); diff --git a/packages/json/lib/commands/MGET.spec.ts b/packages/json/lib/commands/MGET.spec.ts index 456e160dd5..79d0657bf9 100644 --- a/packages/json/lib/commands/MGET.spec.ts +++ b/packages/json/lib/commands/MGET.spec.ts @@ -1,19 +1,19 @@ import { strict as assert } from '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', '$'] - ); - }); + 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); }); diff --git a/packages/json/lib/commands/MGET.ts b/packages/json/lib/commands/MGET.ts index 582b73bf85..a7aea82ac2 100644 --- a/packages/json/lib/commands/MGET.ts +++ b/packages/json/lib/commands/MGET.ts @@ -1,15 +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 function transformArguments(keys: Array, path: string): Array { +export default { + FIRST_KEY_INDEX: 1, + IS_READ_ONLY: true, + transformArguments(keys: Array, path: RedisArgument) { return [ - 'JSON.MGET', - ...keys, - path + 'JSON.MGET', + ...keys, + path ]; -} - -export function transformReply(reply: Array): Array { - return reply.map(transformRedisJsonNullReply); -} + }, + transformReply(reply: UnwrapReply>) { + return reply.map(json => transformRedisJsonNullReply(json)) + } +} as const satisfies Command; diff --git a/packages/json/lib/commands/MSET.ts b/packages/json/lib/commands/MSET.ts index ac4105a85a..a081bfd543 100644 --- a/packages/json/lib/commands/MSET.ts +++ b/packages/json/lib/commands/MSET.ts @@ -11,15 +11,15 @@ export default { FIRST_KEY_INDEX: 1, IS_READ_ONLY: false, transformArguments(items: Array) { - const args = new Array(1 + items.length * 3); + 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); + const item = items[i]; + args[argsIndex++] = item.key; + args[argsIndex++] = item.path; + args[argsIndex++] = transformRedisJsonArgument(item.value); } return args; diff --git a/packages/json/lib/commands/NUMINCRBY.spec.ts b/packages/json/lib/commands/NUMINCRBY.spec.ts index ee3675643d..d2b21df4ca 100644 --- a/packages/json/lib/commands/NUMINCRBY.spec.ts +++ b/packages/json/lib/commands/NUMINCRBY.spec.ts @@ -11,11 +11,11 @@ describe('JSON.NUMINCRBY', () => { }); testUtils.testWithClient('client.json.numIncrBy', async client => { - await client.json.set('key', '$', 0); + const [, reply] = await Promise.all([ + client.json.set('key', '$', 0), + client.json.numIncrBy('key', '$', 1) + ]); - assert.deepEqual( - await client.json.numIncrBy('key', '$', 1), - [1] - ); + assert.deepEqual(reply, [1]); }, GLOBAL.SERVERS.OPEN); }); diff --git a/packages/json/lib/commands/NUMINCRBY.ts b/packages/json/lib/commands/NUMINCRBY.ts index 68c1de2b0b..65cc7db68a 100644 --- a/packages/json/lib/commands/NUMINCRBY.ts +++ b/packages/json/lib/commands/NUMINCRBY.ts @@ -1,4 +1,4 @@ -import { RedisArgument, NumberReply, DoubleReply, NullReply, BlobStringReply, UnwrapReply, Command } from '@redis/client/dist/lib/RESP/types'; +import { RedisArgument, ArrayReply, NumberReply, DoubleReply, NullReply, BlobStringReply, UnwrapReply, Command } from '@redis/client/dist/lib/RESP/types'; export default { FIRST_KEY_INDEX: 1, @@ -8,8 +8,8 @@ export default { }, transformReply: { 2: (reply: UnwrapReply) => { - return JSON.parse(reply.toString()) as number | Array; + return JSON.parse(reply.toString()) as number | Array; }, - 3: undefined as unknown as () => NumberReply | DoubleReply | NullReply + 3: undefined as unknown as () => ArrayReply } } as const satisfies Command; diff --git a/packages/json/lib/commands/NUMMULTBY.spec.ts b/packages/json/lib/commands/NUMMULTBY.spec.ts index 471c276f0b..a02a8f75cc 100644 --- a/packages/json/lib/commands/NUMMULTBY.spec.ts +++ b/packages/json/lib/commands/NUMMULTBY.spec.ts @@ -11,11 +11,11 @@ describe('JSON.NUMMULTBY', () => { }); testUtils.testWithClient('client.json.numMultBy', async client => { - await client.json.set('key', '$', 1); + const [, reply] = await Promise.all([ + client.json.set('key', '$', 1), + client.json.numMultBy('key', '$', 2) + ]); - assert.deepEqual( - await client.json.numMultBy('key', '$', 2), - [2] - ); + assert.deepEqual(reply, [2]); }, GLOBAL.SERVERS.OPEN); }); diff --git a/packages/json/lib/commands/NUMMULTBY.ts b/packages/json/lib/commands/NUMMULTBY.ts index 11a56d0d72..255685a9a5 100644 --- a/packages/json/lib/commands/NUMMULTBY.ts +++ b/packages/json/lib/commands/NUMMULTBY.ts @@ -1,4 +1,5 @@ -import { RedisArgument, Command, ArrayReply, NumberReply, DoubleReply, NullReply } from '@redis/client/dist/lib/RESP/types'; +import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types'; +import NUMINCRBY from './NUMINCRBY'; export default { FIRST_KEY_INDEX: 1, @@ -6,5 +7,5 @@ export default { transformArguments(key: RedisArgument, path: RedisArgument, by: number) { return ['JSON.NUMMULTBY', key, path, by.toString()]; }, - transformReply: undefined as unknown as () => ArrayReply + transformReply: NUMINCRBY.transformReply } as const satisfies Command; diff --git a/packages/json/lib/commands/OBJKEYS.spec.ts b/packages/json/lib/commands/OBJKEYS.spec.ts index ccdb7f97f6..8c3222d671 100644 --- a/packages/json/lib/commands/OBJKEYS.spec.ts +++ b/packages/json/lib/commands/OBJKEYS.spec.ts @@ -22,9 +22,9 @@ describe('JSON.OBJKEYS', () => { }); testUtils.testWithClient('client.json.objKeys', async client => { - assert.deepEqual( + assert.equal( await client.json.objKeys('key'), - [null] + null ); }, GLOBAL.SERVERS.OPEN); }); diff --git a/packages/json/lib/commands/OBJKEYS.ts b/packages/json/lib/commands/OBJKEYS.ts index 6973d6b1f9..fb8ae6bb03 100644 --- a/packages/json/lib/commands/OBJKEYS.ts +++ b/packages/json/lib/commands/OBJKEYS.ts @@ -10,7 +10,7 @@ export default { transformArguments(key: RedisArgument, options?: JsonObjKeysOptions) { const args = ['JSON.OBJKEYS', key]; - if (options?.path) { + if (options?.path !== undefined) { args.push(options.path); } diff --git a/packages/json/lib/commands/OBJLEN.ts b/packages/json/lib/commands/OBJLEN.ts index 756651bde9..f9c45e336a 100644 --- a/packages/json/lib/commands/OBJLEN.ts +++ b/packages/json/lib/commands/OBJLEN.ts @@ -10,7 +10,7 @@ export default { transformArguments(key: RedisArgument, options?: JsonObjLenOptions) { const args = ['JSON.OBJLEN', key]; - if (options?.path) { + if (options?.path !== undefined) { args.push(options.path); } diff --git a/packages/json/lib/commands/STRAPPEND.ts b/packages/json/lib/commands/STRAPPEND.ts index ad3cd377c9..12ee7cc394 100644 --- a/packages/json/lib/commands/STRAPPEND.ts +++ b/packages/json/lib/commands/STRAPPEND.ts @@ -1,4 +1,5 @@ import { RedisArgument, Command, NullReply, NumberReply, ArrayReply } from '@redis/client/dist/lib/RESP/types'; +import { transformRedisJsonArgument } from '.'; export interface JsonStrAppendOptions { path?: RedisArgument; @@ -7,14 +8,14 @@ export interface JsonStrAppendOptions { export default { FIRST_KEY_INDEX: 1, IS_READ_ONLY: false, - transformArguments(key: RedisArgument, append: RedisArgument, options?: JsonStrAppendOptions) { + transformArguments(key: RedisArgument, append: string, options?: JsonStrAppendOptions) { const args = ['JSON.STRAPPEND', key]; - if (options?.path) { + if (options?.path !== undefined) { args.push(options.path); } - args.push(append); + args.push(transformRedisJsonArgument(append)); return args; }, transformReply: undefined as unknown as () => NumberReply | ArrayReply diff --git a/packages/json/lib/commands/TOGGLE.spec.ts b/packages/json/lib/commands/TOGGLE.spec.ts index 1c785d97b1..90a456b920 100644 --- a/packages/json/lib/commands/TOGGLE.spec.ts +++ b/packages/json/lib/commands/TOGGLE.spec.ts @@ -12,7 +12,7 @@ describe('JSON.TOGGLE', () => { testUtils.testWithClient('client.json.toggle', async client => { const [, reply] = await Promise.all([ - client.json.set('key', '$', ''), + client.json.set('key', '$', true), client.json.toggle('key', '$') ]); diff --git a/packages/json/lib/commands/TYPE.spec.ts b/packages/json/lib/commands/TYPE.spec.ts index 2a90a3bf9e..b3124ceb31 100644 --- a/packages/json/lib/commands/TYPE.spec.ts +++ b/packages/json/lib/commands/TYPE.spec.ts @@ -6,14 +6,14 @@ describe('TYPE', () => { describe('transformArguments', () => { it('simple', () => { assert.deepEqual( - transformArguments('key'), + TYPE.transformArguments('key'), ['JSON.TYPE', 'key'] ); }); it('with path', () => { assert.deepEqual( - transformArguments('key', { + TYPE.transformArguments('key', { path: '$' }), ['JSON.TYPE', 'key', '$'] @@ -22,11 +22,9 @@ describe('TYPE', () => { }); testUtils.testWithClient('client.json.type', async client => { - assert.deepEqual( - await client.json.type('key', { - path: '$' - }), - [null] + assert.equal( + await client.json.type('key'), + null ); }, GLOBAL.SERVERS.OPEN); }); diff --git a/packages/json/lib/commands/index.ts b/packages/json/lib/commands/index.ts index 66c0f5efe9..2724ff2565 100644 --- a/packages/json/lib/commands/index.ts +++ b/packages/json/lib/commands/index.ts @@ -1,16 +1,17 @@ +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 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 GET from './GET'; import MERGE from './MERGE'; -// import MGET from './MGET'; +import MGET from './MGET'; import MSET from './MSET'; import NUMINCRBY from './NUMINCRBY'; import NUMMULTBY from './NUMMULTBY'; @@ -22,6 +23,7 @@ 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, @@ -32,8 +34,8 @@ export default { arrInsert: ARRINSERT, ARRLEN, arrLen: ARRLEN, - // ARRPOP, - // arrPop: ARRPOP, + ARRPOP, + arrPop: ARRPOP, ARRTRIM, arrTrim: ARRTRIM, CLEAR, @@ -44,12 +46,12 @@ export default { del: DEL, FORGET, forget: FORGET, - // GET, - // get: GET, + GET, + get: GET, MERGE, merge: MERGE, - // MGET, - // mGet: MGET, + MGET, + mGet: MGET, MSET, mSet: MSET, NUMINCRBY, @@ -80,29 +82,19 @@ export default { type: TYPE }; -// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540 -// eslint-disable-next-line @typescript-eslint/no-empty-interface -interface RedisJSONArray extends Array { } -interface RedisJSONObject { +export type RedisJSON = null | boolean | number | string | Date | Array | { [key: string]: RedisJSON; [key: number]: RedisJSON; -} -export type RedisJSON = null | boolean | number | string | Date | RedisJSONArray | RedisJSONObject; +}; export function transformRedisJsonArgument(json: RedisJSON): string { 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).toString()); } -export function transformRedisJsonNullReply(json: string | null): RedisJSON | null { - if (json === null) return null; - - return transformRedisJsonReply(json); -} - -export function transformNumbersReply(reply: string): number | Array { - return JSON.parse(reply); +export function transformRedisJsonNullReply(json: NullReply | BlobStringReply): NullReply | RedisJSON { + return isNullReply(json) ? json : transformRedisJsonReply(json); }