Develop API with Node.js/Express & MySQL database

Hi, I am currently pursuing the MERN stack. I’ve learnt developing REST APIs using Express & Mongo DB (mongoose library).

Now, I want to learn developing APIs using the MySQL database (mysql package from npm).

I have looked for some resources but I can’t any satisfactory one. Please help me in finding any such resource that explains concepts in details, preferably video tutorials.

MySQL database is a relational database and the data is stored in tables as rows and columns. This storage is different in MongoDB database where data is stored in documents within a collection. The way the stored data is accessed is using SQL for MySQL and MQL (MongoDB Query Language) for MongoDB.

The concepts for programming remain the same - connecting to the database, creating tables, data and accessing it (performing CRUD (Create, Read, Update, Delete) operations), but the syntax, and the way you design the data differ. Relational database data is stored in normalized form. Document databases allow both normalized and denormalized forms of data.

The tools to access and query the data differ and the database access APIs used in the apps (in this case the NodeJS) are there for each of them. So, the NPM package for MSQL is “mysql”, you can install and use it in your code.

Since, you are already familiar with the ExpressJS and NodeJS, you can try coding using the mysql library APIs. One of the aspects about MySQL programming is that, you need to know how to write SQL. This is important. The NodeJS mysql library has APIs which require the knowledge of SQL programming.

I can only suggest that first install a copy of MySQL on your computer, find a tool to access it (like MySQL Workbench or a command-line tool ‘mysql’) and start creating some data and querying it. Next, try the same from your NodeJS app.

1 Like

Hi, Prasadsaya, thanks for the response. I am already familiar with SQL. I have also used it in a couple of projects which I built using different programming languages.

I read a bit of docs of the mysql package on NPM, I was confused how the pooling and all worked. So, I wanted to learn those parts. Thanks, again.

For simple apps, you probably don’t have to think much about the connection pooling - the default configurations work fine. You create a single connection to the database - and do your CRUD queries. But, the default pool is there unseen . For larger applications it is important to understand this concept and how it affects the application and the database access.

Reference: Connection pool - Wikipedia

1 Like

Thanks, man. Much appreciated. Have a good day :slight_smile: