How to connect your Graphback project to MongoDB Atlas
Tags: tutorial, beginners, graphql, mongodb
Published: 2020-11-05 13:54:24

## What Is Graphback?
[Graphback](https://graphback.dev/) is an [open source](https://github.com/aerogear/graphback) project which aims to simplify the process of generating a GraphQL based backend with the help of your data model. Not only that but it also provides you with a starter client template, to begin with. Currently, it supports two databases, PostgreSQL and MongoDB. In this article, I am going to show you how to connect your Graphback project to MongoDB Atlas for easier deployment. So let's get started. 


## Overview
Once you've created your project using the Graphback CLI with one of the MongoDB templates you should have a file structure which looks like this:

![File Structure](https://dev-to-uploads.s3.amazonaws.com/i/tp9haoo5w230vgmmwvir.png)

Open up your `db.ts` file. This is where the code related to database connection is present. If you used the MongoDB template for your app then your `db.ts` file should look like this:

![db.ts](https://dev-to-uploads.s3.amazonaws.com/i/n71a8wus9y6s77lzunay.png)

Here we are going to replace the `url` (using environment variables) with our MongoDB connection string which we get from MongoDB Atlas and we'd be good to go. 



## Getting The MongoDB Connection String
Head over to [MongoDB Atlas](https://www.mongodb.com/cloud/atlas) and log in. Go ahead and create a new project. 

Once you've created a project head over to `Network Access` and click on `Add IP Address`. Then click on the `Allow access from anywhere` button and you'll be good to go.

![Network Access](https://dev-to-uploads.s3.amazonaws.com/i/gihg2qjyggvp1tinnjeo.png)

Next head over to `Database Access`. Here click on `Add New Database User` and let everything remain at its default configuration and just create a name and password for yourself. You'll be needing this so make sure to copy it to someplace if you can't rely on your memory :P

![Database Access](https://dev-to-uploads.s3.amazonaws.com/i/lphz5l607e0qsqanv3r8.png)

Now simply head over to `Clusters` under data storage and click on connect. You'll be presented with three ways to connect. Select the second one which says `Connect your application`. There let everything be as it is and copy your MongoDB connection string. 

![Cluster](https://dev-to-uploads.s3.amazonaws.com/i/5wekaq4prn9x0oqw602n.png)

## Final steps
Replace the `<password>` in your connection string with the one you created earlier when you added the database user. Click on `collections` in your cluster and then click on `Create Database` to create a database. Remember the name you give this database and replace `<dbname>` in your connection string with this.

With this we are good to go.
Open the `.env` file and add the following line in it:
```
DB_CONNECTION_STRING = <your connection string>
```
where `<your connection string>` is the connection string you got from MongoDB Atlas and made changes to. 

Next, head over to your `db.ts` file and replace the code: 
```
const { DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_DATABASE, DB_AUTHSOURCE } = process.env;

if (DB_USER && DB_PASSWORD) {
    url = `mongodb://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}?authSource=${DB_AUTHSOURCE}`;
} else {
    url = `mongodb://${DB_HOST}:${DB_PORT}/${DB_DATABASE}`
  }
```
with
```
const { DB_CONNECTION_STRING } = process.env;
url = DB_CONNECTION_STRING
```

With this, we're done and you should be now able to launch your Graphback project with MongoDB Atlas.

If you're stuck at some step let me know in the comments and I will try my best to help you out.

Thanks for reading :)