Skip to content

TypeScript Redis abstraction we use in backend at MyUnisoft

License

Notifications You must be signed in to change notification settings

MyUnisoft/Redis

Repository files navigation

Redis

MyUnisoft Redis package.

npm version license size

🚧 Requirements

  • Node.js version 18 or higher.
  • Docker (for running tests).

Tip

If you ever want to setup a local instance of Redis,
you can follow the Redis documentation for Mac,
or, you can use Memurai for Windows.

🚀 Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @myunisoft/redis
# or
$ yarn add @myunisoft/redis

📜 API

Connection

This class is used to instantiate and close connection to Redis. You need to re-use this instance in every other classes.

type ConnectionOptions = Partial<RedisOptions> & {
  port?: number;
  host?: string;
  attempt?: number;
  disconnectionTimeout?: number;
};

📚 Usage

import assert from "assert";
import {
  Connection
} from "@myunisoft/redis";

const connection = new Connection();

try {
  (await connection.initialize()).unwrap();
}
finally {
  await connection.close();
}

initialize(): Promise< Result< null, AssertConnectionErr > >

type AssertConnectionErr = "Failed at initializing the Redis connection";

This function either return void, or an error;


getConnectionPerf(): Promise< GetConnectionPerfResponse >

type GetConnectionPerfResponse = {
  isAlive: boolean;
  perf: number;
};

This function is used to check Redis connection state.

const instancePerf = await getConnectionPerf();

if (!instancePerf.isAlive) {
  console.log(instancePerf.isAlive);
  console.log(instancePerf.perf);
}

console.log(instancePerf.isAlive);
console.log(instancePerf.perf);

closeRedis(forceExit: boolean = false): Promise< Result< null, CloseErr > >

type AssertDisconnectionErr = AssertDisconnectionErrorMessage;
type CloseErr = AssertDisconnectionErrorMessage | "Redis connection already closed";

This function is used to close the Redis connection related to the instance.


The package also exports many classes listed below.

Contributors ✨

All Contributors

Thanks goes to these wonderful people (emoji key):

Nicolas Hallaert
Nicolas Hallaert

💻 ⚠️ 📖 👀
PierreDemailly
PierreDemailly

👀 ⚠️
Thomas.G
Thomas.G

💻 ⚠️ 📖 👀

This project follows the all-contributors specification. Contributions of any kind welcome!