TypeScript, InversifyJS, TypeORM and Express 🚀💎 Zafiro is a strongly typed and lightweight web framework for Node.js apps powered by
⚠️ 🚧 This library is under construction 🚧 ⚠️
This step-by-step guide will guide your through the Zafiro basics.
You can install Zafiro using npm:
npm install zafiro reflect-metadata
Creating an application
Zafiro exposes a function named
createApp that allows you to bootstrap an Express application in just a few minutes.
The application entry point look as follows:
You can learn more about all the available configuration options in the wiki page about configuration.
Required Environment Variables
A Zafiro application expects the following environment variables to be available:
DATABASE_HOSTvariable should contain the network address of your database.
DATABASE_PORTvariable should contain the network port of your database.
DATABASE_USERvariable should contain the user name of your database.
DATABASE_PASSWORDvariable should contain the user password of your database.
DATABASE_DBvariable should contain the name of your database.
Required Project Folders
A Zafiro application expects the following directory structure and convention:
/src/controllers/You must add your controllers unders this folder. The controllers are powerd by inversify-express-utils.
/src/entities/You must add your entities unders this folder. The entities are powerd by TypeORM.
⚠️ Please note that each entity and each controller in your application must be defined on its own file and be exported using a
Defining an Entity
You can define an entity as follows:
The Entity API in Zafiro is powered by TypeORM API:
Declaring a Repository
Repository<T> will be generated automatically at runtime. The repository API is powered by TypeORM.
You can access a
Repository<T> by injection it into a
AuthProvider, etc. First you need to declare a type identifier for the repository that you wish to inject:
Then you can inject it using the
@inject(TYPE.DirectMessageRepository) private readonly _dmRepository: Repository<DirectMessage>;
The Dependency Injection API in Zafiro is powered by InversifyJS.
The Repository API in Zafiro is powered by TypeORM API:
Declaring a Controller
You can declare a controller as follows:
The Controllers API in Zafiro is powered by inversify-express-utils.
Thanks to InversifyJS and TypeORM, Zafiro is able to automate a lot of the boilerplate required to create an Express application. As a first step, Zafiro is able to create a database connection, auto-generate the data repositories and inject them into your controllers:
Zafiro has been designed with the goal of providing a great developer experience.
I plan to continue working hard to:
- Make Zafiro very robust.
- Reduce the amount of required boilerplate.
- Improve the developer experience.
An example application is available at zafiro-realworld-example.