What I learned about external configuration of the database connection in NestJS

This page summarizes the projects mentioned and recommended in the original post on dev.to

SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.
surveyjs.io
featured
InfluxDB - Power Real-Time Data Analytics at Scale
Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
www.influxdata.com
featured
  • buzzz

  • As part of my 100 day of code challenge, I am building a web app using NestJS (and a not yet specified frontend framework). So one of the first things I tried setting up was the database connection. I plan on using a mongodb and because I might switch between different devices for development, I decided to sign up for a free account on a shared cluster at mongodb.com.

  • dotenv

    Loads environment variables from .env for nodejs projects.

  • So instead of reading the next few paragraphs in the database part of the NestJS documentation, I started to search for options on how to do this configuration outside of the code, in a configuration- or environment-file, that I simply don’t have to commit. So instead of just reading about the ormconfig.json file, that is a way to configure TypeORM I looked for different options. And I found one, the config module of NestJS. It internally uses dotenv, which is used to load the content of a file called .env into the Node process.env. This is a common approach in Node.js and is useful to quickly swap the applications environment. The .env-file holds key-value pairs to represent the environment. The configuration module of NestJS offers different ways to access and to store environment variables, ranging from different locations for the .env-file, to ignore the file and only use the runtime environment variables or even use a custom environment file, that allows for a more complex structure such as yaml or json. In this case, the values are parsed into key-value pairs for use in the process.env. There is a lot more advanced options for more complex situations, but these are the basic options, that the module offers for a simpler use case.

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
  • TypeORM

    ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.

  • I also discovered the option to configure TypeORM from the environment variables directly, by using the keys TypeORM provides. This would work for me, but there is one thing, that is not possible with this configuration (and most likely I won’t need it for this project, but better having it, than needing it, right?). I can not have more than one database configured.

  • Mongoose

    MongoDB object modeling designed to work in an asynchronous environment.

  • Since I’m new to NestJS (and developing with Node as a backend in general) I started this, by following the frameworks documentation. Although there is support for mongoose(link: https://mongoosejs.com), I decided to go with TypeORM. TypeORM is an Object Relational Mapper, that is written in TypeScript and since NestJS offers an integration, I felt like this would be a good choice. Another reason is, that I have no experience with mongodb, so if, for some reason, I decide, I won’t be using it for this project, it seems like it will be much easier changing databases with TypeORM, than if I would have used mongoose.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts

  • Node Abstract Repository for MongoDB

    5 projects | dev.to | 4 Jun 2023
  • Prisma in 500 Seconds

    4 projects | dev.to | 9 Feb 2023
  • Which ORM are you using with Node?

    2 projects | /r/node | 6 Jan 2023
  • Feedback on experiences with TypeORM vs the other contenders

    4 projects | /r/typescript | 21 Aug 2021
  • Best Node.js ORM?

    2 projects | /r/node | 30 Jun 2021