Creating a TypeScript NodeJS Express App using TypeORM and MySQL – Part1

This tutorial explores how we can create a Node.js application written in TypeScript and connect to a MySQL database using TypeORM package.

TypeORM is an Object Relational Mapper (ORM) for Node.js written in TypeScript that can be used with TypeScript or JavaScript (ES5, ES6, ES7). It support latest JavaScript features and provide features that help you to develop any kind of applications that use database – from small applications with a few tables to large scale enterprise applications.

TypeORM uses the Data Mapper pattern, unlike all other JavaScript ORMs that currently exist, which means you can write loosely coupled, scalable, maintainable applications with fewer problems.

The benefit of using TypeORM for the programmer is the ability to focus on the business logic and worry about persistence only as a secondary problem.

You can download the source code from here.

The second part of this tutorial is available in below link

Creating a TypeScript NodeJS Express App using TypeORM and MySQL – Part2

There are other posts which explains more advanced topics like cascade save and inner joins.  Please refer below links for that.

TypeORM Express Application Using Repository Pattern – Part 1 – Create Tables with Relationships

TypeORM Express Application Using Repository Pattern – Part 2 – Persist Data into Tables

TypeORM Express Application Using Repository Pattern – Part 3 – How to perform InnerJoin

TypeORM Express Application Using Repository Pattern – Part 4 – How to do Cascade Save

Prerequisites
Install Node.js
Install VS Code
Install MySQL Workbench

Getting TypeScript

TypeScript itself is simple to add to any project with npm.

If you’re using VS Code then you’re good to go! VS Code will detect and use the TypeScript version you have installed in your node_modules folder. For other editors, make sure you have the corresponding TypeScript plugin.

Create App Folder and package.json file

Create a new folder ‘TypeScript-NodeJS-Express-TypeORM-Seed’ and open the folder in command prompt.
Create a new node project using npm init command.
This will create a package.json file in the app folder.

Configure TypeScript for the project

You can pass options to the TypeScript compiler by either by using the CLI, or a special file called tsconfig.json. By using this configuration file, we are telling TypeScript things like the build target (can be ES5, ES6, and ES7 at the time of this writing), what module system to expect, where to put the build JavaScript files, or whether it should create source-maps as well.

Add a new file ‘tsconfig.json’ in the folder ‘TypeScript-NodeJS-Express-Seed’ and add below contents into it.

compilerOptionsDescription
"module": "commonjs"The output module type (in your .js files). Node uses commonjs, so that is what we use
"target": "es6"The output language level. Node supports ES6, so we can target that here
"noImplicitAny": trueEnables a stricter setting which throws errors when something has a default any value
"moduleResolution": "node"TypeScript attempts to mimic Node's module resolution strategy.
"sourceMap": trueWe want source maps to be output along side our JavaScript. See the debugging section
"outDir": "dist"Location to output .js files after compilation
"baseUrl": "."Part of configuring module resolution

Install Packages

Install Typings

Project Structure

The project structure is as shown below.

NameDescription
distContains the distributable (or output) from your TypeScript build. This is the code which we run.
node_modulesContains all your npm dependencies
srcContains your source code that will be compiled to the dist dir
src/controllersControllers define functions that respond to various http requests
src/modelsModels that will be used in storing and retrieving data



Create app.ts – Entry point of the app and add below code in it

Create employee-controller.ts in ‘src/controllers’ folder and add below code in it

Compile and Run the App

Open command prompt and go the root folder of the app (HiveStoreAPI folder) and run the command tsc. This will compile the typescript files according to the typeconfig.json configuration. After compilation, the js files are generated in dist folder.

There is an option for TypeScript compiler  ‘watch’ which will watch for any changes in target directory and compiles the files automatically.
It’s better to run ‘tsc -watch’ in root directory in a new command window.

Now go the ‘dist’ folder. Here you can see the trasnspiled code files.

 Go to dist folder in command prompt and run the command ‘node app.js’

Below is a snapshot of the commands and results.

Now open a browser and browse the url  http://localhost:3000/GetEmployee.

Setup MySQL Database

We will be using MySQL workbench to create and interact with database. If you don’t have MySQL workbench installed, please install it from
here. Install both server and workbench. Open workbench and create a database ‘hivetestdb’.

The second part of this tutorial is available in below link

Creating a TypeScript NodeJS Express App using TypeORM and MySQL – Part2

The seed project is available in GitHub. You can download the source code here.

There are other posts which explains more advanced topics like cascade save and inner joins.  Please refer below links for that.

TypeORM Express Application Using Repository Pattern – Part 1 – Create Tables with Relationships

TypeORM Express Application Using Repository Pattern – Part 2 – Persist Data into Tables

TypeORM Express Application Using Repository Pattern – Part 3 – How to perform InnerJoin

TypeORM Express Application Using Repository Pattern – Part 4 – How to do Cascade Save

One comment on “Creating a TypeScript NodeJS Express App using TypeORM and MySQL – Part1

Leave a Reply

Your email address will not be published. Required fields are marked *