A lightweight web framework for Node.js apps powered by InversifyJS, TypeORM and Express
npm install zafiro







:warning: :construction: This library is under construction :construction: :warning:
This step-by-step guide will guide your through the Zafiro basics.
You can install Zafiro using npm:
``sh`
npm install zafiro reflect-metadata
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:
`ts
import "reflect-metadata";
import { createApp } from "zafiro";
import { appBindings } from "./config/ioc_config";
import { expressConfig } from "./config/express_config";
import { CustomAccountRepository } from "./repositories/account_repository";
(async () => {
try {
const app = await createApp({
database: "postgres",
containerModules: [appBindings],
expressConfig: expressConfig,
AccountRepository: CustomAccountRepository
});
app.listen(
3000,
() => console.log(
"Example app listening on port 3000!"
)
);
} catch (e) {
console.log(e.message);
}
})();
`
You can learn more about all the available configuration options in the wiki page about configuration.
A Zafiro application expects the following environment variables to be available:
- The DATABASE_HOST variable should contain the network address of your database.DATABASE_PORT
- The variable should contain the network port of your database.DATABASE_USER
- The variable should contain the user name of your database.DATABASE_PASSWORD
- The variable should contain the user password of your database.DATABASE_DB
- The variable should contain the name of your database.
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.
> :warning: Please note that each entity and each controller in your application must be defined on its own file and be exported using a default ES6 export.
You can define an entity as follows:
`ts
import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";
@Entity()
export default class DirectMessage {
@PrimaryGeneratedColumn()
id: number;
@Column()
senderId: number;
@Column()
recipientId: number;
@Column()
content: string;
@CreateDateColumn()
createdDate: Date;
}
`
The Entity API in Zafiro is powered by TypeORM API:
A Repository will be generated automatically at runtime. The repository API is powered by TypeORM.
You can access a Repository by injection it into a Controller, BaseMiddleware, AuthProvider, etc. First you need to declare a type identifier for the repository that you wish to inject:
`ts`
const TYPE = {
DirectMessageRepository: Symbol.for("Repository
};
Then you can inject it using the @inject annotation:
`t`
@inject(TYPE.DirectMessageRepository) private readonly _dmRepository: Repository
The Dependency Injection API in Zafiro is powered by InversifyJS.
The Repository API in Zafiro is powered by TypeORM API:
You can declare a controller as follows:
`ts
import { injectable, inject } from "inversify";
import { controller, httpGet, BaseHttpController } from "inversify-express-utils";
import { TYPE } from "./contants/types";
@controller("/direct_message")
class UserPreferencesController extends BaseHttpController {
@inject(TYPE.DirectMessageRepository) private readonly _dmRepository: Repository
@httpGet("/")
public async get() {
if (!this.httpContext.user.isAuthenticated()) {
this.httpContext.res.status(403).send("Forbidden");
} else {
return await this._dmRepository.find({
recipientId: this.httpContext.details.id;
});
}
}
}
``
The Controllers API in Zafiro is powered by inversify-express-utils.
I created Zafiro because I love working with JavaScript and Node.js but I miss the boilerplate-automation and the type-safety productivity boost that I have experienced while working with other technologies in the past.

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.