UNPKG

@kurai-io/redis

Version:

Schema-based abstraction layer for redis database interactions

108 lines (107 loc) 4.08 kB
import { ObjectSchema } from '@sigiljs/seal'; import { InferSchema } from '@sigiljs/seal/types'; import { RedisClientType } from 'redis'; import { AnyRedisClient, ClientConfiguration, RedisModelOptions, RedisModelTemplate } from '../types'; /** * Executor class that handles serialization, compression, * and interactions with Redis for a given schema template. */ declare class RedisModel<T extends RedisModelTemplate> { /** Original redis client */ protected readonly client: RedisClientType<any, any, any, any, any>; protected readonly template: T; protected readonly options: Required<Omit<RedisModelOptions, "ttl">> & { ttl?: number; }; protected readonly randomKeyBytesCount: number; /** * @param client redis client instance * @param template seal schema template * @param options schema options * @param config */ constructor(client: AnyRedisClient, template: T, 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: InferSchema<T>): Promise<string>; /** * Expire key after 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 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 ? InferSchema<T> : InferSchema<T> | null>; /** * Deletes the specified key from Redis. * * @param key key to delete (without 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: InferSchema<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 Redis key (without namespace). */ set(value: InferSchema<T>): Promise<string>; }; private fullKey; private serialize; 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>); /** * Creates a new object schema template compatible with Seal. * * @param template schema definition. * @returns an ObjectSchema based on the provided template. */ template<T extends { [key: string]: RedisModelTemplate; }>(template: T): ObjectSchema<T>; /** * Defines a new data schema with the given template and options. * * @param template redis schema template. * @param options schema options. * @returns a RedisSchema instance for data operations. */ model<T extends RedisModelTemplate>(template: T, options?: RedisModelOptions): RedisModel<T>; /** * Disconnects and cleans up the Redis client. */ destroy(): void; connect(): Promise<RedisClientType<any, any, any, any, any>>; } export {};