Skip to main content

Accessing TypeORM

If you want to assess the database, you must first understand that EzBackend is a wrapper of fastify and typeorm

Hence, when thinking about accessing the database, we should be thinking of accessing the database using typeorm

Understanding TypeORM

TypeORM is an Object Relational Mapper (ORM) for the SQL databases Postgres, MySQL, MariaDB, SQLite and Microsoft SQL server.

It lets you connect to any of the databases with the same code. Want to change from SQLite to Postgres? Just change the configuration and everything will still work.

Getting the TypeORM connection

You can get the TypeORM connection within the lifecycle hooks with instance.orm

const app = new EzBackend();

app.setHandler('Check TypeORM Connection', async (instance, opts) => {


The connection is only created in the postInit hook, so you can only access in lifecycle hooks after that.

Now that we have the TypeORM connection, we can solve all our problems by asking ourselves the question 'How do I use the TypeORM connection?'?

Accessing the TypeORM Repo

Both the classes EzModel and EzRepo decorate the instance with a repository directly linked to the model created. The repository is a TypeORM Repository

const app = new EzBackend();

const model = new EzModel('Model', {
//Or EzRepo
var1: Type.VARCHAR,
var2: Type.DATE,

model.setHandler('Check TypeORM Repo', async (instance, opts) => {

app.addApp('model', model, { prefix: 'model' });