or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-2/

Async Mongo Connection Configuration

Design a Nest module that configures a MongoDB connection using asynchronous option providers so different dependency injection patterns can build the connection settings at runtime.

Capabilities

Factory-based options

  • When configured with a factory-based provider, the module awaits an async builder that combines host, port, database name, and credentials from an injected config source into a mongodb://user:pass@host:port/db connection string while passing through any extra driver options returned. @test

Class-based options provider

  • When configured with a class-based provider, the module resolves and awaits a class method that produces the connection options, then merges an injected TLS flag into the resolved options before initializing the connection. @test

Existing provider reuse

  • When configured to reuse an existing provider, the module calls the shared provider exactly once and uses its resolved options for connection initialization without re-instantiating or re-invoking that provider. @test

Implementation

@generates

API

export interface AsyncDbConfig {
  uri: string;
  user?: string;
  pass?: string;
  tls?: boolean;
  appName?: string;
}

export type ConfigSource =
  | { kind: "factory"; useFactory: (...deps: any[]) => Promise<AsyncDbConfig> | AsyncDbConfig; inject?: any[] }
  | { kind: "class"; useClass: new (...deps: any[]) => { createConfig(): Promise<AsyncDbConfig> | AsyncDbConfig }; inject?: any[] }
  | { kind: "existing"; useExisting: any };

export interface AsyncConnectionModuleOptions {
  config: ConfigSource;
  connectionName?: string;
}

export class AsyncConnectionModule {
  static registerAsync(options: AsyncConnectionModuleOptions): DynamicModule;
}

Dependencies { .dependencies }

@nestjs/mongoose { .dependency }

Integrates the Nest application with MongoDB connections and models.