UNPKG

syntropylog

Version:

An instance manager with observability for Node.js applications

285 lines (284 loc) 13.2 kB
/** * @file src/redis/RedisCommandExecutor.ts * @description A thin wrapper around the native `node-redis` client that directly executes commands. * This class's sole responsibility is to pass commands to the underlying client. * It does not contain any logic for instrumentation, connection management, or error handling. */ import { NodeRedisClient, RedisZMember } from './redis.types'; import { RedisValue, RedisListElement, RedisSetMember, RedisSortedSetMember, RedisHashValue, RedisCommandOptions } from '../types'; /** * Executes Redis commands against a native `node-redis` client. * This class acts as a direct pass-through to the client's methods, * decoupling the command execution from the instrumentation and connection logic. */ export declare class RedisCommandExecutor { private client; /** * Constructs a new RedisCommandExecutor. * @param {NodeRedisClient} client The native `node-redis` client (single-node or cluster) to execute commands on. */ constructor(client: NodeRedisClient); /** * Executes the native GET command. * @param {string} key The key to retrieve. * @returns {Promise<string | null>} The value of the key, or null if it does not exist. */ get(key: string): Promise<string | null>; /** * Executes the native SET command. * @param {string} key The key to set. * @param {string} value The value to set. * @param {RedisCommandOptions} [options] Optional SET options (e.g., EX, NX). * @returns {Promise<string | null>} 'OK' if successful, or null. */ set(key: string, value: string, options?: RedisCommandOptions): Promise<string | null>; /** * Executes the native DEL command. * @param {string | string[]} keys The key or keys to delete. * @returns {Promise<number>} The number of keys deleted. */ del(keys: string | string[]): Promise<number>; /** * Executes the native EXISTS command. * @param {string | string[]} keys The key or keys to check. * @returns {Promise<number>} The number of keys that exist. */ exists(keys: string | string[]): Promise<number>; /** * Executes the native EXPIRE command. * @param {string} key The key to set the expiration for. * @param {number} seconds The time-to-live in seconds. * @returns {Promise<boolean>} True if the timeout was set, false otherwise. */ expire(key: string, seconds: number): Promise<boolean>; /** * Executes the native TTL command. * @param {string} key The key to check. * @returns {Promise<number>} The remaining time to live in seconds. */ ttl(key: string): Promise<number>; /** * Executes the native INCR command. * @param {string} key The key to increment. * @returns {Promise<number>} The value after the increment. */ incr(key: string): Promise<number>; /** * Executes the native DECR command. * @param {string} key The key to decrement. * @returns {Promise<number>} The value after the decrement. */ decr(key: string): Promise<number>; /** * Executes the native INCRBY command. * @param {string} key The key to increment. * @param {number} increment The amount to increment by. * @returns {Promise<number>} The value after the increment. */ incrBy(key: string, increment: number): Promise<number>; /** * Executes the native DECRBY command. * @param {string} key The key to decrement. * @param {number} decrement The amount to decrement by. * @returns {Promise<number>} The value after the decrement. */ decrBy(key: string, decrement: number): Promise<number>; /** * Executes the native HGET command. * @param {string} key The key of the hash. * @param {string} field The field to retrieve. * @returns {Promise<string | undefined>} The value of the field, or undefined if it does not exist. */ hGet(key: string, field: string): Promise<string | undefined>; /** * Executes the native HSET command. * @param {string} key The key of the hash. * @param {string | Record<string, RedisHashValue>} fieldOrFields The field to set or an object of field-value pairs. * @param {RedisHashValue} [value] The value to set if a single field is provided. * @returns {Promise<number>} The number of fields that were added. */ hSet(key: string, fieldOrFields: string | Record<string, RedisHashValue>, value?: RedisHashValue): Promise<number>; /** * Executes the native HGETALL command. * @param {string} key The key of the hash. * @returns {Promise<Record<string, string>>} An object containing all fields and values. */ hGetAll(key: string): Promise<Record<string, string>>; /** * Executes the native HDEL command. * @param {string} key The key of the hash. * @param {string | string[]} fields The field or fields to delete. * @returns {Promise<number>} The number of fields that were removed. */ hDel(key: string, fields: string | string[]): Promise<number>; /** * Executes the native HEXISTS command. * @param {string} key The key of the hash. * @param {string} field The field to check. * @returns {Promise<boolean>} True if the field exists, false otherwise. */ hExists(key: string, field: string): Promise<boolean>; /** * Executes the native HINCRBY command. * @param {string} key The key of the hash. * @param {string} field The field to increment. * @param {number} increment The amount to increment by. * @returns {Promise<number>} The value of the field after the increment. */ hIncrBy(key: string, field: string, increment: number): Promise<number>; /** * Executes the native LPUSH command. * @param {string} key The key of the list. * @param {RedisListElement | RedisListElement[]} elements The element or elements to prepend. * @returns {Promise<number>} The length of the list after the operation. */ lPush(key: string, elements: RedisListElement | RedisListElement[]): Promise<number>; /** * Executes the native RPUSH command. * @param {string} key The key of the list. * @param {RedisListElement | RedisListElement[]} elements The element or elements to append. * @returns {Promise<number>} The length of the list after the operation. */ rPush(key: string, elements: RedisListElement | RedisListElement[]): Promise<number>; /** * Executes the native LPOP command. * @param {string} key The key of the list. * @returns {Promise<string | null>} The value of the first element, or null if the list is empty. */ lPop(key: string): Promise<string | null>; /** * Executes the native RPOP command. * @param {string} key The key of the list. * @returns {Promise<string | null>} The value of the last element, or null if the list is empty. */ rPop(key: string): Promise<string | null>; /** * Executes the native LRANGE command. * @param {string} key The key of the list. * @param {number} start The starting index. * @param {number} stop The ending index. * @returns {Promise<string[]>} An array of elements in the specified range. */ lRange(key: string, start: number, stop: number): Promise<string[]>; /** * Executes the native LLEN command. * @param {string} key The key of the list. * @returns {Promise<number>} The length of the list. */ lLen(key: string): Promise<number>; /** * Executes the native LTRIM command. * @param {string} key The key of the list. * @param {number} start The starting index. * @param {number} stop The ending index. * @returns {Promise<string>} 'OK'. */ lTrim(key: string, start: number, stop: number): Promise<string>; /** * Executes the native SADD command. * @param {string} key The key of the set. * @param {RedisSetMember | RedisSetMember[]} members The member or members to add. * @returns {Promise<number>} The number of members added to the set. */ sAdd(key: string, members: RedisSetMember | RedisSetMember[]): Promise<number>; /** * Executes the native SMEMBERS command. * @param {string} key The key of the set. * @returns {Promise<string[]>} An array of all members in the set. */ sMembers(key: string): Promise<string[]>; /** * Executes the native SISMEMBER command. * @param {string} key The key of the set. * @param {RedisSetMember} member The member to check for. * @returns {Promise<boolean>} True if the member is in the set, false otherwise. */ sIsMember(key: string, member: RedisSetMember): Promise<boolean>; /** * Executes the native SREM command. * @param {string} key The key of the set. * @param {RedisSetMember | RedisSetMember[]} members The member or members to remove. * @returns {Promise<number>} The number of members removed from the set. */ sRem(key: string, members: RedisSetMember | RedisSetMember[]): Promise<number>; /** * Executes the native SCARD command. * @param {string} key The key of the set. * @returns {Promise<number>} The cardinality of the set. */ sCard(key: string): Promise<number>; /** * Executes the native ZADD command. * @param {string} key The key of the sorted set. * @param {number | RedisSortedSetMember[]} scoreOrMembers The score for a single member, or an array of member-score objects. * @param {RedisValue} [member] The member to add if a single score is provided. * @returns {Promise<number>} The number of elements added to the sorted set. */ zAdd(key: string, scoreOrMembers: number | RedisSortedSetMember[], member?: RedisValue): Promise<number>; /** * Executes the native ZRANGE command. * @param {string} key The key of the sorted set. * @param {string | number} min The minimum index or score. * @param {string | number} max The maximum index or score. * @param {RedisCommandOptions} [options] Additional options (e.g., REV). * @returns {Promise<string[]>} An array of members in the specified range. */ zRange(key: string, min: string | number, max: string | number, options?: RedisCommandOptions): Promise<string[]>; /** * Executes the native ZRANGE command with the WITHSCORES option. * @param {string} key The key of the sorted set. * @param {string | number} min The minimum index or score. * @param {string | number} max The maximum index or score. * @param {RedisCommandOptions} [options] Additional options (e.g., REV). * @returns {Promise<RedisZMember[]>} An array of members and their scores. */ zRangeWithScores(key: string, min: string | number, max: string | number, options?: RedisCommandOptions): Promise<RedisZMember[]>; /** * Executes the native ZREM command. * @param {string} key The key of the sorted set. * @param {RedisValue | RedisValue[]} members The member or members to remove. * @returns {Promise<number>} The number of members removed. */ zRem(key: string, members: RedisValue | RedisValue[]): Promise<number>; /** * Executes the native ZCARD command. * @param {string} key The key of the sorted set. * @returns {Promise<number>} The cardinality of the sorted set. */ zCard(key: string): Promise<number>; /** * Executes the native ZSCORE command. * @param {string} key The key of the sorted set. * @param {RedisValue} member The member whose score to retrieve. * @returns {Promise<number | null>} The score of the member, or null if it does not exist. */ zScore(key: string, member: RedisValue): Promise<number | null>; /** * Executes the native EVAL command. * @param {string} script The Lua script to execute. * @param {string[]} keys An array of key names. * @param {string[]} args An array of argument values. * @returns {Promise<any>} The result of the script execution. */ eval(script: string, keys: string[], args: string[]): Promise<RedisValue>; /** * Executes the native SUBSCRIBE command. * @param {string} channel The channel to subscribe to. * @param {(message: string, channel: string) => void} listener The callback for received messages. * @returns {Promise<void>} */ subscribe(channel: string, listener: (message: string, channel: string) => void): Promise<void>; /** * Executes the native UNSUBSCRIBE command. * @param {string} [channel] The channel to unsubscribe from. If omitted, unsubscribes from all. * @returns {Promise<void>} */ unsubscribe(channel?: string): Promise<void>; /** * Executes the native PUBLISH command. * @param {string} channel The channel to publish to. * @param {string} message The message to publish. * @returns {Promise<number>} The number of clients that received the message. */ publish(channel: string, message: string): Promise<number>; }