Database
Setting up a database for your application.
Introduction
SaaSKits uses MongoDB as the default database with Prisma as the ORM.
We believe that MongoDB is the best database for bootstrapping SaaS applications because of its flexibility and scalability.
In this guide, we will walk you through the process of setting up a MongoDB database for your application with Atlas.
Create a MongoDB Atlas Account
Sign up for MongoDB Atlas
First, you need to create a MongoDB Atlas account.
Go to https://www.mongodb.com/cloud/atlas and sign up for an account.
Dashboard
Once you have signed up, you will be redirected to the dashboard.
Click on the "Create deployment" button to create a new cluster.
Create a cluster
Click on M0 Sandbox to create a free cluster.
Choose the cloud provider and region of your choice.
Click on the "Create" button to create the cluster.
Create a database user
While the cluster is being created, you will be redirected to the "Security" tab.
On "How would you like to authenticate your connection?", select "Username/Password".
Enter a username and password for your database user. Default username and password will be created for you.
Copy the username and password and save it somewhere safe. You will need it later.
Click on "Create User" to create the database user.
Whitelist your IP address
You will see text like - Where would you like to connect from?
Click on "Add My Current IP Address" to whitelist your IP address.
Whitelist 0.0.0.0/0 to allow access from anywhere. This is not recommended for production.
Click on Finish and Close to finish the setup. Click on Go to Overview.
Connect to your cluster
Click on the "Connect" button to connect to your cluster.
On "Connect to your application", click on "Driver" and select "Node.js".
Copy the connection string and save it somewhere safe. You will need it later.
You will need to replace the <password>
with the password you created earlier.
You will need to add the DB name to the connection string.
mongodb+srv://<username>:<password>@<cluster-url>/<db-name>?retryWrites=true&w=majority
Add your connection string to your .env file
Add your connection string to your .env file.
DATABASE_URL=mongodb+srv://<username>:<password>@<cluster-url>/<database-name>?retryWrites=true&w=majority
Replace the <username>
, <password>
, <cluster-url>
and <database-name>
placeholders with your own values.
Run prisma migration
Run the following command to run the prisma migration.
pnpm run migrate
This will create the database tables for your application.
Conclusion
You have successfully set up a MongoDB database for your application.
Now let's setup authentication for your application.