'NextJs: do have any problem by writing code for mongoose connection in next.config.js

I have been doing research for creating a standard connection for MongoDB using mongoose in React NextJs. But, in every blog, I saw that we need to write code for mongoose connection in a function and call it on every page. function for connect mongodb

That was very disgusting. When I used express I can connect the DB in the index file and do every DB method in routes without re-calling it again. Today, I saw configuring the required packages in next.config.js. So, I think writing mongoose connection in it will work. And it works! Mongoose connection in next.config.js

But, I am afraid that no bloggers show that way of connecting. So, I doubt that writing a connection in it makes any bug or performance issue for the next app?



Solution 1:[1]

from my point of vue i don't see any advantages to put it in next.config, in the official documentation, the way it to pass it through app.module.ts and store your mongo uri in a .env file to avoid exposing sensible crendentials.

@Module({
  imports: [
    ConfigModule.forRoot(),
    MongooseModule.forRoot(process.env.MONGO_URI, {}),
    AuthModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sikarak