Component
A Component is the main building block of your application.
In your application, everything is a Component:
Definition
import { Component, type Context } from "@tymber/core";
export class MyComponent extends Component {
doSomething(ctx: Context) {
// ...
}
}
A component can also have dependencies:
import { Component, INJECT, type Context } from "@tymber/core";
import { MyOtherComponent } from "./MyOtherComponent";
export class MyComponent extends Component {
static [INJECT] = [MyOtherComponent];
constructor(private readonly myOtherComponent: MyOtherComponent) {
super();
}
doSomething(ctx: Context) {
return this.myOtherComponent.doSomeWork(ctx);
}
}
See also: Dependency Injection
Registration
import { type Module, type AppInit } from "@tymber/core";
import { MyComponent } from "./MyComponent";
export const MyModule: Module = {
name: "my-module",
version: "1.2.3",
init(app: AppInit) {
app.component(MyComponent);
},
};
info
Each component will be instantiated once during the application startup.
Context
Context is a special object that contains information about the current context:
- user information
- database transaction details
- tracing details
It must be explicitly passed as the first argument to all public methods of your component, to be passed throughout the chain:
Endpoint.handle(ctx)
└─┬ Service.doSomething(ctx, data)
└─┬ Repository.findById(ctx, id)
└── DB.query(ctx)
Lifecycle
There are two lifecycle methods you can override:
init(): executed on application startupclose(): executed on application shutdown
import { Component, type Context } from "@tymber/core";
export class MyComponent extends Component {
async init() {
// ...
}
async close() {
// ...
}
}