Example using TypeORM with Express

Initial setup

Let's create a simple application called "user" which stores users in the database and allows us to create, update, remove, and get a list of all users, as well as a single user by id within web api.

First, create a directory called "user":

mkdir user

Then switch to the directory and create a new project:

cd user
npm init

Finish the init process by filling in all required application information.

Now we need to install and setup a TypeScript compiler. Lets install it first:

npm i typescript --save-dev

Then let's create a tsconfig.json file which contains the configuration required for the application to compile and run. Create it using your favorite editor and put the following configuration:

{
    "compilerOptions": {
        "lib": ["es5", "es6", "dom"],
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
    }
}

Now let's create a main application endpoint - app.ts inside the src directory:

Let's add a simple console.log inside it:

Now it's time to run our application. To run it, you need to compile your typescript project first:

Once you compile it, you should have a src/app.js file generated. You can run it using:

You should see the, "Application is up and running" message in your console right after you run the application.

You must compile your files each time you make a change. Alternatively, you can set up watcher or install ts-nodearrow-up-right to avoid manual compilation each time.

Adding Express to the application

Let's add Express to our application. First, let's install the packages we need:

  • express is the express engine itself. It allows us to create a web api

  • @types/express is used to have a type information when using express

Let's edit the src/app.ts file and add express-related logic:

Now you can compile and run your project. You should have an express server running now with working routes. However, those routes do not return any content yet.

Adding TypeORM to the application

Finally, let's add TypeORM to the application. In this example, we will use mysql driver. Setup process for other drivers is similar.

Let's install the required packages first:

  • typeorm is the typeorm package itself

  • reflect-metadata is required to make decorators to work properly. Remember to import it before your TypeORM code.

  • mysql is the underlying database driver. If you are using a different database system, you must install the appropriate package

Let's create app-data-source.ts where we set up initial database connection options:

Configure each option as you need. Learn more about options here.

Let's create a user.entity.ts entity inside src/entity:

Let's change src/app.ts to establish database connection and start using myDataSource:

Now you should have a basic express application connected to MySQL database up and running.

Last updated