MongoDB Atlas for a SaaS
I decided to build a SaaS last month called Enhance AI, using OpenAI with GPT-3 to perform some interesting applications of AI to help programmers save time coding.
I needed to pick a good database for this SaaS. As the title suggests, I ended up picking MongoDB, however how I came to this conclusion took some research.
This will cover what I learned by picking and implementing MongoDB Atlas for my project and my thoughts. If you're expecting something technical, this won't be for you.
- Defining the scope and requirements
- Picking databases
- Deciding local vs cloud database
- Setting up the database schema
- Creating the API routes
Defining the Scope & Requirements
The first thing you want to consider is your goal for a project. Mine was to build a startup, in 30 days or less. This means I needed to build fast, and have good documentation whenever I encountered problems.
There was lots of options like firestore, or mysql, however one of the most common startup stacks is called MERN, which is Mongo, Express, React and Nodejs. There are lots of youtube videos on how to set them up (now one more!) and I was quite confident as I had worked with each one of those technologies in the past.
The other requirements was working with JSON objects, which Mongo does quite well as because of the fact that it's a document/collection based database. And finally the fact that it's quite flexible in the fact that it's schema can easily be changed on the fly meant I believe it was a good choice for my startup.
Deciding local vs cloud databases
It's 2021 and soon to be 2022. You are no longer limited to setup an entire production workstation with all items installed, such as a web server, database, files and more. Microservices and cloud services now allow you to distribute all your technologies.
So the question of hosting the database locally or online is an actual decision to make. There are a few advantages and disadvantages to both approaches. Yes you have more control if everything is locally on your machine, but by comparison, having a cloud version of a database means it's accessible anywhere.
As I often swap computers, work remotely, and want more flexibility, I had a look at the cloud version of MongoDb. It's called MongoDB Atlas. I have the option to connect directly to it via my vscode with a plugin, which is quite useful. On top of that, you can scale and grow it with your needs.
Other than that, the setup is exactly the same for connecting it to your project, minus the requirement to install and actually host the database on the machine. This meant I got to save time and effort and simply copy paste the url of the database from Mongo and I was up and running in minutes.
Setting up the Database Schema
Every website and application (hopefully) is unique. However there are some items which remain the same, such as the requirement for users, authentication, etc. I took a few days to plan out how I wanted to structure my database before actually implementing it.
The main difference was the fact that I was using OpenAI, and I wanted to make sure that could help improve my GPT-3 outputs overtime, so I made sure to also implement a history schema. Using express, I setup mongoose as it's one of the easier ways to define a schema, which I could later use in my routes.
Creating the API
With the database picked, the cloud setup, the schema defined, I was able to implement the API with ease.
Most of the requests could be simple GET, POST requests which are then using the findOne command using mongoose with mongo no different to if I was using it locally.
I originally thought I might get some significant delays by using the cloud version of MongoDB, however the server was quite fast and I never had any such delays.
While this was a brief overview of using MongoDB Atlas, it's worthwhile that people try it over the alternative of running up a database locally and manually. You will get the same results with less time and have more flexibility moving forward.
I've done a while video which goes more into depth about this journey but hopefully this gives you a good idea of some of the items I learned during this time.
MongoDB Atlas: https://www.mongodb.com/cloud/atlas/
Youtube Video: Creating a SaaS Database
Follow and support me:
Special thanks if you subscribe to my channel :)