Start TypeORM project with Database Offline
By: Marcos De Aguiar
Tags: Nodejs, Typeorm, Expressjs, Typescript

I was always a big fan of ORM frameworks, they help us deal with application data at a higher level and abstract away the tedious persistence part. Some may criticize the performance of ORM’s, but IMHO if you’re using it to insert and update only, it’s already a big win, you can always plug in your optimized queries later on.

I already had good experiences with Hibernate, nHibernate and Entity Framework. When I started out to code in Node.js/Express, I started to look for a good ORM for this platform as well. Since I’m coding with Typescript, TypeORM with the DataMapper pattern appealed to me best.

Problem:

When I started to see some code samples for TypeORM, one thing called my attention. In most of these samples, the code was trying to open a connection, and then passing the connection object to the controllers, services etc., as seen in the code below.

But what happens when the database server is not online, and you still need to deploy and start your web server? This is not an issue with Entity Framework for example.

The solution:

Ideally I would like to have the Repositories injected as dependencies in my services, but repositories can only be retrieved after a successful connection is established. Interesting enough, if the database goes offline, and then online again, there is no issue with the repositories. They just stop and start working accordingly.

In Nest.js they have this well solved, because they use a factory method for the connection, and injects the repositories on the fly. But since I’m using Express, I had to come up with another solution.

I created a file that keeps trying to connect to the database, until it connects once, then it breaks out of the loop. If the connection fails, it just waits 1 second and tries again. Once the connection is made, there is nothing to worry about if it drops later or not. The repositories just recover without any issue as far as I tested.

For the app starting code, just call the method and let it loop until it connects.

For the services that need the repository, just make kind of a factory method, that checks if the repository was created already, if not, creates and cache it. If it was already created, it just returns the cached instance.

As we can see, the downside is that the repos are not constructor injected in the services that use them, which can get in the way of testing. But I’m willing to do this trade off, since the repositories are straighout database access in TypeORM, and it also would be a big pain to mock it given the sheer number of chained methods it uses, so I would go straight to integration testing with it.

Conclusion:

In the end, this is just a work around, I don’t know if it’s like this by design or not. I would like to see the thoughts of TypeORM’s team about this issue, and if they intend to implement some kind of lazy connection creation, that let us create repositories even if the database is not online at the moment. The main use case for this, is that Node.js is a stand alone process, and it’s not guaranteed that the database will be accessible when we’re deploying it.