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,34 +1,61 @@
|
||||
import { RedisCommandArgument, RedisCommandArguments } from '.';
|
||||
import { SortedSetSide, transformSortedSetMemberReply, transformZMPopArguments, ZMember, ZMPopOptions } from './generic-transformers';
|
||||
import { RedisArgument, NullReply, TuplesReply, BlobStringReply, DoubleReply, ArrayReply, UnwrapReply, Resp2Reply, Command, TypeMapping } from '../RESP/types';
|
||||
import { pushVariadicArgument, RedisVariadicArgument, SortedSetSide, transformSortedSetReply, transformDoubleReply } from './generic-transformers';
|
||||
|
||||
export const FIRST_KEY_INDEX = 2;
|
||||
|
||||
export function transformArguments(
|
||||
keys: RedisCommandArgument | Array<RedisCommandArgument>,
|
||||
side: SortedSetSide,
|
||||
options?: ZMPopOptions
|
||||
): RedisCommandArguments {
|
||||
return transformZMPopArguments(
|
||||
['ZMPOP'],
|
||||
keys,
|
||||
side,
|
||||
options
|
||||
);
|
||||
export interface ZMPopOptions {
|
||||
COUNT?: number;
|
||||
}
|
||||
|
||||
type ZMPopRawReply = null | [
|
||||
key: string,
|
||||
elements: Array<[RedisCommandArgument, RedisCommandArgument]>
|
||||
];
|
||||
export type ZMPopRawReply = NullReply | TuplesReply<[
|
||||
key: BlobStringReply,
|
||||
members: ArrayReply<TuplesReply<[
|
||||
value: BlobStringReply,
|
||||
score: DoubleReply
|
||||
]>>
|
||||
]>;
|
||||
|
||||
type ZMPopReply = null | {
|
||||
key: string,
|
||||
elements: Array<ZMember>
|
||||
};
|
||||
export function transformZMPopArguments(
|
||||
args: Array<RedisArgument>,
|
||||
keys: RedisVariadicArgument,
|
||||
side: SortedSetSide,
|
||||
options?: ZMPopOptions
|
||||
) {
|
||||
args = pushVariadicArgument(args, keys);
|
||||
|
||||
export function transformReply(reply: ZMPopRawReply): ZMPopReply {
|
||||
return reply === null ? null : {
|
||||
args.push(side);
|
||||
|
||||
if (options?.COUNT) {
|
||||
args.push('COUNT', options.COUNT.toString());
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
export type ZMPopArguments = typeof transformZMPopArguments extends (_: any, ...args: infer T) => any ? T : never;
|
||||
|
||||
export default {
|
||||
FIRST_KEY_INDEX: 2,
|
||||
IS_READ_ONLY: false,
|
||||
transformArguments(...args: ZMPopArguments) {
|
||||
return transformZMPopArguments(['ZMPOP'], ...args);
|
||||
},
|
||||
transformReply: {
|
||||
2(reply: UnwrapReply<Resp2Reply<ZMPopRawReply>>, preserve?: any, typeMapping?: TypeMapping) {
|
||||
return reply === null ? null : {
|
||||
key: reply[0],
|
||||
elements: reply[1].map(transformSortedSetMemberReply)
|
||||
};
|
||||
}
|
||||
members: (reply[1] as unknown as UnwrapReply<typeof reply[1]>).map(member => {
|
||||
const [value, score] = member as unknown as UnwrapReply<typeof member>;
|
||||
return {
|
||||
value,
|
||||
score: transformDoubleReply[2](score, preserve, typeMapping)
|
||||
};
|
||||
})
|
||||
};
|
||||
},
|
||||
3(reply: UnwrapReply<ZMPopRawReply>) {
|
||||
return reply === null ? null : {
|
||||
key: reply[0],
|
||||
members: transformSortedSetReply[3](reply[1])
|
||||
};
|
||||
}
|
||||
}
|
||||
} as const satisfies Command;
|
||||
|
Reference in New Issue
Block a user