UNPKG

@am92/redis

Version:
317 lines (316 loc) 8.64 kB
import { SetOptions } from 'redis'; import { RedisSdkConfig, RedisClient } from './TYPES'; /** * Class to create an SDK which interacts with a Redis Instance * * @class */ export declare class RedisSdk { /** * Redis Config used by the SDK */ CONFIG: RedisSdkConfig; /** * Redis Client used by the SDK */ client: RedisClient; /** * Flag identifying if the connection has been established or not */ connected: boolean; /** * Creates an instance of RedisSdk. * * @constructor * @param [config] Redis SDK Configuration */ constructor(config?: RedisSdkConfig); /** * Establish a connection with the Redis Instance * * @async * @returns */ connect(): Promise<void>; /** * Releases the connection with the Redis Instance if established * * @async * @param [forced=false] * @returns */ disconnect(forced?: boolean): Promise<void>; /** * Returns the Redis client as configured using `CONFIG` * * @returns */ getClient(): RedisClient; /** * Prefixes the Redis key with `CONFIG.KEY_PREFIX` * * @param [key=''] * @returns */ prefixKey(key?: string): string; /** * Unprefixes the Redis key with `CONFIG.KEY_PREFIX` * * @param [key=''] * @returns */ unprefixKey(key?: string): string; /** * Gets the value of a Redis key * * @async * @param key Redis key name * @returns */ get(key: string): Promise<string | null>; /** * Sets the value for a Redis key * * @async * @param key Redis key name * @param value Value to be stored in Redis * @param options Options as mentioned by 'set' method of Redis package * @returns */ set(key: string, value: string, options?: SetOptions): Promise<string | null>; /** * Sets the expiry of Redis key in seconds * * @async * @param key Redis key name * @param ttlInSecs Redis key expiry in seconds * @param [mode] Conditions when expiry should be set * @returns */ expire(key: string, ttlInSecs: number, mode?: 'NX' | 'XX' | 'GT' | 'LT'): Promise<boolean>; /** * Sets the expiry of Redis key to Unix timestamp * * @async * @param key Redis key name * @param ttlInSecs Redis key expiry in seconds * @param [mode] Conditions when expiry should be set * @returns */ expireAt(key: string, unixInSecs: number, mode?: 'NX' | 'XX' | 'GT' | 'LT'): Promise<boolean>; /** * Get the expiry of Redis key as Unix timestamp * * @async * @param key Redis key name * @returns */ expireTime(key: string): Promise<number>; /** * Get the expiry of Redis key in seconds. * * @async * @param key Redis key name * @returns */ ttl(key: string): Promise<number>; /** * Gets the value of a Redis key and sets the expiry of Redis key * * @async * @param key Redis key name * @param ttlInSecs Redis key expiry in seconds * @returns */ getAndExpire(key: string, ttlInSecs: number): Promise<string | null>; /** * Sets the value of a Redis key and sets the expiry of Redis key * * @async * @param key Redis key name * @param value Value to be stored in Redis * @param ttlInSecs Redis key expiry in seconds * @param options Options as mentioned by 'set' method of Redis package * @returns */ setAndExpire(key: string, value: string, ttlInSecs: number, options?: SetOptions): Promise<string | null>; /** * Deletes a Redis key or keys * * @async * @param keys Redis key name or Array of Redis key names * @returns */ del(keys: string | string[]): Promise<number>; /** * Gets all Redis keys for a given key pattern * * @async * @param pattern Redis key pattern * @returns */ keys(pattern: string): Promise<string[]>; /** * Deletes all Redis keys for a given key pattern * * @async * @param pattern Redis key pattern * @returns */ delByPattern(pattern: string): Promise<number>; /** * Increments the value of a Redis key * * @async * @param key Redis key name * @param [value=0] Redis value to be incremented by * @returns */ incrBy(key: string, value?: number): Promise<number>; /** * Increments the value of a Redis key and sets the expiry of Redis key * * @async * @param key Redis key name * @param [value=0] Redis value to be incremented by * @param ttlInSecs Redis key expiry in seconds * @returns */ incrByAndExpire(key: string, value: number | undefined, ttlInSecs: number): Promise<number>; /** * Decrements the value of a Redis key * * @async * @param key Redis key name * @param [value=0] Redis value to be decremented by * @returns */ decrBy(key: string, value?: number): Promise<number>; /** * Decrements the value of a Redis key and sets the expiry of Redis key * * @async * @param key Redis key name * @param [value=0] Redis value to be decremented by * @param ttlInSecs Redis key expiry in seconds * @returns */ decrByAndExpire(key: string, value: number | undefined, ttlInSecs: number): Promise<number>; /** * Checks if Redis keys exist or not * * @async * @param [keys=[]] Array of Redis key names * @returns */ exists(keys: string[]): Promise<number>; /** * Creates or modifies the value of a field in a hash * * @async * @param key Redis key name of hash * @param field Hash field * @param value Hash value * @returns */ hSet(key: string, field: string, value: string | number): Promise<number>; /** * Sets the value of a field in a hash only when the field doesn't exist * * @async * @param key Redis key name of hash * @param field Hash field * @param value Hash value * @returns */ hSetNX(key: string, field: string, value: string): Promise<boolean>; /** * Increments the integer value of a field in a hash by a number. Uses 0 as initial value if the field doesn't exist * * @async * @param key Redis key name of hash * @param field Hash field * @param [value=0] Value to be incremented by * @returns */ hIncrBy(key: string, field: string, value?: number): Promise<number>; /** * Returns the value of a field in a hash * * @async * @param key Redis key name of hash * @param field Hash field * @returns */ hGet(key: string, field: string): Promise<string | undefined>; /** * Returns all fields and values in a hash * * @async * @param key Redis key name of hash * @returns */ hGetAll(key: string): Promise<{ [x: string]: string; }>; /** * Returns all fields in a hash * * @async * @param key Redis key name of hash * @returns */ hKeys(key: string): Promise<string[]>; /** * Returns all values in a hash * * @async * @param key Redis key name of hash * @returns */ hVals(key: string): Promise<string[]>; /** * Returns the number of fields in a hash * * @async * @param key Redis key name of hash * @returns */ hLen(key: string): Promise<number>; /** * Returns the length of the value of a field * * @async * @param key Redis key name of hash * @param field Hash field * @returns */ hStrLen(key: string, field: string): Promise<number>; /** * Deletes one or more fields and their values from a hash. Deletes the hash if no fields remain * * @async * @param key Redis key name of hash * @param field Hash field * @returns */ hDel(key: string, field: string): Promise<number>; /** * Determines whether a field exists in a hash * * @async * @param key Redis key name of hash * @param field Hash field * @returns */ hExists(key: string, field: string): Promise<boolean>; /** * Returns the values of all fields in a hash * * @async * @param key Redis key name of hash * @param fields Hash fields * @returns */ hmGet(key: string, fields: string[]): Promise<string[]>; }