@kurai-io/redis
Version:
Schema-based abstraction layer for redis database interactions
93 lines (92 loc) • 3.38 kB
TypeScript
import { RedisClientType } from 'redis';
import { AnyRedisClient, ClientConfiguration, RedisModelOptions } from '../types';
/**
* Executor class that handles serialization, compression,
* and interactions with Redis for a given schema template.
*/
declare class RedisModel<T> {
/** Original redis client */
protected readonly client: RedisClientType<any, any, any, any, any>;
protected readonly options: Required<Omit<RedisModelOptions, "ttl">> & {
ttl?: number;
};
protected readonly randomKeyBytesCount: number;
/**
* @param client redis client instance
* @param options schema options
* @param config
*/
constructor(client: AnyRedisClient, options: RedisModelOptions, config?: Partial<ClientConfiguration>);
/**
* Compresses and writes the specified value to Redis.
*
* @param key key under which the value will be stored (without namespace).
* @param value value to store.
* @returns key used in Redis (without namespace).
*/
set(key: string, value: T): Promise<string>;
/**
* Expire key after a specific ttl or date
*/
expire(key: string, ttl: number | Date, mode?: "NX" | "XX" | "GT" | "LT"): Promise<void>;
/**
* Retrieves data from Redis and validates it against the template.
*
* @param key key in Redis (without a namespace).
* @param force if true, throws an error instead of returning null when
* data is missing or fails validation.
* @returns parsed data matching the template, or null if not in force mode.
*/
get<F extends boolean | undefined>(key: string, force?: F): Promise<F extends true ? T : T | null>;
/**
* Deletes the specified key from Redis.
*
* @param key key to delete (without a namespace).
* @returns result of the deletion operation.
*/
delete(key: string): Promise<number | `${number}`>;
/**
* Retrieves data from Redis and executes a callback if the data exists
* and matches the template.
*
* @param key key to retrieve.
* @param callback function to execute with the retrieved payload.
* @returns callback result or null if no data.
*/
with<R extends any>(key: string, callback: (payload: T) => R): Promise<R | null>;
/**
* Provides a method to set a value with an auto-generated random key.
*/
get randomKey(): {
/**
* Compresses and writes the value under a random key.
*
* @param value value to store.
* @returns generated a Redis key (without a namespace).
*/
set(value: T): Promise<string>;
};
private fullKey;
private waitWhenReady;
}
/**
* Redis client abstraction
*/
export default class RedisClient {
#private;
readonly client: RedisClientType<any, any, any, any, any>;
constructor(client: RedisClientType<any, any, any, any, any>, clientConfig?: Partial<ClientConfiguration>);
/**
* Defines a new data schema with the given template and options.
*
* @param options schema options.
* @returns a RedisSchema instance for data operations.
*/
model<T>(options: RedisModelOptions): RedisModel<T>;
/**
* Disconnects and cleans up the Redis client.
*/
destroy(): void;
connect(): Promise<RedisClientType<any, any, any, any, any>>;
}
export {};