You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
feat(hash field expiration): Added hash field expiration commands (#2907)
* [CAE-686] Added hash field expiration commands * [CAE-686] Improve HSETEX return type * [CAE-686] Minor pushTuples change, renamed HSETEX test * [CAE-686] Changed hsetex function signature for better consistency with other commands * [CAE-686] Fixed hsetex test * [CAE-686] Bumped docker version to 8.0-M05-pre, enabled and fixed tests
This commit is contained in:
110
packages/client/lib/commands/HSETEX.ts
Normal file
110
packages/client/lib/commands/HSETEX.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import { BasicCommandParser, CommandParser } from '../client/parser';
|
||||
import { Command, NumberReply, RedisArgument } from '../RESP/types';
|
||||
|
||||
export interface HSetExOptions {
|
||||
expiration?: {
|
||||
type: 'EX' | 'PX' | 'EXAT' | 'PXAT';
|
||||
value: number;
|
||||
} | {
|
||||
type: 'KEEPTTL';
|
||||
} | 'KEEPTTL';
|
||||
mode?: 'FNX' | 'FXX'
|
||||
}
|
||||
|
||||
export type HashTypes = RedisArgument | number;
|
||||
|
||||
type HSETEXObject = Record<string | number, HashTypes>;
|
||||
|
||||
type HSETEXMap = Map<HashTypes, HashTypes>;
|
||||
|
||||
type HSETEXTuples = Array<[HashTypes, HashTypes]> | Array<HashTypes>;
|
||||
|
||||
export default {
|
||||
parseCommand(
|
||||
parser: CommandParser,
|
||||
key: RedisArgument,
|
||||
fields: HSETEXObject | HSETEXMap | HSETEXTuples,
|
||||
options?: HSetExOptions
|
||||
) {
|
||||
parser.push('HSETEX');
|
||||
parser.pushKey(key);
|
||||
|
||||
if (options?.mode) {
|
||||
parser.push(options.mode)
|
||||
}
|
||||
if (options?.expiration) {
|
||||
if (typeof options.expiration === 'string') {
|
||||
parser.push(options.expiration);
|
||||
} else if (options.expiration.type === 'KEEPTTL') {
|
||||
parser.push('KEEPTTL');
|
||||
} else {
|
||||
parser.push(
|
||||
options.expiration.type,
|
||||
options.expiration.value.toString()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
parser.push('FIELDS')
|
||||
if (fields instanceof Map) {
|
||||
pushMap(parser, fields);
|
||||
} else if (Array.isArray(fields)) {
|
||||
pushTuples(parser, fields);
|
||||
} else {
|
||||
pushObject(parser, fields);
|
||||
}
|
||||
},
|
||||
transformReply: undefined as unknown as () => NumberReply<0 | 1>
|
||||
} as const satisfies Command;
|
||||
|
||||
|
||||
function pushMap(parser: CommandParser, map: HSETEXMap): void {
|
||||
parser.push(map.size.toString())
|
||||
for (const [key, value] of map.entries()) {
|
||||
parser.push(
|
||||
convertValue(key),
|
||||
convertValue(value)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function pushTuples(parser: CommandParser, tuples: HSETEXTuples): void {
|
||||
const tmpParser = new BasicCommandParser
|
||||
_pushTuples(tmpParser, tuples)
|
||||
|
||||
if (tmpParser.redisArgs.length%2 != 0) {
|
||||
throw Error('invalid number of arguments, expected key value ....[key value] pairs, got key without value')
|
||||
}
|
||||
|
||||
parser.push((tmpParser.redisArgs.length/2).toString())
|
||||
parser.push(...tmpParser.redisArgs)
|
||||
}
|
||||
|
||||
function _pushTuples(parser: CommandParser, tuples: HSETEXTuples): void {
|
||||
for (const tuple of tuples) {
|
||||
if (Array.isArray(tuple)) {
|
||||
_pushTuples(parser, tuple);
|
||||
continue;
|
||||
}
|
||||
parser.push(convertValue(tuple));
|
||||
}
|
||||
}
|
||||
|
||||
function pushObject(parser: CommandParser, object: HSETEXObject): void {
|
||||
const len = Object.keys(object).length
|
||||
if (len == 0) {
|
||||
throw Error('object without keys')
|
||||
}
|
||||
|
||||
parser.push(len.toString())
|
||||
for (const key of Object.keys(object)) {
|
||||
parser.push(
|
||||
convertValue(key),
|
||||
convertValue(object[key])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function convertValue(value: HashTypes): RedisArgument {
|
||||
return typeof value === 'number' ? value.toString() : value;
|
||||
}
|
Reference in New Issue
Block a user