Skip to content

Commit

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

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

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

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

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

return connection.asPromise();
}
Expand Down

0 comments on commit d92bacc

Please sign in to comment.