You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
V5 bringing RESP3, Sentinel and TypeMapping to node-redis
RESP3 Support - Some commands responses in RESP3 aren't stable yet and therefore return an "untyped" ReplyUnion. Sentinel TypeMapping Correctly types Multi commands Note: some API changes to be further documented in v4-to-v5.md
This commit is contained in:
@@ -1,73 +1,75 @@
|
||||
import { RedisCommandArgument, RedisCommandArguments } from '.';
|
||||
import { RedisArgument, NumberReply, Command } from '../RESP/types';
|
||||
|
||||
export const FIRST_KEY_INDEX = 1;
|
||||
export type HashTypes = RedisArgument | number;
|
||||
|
||||
type Types = RedisCommandArgument | number;
|
||||
type HSETObject = Record<string | number, HashTypes>;
|
||||
|
||||
type HSETObject = Record<string | number, Types>;
|
||||
type HSETMap = Map<HashTypes, HashTypes>;
|
||||
|
||||
type HSETMap = Map<Types, Types>;
|
||||
type HSETTuples = Array<[HashTypes, HashTypes]> | Array<HashTypes>;
|
||||
|
||||
type HSETTuples = Array<[Types, Types]> | Array<Types>;
|
||||
type GenericArguments = [key: RedisArgument];
|
||||
|
||||
type GenericArguments = [key: RedisCommandArgument];
|
||||
|
||||
type SingleFieldArguments = [...generic: GenericArguments, field: Types, value: Types];
|
||||
type SingleFieldArguments = [...generic: GenericArguments, field: HashTypes, value: HashTypes];
|
||||
|
||||
type MultipleFieldsArguments = [...generic: GenericArguments, value: HSETObject | HSETMap | HSETTuples];
|
||||
|
||||
export function transformArguments(...[ key, value, fieldValue ]: SingleFieldArguments | MultipleFieldsArguments): RedisCommandArguments {
|
||||
const args: RedisCommandArguments = ['HSET', key];
|
||||
export type HSETArguments = SingleFieldArguments | MultipleFieldsArguments;
|
||||
|
||||
if (typeof value === 'string' || typeof value === 'number' || Buffer.isBuffer(value)) {
|
||||
args.push(
|
||||
convertValue(value),
|
||||
convertValue(fieldValue!)
|
||||
);
|
||||
export default {
|
||||
FIRST_KEY_INDEX: 1,
|
||||
transformArguments(...[key, value, fieldValue]: SingleFieldArguments | MultipleFieldsArguments) {
|
||||
const args: Array<RedisArgument> = ['HSET', key];
|
||||
|
||||
if (typeof value === 'string' || typeof value === 'number' || value instanceof Buffer) {
|
||||
args.push(
|
||||
convertValue(value),
|
||||
convertValue(fieldValue!)
|
||||
);
|
||||
} else if (value instanceof Map) {
|
||||
pushMap(args, value);
|
||||
pushMap(args, value);
|
||||
} else if (Array.isArray(value)) {
|
||||
pushTuples(args, value);
|
||||
pushTuples(args, value);
|
||||
} else {
|
||||
pushObject(args, value);
|
||||
pushObject(args, value);
|
||||
}
|
||||
|
||||
return args;
|
||||
},
|
||||
transformReply: undefined as unknown as () => NumberReply
|
||||
} as const satisfies Command;
|
||||
|
||||
function pushMap(args: Array<RedisArgument>, map: HSETMap): void {
|
||||
for (const [key, value] of map.entries()) {
|
||||
args.push(
|
||||
convertValue(key),
|
||||
convertValue(value)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function pushMap(args: RedisCommandArguments, map: HSETMap): void {
|
||||
for (const [key, value] of map.entries()) {
|
||||
args.push(
|
||||
convertValue(key),
|
||||
convertValue(value)
|
||||
);
|
||||
function pushTuples(args: Array<RedisArgument>, tuples: HSETTuples): void {
|
||||
for (const tuple of tuples) {
|
||||
if (Array.isArray(tuple)) {
|
||||
pushTuples(args, tuple);
|
||||
continue;
|
||||
}
|
||||
|
||||
args.push(convertValue(tuple));
|
||||
}
|
||||
}
|
||||
|
||||
function pushTuples(args: RedisCommandArguments, tuples: HSETTuples): void {
|
||||
for (const tuple of tuples) {
|
||||
if (Array.isArray(tuple)) {
|
||||
pushTuples(args, tuple);
|
||||
continue;
|
||||
}
|
||||
|
||||
args.push(convertValue(tuple));
|
||||
}
|
||||
function pushObject(args: Array<RedisArgument>, object: HSETObject): void {
|
||||
for (const key of Object.keys(object)) {
|
||||
args.push(
|
||||
convertValue(key),
|
||||
convertValue(object[key])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function pushObject(args: RedisCommandArguments, object: HSETObject): void {
|
||||
for (const key of Object.keys(object)) {
|
||||
args.push(
|
||||
convertValue(key),
|
||||
convertValue(object[key])
|
||||
);
|
||||
}
|
||||
function convertValue(value: HashTypes): RedisArgument {
|
||||
return typeof value === 'number' ?
|
||||
value.toString() :
|
||||
value;
|
||||
}
|
||||
|
||||
function convertValue(value: Types): RedisCommandArgument {
|
||||
return typeof value === 'number' ?
|
||||
value.toString() :
|
||||
value;
|
||||
}
|
||||
|
||||
export declare function transformReply(): number;
|
||||
|
Reference in New Issue
Block a user