Moject is an IoC container and an app factory package built around the modules idea of Angular and NestJs.
npm install moject Use @Module decorator to define modules, @Injectable and @Inject decorators to define and inject dependencies.
See examples.
Each module is an isolated DI container. Modules can be used to group related dependencies. Moject doesn't dictate how to group dependencies, you can use module-per-feature, module-per-layer or any other approach.
@Module decorator is used to define a module:
@Module({imports: [/* modules to import */],providers: [/* providers */],exports: [/* providers and modules to export */]})exportclassAppModule{staticasyncbeforeStart(){// invoked before application starts}staticasyncbeforeInit(){// invoked before modules initialisation}asyncafterInit(){// invoked after modules initialisation}asyncbeforeStop(){// invoked before application stops}asyncafterStop(){// invoked after application stops}}There are 3 types of providers: class, factory and value:
@Module({providers: [// Class provider{identifier: 'APP_CLASS',useClass: AppClass,},// Shortened syntax for class providerAppClass,// Factory provider{identifier: 'FACTORY',useFactory: ()=>{returnnewAppClass();},},// Factory provider{identifier: 'FACTORY',useFactory: ()=>{returnnewAppClass();},},// Value provider{identifier: 'VALUE',useValue: 'value to inject',},],})classAppModule{}Each class provider must be marked with @Injectable decorator as shown below:
@Injectable()classAppClass{}Use @Inject decorator to inject a dependency by its identifier:
@Injectable()classSomeOtherClass{constructor(@Inject(AppClass)appClass: AppClass){// ...}}SINGLETONEA single instance of the provider is shared across the entire application. This is the default scope.TRANSIENTEach consumer that injects a transient provider will receive a new, dedicated instance.
@Module({providers: [// Singletone scope{identifier: SomeClass,useClass: SomeClass,scope: 'SINGLETONE'},// Transient scope{identifier: SomeOtherClass,useClass: SomeClassClass,scope: 'TRANSIENT'},],})classAppModule{}Each module can import other modules and export its providers or imported modules.
// Databse module
@Module({providers: [DB],exports: [DB],})classDatabaseModule{}// App module
@Module({imports: [DatabaseModule],})classAppModule{// Inject a provider imported from DatabaseModuleconstructor(@Inject(DB)privategateway: DB){}asyncafterInit(){awaitthis.gateway.migrate();}}Use App class to initialise modules and start an app as shown below:
import{AppModule}from'./app.module';// Build appconstapp=App.create(AppModule);// Start appapp.start().catch(console.error);Once app.start is called, all your modules get initialised and dependencies resolved.
Moject provides you with a built-in logger, that can be used across the entire app:
import{AppLogger,IDENTIFIERS}from'moject';
@Module()classAppModule{constructor(@Inject(IDENTIFIERS.LOGGER)privatereadonlylogger: AppLogger){this.logger.log('Hello, world!');}}There is no need to import or provide the logger. Just inject it in your code as shown above.
If you use debug method of the logger, don't forget to set NODE_DEBUG env variable in order to see debug messages.
Moject is MIT licensed.