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

RedisJSON

This commit is contained in:
Leibale
2023-09-05 18:19:31 -04:00
parent 5bab7fa6fd
commit c12dc79950
36 changed files with 271 additions and 308 deletions

View File

@@ -178,6 +178,7 @@ Some command arguments/replies have changed to align more closely to data types
- `TOPK.QUERY`: `Array<number>` -> `Array<boolean>` - `TOPK.QUERY`: `Array<number>` -> `Array<boolean>`
- `GRAPH.SLOWLOG`: `timestamp` has been changed from `Date` to `number` - `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.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] - `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] - : `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` - `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`

View File

@@ -4,6 +4,10 @@ export function isNullReply(reply: unknown): reply is NullReply {
return reply === null; return reply === null;
} }
export function isArrayReply(reply: unknown): reply is ArrayReply<unknown> {
return Array.isArray(reply);
}
export const transformBooleanReply = { export const transformBooleanReply = {
2: (reply: NumberReply<0 | 1>) => reply as unknown as UnwrapReply<typeof reply> === 1, 2: (reply: NumberReply<0 | 1>) => reply as unknown as UnwrapReply<typeof reply> === 1,
3: undefined as unknown as () => BooleanReply 3: undefined as unknown as () => BooleanReply

View File

@@ -4,14 +4,14 @@ import ARRAPPEND from './ARRAPPEND';
describe('ARRAPPEND', () => { describe('ARRAPPEND', () => {
describe('transformArguments', () => { describe('transformArguments', () => {
it('single JSON', () => { it('single element', () => {
assert.deepEqual( assert.deepEqual(
ARRAPPEND.transformArguments('key', '$', 1), ARRAPPEND.transformArguments('key', '$', 'value'),
['JSON.ARRAPPEND', 'key', '$', '1'] ['JSON.ARRAPPEND', 'key', '$', '"value"']
); );
}); });
it('multiple JSONs', () => { it('multiple elements', () => {
assert.deepEqual( assert.deepEqual(
ARRAPPEND.transformArguments('key', '$', 1, 2), ARRAPPEND.transformArguments('key', '$', 1, 2),
['JSON.ARRAPPEND', 'key', '$', '1', '2'] ['JSON.ARRAPPEND', 'key', '$', '1', '2']
@@ -22,7 +22,7 @@ describe('ARRAPPEND', () => {
testUtils.testWithClient('client.json.arrAppend', async client => { testUtils.testWithClient('client.json.arrAppend', async client => {
const [, reply] = await Promise.all([ const [, reply] = await Promise.all([
client.json.set('key', '$', []), client.json.set('key', '$', []),
client.json.arrAppend('key', '$', 1) client.json.arrAppend('key', '$', 'value')
]); ]);
assert.deepEqual(reply, [1]); assert.deepEqual(reply, [1]);

View File

@@ -4,11 +4,21 @@ import { RedisArgument, NumberReply, ArrayReply, NullReply, Command } from '@red
export default { export default {
FIRST_KEY_INDEX: 1, FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false, IS_READ_ONLY: false,
transformArguments(key: RedisArgument, path: RedisArgument, ...jsons: Array<RedisJSON>) { transformArguments(
const args = ['JSON.ARRAPPEND', key, path]; 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);
for (const json of jsons) { let argsIndex = 4;
args.push(transformRedisJsonArgument(json)); for (let i = 0; i < jsons.length; i++) {
args[argsIndex++] = transformRedisJsonArgument(jsons[i]);
} }
return args; return args;

View File

@@ -6,33 +6,32 @@ describe('JSON.ARRINDEX', () => {
describe('transformArguments', () => { describe('transformArguments', () => {
it('simple', () => { it('simple', () => {
assert.deepEqual( assert.deepEqual(
ARRINDEX.transformArguments('key', '$', 'json'), ARRINDEX.transformArguments('key', '$', 'value'),
['JSON.ARRINDEX', 'key', '$', '"json"'] ['JSON.ARRINDEX', 'key', '$', '"value"']
); );
}); });
describe('with range', () => { describe('with range', () => {
it('start only', () => { it('start only', () => {
assert.deepEqual( assert.deepEqual(
ARRINDEX.transformArguments('key', '$', 'json', { ARRINDEX.transformArguments('key', '$', 'value', {
range: { range: {
start: 0 start: 0
} }
}), }),
['JSON.ARRINDEX', 'key', '$', '"json"', '0'] ['JSON.ARRINDEX', 'key', '$', '"value"', '0']
); );
}); });
it('with start and stop', () => { it('with start and stop', () => {
assert.deepEqual( assert.deepEqual(
ARRINDEX.transformArguments('key', '$', 'json', { ARRINDEX.transformArguments('key', '$', 'value', {
range: { range: {
start: 0, start: 0,
stop: 1 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 => { testUtils.testWithClient('client.json.arrIndex', async client => {
const [, reply] = await Promise.all([ const [, reply] = await Promise.all([
client.json.set('key', '$', []), client.json.set('key', '$', []),
client.json.arrIndex('key', '$', 'json') client.json.arrIndex('key', '$', 'value')
]); ]);
assert.deepEqual(reply, [-1]); assert.deepEqual(reply, [-1]);

View File

@@ -4,14 +4,14 @@ import ARRINSERT from './ARRINSERT';
describe('JSON.ARRINSERT', () => { describe('JSON.ARRINSERT', () => {
describe('transformArguments', () => { describe('transformArguments', () => {
it('single JSON', () => { it('single element', () => {
assert.deepEqual( assert.deepEqual(
ARRINSERT.transformArguments('key', '$', 0, 'json'), ARRINSERT.transformArguments('key', '$', 0, 'value'),
['JSON.ARRINSERT', 'key', '$', '0', '"json"'] ['JSON.ARRINSERT', 'key', '$', '0', '"value"']
); );
}); });
it('multiple JSONs', () => { it('multiple elements', () => {
assert.deepEqual( assert.deepEqual(
ARRINSERT.transformArguments('key', '$', 0, '1', '2'), ARRINSERT.transformArguments('key', '$', 0, '1', '2'),
['JSON.ARRINSERT', 'key', '$', '0', '"1"', '"2"'] ['JSON.ARRINSERT', 'key', '$', '0', '"1"', '"2"']
@@ -22,7 +22,7 @@ describe('JSON.ARRINSERT', () => {
testUtils.testWithClient('client.json.arrInsert', async client => { testUtils.testWithClient('client.json.arrInsert', async client => {
const [, reply] = await Promise.all([ const [, reply] = await Promise.all([
client.json.set('key', '$', []), client.json.set('key', '$', []),
client.json.arrInsert('key', '$', 0, 'json') client.json.arrInsert('key', '$', 0, 'value')
]); ]);
assert.deepEqual(reply, [1]); assert.deepEqual(reply, [1]);

View File

@@ -11,16 +11,16 @@ export default {
json: RedisJSON, json: RedisJSON,
...jsons: Array<RedisJSON> ...jsons: Array<RedisJSON>
) { ) {
const args = [ const args = new Array<RedisArgument>(4 + jsons.length);
'JSON.ARRINSERT', args[0] = 'JSON.ARRINSERT';
key, args[1] = key;
path, args[2] = path;
index.toString(), args[3] = index.toString();
transformRedisJsonArgument(json) args[4] = transformRedisJsonArgument(json);
];
for (const json of jsons) { let argsIndex = 5;
args.push(transformRedisJsonArgument(json)); for (let i = 0; i < jsons.length; i++) {
args[argsIndex++] = transformRedisJsonArgument(jsons[i]);
} }
return args; return args;

View File

@@ -27,6 +27,6 @@ describe('JSON.ARRLEN', () => {
client.json.arrLen('key') client.json.arrLen('key')
]); ]);
assert.deepEqual(reply, 0); assert.equal(reply, 0);
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
}); });

View File

@@ -10,7 +10,7 @@ export default {
transformArguments(key: RedisArgument, options?: JsonArrLenOptions) { transformArguments(key: RedisArgument, options?: JsonArrLenOptions) {
const args = ['JSON.ARRLEN', key]; const args = ['JSON.ARRLEN', key];
if (options?.path) { if (options?.path !== undefined) {
args.push(options.path); args.push(options.path);
} }

View File

@@ -1,57 +1,66 @@
import { strict as assert } from 'assert'; import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils'; import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './ARRPOP'; import ARRPOP from './ARRPOP';
describe('ARRPOP', () => { describe('JSON.ARRPOP', () => {
describe('transformArguments', () => { describe('transformArguments', () => {
it('key', () => { it('simple', () => {
assert.deepEqual( assert.deepEqual(
transformArguments('key'), ARRPOP.transformArguments('key'),
['JSON.ARRPOP', '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('client.json.arrPop', () => { it('with path', () => {
testUtils.testWithClient('null', async client => { assert.deepEqual(
await client.json.set('key', '.', []); ARRPOP.transformArguments('key', {
path: '$'
assert.equal( }),
await client.json.arrPop('key', '.'), ['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 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]; const args = ['JSON.ARRPOP', key];
if (path) { if (options) {
args.push(path); args.push(options.path);
if (index !== undefined && index !== null) { if (options.index !== undefined) {
args.push(index.toString()); args.push(options.index.toString());
} }
} }
return args; 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

@@ -22,9 +22,11 @@ describe('JSON.CLEAR', () => {
}); });
testUtils.testWithClient('client.json.clear', async client => { testUtils.testWithClient('client.json.clear', async client => {
assert.deepEqual( const [, reply] = await Promise.all([
await client.json.clear('key'), client.json.set('key', '$', null),
0 client.json.clear('key')
); ]);
assert.equal(reply, 0);
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
}); });

View File

@@ -10,7 +10,7 @@ export default {
transformArguments(key: RedisArgument, options?: JsonClearOptions) { transformArguments(key: RedisArgument, options?: JsonClearOptions) {
const args = ['JSON.CLEAR', key]; const args = ['JSON.CLEAR', key];
if (options?.path) { if (options?.path !== undefined) {
args.push(options.path); args.push(options.path);
} }

View File

@@ -22,7 +22,7 @@ describe('JSON.DEBUG MEMORY', () => {
}); });
testUtils.testWithClient('client.json.debugMemory', async client => { testUtils.testWithClient('client.json.debugMemory', async client => {
assert.deepEqual( assert.equal(
await client.json.debugMemory('key'), await client.json.debugMemory('key'),
0 0
); );

View File

@@ -10,7 +10,7 @@ export default {
transformArguments(key: RedisArgument, options?: JsonDebugMemoryOptions) { transformArguments(key: RedisArgument, options?: JsonDebugMemoryOptions) {
const args = ['JSON.DEBUG', 'MEMORY', key]; const args = ['JSON.DEBUG', 'MEMORY', key];
if (options?.path) { if (options?.path !== undefined) {
args.push(options.path); args.push(options.path);
} }

View File

@@ -22,7 +22,7 @@ describe('JSON.DEL', () => {
}); });
testUtils.testWithClient('client.json.del', async client => { testUtils.testWithClient('client.json.del', async client => {
assert.deepEqual( assert.equal(
await client.json.del('key'), await client.json.del('key'),
0 0
); );

View File

@@ -10,7 +10,7 @@ export default {
transformArguments(key: RedisArgument, options?: JsonDelOptions) { transformArguments(key: RedisArgument, options?: JsonDelOptions) {
const args = ['JSON.DEL', key]; const args = ['JSON.DEL', key];
if (options?.path) { if (options?.path !== undefined) {
args.push(options.path); args.push(options.path);
} }

View File

@@ -22,7 +22,7 @@ describe('JSON.FORGET', () => {
}); });
testUtils.testWithClient('client.json.forget', async client => { testUtils.testWithClient('client.json.forget', async client => {
assert.deepEqual( assert.equal(
await client.json.forget('key'), await client.json.forget('key'),
0 0
); );

View File

@@ -10,7 +10,7 @@ export default {
transformArguments(key: RedisArgument, options?: JsonForgetOptions) { transformArguments(key: RedisArgument, options?: JsonForgetOptions) {
const args = ['JSON.FORGET', key]; const args = ['JSON.FORGET', key];
if (options?.path) { if (options?.path !== undefined) {
args.push(options.path); args.push(options.path);
} }

View File

@@ -1,78 +1,37 @@
import { strict as assert } from 'assert'; import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils'; import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './GET'; import GET from './GET';
describe('GET', () => { describe('JSON.GET', () => {
describe('transformArguments', () => { describe('transformArguments', () => {
describe('path', () => { it('simple', () => {
it('string', () => { assert.deepEqual(
assert.deepEqual( GET.transformArguments('key'),
transformArguments('key', { path: '$' }), ['JSON.GET', 'key']
['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']
);
});
}); });
testUtils.testWithClient('client.json.get', async client => { describe('with path', () => {
assert.equal( it('string', () => {
await client.json.get('key'), assert.deepEqual(
null 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 { pushVariadicArguments } from '@redis/client/dist/lib/commands/generic-transformers'; import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
// import { RedisCommandArguments } from '@redis/client/dist/lib/commands'; 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 { if (options?.path !== undefined) {
// path?: string | Array<string>; args = pushVariadicArguments(args, options.path);
// INDENT?: string; }
// NEWLINE?: string;
// SPACE?: string;
// NOESCAPE?: true;
// }
// export function transformArguments(key: string, options?: GetOptions): RedisCommandArguments { return args;
// let args: RedisCommandArguments = ['JSON.GET', key]; },
transformReply: transformRedisJsonNullReply
// if (options?.path) { } as const satisfies Command;
// 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 '.';

View File

@@ -6,7 +6,7 @@ describe('MERGE', () => {
it('transformArguments', () => { it('transformArguments', () => {
assert.deepEqual( assert.deepEqual(
MERGE.transformArguments('key', '$', 'value'), MERGE.transformArguments('key', '$', 'value'),
['JSON.MERGE', 'key', '$', 'value'] ['JSON.MERGE', 'key', '$', '"value"']
); );
}); });

View File

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

View File

@@ -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 default {
FIRST_KEY_INDEX: 1,
export function transformArguments(keys: Array<string>, path: string): Array<string> { IS_READ_ONLY: true,
transformArguments(keys: Array<RedisArgument>, path: RedisArgument) {
return [ return [
'JSON.MGET', 'JSON.MGET',
...keys, ...keys,
path path
]; ];
} },
transformReply(reply: UnwrapReply<ArrayReply<NullReply | BlobStringReply>>) {
export function transformReply(reply: Array<string | null>): Array<RedisJSON | null> { return reply.map(json => transformRedisJsonNullReply(json))
return reply.map(transformRedisJsonNullReply); }
} } as const satisfies Command;

View File

@@ -11,15 +11,15 @@ export default {
FIRST_KEY_INDEX: 1, FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false, IS_READ_ONLY: false,
transformArguments(items: Array<JsonMSetItem>) { transformArguments(items: Array<JsonMSetItem>) {
const args = new Array(1 + items.length * 3); const args = new Array<RedisArgument>(1 + items.length * 3);
args[0] = 'JSON.MSET'; args[0] = 'JSON.MSET';
let argsIndex = 1; let argsIndex = 1;
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
const item = items[i]; const item = items[i];
args[argsIndex++] = item.key; args[argsIndex++] = item.key;
args[argsIndex++] = item.path; args[argsIndex++] = item.path;
args[argsIndex++] = transformRedisJsonArgument(item.value); args[argsIndex++] = transformRedisJsonArgument(item.value);
} }
return args; return args;

View File

@@ -11,11 +11,11 @@ describe('JSON.NUMINCRBY', () => {
}); });
testUtils.testWithClient('client.json.numIncrBy', async client => { 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( assert.deepEqual(reply, [1]);
await client.json.numIncrBy('key', '$', 1),
[1]
);
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
}); });

View File

@@ -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 { export default {
FIRST_KEY_INDEX: 1, FIRST_KEY_INDEX: 1,
@@ -8,8 +8,8 @@ export default {
}, },
transformReply: { transformReply: {
2: (reply: UnwrapReply<BlobStringReply>) => { 2: (reply: UnwrapReply<BlobStringReply>) => {
return JSON.parse(reply.toString()) as number | Array<number>; return JSON.parse(reply.toString()) as number | Array<null | number>;
}, },
3: undefined as unknown as () => NumberReply | DoubleReply | NullReply 3: undefined as unknown as () => ArrayReply<NumberReply | DoubleReply | NullReply>
} }
} as const satisfies Command; } as const satisfies Command;

View File

@@ -11,11 +11,11 @@ describe('JSON.NUMMULTBY', () => {
}); });
testUtils.testWithClient('client.json.numMultBy', async client => { 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( assert.deepEqual(reply, [2]);
await client.json.numMultBy('key', '$', 2),
[2]
);
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
}); });

View File

@@ -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 { export default {
FIRST_KEY_INDEX: 1, FIRST_KEY_INDEX: 1,
@@ -6,5 +7,5 @@ export default {
transformArguments(key: RedisArgument, path: RedisArgument, by: number) { transformArguments(key: RedisArgument, path: RedisArgument, by: number) {
return ['JSON.NUMMULTBY', key, path, by.toString()]; return ['JSON.NUMMULTBY', key, path, by.toString()];
}, },
transformReply: undefined as unknown as () => ArrayReply<NumberReply | DoubleReply | NullReply> transformReply: NUMINCRBY.transformReply
} as const satisfies Command; } as const satisfies Command;

View File

@@ -22,9 +22,9 @@ describe('JSON.OBJKEYS', () => {
}); });
testUtils.testWithClient('client.json.objKeys', async client => { testUtils.testWithClient('client.json.objKeys', async client => {
assert.deepEqual( assert.equal(
await client.json.objKeys('key'), await client.json.objKeys('key'),
[null] null
); );
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
}); });

View File

@@ -10,7 +10,7 @@ export default {
transformArguments(key: RedisArgument, options?: JsonObjKeysOptions) { transformArguments(key: RedisArgument, options?: JsonObjKeysOptions) {
const args = ['JSON.OBJKEYS', key]; const args = ['JSON.OBJKEYS', key];
if (options?.path) { if (options?.path !== undefined) {
args.push(options.path); args.push(options.path);
} }

View File

@@ -10,7 +10,7 @@ export default {
transformArguments(key: RedisArgument, options?: JsonObjLenOptions) { transformArguments(key: RedisArgument, options?: JsonObjLenOptions) {
const args = ['JSON.OBJLEN', key]; const args = ['JSON.OBJLEN', key];
if (options?.path) { if (options?.path !== undefined) {
args.push(options.path); args.push(options.path);
} }

View File

@@ -1,4 +1,5 @@
import { RedisArgument, Command, NullReply, NumberReply, ArrayReply } from '@redis/client/dist/lib/RESP/types'; import { RedisArgument, Command, NullReply, NumberReply, ArrayReply } from '@redis/client/dist/lib/RESP/types';
import { transformRedisJsonArgument } from '.';
export interface JsonStrAppendOptions { export interface JsonStrAppendOptions {
path?: RedisArgument; path?: RedisArgument;
@@ -7,14 +8,14 @@ export interface JsonStrAppendOptions {
export default { export default {
FIRST_KEY_INDEX: 1, FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false, IS_READ_ONLY: false,
transformArguments(key: RedisArgument, append: RedisArgument, options?: JsonStrAppendOptions) { transformArguments(key: RedisArgument, append: string, options?: JsonStrAppendOptions) {
const args = ['JSON.STRAPPEND', key]; const args = ['JSON.STRAPPEND', key];
if (options?.path) { if (options?.path !== undefined) {
args.push(options.path); args.push(options.path);
} }
args.push(append); args.push(transformRedisJsonArgument(append));
return args; return args;
}, },
transformReply: undefined as unknown as () => NumberReply | ArrayReply<NullReply | NumberReply> transformReply: undefined as unknown as () => NumberReply | ArrayReply<NullReply | NumberReply>

View File

@@ -12,7 +12,7 @@ describe('JSON.TOGGLE', () => {
testUtils.testWithClient('client.json.toggle', async client => { testUtils.testWithClient('client.json.toggle', async client => {
const [, reply] = await Promise.all([ const [, reply] = await Promise.all([
client.json.set('key', '$', ''), client.json.set('key', '$', true),
client.json.toggle('key', '$') client.json.toggle('key', '$')
]); ]);

View File

@@ -6,14 +6,14 @@ describe('TYPE', () => {
describe('transformArguments', () => { describe('transformArguments', () => {
it('simple', () => { it('simple', () => {
assert.deepEqual( assert.deepEqual(
transformArguments('key'), TYPE.transformArguments('key'),
['JSON.TYPE', 'key'] ['JSON.TYPE', 'key']
); );
}); });
it('with path', () => { it('with path', () => {
assert.deepEqual( assert.deepEqual(
transformArguments('key', { TYPE.transformArguments('key', {
path: '$' path: '$'
}), }),
['JSON.TYPE', 'key', '$'] ['JSON.TYPE', 'key', '$']
@@ -22,11 +22,9 @@ describe('TYPE', () => {
}); });
testUtils.testWithClient('client.json.type', async client => { testUtils.testWithClient('client.json.type', async client => {
assert.deepEqual( assert.equal(
await client.json.type('key', { await client.json.type('key'),
path: '$' null
}),
[null]
); );
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
}); });

View File

@@ -1,16 +1,17 @@
import { BlobStringReply, NullReply, UnwrapReply } from '@redis/client/dist/lib/RESP/types';
import ARRAPPEND from './ARRAPPEND'; import ARRAPPEND from './ARRAPPEND';
import ARRINDEX from './ARRINDEX'; import ARRINDEX from './ARRINDEX';
import ARRINSERT from './ARRINSERT'; import ARRINSERT from './ARRINSERT';
import ARRLEN from './ARRLEN'; import ARRLEN from './ARRLEN';
// import ARRPOP from './ARRPOP'; import ARRPOP from './ARRPOP';
import ARRTRIM from './ARRTRIM'; import ARRTRIM from './ARRTRIM';
import CLEAR from './CLEAR'; import CLEAR from './CLEAR';
import DEBUG_MEMORY from './DEBUG_MEMORY'; import DEBUG_MEMORY from './DEBUG_MEMORY';
import DEL from './DEL'; import DEL from './DEL';
import FORGET from './FORGET'; import FORGET from './FORGET';
// import GET from './GET'; import GET from './GET';
import MERGE from './MERGE'; import MERGE from './MERGE';
// import MGET from './MGET'; import MGET from './MGET';
import MSET from './MSET'; import MSET from './MSET';
import NUMINCRBY from './NUMINCRBY'; import NUMINCRBY from './NUMINCRBY';
import NUMMULTBY from './NUMMULTBY'; import NUMMULTBY from './NUMMULTBY';
@@ -22,6 +23,7 @@ import STRAPPEND from './STRAPPEND';
import STRLEN from './STRLEN'; import STRLEN from './STRLEN';
import TOGGLE from './TOGGLE'; import TOGGLE from './TOGGLE';
import TYPE from './TYPE'; import TYPE from './TYPE';
import { isNullReply } from '@redis/client/dist/lib/commands/generic-transformers';
export default { export default {
ARRAPPEND, ARRAPPEND,
@@ -32,8 +34,8 @@ export default {
arrInsert: ARRINSERT, arrInsert: ARRINSERT,
ARRLEN, ARRLEN,
arrLen: ARRLEN, arrLen: ARRLEN,
// ARRPOP, ARRPOP,
// arrPop: ARRPOP, arrPop: ARRPOP,
ARRTRIM, ARRTRIM,
arrTrim: ARRTRIM, arrTrim: ARRTRIM,
CLEAR, CLEAR,
@@ -44,12 +46,12 @@ export default {
del: DEL, del: DEL,
FORGET, FORGET,
forget: FORGET, forget: FORGET,
// GET, GET,
// get: GET, get: GET,
MERGE, MERGE,
merge: MERGE, merge: MERGE,
// MGET, MGET,
// mGet: MGET, mGet: MGET,
MSET, MSET,
mSet: MSET, mSet: MSET,
NUMINCRBY, NUMINCRBY,
@@ -80,29 +82,19 @@ export default {
type: TYPE type: TYPE
}; };
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540 export type RedisJSON = null | boolean | number | string | Date | Array<RedisJSON> | {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface RedisJSONArray extends Array<RedisJSON> { }
interface RedisJSONObject {
[key: string]: RedisJSON; [key: string]: RedisJSON;
[key: number]: RedisJSON; [key: number]: RedisJSON;
} };
export type RedisJSON = null | boolean | number | string | Date | RedisJSONArray | RedisJSONObject;
export function transformRedisJsonArgument(json: RedisJSON): string { export function transformRedisJsonArgument(json: RedisJSON): string {
return JSON.stringify(json); return JSON.stringify(json);
} }
export function transformRedisJsonReply(json: string): RedisJSON { export function transformRedisJsonReply(json: BlobStringReply): RedisJSON {
return JSON.parse(json); return JSON.parse((json as unknown as UnwrapReply<typeof json>).toString());
} }
export function transformRedisJsonNullReply(json: string | null): RedisJSON | null { export function transformRedisJsonNullReply(json: NullReply | BlobStringReply): NullReply | RedisJSON {
if (json === null) return null; return isNullReply(json) ? json : transformRedisJsonReply(json);
return transformRedisJsonReply(json);
}
export function transformNumbersReply(reply: string): number | Array<number> {
return JSON.parse(reply);
} }