DEV Community

Arsh Sharma
Arsh Sharma

Posted on

How to connect your Graphback project to MongoDB Atlas

What Is Graphback?

Graphback is an open source 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

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

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 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

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

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

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>
Enter fullscreen mode Exit fullscreen mode

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}`
  }
Enter fullscreen mode Exit fullscreen mode

with

const { DB_CONNECTION_STRING } = process.env;
url = DB_CONNECTION_STRING
Enter fullscreen mode Exit fullscreen mode

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 :)

Top comments (0)