0.4.1 • Published 5 months ago
armony v0.4.1
Armony
Yet another typescript framework for Node.js application
Armony is a small framework to use on your typescript backend projects to easily add some cool features:
- Dependency injection with customizable binding
- Singleton logic
- Bootable provider logic
- Observer pattern
- Quick config files loading
Install
Armony use pnpm, but you can install it with any package manager:
$ pnpm i armony
$ npm i armony
$ yarn add armony
Typical Usage
The main purpose of armony is to standardize your base application logic between backend typescript projects. It provides a binding for classes for dependency injection
import { Armony, ArmonyConfig, Environment, Injection, LoggerConfig } from 'armony';
import winston from 'winston';
// Customize default config
const armonyConfig = new ArmonyConfig({
env: (process.env.NODE_ENV as Environment) || 'production',
debug: false,
locale: 'fr',
timezone: 'UTC'
});
// As a good practice, Armony forces you to have a logger
// Default one is Winston
const loggerConfig = new LoggerConfig({
transports: [
new winston.transports.Console({
level: 'debug',
format: winston.format.combine(winston.format.colorize(), winston.format.simple(), winston.format.timestamp()),
})
],
});
// Instanciate your armony app with its config
const armony = new Armony(armonyConfig);
// Add any other configuration file
armony.setConfig(loggerConfig);
// Create your first provider
// Armony will be automatically injected if you add the @Injection decorator
@Injection
class MyFirstProvider {
constructor(private armony: Armony) {}
public foo() {
console.log('armony:');
console.log('env: ', this.armony.config.env);
console.log('locale: ', this.armony.config.locale);
}
}
// Create any other provider
// Dependencies will be injected automatically if you add @Injection decorator
@Injection
class MySecondProvider {
constructor(private armony: Armony, private firstProvider: MyFirstProvider) {}
public foo() {
this.firstProvider.foo();
}
}
// Declare providers to armony
armony.bindProvider(MyFirstProvider);
armony.bindProvider(MySecondProvider);
// Boot armony
armony.boot().then(() => {
// use Armony as a factory that will inject dependencies for you
const mySecondProvider = armony.provider(MySecondProvider);
mySecondProvider.foo();
});