@xnestjs/ioredis 1.2.2 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,8 +3,10 @@ var RedisCoreModule_1;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.RedisCoreModule = void 0;
5
5
  const tslib_1 = require("tslib");
6
+ const node_process_1 = tslib_1.__importDefault(require("node:process"));
6
7
  const common_1 = require("@nestjs/common");
7
8
  const core_1 = require("@nestjs/core");
9
+ const ansi_colors_1 = tslib_1.__importDefault(require("ansi-colors"));
8
10
  const crypto = tslib_1.__importStar(require("crypto"));
9
11
  const ioredis_1 = tslib_1.__importStar(require("ioredis"));
10
12
  const redis_constants_js_1 = require("./redis.constants.js");
@@ -89,23 +91,39 @@ let RedisCoreModule = RedisCoreModule_1 = class RedisCoreModule {
89
91
  if (db > 0)
90
92
  options.db = db;
91
93
  }
92
- const standalone = new ioredis_1.default(options);
93
- client = new redis_client_js_1.RedisClient({ standalone });
94
- }
95
- if (!options.lazyConnect) {
96
- await new Promise((resolve, reject) => {
97
- client.redis.once('ready', () => {
98
- client.redis.removeListener('error', reject);
99
- resolve();
100
- });
101
- client.redis.once('error', e => {
102
- client.redis.removeListener('ready', resolve);
103
- reject(e);
104
- });
94
+ const standalone = new ioredis_1.default({
95
+ ...options,
96
+ lazyConnect: true,
105
97
  });
98
+ client = new redis_client_js_1.RedisClient({ standalone });
106
99
  }
107
100
  return client;
108
101
  }
102
+ async onApplicationBootstrap() {
103
+ const opts = this.options;
104
+ const logger = node_process_1.default.env.NODE_ENV === 'test' ? undefined : opts.logger;
105
+ if (!opts.lazyConnect) {
106
+ const isCluster = (0, utils_js_1.isClusterOptions)(opts);
107
+ const hosts = isCluster
108
+ ? opts.nodes
109
+ .map(x => (typeof x === 'object' ? x.host + ':' + x.port : typeof x === 'number' ? 'localhost:' + x : x))
110
+ .join(', ')
111
+ : opts.host;
112
+ if (hosts) {
113
+ logger?.log('Connecting to redis at ' + ansi_colors_1.default.blue(hosts));
114
+ common_1.Logger.flush();
115
+ const client = this.moduleRef.get(this.options.token || redis_client_js_1.RedisClient);
116
+ try {
117
+ await client.redis.connect();
118
+ await client.redis.ping();
119
+ }
120
+ catch (e) {
121
+ logger?.error('Redis connection failed: ' + e.message);
122
+ throw e;
123
+ }
124
+ }
125
+ }
126
+ }
109
127
  async onApplicationShutdown() {
110
128
  try {
111
129
  const client = this.moduleRef.get(this.options.token || redis_client_js_1.RedisClient);
@@ -1,7 +1,9 @@
1
1
  var RedisCoreModule_1;
2
2
  import { __decorate, __metadata, __param } from "tslib";
3
- import { Global, Inject, Module } from '@nestjs/common';
3
+ import process from 'node:process';
4
+ import { Global, Inject, Logger, Module, } from '@nestjs/common';
4
5
  import { ModuleRef } from '@nestjs/core';
6
+ import colors from 'ansi-colors';
5
7
  import * as crypto from 'crypto';
6
8
  import Redis, { Cluster } from 'ioredis';
7
9
  import { IOREDIS_MODULE_OPTIONS, IOREDIS_MODULE_TOKEN } from './redis.constants.js';
@@ -86,23 +88,39 @@ let RedisCoreModule = RedisCoreModule_1 = class RedisCoreModule {
86
88
  if (db > 0)
87
89
  options.db = db;
88
90
  }
89
- const standalone = new Redis(options);
90
- client = new RedisClient({ standalone });
91
- }
92
- if (!options.lazyConnect) {
93
- await new Promise((resolve, reject) => {
94
- client.redis.once('ready', () => {
95
- client.redis.removeListener('error', reject);
96
- resolve();
97
- });
98
- client.redis.once('error', e => {
99
- client.redis.removeListener('ready', resolve);
100
- reject(e);
101
- });
91
+ const standalone = new Redis({
92
+ ...options,
93
+ lazyConnect: true,
102
94
  });
95
+ client = new RedisClient({ standalone });
103
96
  }
104
97
  return client;
105
98
  }
99
+ async onApplicationBootstrap() {
100
+ const opts = this.options;
101
+ const logger = process.env.NODE_ENV === 'test' ? undefined : opts.logger;
102
+ if (!opts.lazyConnect) {
103
+ const isCluster = isClusterOptions(opts);
104
+ const hosts = isCluster
105
+ ? opts.nodes
106
+ .map(x => (typeof x === 'object' ? x.host + ':' + x.port : typeof x === 'number' ? 'localhost:' + x : x))
107
+ .join(', ')
108
+ : opts.host;
109
+ if (hosts) {
110
+ logger?.log('Connecting to redis at ' + colors.blue(hosts));
111
+ Logger.flush();
112
+ const client = this.moduleRef.get(this.options.token || RedisClient);
113
+ try {
114
+ await client.redis.connect();
115
+ await client.redis.ping();
116
+ }
117
+ catch (e) {
118
+ logger?.error('Redis connection failed: ' + e.message);
119
+ throw e;
120
+ }
121
+ }
122
+ }
123
+ }
106
124
  async onApplicationShutdown() {
107
125
  try {
108
126
  const client = this.moduleRef.get(this.options.token || RedisClient);
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@xnestjs/ioredis",
3
- "version": "1.2.2",
3
+ "version": "1.2.5",
4
4
  "description": "NestJS extension library for ioredis",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
7
7
  "dependencies": {
8
+ "ansi-colors": "^4.1.3",
8
9
  "tslib": "^2.8.1"
9
10
  },
10
11
  "peerDependencies": {
@@ -1,12 +1,13 @@
1
- import { DynamicModule, OnApplicationShutdown } from '@nestjs/common';
1
+ import { DynamicModule, OnApplicationBootstrap, OnApplicationShutdown } from '@nestjs/common';
2
2
  import { ModuleRef } from '@nestjs/core';
3
3
  import { RedisClientAsyncOptions, RedisClientOptions, RedisClusterAsyncOptions, RedisClusterOptions } from './redis.interface.js';
4
- export declare class RedisCoreModule implements OnApplicationShutdown {
4
+ export declare class RedisCoreModule implements OnApplicationBootstrap, OnApplicationShutdown {
5
5
  private readonly options;
6
6
  private readonly moduleRef;
7
7
  constructor(options: RedisClientOptions | RedisClusterOptions, moduleRef: ModuleRef);
8
8
  static forRoot(options: RedisClientOptions | RedisClusterOptions): DynamicModule;
9
9
  static forRootAsync(asyncOptions: RedisClientAsyncOptions | RedisClusterAsyncOptions): DynamicModule;
10
10
  private static _createClient;
11
+ onApplicationBootstrap(): Promise<void>;
11
12
  onApplicationShutdown(): Promise<void>;
12
13
  }
@@ -1,3 +1,4 @@
1
+ import { Logger } from '@nestjs/common';
1
2
  import type { ModuleMetadata } from '@nestjs/common/interfaces';
2
3
  import type { RedisOptions } from 'ioredis';
3
4
  import type { ClusterOptions } from 'ioredis/built/cluster/ClusterOptions';
@@ -7,12 +8,14 @@ export interface RedisClientOptions extends RedisOptions {
7
8
  * Injection token
8
9
  */
9
10
  token?: any;
11
+ logger?: Logger;
10
12
  }
11
13
  export interface RedisClusterOptions extends ClusterOptions {
12
14
  /**
13
15
  * Injection token
14
16
  */
15
17
  token?: any;
18
+ logger?: Logger;
16
19
  nodes: ClusterNode[];
17
20
  }
18
21
  export interface RedisClientAsyncOptions extends Pick<ModuleMetadata, 'imports'> {