a shot of dev knowledge

Related tags

How to use a database URL parser in NodeJs

One of the points in The Twelve-Factor App is Config.

An app’s config is everything that is likely to vary between deploys (i.e., staging, production, developer environments, etc). Most applications that require persistent storage use databases. Applications connect to databases using certain parameters and credentials that are usually stored in a type of Config. The database config usually includes a database driver, port, name, username and password.

You may have seen one or more of these env variables in projects:

  • DB_CONNECTION
  • DB_HOST
  • DB_PORT
  • DB_USER
  • DB_PASSWORD
  • DB_DATABASE

These separate env variables can be combined into a single variable using a URL parser. parse-database-url is a simple NodeJs library that parses database configurations passed in as URLs.

You can name the single combined variable DATABASE_URL. The structure of the DATABASE_URL is:

DATABASE_URL=driver://username:password@hostname:port/database_name

An example is:

DATABASE_URL=mysql://root:password123@localhost:3306/app_db

To use the library, simply install with npm:

npm install parse-database-url

Import the library and parse the DATABASE_URL:

const parseDbUrl = require("parse-database-url");
const dbConfig = parseDbUrl(process.env.DATABASE_URL);

dbConfig is an object with database configurations. You can apply this to wherever you set Config in your application.

{
  "driver": "mysql",
  "user": "root",
  "password": "password123",
  "host": "localhost",
  "port": "3306",
  "database": "app_db"
}

By using this process, you reduce the number of env variables.

Note: There are some known issues with this method. If your database password contains some special characters, it’ll break the parsing and the result will be inconsistent. Also, if you put a non-existing driver, the library will not validate it.

Related tags

RELATED COURSES
View all Courses
Related Courses
Related Courses
View all Courses

Keep Exploring