Skip to content

Commit

Permalink
Merge pull request #1550 from jiayisheji/master
Browse files Browse the repository at this point in the history
feat: Add connection error handle
  • Loading branch information
kamilmysliwiec committed Oct 31, 2022
2 parents cd646d5 + 2effb51 commit 4fae301
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion 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,
Expand All @@ -9,6 +9,7 @@ export interface MongooseModuleOptions
retryDelay?: number;
connectionName?: string;
connectionFactory?: (connection: any, name: string) => any;
connectionErrorFactory?: (error: MongooseError) => MongooseError;
}

export interface MongooseOptionsFactory {
Expand Down
24 changes: 22 additions & 2 deletions lib/mongoose-core.module.ts
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,
connectionErrorFactory,
...mongooseOptions
} = options;

const mongooseConnectionFactory =
connectionFactory || ((connection) => connection);

const mongooseConnectionError =
connectionErrorFactory || ((error) => error);

const mongooseConnectionName = getConnectionToken(connectionName);

const mongooseConnectionNameProvider = {
Expand All @@ -59,7 +65,12 @@ export class MongooseCoreModule implements OnApplicationShutdown {
await mongoose.createConnection(uri, mongooseOptions).asPromise(),
mongooseConnectionName,
),
).pipe(handleRetry(retryAttempts, retryDelay)),
).pipe(
handleRetry(retryAttempts, retryDelay),
catchError((error) => {
throw mongooseConnectionError(error);
}),
),
),
};
return {
Expand Down Expand Up @@ -87,12 +98,16 @@ export class MongooseCoreModule implements OnApplicationShutdown {
retryDelay,
uri,
connectionFactory,
connectionErrorFactory,
...mongooseOptions
} = mongooseModuleOptions;

const mongooseConnectionFactory =
connectionFactory || ((connection) => connection);

const mongooseConnectionError =
connectionErrorFactory || ((error) => error);

return await lastValueFrom(
defer(async () =>
mongooseConnectionFactory(
Expand All @@ -101,7 +116,12 @@ export class MongooseCoreModule implements OnApplicationShutdown {
.asPromise(),
mongooseConnectionName,
),
).pipe(handleRetry(retryAttempts, retryDelay)),
).pipe(
handleRetry(retryAttempts, retryDelay),
catchError((error) => {
throw mongooseConnectionError(error);
}),
),
);
},
inject: [MONGOOSE_MODULE_OPTIONS],
Expand Down

0 comments on commit 4fae301

Please sign in to comment.