Skip to content

Commit

Permalink
Revert "feat: depracted lazyconnection option and replaced with facto…
Browse files Browse the repository at this point in the history
…ryoptions"

This reverts commit d92bacc.
  • Loading branch information
prateekkathal committed Apr 11, 2024
1 parent d92bacc commit 26a1c3d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
8 changes: 0 additions & 8 deletions lib/interfaces/mongoose-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
26 changes: 10 additions & 16 deletions lib/mongoose-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
MongooseModuleFactoryOptions,
MongooseModuleOptions,
MongooseOptionsFactory,
MongooseFactoryOptions,
} from './interfaces/mongoose-options.interface';
import {
MONGOOSE_CONNECTION_NAME,
Expand All @@ -44,7 +43,7 @@ export class MongooseCoreModule implements OnApplicationShutdown {
connectionFactory,
connectionErrorFactory,
lazyConnection,
factoryOptions,
onConnectionCreate,
...mongooseOptions
} = options;

Expand All @@ -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<any> =>
Expand All @@ -74,7 +69,8 @@ export class MongooseCoreModule implements OnApplicationShutdown {
await this.createMongooseConnection(
uri,
mongooseOptions,
mongooseFactoryOptions,
lazyConnection,
onConnectionCreate,
),
mongooseConnectionName,
),
Expand Down Expand Up @@ -113,7 +109,7 @@ export class MongooseCoreModule implements OnApplicationShutdown {
connectionFactory,
connectionErrorFactory,
lazyConnection,
factoryOptions,
onConnectionCreate,
...mongooseOptions
} = mongooseModuleOptions;

Expand All @@ -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,
),
Expand Down Expand Up @@ -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<Connection> {
const connection = mongoose.createConnection(uri, mongooseOptions);

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

factoryOptions?.onConnectionCreate?.(connection);
onConnectionCreate?.(connection);

return connection.asPromise();
}
Expand Down

0 comments on commit 26a1c3d

Please sign in to comment.