From eec8f1842743f41bdb64d3fe38b9f6473cfe4f5d Mon Sep 17 00:00:00 2001 From: jiayi <690405541@qq.com> Date: Sun, 9 Oct 2022 11:28:13 +0800 Subject: [PATCH 1/5] feat: Add connection error handle --- lib/interfaces/mongoose-options.interface.ts | 3 ++- lib/mongoose-core.module.ts | 26 ++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/interfaces/mongoose-options.interface.ts b/lib/interfaces/mongoose-options.interface.ts index f3e84e9b..b5d5e14a 100644 --- a/lib/interfaces/mongoose-options.interface.ts +++ b/lib/interfaces/mongoose-options.interface.ts @@ -1,5 +1,5 @@ import { ModuleMetadata, Type } from '@nestjs/common'; -import { ConnectOptions } from 'mongoose'; +import { ConnectOptions, MongooseError } from 'mongoose'; export interface MongooseModuleOptions extends ConnectOptions, @@ -9,6 +9,7 @@ export interface MongooseModuleOptions retryDelay?: number; connectionName?: string; connectionFactory?: (connection: any, name: string) => any; + connectionError?: (error: MongooseError) => void; } export interface MongooseOptionsFactory { diff --git a/lib/mongoose-core.module.ts b/lib/mongoose-core.module.ts index 77f311f6..e8a0f67c 100644 --- a/lib/mongoose-core.module.ts +++ b/lib/mongoose-core.module.ts @@ -10,6 +10,7 @@ import { import { ModuleRef } from '@nestjs/core'; import * as mongoose from 'mongoose'; import { defer, lastValueFrom } from 'rxjs'; +import { catchError } from 'rxjs/operators'; import { getConnectionToken, handleRetry } from './common/mongoose.utils'; import { MongooseModuleAsyncOptions, @@ -39,11 +40,16 @@ export class MongooseCoreModule implements OnApplicationShutdown { retryDelay, connectionName, connectionFactory, + connectionError, ...mongooseOptions } = options; const mongooseConnectionFactory = connectionFactory || ((connection) => connection); + + const mongooseConnectionError = + connectionError || ((error) => {}); + const mongooseConnectionName = getConnectionToken(connectionName); const mongooseConnectionNameProvider = { @@ -59,7 +65,13 @@ export class MongooseCoreModule implements OnApplicationShutdown { await mongoose.createConnection(uri, mongooseOptions).asPromise(), mongooseConnectionName, ), - ).pipe(handleRetry(retryAttempts, retryDelay)), + ).pipe( + handleRetry(retryAttempts, retryDelay), + catchError((error) => { + mongooseConnectionError(error); + throw error; + }), + ), ), }; return { @@ -87,12 +99,16 @@ export class MongooseCoreModule implements OnApplicationShutdown { retryDelay, uri, connectionFactory, + connectionError, ...mongooseOptions } = mongooseModuleOptions; const mongooseConnectionFactory = connectionFactory || ((connection) => connection); + const mongooseConnectionError = + connectionError || ((error) => {}); + return await lastValueFrom( defer(async () => mongooseConnectionFactory( @@ -101,7 +117,13 @@ export class MongooseCoreModule implements OnApplicationShutdown { .asPromise(), mongooseConnectionName, ), - ).pipe(handleRetry(retryAttempts, retryDelay)), + ).pipe( + handleRetry(retryAttempts, retryDelay), + catchError((error) => { + mongooseConnectionError(error); + throw error; + }), + ), ); }, inject: [MONGOOSE_MODULE_OPTIONS], From fa2491ec4f8f08057581ecb53b23299c53657b2b Mon Sep 17 00:00:00 2001 From: jiayi <690405541@qq.com> Date: Thu, 27 Oct 2022 09:17:18 +0800 Subject: [PATCH 2/5] Update lib/mongoose-core.module.ts Return MongooseError Co-authored-by: Kamil Mysliwiec --- lib/mongoose-core.module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongoose-core.module.ts b/lib/mongoose-core.module.ts index e8a0f67c..0b53daee 100644 --- a/lib/mongoose-core.module.ts +++ b/lib/mongoose-core.module.ts @@ -48,7 +48,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { connectionFactory || ((connection) => connection); const mongooseConnectionError = - connectionError || ((error) => {}); + connectionError || ((error) => error); const mongooseConnectionName = getConnectionToken(connectionName); From 923ca93529a3987a6c39a358836dc62b6e9bc857 Mon Sep 17 00:00:00 2001 From: jiayi <690405541@qq.com> Date: Thu, 27 Oct 2022 09:21:10 +0800 Subject: [PATCH 3/5] perf: Return MongooseError --- lib/interfaces/mongoose-options.interface.ts | 2 +- lib/mongoose-core.module.ts | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/interfaces/mongoose-options.interface.ts b/lib/interfaces/mongoose-options.interface.ts index b5d5e14a..5db9b06d 100644 --- a/lib/interfaces/mongoose-options.interface.ts +++ b/lib/interfaces/mongoose-options.interface.ts @@ -9,7 +9,7 @@ export interface MongooseModuleOptions retryDelay?: number; connectionName?: string; connectionFactory?: (connection: any, name: string) => any; - connectionError?: (error: MongooseError) => void; + connectionError?: (error: MongooseError) => MongooseError; } export interface MongooseOptionsFactory { diff --git a/lib/mongoose-core.module.ts b/lib/mongoose-core.module.ts index 0b53daee..8565cbbf 100644 --- a/lib/mongoose-core.module.ts +++ b/lib/mongoose-core.module.ts @@ -68,8 +68,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { ).pipe( handleRetry(retryAttempts, retryDelay), catchError((error) => { - mongooseConnectionError(error); - throw error; + throw mongooseConnectionError(error); }), ), ), @@ -107,7 +106,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { connectionFactory || ((connection) => connection); const mongooseConnectionError = - connectionError || ((error) => {}); + connectionError || ((error) => error); return await lastValueFrom( defer(async () => @@ -120,8 +119,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { ).pipe( handleRetry(retryAttempts, retryDelay), catchError((error) => { - mongooseConnectionError(error); - throw error; + throw mongooseConnectionError(error); }), ), ); From 5b6503a5d6d7e08a0e112cebae82e94e6187e035 Mon Sep 17 00:00:00 2001 From: jiayi <690405541@qq.com> Date: Fri, 28 Oct 2022 09:15:08 +0800 Subject: [PATCH 4/5] perf: connectionError to connectionErrorFactory --- lib/interfaces/mongoose-options.interface.ts | 2 +- lib/mongoose-core.module.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/interfaces/mongoose-options.interface.ts b/lib/interfaces/mongoose-options.interface.ts index 5db9b06d..01111a66 100644 --- a/lib/interfaces/mongoose-options.interface.ts +++ b/lib/interfaces/mongoose-options.interface.ts @@ -9,7 +9,7 @@ export interface MongooseModuleOptions retryDelay?: number; connectionName?: string; connectionFactory?: (connection: any, name: string) => any; - connectionError?: (error: MongooseError) => MongooseError; + connectionErrorFactory?: (error: MongooseError) => MongooseError; } export interface MongooseOptionsFactory { diff --git a/lib/mongoose-core.module.ts b/lib/mongoose-core.module.ts index 8565cbbf..34b94046 100644 --- a/lib/mongoose-core.module.ts +++ b/lib/mongoose-core.module.ts @@ -40,7 +40,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { retryDelay, connectionName, connectionFactory, - connectionError, + connectionErrorFactory, ...mongooseOptions } = options; @@ -48,7 +48,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { connectionFactory || ((connection) => connection); const mongooseConnectionError = - connectionError || ((error) => error); + connectionErrorFactory || ((error) => error); const mongooseConnectionName = getConnectionToken(connectionName); @@ -98,7 +98,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { retryDelay, uri, connectionFactory, - connectionError, + connectionErrorFactory, ...mongooseOptions } = mongooseModuleOptions; @@ -106,7 +106,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { connectionFactory || ((connection) => connection); const mongooseConnectionError = - connectionError || ((error) => error); + connectionErrorFactory || ((error) => error); return await lastValueFrom( defer(async () => From 2effb51a8875b0362e1de82b77ffaa40beffca0e Mon Sep 17 00:00:00 2001 From: Kamil Mysliwiec Date: Mon, 31 Oct 2022 09:22:42 +0100 Subject: [PATCH 5/5] Update lib/mongoose-core.module.ts --- lib/mongoose-core.module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongoose-core.module.ts b/lib/mongoose-core.module.ts index 34b94046..c0194972 100644 --- a/lib/mongoose-core.module.ts +++ b/lib/mongoose-core.module.ts @@ -48,7 +48,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { connectionFactory || ((connection) => connection); const mongooseConnectionError = - connectionErrorFactory || ((error) => error); + connectionErrorFactory || ((error) => error); const mongooseConnectionName = getConnectionToken(connectionName);