@inderes/redis-client
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Description

The RedisClient library is a TypeScript utility designed to simplify interactions with Redis databases. It provides a streamlined interface for setting, getting, and deleting data in Redis, with support for JSON data handling and schema validation using Zod. The library also integrates custom logging capabilities to aid in debugging and operational monitoring.

Features

  • Easy configuration and initialization of Redis connections.
  • Support for JSON data serialization and deserialization.
  • Schema validation with Zod for robust data integrity.
  • Flexible data expiry settings for various time-to-live requirements.

Initializing the Client

First, import and initialize the RedisClient with your Redis server configuration and logger instance.

import Logger from "@inderes/logger";
import { RedisClient, RedisClientOptions } from "./path/to/redisClient";

const options: RedisClientOptions = {
  host: "localhost",
  port: 6379,
  logger: new Logger(),
};

const client = new RedisClient(options);

Setting Data

To set data in Redis, use the set method. You can specify a key, value, and optionally a schema for validation and an expiry time.

import { z } from "zod";

const testObject = {
  name: "John Doe",
  age: 30,
};

await client.set({
  key: "user:1",
  value: testObject,
  expiry: "1h", // Data expires in 1 hour
});

const mySchema = z.object({
  name: z.string(),
  age: z.number(),
});

await redisClient.set({ key: "mySchema", value: testObject, schema: mySchema });

Getting Data

Retrieve data from Redis using the get method. You can also specify a schema to validate the data upon retrieval.

const userData = await client.get({
  key: "user:1",
  schema: userSchema, // Assuming userSchema is a Zod schema
});

Deleting Data

Remove data from Redis by specifying the key in the delete method.

const deletedCount = await client.delete({ key: "user:1" });

Readme

Keywords

none

Package Sidebar

Install

npm i @inderes/redis-client

Weekly Downloads

0

Version

1.0.1

License

none

Unpacked Size

14.2 kB

Total Files

6

Last publish

Collaborators

  • pitkane
  • teoraunio
  • cahva