Member-only story
Handling File Uploads With NestJS and MySQL
Connect to a MySQL database using TypeORM, create an entity, and upload images to the Nestjs application.

Introduction
Many developers despise dealing with file uploads. This can be attributed to a lack of knowledge about the best approach to take or difficulties determining how to configure their Nest.js application to handle file uploads. Many people may want to save their files directly to a MySQL database, or save image names and have the image saved on disk storage: it all depends on their preferences and the goals they want to achieve. This tutorial will teach you how to build a file uploading functionality using Nestjs and MySQL.
Prerequisites
Before you begin following this tutorial, ensure your system meets the following requirements:
- Your system is running Node.js with version 14 or later.
- Your system has a MySQL database installed.
- You have installed Postman.
Setting Up NestJS
Once the above-mentioned requirements are met, proceed to install the Nestjs CLI and create a new project by running the following commands:
$ npm i -g @nestjs/cli
$ nest new file-upload
These commands will install the Nestjs CLI and create a new Nestjs project with the folder structure below.
📦file-upload
┣ 📂src
┃ ┣ 📜app.controller.spec.ts
┃ ┣ 📜app.controller.ts
┃ ┣ 📜app.module.ts
┃ ┣ 📜app.service.ts
┃ ┣ 📜image.entity.ts
┃ ┗ 📜main.ts
┣ 📂test
┃ ┣ 📜app.e2e-spec.ts
┃ ┗ 📜jest-e2e.json
┣ 📜.eslintrc.js
┣ 📜.gitignore
┣ 📜.prettierrc
┣ 📜README.md
┣ 📜nest-cli.json
┣ 📜package-lock.json
┣ 📜package.json
┣ 📜tsconfig.build.json
┗ 📜tsconfig.json
After the Nestjs project has been created, move on to the next step — install the required dependencies for your application by running the following command:
npm install --save @nestjs/typeorm typeorm mysql2
In the above command, you’ve installed the TypeORM and mysql2 modules: they will enable you to connect your application to a MySQL database and perform operations on…