nestjs / mongoose

Mongoose module for Nest framework (node.js) 🍸
https://nestjs.com
MIT License
527 stars 118 forks source link

Unable to use 'connectionName' with forRootAsync initialization #2172

Closed anteqkois closed 4 months ago

anteqkois commented 4 months ago

Is there an existing issue for this?

Current behavior

Can not connect to the database when I use connectionName argument, I get " Unable to connect to the database. Retrying (1)" error.

Minimum reproduction code

Can not provide databse connection

Steps to reproduce

Use this module to connect to the databases:

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MongooseModule, MongooseModuleOptions } from '@nestjs/mongoose';
import { DatabaseConnection } from './types';

@Module({
    imports: [
        // MongooseModule.forRoot('mongodb+srv://<user>:<pass>@<host>/<db>', {
        //  connectionName: DatabaseConnection.TEST,
        // }),
        MongooseModule.forRootAsync({
            imports: [ConfigModule],
            useFactory: async (configService: ConfigService): Promise<MongooseModuleOptions> => {
                const protocol = configService.getOrThrow<string>('MONGO_PROTOCOL');
                const username = configService.getOrThrow<string>('MONGO_USERNAME');
                const password = configService.getOrThrow<string>('MONGO_PASSWORD');
                const host = configService.getOrThrow<string>('MONGO_HOST');
                const port = configService.get<string>('MONGO_PORT');
                const database = configService.get<string>('MONGO_DATABASE');

                const uri = `${protocol}://${username}:${password}@${host}${port ? ':' + port : ''}${database ? '/' + database : ''}`;
                console.log(uri);
                return {
                    uri,
                    // useCreateIndex: true,
                    autoIndex: true,
                    connectionName: DatabaseConnection.TEST,
                    authSource: 'admin',
                    replicaSet: '...',
                    readPreference: 'primary',
                    ssl: true,
                    connectionFactory(connection, name) {
                        console.log('NAME', name);
                        connection.on('error', (error) => {
                            console.log('DB connection failed! for error: ', error);
                        });

                        return connection;
                    },
                };
            },
            inject: [ConfigService],
        }),
    ],
    exports: [MongooseModule],
})
export class MongodbModule {}

Using simple forRoot works, but forRootAsync not. When I use forRootAsync without connectionName it also works. It don't provide any additional error logs

Expected behavior

Connect to database

Package version

10.0.4

mongoose version

10.0.4

NestJS version

8.1.3

Node.js version

20.11.1

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 4 months ago

Thank you for taking the time to submit your report! From the looks of it, this could be better discussed on our Discord. If you haven't already, please join here and send a new post in the #⁠ 🐈 nestjs-help forum. Make sure to include a link to this issue, so you don't need to write it all again. We have a large community of helpful members, who will assist you in getting this to work.