@dunx/core
v3.1.0
Published
DI container, modules, lifecycle and the injectable Logger contract for the dunx framework
Downloads
9,304
Maintainers
Readme
@dunx/core
The dependency injection container, modules, lifecycle, configuration, and the
Logger and RequestContext contracts for
dunx.
Zero dependencies. That is a constraint rather than a coincidence: it lets
@dunx/http inject a logger without pulling a logging implementation in behind
it.
Install
bun add @dunx/coreUsage
Constructor injection, with no annotation of any kind:
import { Module } from '@dunx/core';
export class UsersService {
constructor(private readonly repo: UsersRepository) {}
}
@Module({
providers: [UsersRepository, UsersService],
exports: [UsersService], // absent means nothing is exported
})
export class UsersModule {}
const app = await AppFactory.create(AppModule);No @Injectable, no @Inject, no reflect-metadata, no
experimentalDecorators. @dunx/transform reads each class's constructor
parameter types at load time, so an app opts in with one line of bunfig.toml:
preload = ["@dunx/transform/preload"]
[test]
preload = ["@dunx/transform/preload"]What is here
| Area | What it covers | Guide |
| ------------- | ------------------------------------------------------------ | ------------------------------------------------------ |
| Providers | Constructor injection, provide(), token(), inject() | Providers |
| Modules | Scoping, imports, exports, global, forRoot | Modules |
| Lifecycle | onInit, onBeforeShutdown, onShutdown, signal handlers | Lifecycle |
| Configuration | ConfigModule.forRoot({ validate }), one validation function | Configuration |
| Logging | The Logger contract and ConsoleLogger | Logging |
Notes
- The container is scoped. Every module reference is a scope holding what it
declares,
exportsis its public surface, andglobal: truepublishes those exports app-wide. An absentexportsexports nothing. - A parameter whose type erases - an interface, a primitive, a union, a
type-only import - is a boot error naming that parameter, not a silent
undefined. A parameter with a default keeps its default instead. - Two contracts are always resolvable:
Loggerdefaults toConsoleLoggerandRequestContexttoAsyncLocalStorage, so@dunx/httpcan log every request in an app that imported no logging module. A module binding either one wins. ConsoleLoggerbatchesinfoand below into one write per event-loop turn;warnand above are never batched and flush what is queued behind them.
License
MIT
