How to Setup Multiple Databases in Sails

Setting up Separate Databases for Development and Production

In my pursuit of using Sails to create APIs correctly, I keep learning new things. There is always something new to learn every time I work with Sails.

Today I will show you how to set up separate databases for development and production. You seldom find features like this in other frameworks like Sails.

💡
The resources at the bottom can help if you encounter any challenges.

Prerequisites

Before we get started, for this article we will make use of

  • Sails.js (obviously😉)

  • MongoDB

  • MongoDB Compass

You can set up more than one database, but this article focuses on setting up one locally and one for production.

Steps to follow

There are some steps to follow to achieve our mutual goal. Steps like:

  1. Fetch connection strings from MongoDB Atlas and locally.

  2. Place the strings in the right files.

  3. Connect to the MongoDB Compass application.

Let's get started!😃

Step 1: Fetch connection strings

To carry out this step, you have to make sure that the MongoDB community server is installed on your machine and that you have created a cluster on MongoDB Atlas.

💡
Head to the bottom of the article to find resources on how to get that done.

Now you are ready to get those connection strings.

Fetching from MongoDB Atlas

  1. Connect your cluster.

On your dashboard, you should see something similar to the image below. What you need to do is click on Connect.

  1. Choose how you want to connect the cluster.

You are presented with different methods to connect your cluster. Feel free to explore if you are interested, but for this tutorial, you should choose drivers.

  1. Grab the connection string.

You can pick the version of Node you prefer to work with (I will advise you to have the latest version installed). You can ignore the command to install the MongoDB package; what you will install instead is sails-mongo. It is an adapter for MongoDB that you use when working with Sails.

$ npm install sails-mongo

Once you are done installing the adapter, the next thing is to copy the connection string, something like this:

mongodb+srv://<username>:<password>@cluster0.836bk4k.mongodb.net/?retryWrites=true&w=majority

Awesome! You are getting it right, you should applaud yourself nicely.👏

Fetching locally

  1. Run MongoDB Compass.

As stated earlier, you should have this software installed. You should come face to face with this interface:

  1. Grab the connection string.

The connection string for connecting locally is in sight:

mongodb://localhost:27017/

If you guessed it, then you are correct!

Step 2: Place the strings in the right files

Now that you have seen how to get your connection strings from MongoDB Atlas and locally, the next thing to do is to place them in the right files.

Placing the local string

The local string will be placed in the config/datastores.js .

💡
Make sure you have installed sails-mongo, it will be used as the chosen adapter.

Locate the default object, you will find where to choose your adapter and paste your string. Your outcome should look like this:

That's all you need to set up locally.

Placing the string for production

You will place the string fetched from MongoDB Atlas in config/env/production.js .

Follow the same process and locate the default object. The only thing you have to change is the url .

Noticed the link is a bit different right? The link has been trimmed to get exactly what we need to get going. All you have to do is:

  • Fill in your username

  • Fill in your password

  • Fill in the name of your database

Once those pieces of information are filled, the link is ready for usage.

Step 3: Connect MongoDB Compass

You can choose to open multiple instances of MongoDB Compass by pressing Ctrl + N or cmd + N . You can also choose to use one instance to test both connections.
Connect Locally

To connect your MongoDB Compass locally, paste your local connection string (it should be there by default) and click connect.

If you face any error similar to a connection refused, check if the community server was properly installed on your machine.

Connect MongoDB Atlas

Paste your connection string in the uri field. Your string should look like this:

mongodb+srv://<username>:<password>@cluster0.836bk4k.mongodb.net/

Conclusion

There are more powerful and interesting features in Sails. Stick around so we can explore them together.

Until next time...

"Every winner and every loser have the same goals. The goal of the winner is to commit to the activities and the goal of the loser is to commit to the goal." - Alex Hormozi

RESOURCES