You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
new "transform arguments" API for better key and metadata extraction (#2733)
* Parser support with all commands * remove "dist" from all imports for consistency * address most of my review comments * small tweak to multi type mapping handling * tweak multi commands / fix addScript cases * nits * addressed all in person review comments * revert addCommand/addScript changes to multi-commands addCommand needs to be there for sendCommand like ability within a multi. If its there, it might as well be used by createCommand() et al, to avoid repeating code. addScript is there (even though only used once), but now made private to keep the logic for bookkeeping near each other.
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import { strict as assert } from 'node:assert';
|
||||
import testUtils, { GLOBAL } from '../../test-utils';
|
||||
import INCRBY from './INCRBY';
|
||||
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
|
||||
|
||||
describe('CMS.INCRBY', () => {
|
||||
describe('transformArguments', () => {
|
||||
it('single item', () => {
|
||||
assert.deepEqual(
|
||||
INCRBY.transformArguments('key', {
|
||||
parseArgs(INCRBY, 'key', {
|
||||
item: 'item',
|
||||
incrementBy: 1
|
||||
}),
|
||||
@@ -16,7 +17,7 @@ describe('CMS.INCRBY', () => {
|
||||
|
||||
it('multiple items', () => {
|
||||
assert.deepEqual(
|
||||
INCRBY.transformArguments('key', [{
|
||||
parseArgs(INCRBY, 'key', [{
|
||||
item: 'a',
|
||||
incrementBy: 1
|
||||
}, {
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import { RedisArgument, ArrayReply, NumberReply, Command } from '@redis/client/dist/lib/RESP/types';
|
||||
import { CommandParser } from '@redis/client/lib/client/parser';
|
||||
import { RedisArgument, ArrayReply, NumberReply, Command } from '@redis/client/lib/RESP/types';
|
||||
|
||||
export interface BfIncrByItem {
|
||||
item: RedisArgument;
|
||||
@@ -6,27 +7,26 @@ export interface BfIncrByItem {
|
||||
}
|
||||
|
||||
export default {
|
||||
FIRST_KEY_INDEX: 1,
|
||||
IS_READ_ONLY: false,
|
||||
transformArguments(
|
||||
parseCommand(
|
||||
parser: CommandParser,
|
||||
key: RedisArgument,
|
||||
items: BfIncrByItem | Array<BfIncrByItem>
|
||||
) {
|
||||
const args = ['CMS.INCRBY', key];
|
||||
parser.push('CMS.INCRBY');
|
||||
parser.pushKey(key);
|
||||
|
||||
if (Array.isArray(items)) {
|
||||
for (const item of items) {
|
||||
pushIncrByItem(args, item);
|
||||
pushIncrByItem(parser, item);
|
||||
}
|
||||
} else {
|
||||
pushIncrByItem(args, items);
|
||||
pushIncrByItem(parser, items);
|
||||
}
|
||||
|
||||
return args;
|
||||
},
|
||||
transformReply: undefined as unknown as () => ArrayReply<NumberReply>
|
||||
} as const satisfies Command;
|
||||
|
||||
function pushIncrByItem(args: Array<RedisArgument>, { item, incrementBy }: BfIncrByItem): void {
|
||||
args.push(item, incrementBy.toString());
|
||||
function pushIncrByItem(parser: CommandParser, { item, incrementBy }: BfIncrByItem): void {
|
||||
parser.push(item, incrementBy.toString());
|
||||
}
|
||||
|
@@ -1,11 +1,12 @@
|
||||
import { strict as assert } from 'node:assert';
|
||||
import testUtils, { GLOBAL } from '../../test-utils';
|
||||
import INFO from './INFO';
|
||||
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
|
||||
|
||||
describe('CMS.INFO', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
INFO.transformArguments('key'),
|
||||
parseArgs(INFO, 'key'),
|
||||
['CMS.INFO', 'key']
|
||||
);
|
||||
});
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import { RedisArgument, TuplesToMapReply, NumberReply, UnwrapReply, Resp2Reply, Command, SimpleStringReply, TypeMapping } from '@redis/client/dist/lib/RESP/types';
|
||||
import { CommandParser } from '@redis/client/lib/client/parser';
|
||||
import { RedisArgument, TuplesToMapReply, NumberReply, UnwrapReply, Resp2Reply, Command, SimpleStringReply, TypeMapping } from '@redis/client/lib/RESP/types';
|
||||
import { transformInfoV2Reply } from '../bloom';
|
||||
|
||||
export type CmsInfoReplyMap = TuplesToMapReply<[
|
||||
@@ -14,10 +15,10 @@ export interface CmsInfoReply {
|
||||
}
|
||||
|
||||
export default {
|
||||
FIRST_KEY_INDEX: 1,
|
||||
IS_READ_ONLY: true,
|
||||
transformArguments(key: RedisArgument) {
|
||||
return ['CMS.INFO', key];
|
||||
parseCommand(parser: CommandParser, key: RedisArgument) {
|
||||
parser.push('CMS.INFO');
|
||||
parser.pushKey(key);
|
||||
},
|
||||
transformReply: {
|
||||
2: (reply: UnwrapReply<Resp2Reply<CmsInfoReplyMap>>, _, typeMapping?: TypeMapping): CmsInfoReply => {
|
||||
|
@@ -1,11 +1,12 @@
|
||||
import { strict as assert } from 'node:assert';
|
||||
import testUtils, { GLOBAL } from '../../test-utils';
|
||||
import INITBYDIM from './INITBYDIM';
|
||||
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
|
||||
|
||||
describe('CMS.INITBYDIM', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
INITBYDIM.transformArguments('key', 1000, 5),
|
||||
parseArgs(INITBYDIM, 'key', 1000, 5),
|
||||
['CMS.INITBYDIM', 'key', '1000', '5']
|
||||
);
|
||||
});
|
||||
|
@@ -1,10 +1,12 @@
|
||||
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/dist/lib/RESP/types';
|
||||
import { CommandParser } from '@redis/client/lib/client/parser';
|
||||
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/lib/RESP/types';
|
||||
|
||||
export default {
|
||||
FIRST_KEY_INDEX: 1,
|
||||
IS_READ_ONLY: false,
|
||||
transformArguments(key: RedisArgument, width: number, depth: number) {
|
||||
return ['CMS.INITBYDIM', key, width.toString(), depth.toString()];
|
||||
parseCommand(parser: CommandParser, key: RedisArgument, width: number, depth: number) {
|
||||
parser.push('CMS.INITBYDIM');
|
||||
parser.pushKey(key);
|
||||
parser.push(width.toString(), depth.toString());
|
||||
},
|
||||
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
|
||||
} as const satisfies Command;
|
||||
|
@@ -1,11 +1,12 @@
|
||||
import { strict as assert } from 'node:assert';
|
||||
import testUtils, { GLOBAL } from '../../test-utils';
|
||||
import INITBYPROB from './INITBYPROB';
|
||||
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
|
||||
|
||||
describe('CMS.INITBYPROB', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
INITBYPROB.transformArguments('key', 0.001, 0.01),
|
||||
parseArgs(INITBYPROB, 'key', 0.001, 0.01),
|
||||
['CMS.INITBYPROB', 'key', '0.001', '0.01']
|
||||
);
|
||||
});
|
||||
|
@@ -1,10 +1,12 @@
|
||||
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/dist/lib/RESP/types';
|
||||
import { CommandParser } from '@redis/client/lib/client/parser';
|
||||
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/lib/RESP/types';
|
||||
|
||||
export default {
|
||||
FIRST_KEY_INDEX: 1,
|
||||
IS_READ_ONLY: false,
|
||||
transformArguments(key: RedisArgument, error: number, probability: number) {
|
||||
return ['CMS.INITBYPROB', key, error.toString(), probability.toString()];
|
||||
parseCommand(parser: CommandParser, key: RedisArgument, error: number, probability: number) {
|
||||
parser.push('CMS.INITBYPROB');
|
||||
parser.pushKey(key);
|
||||
parser.push(error.toString(), probability.toString());
|
||||
},
|
||||
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
|
||||
} as const satisfies Command;
|
||||
|
@@ -1,19 +1,20 @@
|
||||
import { strict as assert } from 'node:assert';
|
||||
import testUtils, { GLOBAL } from '../../test-utils';
|
||||
import MERGE from './MERGE';
|
||||
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
|
||||
|
||||
describe('CMS.MERGE', () => {
|
||||
describe('transformArguments', () => {
|
||||
it('without WEIGHTS', () => {
|
||||
assert.deepEqual(
|
||||
MERGE.transformArguments('destination', ['source']),
|
||||
parseArgs(MERGE, 'destination', ['source']),
|
||||
['CMS.MERGE', 'destination', '1', 'source']
|
||||
);
|
||||
});
|
||||
|
||||
it('with WEIGHTS', () => {
|
||||
assert.deepEqual(
|
||||
MERGE.transformArguments('destination', [{
|
||||
parseArgs(MERGE, 'destination', [{
|
||||
name: 'source',
|
||||
weight: 1
|
||||
}]),
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/dist/lib/RESP/types';
|
||||
import { CommandParser } from '@redis/client/lib/client/parser';
|
||||
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/lib/RESP/types';
|
||||
|
||||
interface BfMergeSketch {
|
||||
name: RedisArgument;
|
||||
@@ -8,26 +9,27 @@ interface BfMergeSketch {
|
||||
export type BfMergeSketches = Array<RedisArgument> | Array<BfMergeSketch>;
|
||||
|
||||
export default {
|
||||
FIRST_KEY_INDEX: 1,
|
||||
IS_READ_ONLY: false,
|
||||
transformArguments(
|
||||
parseCommand(
|
||||
parser: CommandParser,
|
||||
destination: RedisArgument,
|
||||
source: BfMergeSketches
|
||||
) {
|
||||
let args = ['CMS.MERGE', destination, source.length.toString()];
|
||||
parser.push('CMS.MERGE');
|
||||
parser.pushKey(destination);
|
||||
parser.push(source.length.toString());
|
||||
|
||||
if (isPlainSketches(source)) {
|
||||
args = args.concat(source);
|
||||
parser.pushVariadic(source);
|
||||
} else {
|
||||
const { length } = args;
|
||||
args[length + source.length] = 'WEIGHTS';
|
||||
for (let i = 0; i < source.length; i++) {
|
||||
args[length + i] = source[i].name;
|
||||
args[length + source.length + i + 1] = source[i].weight.toString();
|
||||
parser.push(source[i].name);
|
||||
}
|
||||
parser.push('WEIGHTS');
|
||||
for (let i = 0; i < source.length; i++) {
|
||||
parser.push(source[i].weight.toString())
|
||||
}
|
||||
}
|
||||
|
||||
return args;
|
||||
},
|
||||
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
|
||||
} as const satisfies Command;
|
||||
|
@@ -1,11 +1,12 @@
|
||||
import { strict as assert } from 'node:assert';
|
||||
import testUtils, { GLOBAL } from '../../test-utils';
|
||||
import QUERY from './QUERY';
|
||||
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
|
||||
|
||||
describe('CMS.QUERY', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
QUERY.transformArguments('key', 'item'),
|
||||
parseArgs(QUERY, 'key', 'item'),
|
||||
['CMS.QUERY', 'key', 'item']
|
||||
);
|
||||
});
|
||||
|
@@ -1,11 +1,13 @@
|
||||
import { ArrayReply, NumberReply, Command, RedisArgument } from '@redis/client/dist/lib/RESP/types';
|
||||
import { RedisVariadicArgument, pushVariadicArguments } from '@redis/client/dist/lib/commands/generic-transformers';
|
||||
import { CommandParser } from '@redis/client/lib/client/parser';
|
||||
import { ArrayReply, NumberReply, Command, RedisArgument } from '@redis/client/lib/RESP/types';
|
||||
import { RedisVariadicArgument } from '@redis/client/lib/commands/generic-transformers';
|
||||
|
||||
export default {
|
||||
FIRST_KEY_INDEX: 1,
|
||||
IS_READ_ONLY: true,
|
||||
transformArguments(key: RedisArgument, items: RedisVariadicArgument) {
|
||||
return pushVariadicArguments(['CMS.QUERY', key], items);
|
||||
parseCommand(parser: CommandParser, key: RedisArgument, items: RedisVariadicArgument) {
|
||||
parser.push('CMS.QUERY');
|
||||
parser.pushKey(key);
|
||||
parser.pushVariadic(items);
|
||||
},
|
||||
transformReply: undefined as unknown as () => ArrayReply<NumberReply>
|
||||
} as const satisfies Command;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import type { RedisCommands } from '@redis/client/dist/lib/RESP/types';
|
||||
import type { RedisCommands } from '@redis/client/lib/RESP/types';
|
||||
import INCRBY from './INCRBY';
|
||||
import INFO from './INFO';
|
||||
import INITBYDIM from './INITBYDIM';
|
||||
|
Reference in New Issue
Block a user