mongodb nest.js 1

mongodb nest.js

import * as mongoose from 'mongoose';

export const databaseProviders = [
  {
    provide: 'DATABASE_CONNECTION',
    useFactory: (): Promise =>
      mongoose.connect('mongodb://localhost/nest'),
  },
];

Here is what the above code is Doing:
1. We are importing the mongoose module.
2. We are creating a databaseProviders constant that is an array of providers.
3. We are creating a provider named DATABASE_CONNECTION.
4. We are using the useFactory method to create a new MongoDB connection.
5. We are using the mongoose.connect method to connect to the database.

Now, let’s add the databaseProviders constant to the providers array in the app.module.ts file:

Similar Posts