If you’re developing web applications, you’re almost certainly going to be constantly interacting with a database. And when it comes time to select the _way _you’ll interact, the choices can be overwhelming.

In this article, we’re going to look in detail at 5 different ways to interact with your database using JavaScript, and we’ll talk about the pros and cons of each. We’ll start with the lowest-level choice — SQL Commands — then move through to higher-level abstractions.

Choosing the right database library for your JavaScript application can have a big impact on the maintainability, scalability, and performance of your code, so it’s worth spending some time to figure out your options.

Our Sample Application

We’re going to use a trivial Express application hosted on Heroku as our example. All the code for this article is in this GitHub repository. Feel free to clone it and follow along.

Pre-requisites

To run the sample application, you’ll need the following software on your machine:

  • A unix-like terminal environment (Mac OSX and Linux are fine. If you’re using Windows, you’ll need the Windows Subsystem for Linux).
  • git (and a github account).
  • npm (version 6 or later).
  • The Heroku command-line tool.

If you don’t have a Heroku account already, you’ll need to sign up for a free account. If you don’t want to sign up for Heroku, you can also run the application locally against a local Postgres instance. If you’re comfortable with that, it should be pretty easy to see what changes you need to make instead of deploying to Heroku.

Once you’ve installed all the above, run heroku login in a terminal, and you’re ready to get started.

Build and Deploy the Hello World App

To start, we’ll set up the following:

  • A trivial Express application that just serves a “Hello, World” web page.
  • Postgres database.
  • Two tables, representing “users” and “comments” (a user has many comments).
  • Some sample data (in this case, generated via mockaroo.com).

#javascript #database #heroku #sql #mysql

Top 5 Ways to Query your Relational Database in JavaScript [Part 1]
3.50 GEEK