How to Connect TypeORM with MongoDB Atlas using TypeScript

Posted January 29, 2024
How to Connect TypeORM with MongoDB Atlas using TypeScript

TypeORM works great with MongoDB. However, TypeORM has different ways to connect to MongoDB. MongoDB Atlas and MongoDB community (localhost) use other TypeORM connection parameters.

This guide will teach you how to create a Nets.js TypeScript app and use TypeORM to connect to MongoDB Atlas.

Ready? Dive and learn this easy way to connect TypeORM with MongoDB Atlas using TypeScript

How TypeORM works without MongoDB Atlas

If you are using your connection to access MongoDB, you will most likely use the following connection parameters:

    TypeOrmModule.forRoot({
    type: "mongodb",
    host: "localhost",
    port: 27017,
    database: "test",
    synchronize: true,
    entities: [],
  }),

These are connection variables for a localhost MongoDB server. Now, MongoDB Atlas uses passwords and usernames to access the cloud server with TypeORM. You would most likely use the following TypeScript code to let TypeORM connect to MongoDB Atlas:

{
  type: "mongodb",
  // Hostname of the MongoDB server
  host: "cluster0-****.mongodb.net",
  // Port number for MongoDB connection
  port: 27017,
  // Name of the database to connect to
  database: "you-db",
  synchronize: true,

  username: 'add-the-username',

  // Password for authenticating with MongoDB
  password: 'fill-with-your-password',
  // Array of entities
  entities: [Post],
  // Use the new MongoDB Unified Topology
  useUnifiedTopology: true,
  // Use the new connection parser
  useNewUrlParser: true,
}

Once you use this example, you will note that TypeORM won’t be able to connect to MongoDB Atlas. You will get errors, and TypeORM won’t access the Atlas; what is the solution here?

The Right Way

You will note that the field by field" configuration does not work fine as expected. The Major challenge is that TYPEORM documentation is unclear on how MongoDB connection should be created. Now, to connect to MongoDB Atlas and TypeORM, you must:

  • Ensure that the IP address you are using is whitelisted in MongoDB. To whitelist an IP address, follow these steps:

    1. Navigate to your MongoDB Atlas Project dashboard.
    2. Access the “Network Access” tab from the left-hand navigation menu.
    3. Within the “Network Access” tab, go to the “IP Whitelist” tab.
    4. Add the IP address that needs to be whitelisted.

Note: If whitelisting specific IPs is challenging, you can use 0.0.0.0 to allow connections from any IP (less secure).

  • Enable SSL configuration to ensure TypeORM connection to MongoDB Atlas is safe. You must add ssl:true in your TypeORM database settings.
  • You’ll need to add a database authentication strategy and use authSource:admin

Here, the overall TypeORM MongoDB Atlas configuration will be as follows:

{
  "type": "mongodb",
  // Hostname of the MongoDB server
  "host": "cluster0-shard-00-02-xxxxx.mongodb.net",
  // Port number for MongoDB connection
  "port": 27017,
  // Username for authenticating with MongoDB
  "username": "root",
  // Password for authenticating with MongoDB Atlas
  "password": "password",
  // Name of the database to connect to
  "database": "name-database",
  // Whether to synchronize database schema with entities
  "synchronize": true,
  // Your entities
  "entities": [],

  // Extra driver options for MongoDB connection
  "driverExtra": {
    // Enable SSL for secure communication
    "ssl": true,
    // database authentication source for connecting to MongoDB
    "authSource": "admin"
  }
}

The BEST sol: Connecting TypeORM and MongoDB Atlas using useNewUrlParser

MongoDB Atlas provides a URL to your cloud database; how about we use it alongside TypeORM?

Here, you must ensure that the useNewUrlParser is set. Then, add your TypeORM connection as part of the URL connection string.

This should be the best solution to let TypeORM connect to MongoDB Atlas.

{
  //MongoDB, in this case
  type: "mongodb",
  // Connection URL for MongoDB Atlas
  // Ensure you have the username, password, cluster information, and database name
  url: "mongodb+srv:xxxxcluster0-xxxx-xxxx-xxxx.mongodb.netXXXx",
  // Whether to use the new connection parser
  useNewUrlParser: true,
  synchronize: true,
  // Enable logging for database operations
  logging: true,
  // The authentication source
  authSource: "admin",
  entities: []
}

If this fails, remove the authSource: "admin", line and Try your connection again.

Create TypeORM CRUD app using MongoDB Atlas

If you want to build a CRUD app that uses this MongoDB connection with MongoDB Atlas, you don’t need any specific steps. Other settings and code setups remain the same.

Check:

Conclusion

If you use the above examples correctly, TypeORM should connect to MongoDB Atlas without a challenge. Otherwise, investigate your connection context and ensure TypeORM can access Atlas. Replace the placeholders with your actual MongoDB Atlas information.

How to Connect TypeORM with MongoDB Atlas using TypeScript

Written By:

Joseph Chege