moject
v1.0.0
Published
Moject is a dependency injection and app factory package built around the modules idea of Angular and NestJs.
Downloads
11
Readme
Moject
Moject
is an IoC container and an app factory package built around the modules idea of Angular and NestJs.
Usage
npm install moject
Use @Module
decorator to define modules, @Injectable
and @Inject
decorators to define and inject dependencies.
Examples
See examples.
Modules
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 */]
})
export class AppModule {
static async beforeStart() {
// invoked before application starts
}
static async beforeInit() {
// invoked before modules initialisation
}
async afterInit() {
// invoked after modules initialisation
}
async beforeStop() {
// invoked before application stops
}
async afterStop() {
// invoked after application stops
}
}
Providers
There are 3 types of providers: class
, factory
and value
:
@Module({
providers: [
// Class provider
{
identifier: 'APP_CLASS',
useClass: AppClass,
},
// Shortened syntax for class provider
AppClass,
// Factory provider
{
identifier: 'FACTORY',
useFactory: () => {
return new AppClass();
},
},
// Factory provider
{
identifier: 'FACTORY',
useFactory: () => {
return new AppClass();
},
},
// Value provider
{
identifier: 'VALUE',
useValue: 'value to inject',
},
],
})
class AppModule {}
Each class provider must be marked with @Injectable
decorator as shown below:
@Injectable()
class AppClass {}
Use @Inject
decorator to inject a dependency by its identifier:
@Injectable()
class SomeOtherClass {
constructor(@Inject(AppClass) appClass: AppClass) {
// ...
}
}
Injection scopes
SINGLETONE
A single instance of the provider is shared across the entire application. This is the default scope.TRANSIENT
Each 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'
},
],
})
class AppModule {}
Import/Exoprt
Each module can import other modules and export its providers or imported modules.
// Databse module
@Module({
providers: [DB],
exports: [DB],
})
class DatabaseModule {}
// App module
@Module({
imports: [DatabaseModule],
})
class AppModule {
// Inject a provider imported from DatabaseModule
constructor(@Inject(DB) private gateway: DB) {}
async afterInit() {
await this.gateway.migrate();
}
}
App Factory
Use App
class to initialise modules and start an app as shown below:
import { AppModule } from './app.module';
// Build app
const app = App.create(AppModule);
// Start app
app.start().catch(console.error);
Once app.start
is called, all your modules get initialised and dependencies resolved.
Logger
Moject
provides you with a built-in logger, that can be used across the entire app:
import { AppLogger, IDENTIFIERS } from 'moject';
@Module()
class AppModule {
constructor(@Inject(IDENTIFIERS.LOGGER) private readonly logger: 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.
License
Moject is MIT licensed.