Nestjs and Mysql in 5 Minutes

A quick tutorial for building scalable Node.js applications

Shai Ben Shimol
4 min readJan 26, 2019

🔭In this tutorial I will show you how easy is to setup and execute a Netsjs project using the following tech stack:

Install and create Nestjs project

npm install -g typescript
npm install -g @nestjs/cli
nest new my-nest-projectc:\\> cd my-nest-project

Open “my-nest-project” in vscode with code . command

Project Structure

  • node_modules: include the packages modules
  • src: include the app source files
  • test: end-to-end test app
  • nest-cli.json: The root level of an Netsjs workspace provides workspace-wide and project-specific configuration defaults for build and development tools provided by the Nestjs. Path values given in the configuration are relative to the root workspace folder.
  • packge.json: lists the packages your project depends on.

Module, Service and Controller Meaning:

Modules — Architecture

Every Nestjs app has at least one @Module() class — root module. The root @Module() for an app is so named because it can include child @Module() in A hierarchy of any depth.

The most important properties are as follows:

  • imports: other modules whose exported classes are needed by component templates declared in this Module.
  • controllers: the set of controllers which have to be created.
  • providers: creators of services that this NgModule contributes to the global collection of services; they become accessible in all parts of the app.
  • exports: the subset of declarations that should be visible and usable in the component templates of other Modules.

Services

Service is a layer category encompassing any value, function, or feature that an app needs. A service is typically a class with a narrow, well-defined purpose. It should do something specific and do it well.

Controllers

A controller is a class that handles HTTP requests. The public methods of the Controller are called action methods or simply actions. When the Nestjs Framework receives a request, it routes the request to an action.

To determine which action to invoke, the framework uses a routing table.

Let’s Get Started

Create users module, service and controller.

nest g module users
nest g service users
nest g controller users
nest g class users/user.entity
In this section we’ll not use spec file.

Installing Mysql And typeORM

npm install --save @nestjs/typeorm typeorm mysql

Time to write some code!

  • Open user.entity.ts file and Type
  • Open users.service.ts and type
  • Open users.controller.ts and type
  • Create ormconfig.json file in the root project with the following attributes
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "root",
"database": "my_nestjs_project",
"entities": ["src/**/**.entity{.ts,.js}"],
"synchronize": true
}
  • Open users.module.ts file and it looks like
  • Now open app.module.ts and import the database config file

Open terminal in vscode and run

npm run start
Nest compilation

Testing via POSTMAN

  • Create User
  • Get User by id

Conclusion

We made a good progress in this very first part of building Nestjs application. We got most of the architecture of Nestjs decisions.

My ko-fi

🍺Cheers! 🍺

--

--

Shai Ben Shimol
Shai Ben Shimol

Written by Shai Ben Shimol

Software engineer, Software Architect Specialist in Web and Mobile development...

Responses (10)