From 388edd2cbd61f7ff66dea9efe41d30cf3461d001 Mon Sep 17 00:00:00 2001 From: Prateek Kathal Date: Wed, 20 Mar 2024 19:13:12 -0400 Subject: [PATCH 1/5] feat: added onconnectioncreate module option --- lib/interfaces/mongoose-options.interface.ts | 3 ++- lib/mongoose-core.module.ts | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/interfaces/mongoose-options.interface.ts b/lib/interfaces/mongoose-options.interface.ts index 4ce30808..7e6176d9 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, MongooseError } from 'mongoose'; +import { ConnectOptions, MongooseError, Connection } from 'mongoose'; export interface MongooseModuleOptions extends ConnectOptions { uri?: string; @@ -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 { diff --git a/lib/mongoose-core.module.ts b/lib/mongoose-core.module.ts index f416e3e0..528083f2 100644 --- a/lib/mongoose-core.module.ts +++ b/lib/mongoose-core.module.ts @@ -43,6 +43,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { connectionFactory, connectionErrorFactory, lazyConnection, + onConnectionCreate, ...mongooseOptions } = options; @@ -69,6 +70,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { uri, mongooseOptions, lazyConnection, + onConnectionCreate, ), mongooseConnectionName, ), @@ -107,6 +109,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { connectionFactory, connectionErrorFactory, lazyConnection, + onConnectionCreate, ...mongooseOptions } = mongooseModuleOptions; @@ -123,6 +126,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { uri as string, mongooseOptions, lazyConnection, + onConnectionCreate, ), mongooseConnectionName, ), @@ -191,6 +195,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { uri: string, mongooseOptions: ConnectOptions, lazyConnection?: boolean, + onConnectionCreate?: MongooseModuleOptions['onConnectionCreate'], ): Promise { const connection = mongoose.createConnection(uri, mongooseOptions); @@ -198,6 +203,11 @@ export class MongooseCoreModule implements OnApplicationShutdown { return connection; } + const mongooseOnConnectionCreate = onConnectionCreate || (() => {}); + if (mongooseOnConnectionCreate) { + mongooseOnConnectionCreate(connection); + } + return connection.asPromise(); } From 64fdcddf968f0c0420286d687bc692b36ece640b Mon Sep 17 00:00:00 2001 From: Kamil Mysliwiec Date: Fri, 5 Apr 2024 08:42:10 +0200 Subject: [PATCH 2/5] Update lib/mongoose-core.module.ts --- lib/mongoose-core.module.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/mongoose-core.module.ts b/lib/mongoose-core.module.ts index 528083f2..194a2be4 100644 --- a/lib/mongoose-core.module.ts +++ b/lib/mongoose-core.module.ts @@ -203,10 +203,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { return connection; } - const mongooseOnConnectionCreate = onConnectionCreate || (() => {}); - if (mongooseOnConnectionCreate) { - mongooseOnConnectionCreate(connection); - } + onConnectionCreate?.(connection); return connection.asPromise(); } From d92bacce6548ffb0425675baecbc9a0a80c7f644 Mon Sep 17 00:00:00 2001 From: Prateek Kathal Date: Wed, 10 Apr 2024 19:28:16 -0400 Subject: [PATCH 3/5] feat: depracted lazyconnection option and replaced with factoryoptions --- lib/interfaces/mongoose-options.interface.ts | 8 ++++++ lib/mongoose-core.module.ts | 26 ++++++++++++-------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/interfaces/mongoose-options.interface.ts b/lib/interfaces/mongoose-options.interface.ts index 7e6176d9..fb2073d1 100644 --- a/lib/interfaces/mongoose-options.interface.ts +++ b/lib/interfaces/mongoose-options.interface.ts @@ -8,6 +8,14 @@ export interface MongooseModuleOptions extends ConnectOptions { connectionName?: string; connectionFactory?: (connection: any, name: string) => any; connectionErrorFactory?: (error: MongooseError) => MongooseError; + /** + * @deprecated Method has been moved to `factoryOptions` + */ + lazyConnection?: boolean; + factoryOptions?: MongooseFactoryOptions; +} + +export interface MongooseFactoryOptions { lazyConnection?: boolean; onConnectionCreate?: (connection: Connection) => void; } diff --git a/lib/mongoose-core.module.ts b/lib/mongoose-core.module.ts index 194a2be4..f5473c46 100644 --- a/lib/mongoose-core.module.ts +++ b/lib/mongoose-core.module.ts @@ -18,6 +18,7 @@ import { MongooseModuleFactoryOptions, MongooseModuleOptions, MongooseOptionsFactory, + MongooseFactoryOptions, } from './interfaces/mongoose-options.interface'; import { MONGOOSE_CONNECTION_NAME, @@ -43,7 +44,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { connectionFactory, connectionErrorFactory, lazyConnection, - onConnectionCreate, + factoryOptions, ...mongooseOptions } = options; @@ -60,6 +61,10 @@ export class MongooseCoreModule implements OnApplicationShutdown { useValue: mongooseConnectionName, }; + // TODO: Remove when "lazyConnection" option is removed (post deprecation) + const mongooseFactoryOptions: MongooseFactoryOptions = + factoryOptions || lazyConnection ? { lazyConnection } : {}; + const connectionProvider = { provide: mongooseConnectionName, useFactory: async (): Promise => @@ -69,8 +74,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { await this.createMongooseConnection( uri, mongooseOptions, - lazyConnection, - onConnectionCreate, + mongooseFactoryOptions, ), mongooseConnectionName, ), @@ -109,7 +113,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { connectionFactory, connectionErrorFactory, lazyConnection, - onConnectionCreate, + factoryOptions, ...mongooseOptions } = mongooseModuleOptions; @@ -119,14 +123,17 @@ export class MongooseCoreModule implements OnApplicationShutdown { const mongooseConnectionError = connectionErrorFactory || ((error) => error); + // TODO: Remove when "lazyConnection" option is removed (post deprecation) + const mongooseFactoryOptions: MongooseFactoryOptions = + factoryOptions || lazyConnection ? { lazyConnection } : {}; + return await lastValueFrom( defer(async () => mongooseConnectionFactory( await this.createMongooseConnection( uri as string, mongooseOptions, - lazyConnection, - onConnectionCreate, + mongooseFactoryOptions, ), mongooseConnectionName, ), @@ -194,16 +201,15 @@ export class MongooseCoreModule implements OnApplicationShutdown { private static async createMongooseConnection( uri: string, mongooseOptions: ConnectOptions, - lazyConnection?: boolean, - onConnectionCreate?: MongooseModuleOptions['onConnectionCreate'], + factoryOptions?: MongooseFactoryOptions, ): Promise { const connection = mongoose.createConnection(uri, mongooseOptions); - if (lazyConnection) { + if (factoryOptions?.lazyConnection) { return connection; } - onConnectionCreate?.(connection); + factoryOptions?.onConnectionCreate?.(connection); return connection.asPromise(); } From 26a1c3d6331e8a3c93bdd171a201f41e37b8169e Mon Sep 17 00:00:00 2001 From: Prateek Kathal Date: Thu, 11 Apr 2024 11:00:37 -0400 Subject: [PATCH 4/5] Revert "feat: depracted lazyconnection option and replaced with factoryoptions" This reverts commit d92bacce6548ffb0425675baecbc9a0a80c7f644. --- lib/interfaces/mongoose-options.interface.ts | 8 ------ lib/mongoose-core.module.ts | 26 ++++++++------------ 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/lib/interfaces/mongoose-options.interface.ts b/lib/interfaces/mongoose-options.interface.ts index fb2073d1..7e6176d9 100644 --- a/lib/interfaces/mongoose-options.interface.ts +++ b/lib/interfaces/mongoose-options.interface.ts @@ -8,14 +8,6 @@ export interface MongooseModuleOptions extends ConnectOptions { connectionName?: string; connectionFactory?: (connection: any, name: string) => any; connectionErrorFactory?: (error: MongooseError) => MongooseError; - /** - * @deprecated Method has been moved to `factoryOptions` - */ - lazyConnection?: boolean; - factoryOptions?: MongooseFactoryOptions; -} - -export interface MongooseFactoryOptions { lazyConnection?: boolean; onConnectionCreate?: (connection: Connection) => void; } diff --git a/lib/mongoose-core.module.ts b/lib/mongoose-core.module.ts index f5473c46..194a2be4 100644 --- a/lib/mongoose-core.module.ts +++ b/lib/mongoose-core.module.ts @@ -18,7 +18,6 @@ import { MongooseModuleFactoryOptions, MongooseModuleOptions, MongooseOptionsFactory, - MongooseFactoryOptions, } from './interfaces/mongoose-options.interface'; import { MONGOOSE_CONNECTION_NAME, @@ -44,7 +43,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { connectionFactory, connectionErrorFactory, lazyConnection, - factoryOptions, + onConnectionCreate, ...mongooseOptions } = options; @@ -61,10 +60,6 @@ export class MongooseCoreModule implements OnApplicationShutdown { useValue: mongooseConnectionName, }; - // TODO: Remove when "lazyConnection" option is removed (post deprecation) - const mongooseFactoryOptions: MongooseFactoryOptions = - factoryOptions || lazyConnection ? { lazyConnection } : {}; - const connectionProvider = { provide: mongooseConnectionName, useFactory: async (): Promise => @@ -74,7 +69,8 @@ export class MongooseCoreModule implements OnApplicationShutdown { await this.createMongooseConnection( uri, mongooseOptions, - mongooseFactoryOptions, + lazyConnection, + onConnectionCreate, ), mongooseConnectionName, ), @@ -113,7 +109,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { connectionFactory, connectionErrorFactory, lazyConnection, - factoryOptions, + onConnectionCreate, ...mongooseOptions } = mongooseModuleOptions; @@ -123,17 +119,14 @@ export class MongooseCoreModule implements OnApplicationShutdown { const mongooseConnectionError = connectionErrorFactory || ((error) => error); - // TODO: Remove when "lazyConnection" option is removed (post deprecation) - const mongooseFactoryOptions: MongooseFactoryOptions = - factoryOptions || lazyConnection ? { lazyConnection } : {}; - return await lastValueFrom( defer(async () => mongooseConnectionFactory( await this.createMongooseConnection( uri as string, mongooseOptions, - mongooseFactoryOptions, + lazyConnection, + onConnectionCreate, ), mongooseConnectionName, ), @@ -201,15 +194,16 @@ export class MongooseCoreModule implements OnApplicationShutdown { private static async createMongooseConnection( uri: string, mongooseOptions: ConnectOptions, - factoryOptions?: MongooseFactoryOptions, + lazyConnection?: boolean, + onConnectionCreate?: MongooseModuleOptions['onConnectionCreate'], ): Promise { const connection = mongoose.createConnection(uri, mongooseOptions); - if (factoryOptions?.lazyConnection) { + if (lazyConnection) { return connection; } - factoryOptions?.onConnectionCreate?.(connection); + onConnectionCreate?.(connection); return connection.asPromise(); } From 320c264231843674aecfd877f479842bd93029b1 Mon Sep 17 00:00:00 2001 From: Prateek Kathal Date: Thu, 11 Apr 2024 11:15:55 -0400 Subject: [PATCH 5/5] feat: replaced third argument for createmongooseconnection --- lib/mongoose-core.module.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/mongoose-core.module.ts b/lib/mongoose-core.module.ts index 194a2be4..4902a53f 100644 --- a/lib/mongoose-core.module.ts +++ b/lib/mongoose-core.module.ts @@ -66,12 +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( @@ -125,8 +123,7 @@ export class MongooseCoreModule implements OnApplicationShutdown { await this.createMongooseConnection( uri as string, mongooseOptions, - lazyConnection, - onConnectionCreate, + { lazyConnection, onConnectionCreate }, ), mongooseConnectionName, ), @@ -194,16 +191,18 @@ export class MongooseCoreModule implements OnApplicationShutdown { private static async createMongooseConnection( uri: string, mongooseOptions: ConnectOptions, - lazyConnection?: boolean, - onConnectionCreate?: MongooseModuleOptions['onConnectionCreate'], + factoryOptions: { + lazyConnection?: boolean; + onConnectionCreate?: MongooseModuleOptions['onConnectionCreate']; + }, ): Promise { const connection = mongoose.createConnection(uri, mongooseOptions); - if (lazyConnection) { + if (factoryOptions?.lazyConnection) { return connection; } - onConnectionCreate?.(connection); + factoryOptions?.onConnectionCreate?.(connection); return connection.asPromise(); }