Connecting a NestJS app in a NX MonoRepo to a Digital Ocean MongoDB

Taylor Ackley
2 min readJul 23, 2021

Digital Ocean is a great platform that is super easy to use. They also have gotten in the habit of enforcing some best practices, and that includes forcing you to connect to your MongoDB instance with a certificate provided by Digital Ocean.

If you are running NestJS, it's pretty easy to get that certificate set up and connected to MongoDB.

Once you have created your MongoDB database, you should see an overview in the Digital Ocean app with some basic information about your database.

Among the sections, should be a Connection Details section, usually top-right.

Locate the blue link labeled “Download CA Certificate” and select it. When prompted, download your NestJS app assets folder. If you are in a NX monorepo, it will probably be located in apps/api/src/assets. It should be at the same level as main.ts. Confirm the certificate is there and named ca-certificate.crt.

IMPORTANT

For real applications with sensitive customer data, don’t commit your certificate to a git repo. Use a private location to host your certs, such as a Digital Ocean volume or somewhere else secure where you can request it via a secure channel and copy it your server with automation.

Open the dropdown that defaults to Connection Parameters and select Connection String instead.

Copy that connection string and in a text editor, replace this specific:

&tlsCAFile=%3Creplace-with-path-to-CA-cert%3E

with

&tlsCAFile=dist/apps/api/assets/ca-certificate.crt

If your NestJS app is NOT in a mono repo, the path may be slightly different based on your setup. You only need to know where the build command is going to output to in your repo, and reference the assets folder.

Commit your change, then add your connection string to your app's environment variables.

All done!

--

--