Skip to content

Commit

Permalink
feat: Add connection error handle
Browse files Browse the repository at this point in the history
  • Loading branch information
jiayisheji committed Oct 9, 2022
1 parent e808034 commit eec8f18
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/interfaces/mongoose-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ModuleMetadata, Type } from '@nestjs/common';
import { ConnectOptions } from 'mongoose';
import { ConnectOptions, MongooseError } from 'mongoose';

export interface MongooseModuleOptions
extends ConnectOptions,
Expand All @@ -9,6 +9,7 @@ export interface MongooseModuleOptions
retryDelay?: number;
connectionName?: string;
connectionFactory?: (connection: any, name: string) => any;
connectionError?: (error: MongooseError) => void;
}

export interface MongooseOptionsFactory {
Expand Down
26 changes: 24 additions & 2 deletions lib/mongoose-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = {
Expand All @@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -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],
Expand Down

0 comments on commit eec8f18

Please sign in to comment.