in

Gatsby + Strapi: MongoDB Setup

Strapi vs Contentful

I’ve already covered the benefits of Gatsby and the basics of working with it. We even covered a little bit of working with the Contentful headless CMS. That works fine, but Contentful has it’s downsides. For example it’s free plan only allows you to have 5000 records (media files), after that it’s a whopping $40/month. A little rich for my blood for a personal blog. If I want to move away from them there is no easy way to export my content to Markdown to take it somewhere else. I think they do this on purpose to get you into tech debt with them. And additionally, they do not have a robust way to create associations with your different collections like Strapi does. Therefore Strapi makes it WAY easier to query for all of my items in a specific category or with a specific tag.

The downside… Contentful is SO much easier to use. They have everything baked right in. You don’t have to worry about managing your own database. My first foray into Strapi was challenging right off the bat. I installed it with the quickstart option which uses sqlite as the database. Then I made the unforgivable sin of naming two columns with the name slug and the whole application crashed with no way to restart it without manually going into the database to delete the duplicate column. And I only discovered that was what I needed to do after an hour of tinkering. Apparently this is not an issue with MongoDB, but then of course you have to set up and integrate your own Mongo instance…

Well they say that nothing worth doing is easy so let’s do this the proper way.

Setting Up Strapi With Mongo DB

The documentation for Strapi Databases is here. I would recommend that you take the extra time and effort to go the MongoDB route as I KNOW it is more stable than sqlite. And with the amount of time and effort we are going to go through to convert all 150+ of our blog posts to Markdown… we don’t want to be doing this again.

We could run a local instance of MongoDB, but then I need to manually start my server every time I restart my computer, so every day. Womp womp. I would also need to have some sort of backup solution. So it seems to me that the best thing to do is to create a MongoDB in the cloud and wire that in right from the start.

And this is where we descend into the 🔥 7th level of hell 🔥. In between the last paragraph and this one was a very frustrating 4 hours where I accomplished absolutely nothing. Like many things in life, we could solve this problem easily if we threw money at it. The best managed mongo repository is MongoDB Atlas, which for the minimum paid plan starts at a whopping $56+ dollars/month. They have a free tier… which has no backups. Most other cloud providers were equally expensive. With the exception of a Digital Ocean Droplet, which I can install MongoDB on with a backup for $6/month. That is a little more reasonable.

The issue comes in when we try to connect Strapi to this database. By default Digital Ocean has closed all ports except local on the installation. Which is making it incredibly woeful to actually connect. I won’t make you follow me down the road of sadness but I’ll come back when I actually have a solution.

After considerable hemming and hawing over this, I’ve decided that I NEED to have some sort of managed DB solution, I can’t be taking the time to do all my own database updates. I’ve decided that even the free tier of MongoDB Atlas is going to be more stable than my self run cluster on Digital Ocean. I did eventually figure out the connection issues I was having, but the amount of time I spent on this made me realize that dammit Jim, I’m a web developer, not a sysadmin!

And after struggling for several more hours trying to config my database file myself I actually followed the directions here and it worked perfectly.