npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zafiro

v1.0.0-alpha.14

Published

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

Downloads

40

Readme

:gem: Zafiro is a strongly typed and lightweight web framework for Node.js apps powered by TypeScript, InversifyJS, TypeORM and Express :rocket:

Join the chat at https://gitter.im/inversify/InversifyJS npm version Build Status Dependencies img Known Vulnerabilities Twitter Follow

:warning: :construction: This library is under construction :construction: :warning:

The Basics

This step-by-step guide will guide your through the Zafiro basics.

Installation

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:

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.

Required Environment Variables

A Zafiro application expects the following environment variables to be available:

  • The DATABASE_HOST variable should contain the network address of your database.
  • The DATABASE_PORT variable should contain the network port of your database.
  • The DATABASE_USER variable should contain the user name of your database.
  • The DATABASE_PASSWORD variable should contain the user password of your database.
  • The 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.

: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.

Defining an Entity

You can define an entity as follows:

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:

Declaring a Repository

A 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 Controller, BaseMiddleware, AuthProvider, etc. First you need to declare a type identifier for the repository that you wish to inject:

const TYPE = {
    DirectMessageRepository: Symbol.for("Repository<DirectMessage>")
};

Then you can inject it using the @inject annotation:

@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:

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<DirectMessage>;

    @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.

Why Zafiro?

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.

Example Application

An example application is available at zafiro-realworld-example.