Skip to content

Commit

Permalink
Merge pull request #2096 from prateekkathal/on-connection-create
Browse files Browse the repository at this point in the history
feat: added onconnectioncreate module option
  • Loading branch information
kamilmysliwiec authored Apr 12, 2024
2 parents afd8a1c + 320c264 commit 56ac9b4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 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, MongooseError } from 'mongoose';
import { ConnectOptions, MongooseError, Connection } from 'mongoose';

export interface MongooseModuleOptions extends ConnectOptions {
uri?: string;
Expand All @@ -9,6 +9,7 @@ export interface MongooseModuleOptions extends ConnectOptions {
connectionFactory?: (connection: any, name: string) => any;
connectionErrorFactory?: (error: MongooseError) => MongooseError;
lazyConnection?: boolean;
onConnectionCreate?: (connection: Connection) => void;
}

export interface MongooseOptionsFactory {
Expand Down
20 changes: 13 additions & 7 deletions lib/mongoose-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class MongooseCoreModule implements OnApplicationShutdown {
connectionFactory,
connectionErrorFactory,
lazyConnection,
onConnectionCreate,
...mongooseOptions
} = options;

Expand All @@ -65,11 +66,10 @@ export class MongooseCoreModule implements OnApplicationShutdown {
await lastValueFrom(
defer(async () =>
mongooseConnectionFactory(
await this.createMongooseConnection(
uri,
mongooseOptions,
await this.createMongooseConnection(uri, mongooseOptions, {
lazyConnection,
),
onConnectionCreate,
}),
mongooseConnectionName,
),
).pipe(
Expand Down Expand Up @@ -107,6 +107,7 @@ export class MongooseCoreModule implements OnApplicationShutdown {
connectionFactory,
connectionErrorFactory,
lazyConnection,
onConnectionCreate,
...mongooseOptions
} = mongooseModuleOptions;

Expand All @@ -122,7 +123,7 @@ export class MongooseCoreModule implements OnApplicationShutdown {
await this.createMongooseConnection(
uri as string,
mongooseOptions,
lazyConnection,
{ lazyConnection, onConnectionCreate },
),
mongooseConnectionName,
),
Expand Down Expand Up @@ -190,14 +191,19 @@ export class MongooseCoreModule implements OnApplicationShutdown {
private static async createMongooseConnection(
uri: string,
mongooseOptions: ConnectOptions,
lazyConnection?: boolean,
factoryOptions: {
lazyConnection?: boolean;
onConnectionCreate?: MongooseModuleOptions['onConnectionCreate'];
},
): Promise<Connection> {
const connection = mongoose.createConnection(uri, mongooseOptions);

if (lazyConnection) {
if (factoryOptions?.lazyConnection) {
return connection;
}

factoryOptions?.onConnectionCreate?.(connection);

return connection.asPromise();
}

Expand Down

0 comments on commit 56ac9b4

Please sign in to comment.