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>`
- `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`

View File

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

View File

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

View File

@@ -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<RedisJSON>) {
const args = ['JSON.ARRAPPEND', key, path];
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);
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;

View File

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

View File

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

View File

@@ -11,16 +11,16 @@ export default {
json: RedisJSON,
...jsons: Array<RedisJSON>
) {
const args = [
'JSON.ARRINSERT',
key,
path,
index.toString(),
transformRedisJsonArgument(json)
];
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);
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;

View File

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

View File

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

View File

@@ -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('JSON.ARRPOP', () => {
describe('transformArguments', () => {
it('key', () => {
it('simple', () => {
assert.deepEqual(
transformArguments('key'),
ARRPOP.transformArguments('key'),
['JSON.ARRPOP', 'key']
);
});
it('key, path', () => {
it('with path', () => {
assert.deepEqual(
transformArguments('key', '$'),
ARRPOP.transformArguments('key', {
path: '$'
}),
['JSON.ARRPOP', 'key', '$']
);
});
it('key, path, index', () => {
it('with path and index', () => {
assert.deepEqual(
transformArguments('key', '$', 0),
ARRPOP.transformArguments('key', {
path: '$',
index: 0
}),
['JSON.ARRPOP', 'key', '$', '0']
);
});
});
describe('client.json.arrPop', () => {
testUtils.testWithClient('null', async client => {
await client.json.set('key', '.', []);
testUtils.testWithClient('without path and value', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '$', []),
client.json.arrPop('key')
]);
assert.equal(
await client.json.arrPop('key', '.'),
null
);
assert.equal(reply, null);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('with value', async client => {
await client.json.set('key', '.', ['value']);
testUtils.testWithClient('. path with value', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '.', ['value']),
client.json.arrPop('key', {
path: '.'
})
]);
assert.equal(
await client.json.arrPop('key', '.'),
'value'
);
assert.equal(reply, 'value');
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('array', async client => {
await client.json.set('key', '$', ['value']);
testUtils.testWithClient('$ path with value', async client => {
const [, reply] = await Promise.all([
client.json.set('key', '$', ['value']),
client.json.arrPop('key', {
path: '$'
})
]);
assert.deepEqual(
await client.json.arrPop('key', '$'),
['value']
);
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;
}
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);
},
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;
return transformRedisJsonNullReply(reply);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,72 +1,31 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './GET';
import GET from './GET';
describe('GET', () => {
describe('JSON.GET', () => {
describe('transformArguments', () => {
describe('path', () => {
it('simple', () => {
assert.deepEqual(
GET.transformArguments('key'),
['JSON.GET', 'key']
);
});
describe('with path', () => {
it('string', () => {
assert.deepEqual(
transformArguments('key', { path: '$' }),
GET.transformArguments('key', { path: '$' }),
['JSON.GET', 'key', '$']
);
});
it('array', () => {
assert.deepEqual(
transformArguments('key', { path: ['$.1', '$.2'] }),
GET.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 => {

View File

@@ -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<string>;
// 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;

View File

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

View File

@@ -1,11 +1,11 @@
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'], '$'),
MGET.transformArguments(['1', '2'], '$'),
['JSON.MGET', '1', '2', '$']
);
});

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 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
];
}
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

@@ -11,7 +11,7 @@ export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
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';
let argsIndex = 1;

View File

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

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 {
FIRST_KEY_INDEX: 1,
@@ -8,8 +8,8 @@ export default {
},
transformReply: {
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;

View File

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

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 {
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<NumberReply | DoubleReply | NullReply>
transformReply: NUMINCRBY.transformReply
} as const satisfies Command;

View File

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

View File

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

View File

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

View File

@@ -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<NullReply | NumberReply>

View File

@@ -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', '$')
]);

View File

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

View File

@@ -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<RedisJSON> { }
interface RedisJSONObject {
export type RedisJSON = null | boolean | number | string | Date | Array<RedisJSON> | {
[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<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);
}