(Type) ORM — TypeScript

Dávid Csejtei
The Startup

--

In the last couple of years I used ORM (Object-Relational Mapping) in my Node.js projects a lot. Every time when I had to work with any relational databases I just grapped SequelizeORM to make my life easier. Unfortunately, even though Sequelize has TypeScript support according to its documentation, my experience wasn’t good at all with that. If you ask me, Sequelize is a great option in Node.js if you don’t use TypeScript, but I had to find a better option for my TypeScripted projects. Two day ago I read this library’s name somewhere: TypeORM.

I wish I had a library which:

  • REALLY uses TypeScript
  • has easy configuration process
  • supports advanced code splitting (DAO-s or Repository-s, models, migrations, etc.)
  • supports multiple database engines
  • has some kind of cli support (create models / entities, etc.)

TypeORM — first impressions

First of all, I went over the official documentation. It was easy to follow and it had enough examples as well. After that I created a new Node.js project with Typescript and Express and installed TypeORM. I read about the CLI support of TypeORM, but I wanted to do things manually for the first time to understand every little details.

My goal was to make a POC project to check how many points could fulfilled from my wish list. This little project can connect to an existing relational database running on a PostgreSQL engine and get all the rows from a table called user. Moreover, the project provides an API to work with.

Here is my POC (Proof of Concept) process:

  1. Added the ormconfig.json configuration file in the project root

2. I made a db folder in my project root and add the following directories into that: entities, migrations, repositories and subscribers.

3. Built up the database connection and initialize an express instance in my application entry point (index.ts) using the ormconfig.json file.

4. Created the first entity file (db/entities/User.ts):

5. Made a custom repository for Users (db/repositories/UserRepository.ts):

6. Created a custom subscriber for User entity just to try this feature too (db/subscribers/UserSubscriber.ts):

7. Made my first controller endpoint (controllers/GetAllUsers.ts):

8. Extends index.ts with my first endpoint on GET users/all route (in the index.ts):

9. Despite I said I didn’t want to use the CLI support of TypeORM making the migration file for User was an exception. That’s because all migrations have to start with a timestamp, so it was easier to generate a migration file through the CLI application:
typeorm migration:create -n DefaultUsers -d src/db/migrations.

I didn’t use migration so just leaved empty in this POC. After I read the migration part of the documentation it was obvious that migrations could do what I need that for.

10. Started application and called the /users/all endpoint to make sure that it was working… and it worked smoothly. Perfect!

Conclusion — TypeORM seems usable

The POC proved me that the TypeORM can be a good solution for the ORM problem in TypeScript. Let’s check my wish list again:

  • REALLY uses TypeScript 🗹
  • has easy configuration process 🗹
  • supports advanced code splitting (DAO-s or Repository-s, models, migrations, etc.) 🗹
  • supports multiple database engines 🗹
  • has some kind of cli support (create models / entities, etc.) 🗹

To be honest, I can’t wait to apply this library in my next project.

--

--

Dávid Csejtei
The Startup

Senior Full-Stack Software Engineer and Instructor