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,3 +1,4 @@
|
||||
import { CommandParser } from '../client/parser';
|
||||
import { RedisArgument, NumberReply, Command } from '../RESP/types';
|
||||
|
||||
export type HashTypes = RedisArgument | number;
|
||||
@@ -17,51 +18,49 @@ type MultipleFieldsArguments = [...generic: GenericArguments, value: HSETObject
|
||||
export type HSETArguments = SingleFieldArguments | MultipleFieldsArguments;
|
||||
|
||||
export default {
|
||||
FIRST_KEY_INDEX: 1,
|
||||
transformArguments(...[key, value, fieldValue]: SingleFieldArguments | MultipleFieldsArguments) {
|
||||
const args: Array<RedisArgument> = ['HSET', key];
|
||||
parseCommand(parser: CommandParser, ...[key, value, fieldValue]: SingleFieldArguments | MultipleFieldsArguments) {
|
||||
parser.push('HSET');
|
||||
parser.pushKey(key);
|
||||
|
||||
if (typeof value === 'string' || typeof value === 'number' || value instanceof Buffer) {
|
||||
args.push(
|
||||
parser.push(
|
||||
convertValue(value),
|
||||
convertValue(fieldValue!)
|
||||
);
|
||||
} else if (value instanceof Map) {
|
||||
pushMap(args, value);
|
||||
pushMap(parser, value);
|
||||
} else if (Array.isArray(value)) {
|
||||
pushTuples(args, value);
|
||||
pushTuples(parser, value);
|
||||
} else {
|
||||
pushObject(args, value);
|
||||
pushObject(parser, value);
|
||||
}
|
||||
|
||||
return args;
|
||||
},
|
||||
transformReply: undefined as unknown as () => NumberReply
|
||||
} as const satisfies Command;
|
||||
|
||||
function pushMap(args: Array<RedisArgument>, map: HSETMap): void {
|
||||
function pushMap(parser: CommandParser, map: HSETMap): void {
|
||||
for (const [key, value] of map.entries()) {
|
||||
args.push(
|
||||
parser.push(
|
||||
convertValue(key),
|
||||
convertValue(value)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function pushTuples(args: Array<RedisArgument>, tuples: HSETTuples): void {
|
||||
function pushTuples(parser: CommandParser, tuples: HSETTuples): void {
|
||||
for (const tuple of tuples) {
|
||||
if (Array.isArray(tuple)) {
|
||||
pushTuples(args, tuple);
|
||||
pushTuples(parser, tuple);
|
||||
continue;
|
||||
}
|
||||
|
||||
args.push(convertValue(tuple));
|
||||
parser.push(convertValue(tuple));
|
||||
}
|
||||
}
|
||||
|
||||
function pushObject(args: Array<RedisArgument>, object: HSETObject): void {
|
||||
function pushObject(parser: CommandParser, object: HSETObject): void {
|
||||
for (const key of Object.keys(object)) {
|
||||
args.push(
|
||||
parser.push(
|
||||
convertValue(key),
|
||||
convertValue(object[key])
|
||||
);
|
||||
|
Reference in New Issue
Block a user