7.69.4 • Published 6 days ago

@tsed/ioredis v7.69.4

Weekly downloads
-
License
MIT
Repository
github
Last release
6 days ago

Build & Release PR Welcome npm version semantic-release code style: prettier github opencollective

A package of Ts.ED framework. See website: https://tsed.io/tutorials/redis.html

Features

Currently, @tsed/ioredis allows you:

  • Configure one or more Redis database connections via the @Configuration configuration.
  • Share redis connection with @tsed/platform-cache.
  • Support classic Redis connection and Cluster connection.
  • Inject connection to another service.
  • Mock connection for unit/integration test.

Installation

npm install --save @tsed/ioredis ioredis ioredis-mock

Minimal supported ioredis version is v5+

Create connection

Create a new RedisConnection.ts file in your project:

import Redis from "ioredis";
import {registerConnectionProvider} from "@tsed/ioredis";

export const REDIS_CONNECTION = Symbol.for("REDIS_CONNECTION");
export type REDIS_CONNECTION = Redis;

registerConnectionProvider({
  provide: REDIS_CONNECTION,
  name: "default"
});

registerConnectionProvider create automatically an injectable RedisConnection

Then, edit your Server.ts:

import {Configuration} from "@tsed/di";
import "@tsed/platform-cache"; // add this module if you want to use cache
import "@tsed/ioredis";

@Configuration({
  ioredis: [
    {
      name: "default",
      // share the redis connection with @tsed/platform-cache
      cache: true

      // redis options
      // host: "localhost",
      // port: 6379,
      // username: "...",
      // password: "...",
      // tls: false,
      // db: 0
    }
  ],
  // cache options
  cache: {
    ttl: 300
  }
})
class MyModule {}

And finally, use the connection in your services:

import {Injectable} from "@tsed/di";

@Injectable()
export class ClientRepository {
  @Inject(REDIS_CONNECTION)
  protected connection: REDIS_CONNECTION; // ioredis instance

  async keys() {
    return this.connection.keys("clients:*");
  }
}

See ioredis documentation for more details.

Cluster configuration

import {Configuration} from "@tsed/di";
import "@tsed/platform-cache"; // add this module if you want to use cache
import "@tsed/ioredis";

@Configuration({
  ioredis: [
    {
      name: "default",
      // share the redis connection with @tsed/platform-cache
      cache: true,
      // cluster options
      nodes: ["..."],
      // other cluster options
      scaleReads: "all",
      maxRedirections: 16,
      retryDelayOnTryAgain: 100,
      retryDelayOnFailover: 200,
      retryDelayOnClusterDown: 1000,
      slotsRefreshTimeout: 15000,
      slotsRefreshInterval: 20000,
      enableOfflineQueue: true,
      enableReadyCheck: true,
      redisOptions: {
        noDelay: true,
        connectTimeout: 15000,
        autoResendUnfulfilledCommands: true,
        maxRetriesPerRequest: 5,
        enableAutoPipelining: true,
        autoPipeliningIgnoredCommands: ["scan"]
      }
      //
    }
  ],
  // cache options
  cache: {
    ttl: 300
  }
})
class MyModule {}

Testing

Ts.ED provides a utility that allows you to test a service that consumes a Redis connection. This use relies on the awesome ioredis-mock module.

Here is a class that consume a redis connection:

import {v4} from "uuid";
import {Injectable} from "@tsed/di";
import {serialize, deserialize} from "@tsed/json-mapper";
import {REDIS_CONNECTION} from "./RedisConnection";
import {ClientModel} from "./ClientModel";

@Injectable()
export class ClientRepository {
  @Inject(REDIS_CONNECTION)
  protected connection: REDIS_CONNECTION; // ioredis instance

  async get(id: string) {
    const raw = await this.connection.get("clients:" + id);

    if (!raw) {
      return undefined;
    }

    return deserialize(JSON.parse(raw), {type: ClientModel});
  }

  async save(client: ClientModel) {
    client.id = client.id || v4();

    this.connection.set("clients:" + client.id, JSON.stringify(serialize(client)));

    return client;
  }
}

The ClientModel:

import {Schema} from "@tsed/schema";

export class ClientModel {
  @Property()
  id: string;

  @Property()
  name: string;
}

And his test:

import {ClientRepository} from "./ClientRepository";
import {REDIS_CONNECTION} from "./RedisConnection";
import {ClientModel} from "./ClientModel";

describe("IORedisTest", () => {
  beforeEach(() => IORedisTest.create()); // create a new sandbox with ioredis-mock connection
  afterEach(() => IORedisTest.reset());

  it("should return nothing", async () => {
    const service = IORedisTest.get<MyRepository>(MyRepository);

    const client = await service.get("uid");

    expect(client).toEqual(undefined);
  });

  it("should return all keys", async () => {
    const service = IORedisTest.get<MyRepository>(MyRepository);
    const client = new ClientModel();
    client.name = "name";

    const newClient = await service.save(client);

    expect(newClient.id).toBeInstanceOf(String);
    expect(newClient.name).toEqual("name");

    const clientFound = await service.get(newClient.id);

    expect(clientFound).toBeInstanceOf(ClientModel);
    expect(clientFound.id).toEqual(newClient.id);
    expect(clientFound.name).toEqual("name");
  });
});

Contributors

Please read contributing guidelines here

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

License

The MIT License (MIT)

Copyright (c) 2016 - 2022 Romain Lenzotti

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

7.69.4

6 days ago

7.69.3

8 days ago

7.69.2

11 days ago

7.69.1

13 days ago

7.69.0

13 days ago

7.68.6

14 days ago

7.68.4

18 days ago

7.68.5

18 days ago

7.68.3

24 days ago

7.68.2

25 days ago

7.68.0

26 days ago

7.68.1

26 days ago

7.67.8

28 days ago

7.67.7

28 days ago

7.67.6

1 month ago

7.67.5

1 month ago

7.67.4

2 months ago

7.67.3

2 months ago

7.67.1

2 months ago

7.67.2

2 months ago

7.67.0

2 months ago

7.66.0

2 months ago

7.65.0

2 months ago

7.63.3

2 months ago

7.64.0

2 months ago

7.63.2

2 months ago

7.63.1

2 months ago

7.63.0

2 months ago

7.62.3

3 months ago

7.62.2

3 months ago

7.62.1

3 months ago

7.62.0

3 months ago

7.61.2

3 months ago

7.61.1

3 months ago

7.61.0

4 months ago

7.60.1

4 months ago

7.60.0

4 months ago

7.59.0

4 months ago

7.59.1

4 months ago

7.58.0

4 months ago

7.57.1

4 months ago

7.57.0

4 months ago

7.56.0

4 months ago

7.55.0

5 months ago

7.54.0

5 months ago

7.53.0

5 months ago

7.52.0

6 months ago

7.51.2

6 months ago

7.51.1

6 months ago

7.51.0

6 months ago

7.50.0

6 months ago

7.49.0

6 months ago

7.40.0

7 months ago

7.48.0

6 months ago

7.44.0

6 months ago

7.44.1

6 months ago

7.35.0-rc.1

9 months ago

7.36.1

8 months ago

7.36.0

8 months ago

7.36.3

8 months ago

7.36.2

8 months ago

7.32.0

10 months ago

7.36.8

8 months ago

7.36.5

8 months ago

7.36.7

8 months ago

7.36.6

8 months ago

7.47.0

6 months ago

7.47.1

6 months ago

7.43.0

7 months ago

7.43.1

7 months ago

7.43.2

6 months ago

7.31.0

11 months ago

7.35.1

9 months ago

7.39.0

7 months ago

7.35.0

9 months ago

7.39.1

7 months ago

7.42.0

7 months ago

7.46.0

6 months ago

7.36.0-rc.2

8 months ago

7.36.0-rc.3

8 months ago

7.36.0-rc.1

8 months ago

7.34.3

9 months ago

7.34.2

10 months ago

7.38.1

7 months ago

7.36.0-rc.4

8 months ago

7.34.5

9 months ago

7.38.0

7 months ago

7.36.0-rc.5

8 months ago

7.34.4

9 months ago

7.30.3

11 months ago

7.30.2

11 months ago

7.34.1

10 months ago

7.34.0

10 months ago

7.34.7

9 months ago

7.34.6

9 months ago

7.34.9

9 months ago

7.34.8

9 months ago

7.41.0

7 months ago

7.41.1

7 months ago

7.41.2

7 months ago

7.45.0

6 months ago

7.35.0-beta.4

9 months ago

7.35.0-beta.5

9 months ago

7.35.0-beta.1

9 months ago

7.35.0-beta.2

9 months ago

7.35.0-beta.3

9 months ago

7.37.0

7 months ago

7.33.4

10 months ago

7.33.3

10 months ago

7.33.0

10 months ago

7.33.2

10 months ago

7.33.1

10 months ago

7.30.1

11 months ago

7.31.0-rc.2

11 months ago

7.31.0-rc.1

11 months ago

7.30.0

11 months ago

7.29.1

12 months ago

7.29.0

1 year ago

7.28.0

1 year ago

7.25.0

1 year ago

7.27.1

1 year ago

7.27.0

1 year ago

7.23.1

1 year ago

7.27.2

1 year ago

7.26.0

1 year ago

7.24.1

1 year ago

7.26.2

1 year ago

7.26.1

1 year ago

7.24.0

1 year ago

7.26.3

1 year ago

7.18.4

1 year ago

7.18.3-rc.1

1 year ago

7.18.2

1 year ago

7.18.3

1 year ago

7.18.1

1 year ago

7.21.1

1 year ago

7.21.0

1 year ago

7.23.0

1 year ago

7.19.0

1 year ago

7.20.0

1 year ago

7.22.0

1 year ago

7.22.2

1 year ago

7.22.1

1 year ago

7.18.0

1 year ago

7.16.2

1 year ago

7.15.1

1 year ago

7.17.0

1 year ago

7.15.0

1 year ago

7.16.0

1 year ago

7.16.1

1 year ago

7.14.2

1 year ago

7.13.4

1 year ago

7.13.7

1 year ago

7.13.5

1 year ago

7.13.6

1 year ago

7.14.0

1 year ago

7.14.1

1 year ago

7.10.0-rc.2

1 year ago

7.10.0-rc.3

1 year ago

7.10.0-rc.1

1 year ago

7.10.0-rc.6

1 year ago

7.10.0-rc.7

1 year ago

7.10.0-rc.4

1 year ago

7.10.0-rc.5

1 year ago

7.10.0-rc.8

1 year ago

7.8.3

1 year ago

7.8.2

1 year ago

7.8.1

1 year ago

7.13.3

1 year ago

7.13.1

1 year ago

7.13.2

1 year ago

7.13.0

1 year ago

7.11.0

1 year ago

7.9.0

1 year ago

7.12.0

1 year ago

7.10.0

1 year ago

7.10.1

1 year ago

7.0.0

2 years ago

7.4.0

2 years ago

7.3.1

2 years ago

7.2.2

2 years ago

7.3.0

2 years ago

7.2.1

2 years ago

7.2.0

2 years ago

7.1.1

2 years ago

7.0.2

2 years ago

7.1.0

2 years ago

7.0.1

2 years ago

7.8.0

1 year ago

7.7.1

1 year ago

7.5.3

1 year ago

7.7.0

1 year ago

7.5.2

1 year ago

7.4.3

2 years ago

7.6.0

1 year ago

7.5.1

2 years ago

7.4.2

2 years ago

7.2.4

2 years ago

7.5.0

2 years ago

7.4.1

2 years ago

7.2.3

2 years ago

7.0.0-rc.7

2 years ago

6.133.1

2 years ago

6.133.0

2 years ago

7.0.0-rc.6

2 years ago

6.132.1

2 years ago

6.132.0

2 years ago

6.131.1

2 years ago

6.131.0

2 years ago

6.130.0

2 years ago

7.0.0-rc.5

2 years ago

6.129.0

2 years ago