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,71 +1,82 @@
|
||||
import { RedisCommandArgument, RedisCommandArguments } from '.';
|
||||
import { transformNumberInfinityArgument, ZMember } from './generic-transformers';
|
||||
import { RedisArgument, Command } from '../RESP/types';
|
||||
import { SortedSetMember, transformDoubleArgument, transformDoubleReply } from './generic-transformers';
|
||||
|
||||
export const FIRST_KEY_INDEX = 1;
|
||||
|
||||
interface NX {
|
||||
NX?: true;
|
||||
export interface ZAddOptions {
|
||||
condition?: 'NX' | 'XX';
|
||||
/**
|
||||
* @deprecated Use `{ condition: 'NX' }` instead.
|
||||
*/
|
||||
NX?: boolean;
|
||||
/**
|
||||
* @deprecated Use `{ condition: 'XX' }` instead.
|
||||
*/
|
||||
XX?: boolean;
|
||||
comparison?: 'LT' | 'GT';
|
||||
/**
|
||||
* @deprecated Use `{ comparison: 'LT' }` instead.
|
||||
*/
|
||||
LT?: boolean;
|
||||
/**
|
||||
* @deprecated Use `{ comparison: 'GT' }` instead.
|
||||
*/
|
||||
GT?: boolean;
|
||||
CH?: boolean;
|
||||
}
|
||||
|
||||
interface XX {
|
||||
XX?: true;
|
||||
}
|
||||
|
||||
interface LT {
|
||||
LT?: true;
|
||||
}
|
||||
|
||||
interface GT {
|
||||
GT?: true;
|
||||
}
|
||||
|
||||
interface CH {
|
||||
CH?: true;
|
||||
}
|
||||
|
||||
interface INCR {
|
||||
INCR?: true;
|
||||
}
|
||||
|
||||
type ZAddOptions = (NX | (XX & LT & GT)) & CH & INCR;
|
||||
|
||||
export function transformArguments(
|
||||
key: RedisCommandArgument,
|
||||
members: ZMember | Array<ZMember>,
|
||||
export default {
|
||||
FIRST_KEY_INDEX: 1,
|
||||
transformArguments(
|
||||
key: RedisArgument,
|
||||
members: SortedSetMember | Array<SortedSetMember>,
|
||||
options?: ZAddOptions
|
||||
): RedisCommandArguments {
|
||||
) {
|
||||
const args = ['ZADD', key];
|
||||
|
||||
if ((<NX>options)?.NX) {
|
||||
args.push('NX');
|
||||
} else {
|
||||
if ((<XX>options)?.XX) {
|
||||
args.push('XX');
|
||||
}
|
||||
if (options?.condition) {
|
||||
args.push(options.condition);
|
||||
} else if (options?.NX) {
|
||||
args.push('NX');
|
||||
} else if (options?.XX) {
|
||||
args.push('XX');
|
||||
}
|
||||
|
||||
if ((<GT>options)?.GT) {
|
||||
args.push('GT');
|
||||
} else if ((<LT>options)?.LT) {
|
||||
args.push('LT');
|
||||
}
|
||||
if (options?.comparison) {
|
||||
args.push(options.comparison);
|
||||
} else if (options?.LT) {
|
||||
args.push('LT');
|
||||
} else if (options?.GT) {
|
||||
args.push('GT');
|
||||
}
|
||||
|
||||
if ((<CH>options)?.CH) {
|
||||
args.push('CH');
|
||||
if (options?.CH) {
|
||||
args.push('CH');
|
||||
}
|
||||
|
||||
if ((<INCR>options)?.INCR) {
|
||||
args.push('INCR');
|
||||
}
|
||||
|
||||
for (const { score, value } of (Array.isArray(members) ? members : [members])) {
|
||||
args.push(
|
||||
transformNumberInfinityArgument(score),
|
||||
value
|
||||
);
|
||||
}
|
||||
pushMembers(args, members);
|
||||
|
||||
return args;
|
||||
},
|
||||
transformReply: transformDoubleReply
|
||||
} as const satisfies Command;
|
||||
|
||||
export function pushMembers(
|
||||
args: Array<RedisArgument>,
|
||||
members: SortedSetMember | Array<SortedSetMember>) {
|
||||
if (Array.isArray(members)) {
|
||||
for (const member of members) {
|
||||
pushMember(args, member);
|
||||
}
|
||||
} else {
|
||||
pushMember(args, members);
|
||||
}
|
||||
}
|
||||
|
||||
export { transformNumberInfinityReply as transformReply } from './generic-transformers';
|
||||
function pushMember(
|
||||
args: Array<RedisArgument>,
|
||||
member: SortedSetMember
|
||||
) {
|
||||
args.push(
|
||||
transformDoubleArgument(member.score),
|
||||
member.value
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user