MySQL Database with Node.js

Jake Cyr
Oct 2, 2020

This article is going to overview how to make database queries to a MySQL database from a Node.js application.

Setup before getting started:

Commands for installations above:

npm i --save dotenv
npm i --save prohairesis

In your root code folder create a ‘.env’ file with the following contents (replacing the value with your database connection string):

DATABASE_CONNECTION_STRING=mysql://abc

Now we’re ready to start coding!

Let’s do a simple database query from our Users SQL table:

You can also run prepared statements to avoid SQL injection like so:

There are a bunch of other helpful methods on the Prohairesis database instance you can use. For example, you can use the getOne method to return a single record as an object or null if it wasn’t found. See the interface below for all methods:

And that’s it! If you have any questions feel free to leave a comment below.

Checkout my video on YouTube covering this topic:

--

--

Jake Cyr

Proficient in AI and cloud tech, advancing systems development with a commitment to continual growth.