Define and implement core constructs such as Application and Component for LoopBack 4 framework
npx @tessl/cli install tessl/npm-loopback--core@7.0.0LoopBack Core (@loopback/core) is the foundational framework for building LoopBack 4 applications. It provides the core constructs including Application containers, Component systems, Server abstractions, and lifecycle management. The package includes a complete dependency injection system re-exported from @loopback/context, enabling fast, scalable, and extensible Node.js applications and microservices.
npm install @loopback/coreimport { Application, Component, Server } from "@loopback/core";For specific functionality:
import {
Application,
Component,
lifeCycleObserver,
service,
extensionPoint,
extensions
} from "@loopback/core";CommonJS:
const { Application, Component, Server } = require("@loopback/core");import { Application, Component } from "@loopback/core";
// Create an application
const app = new Application({
name: 'my-app',
shutdown: {
signals: ['SIGTERM'],
gracePeriod: 5000
}
});
// Register a component
class MyComponent implements Component {
controllers = [MyController];
providers = {
'services.logger': LoggerProvider
};
}
app.component(MyComponent);
// Start the application
await app.start();
console.log('Application is running');
// Graceful shutdown
process.on('SIGTERM', async () => {
await app.stop();
});LoopBack Core is built around several key architectural patterns:
Core application class that serves as the main container for components, servers, controllers, and services. Extends Context to provide dependency injection throughout the application.
class Application extends Context implements LifeCycleObserver {
constructor(config?: ApplicationConfig, parent?: Context);
constructor(parent: Context);
// Component registration
component<T extends Component = Component>(
componentCtor: Constructor<T>,
nameOrOptions?: string | BindingFromClassOptions
): Binding<T>;
// Server management
server<T extends Server>(
ctor: Constructor<T>,
nameOrOptions?: string | BindingFromClassOptions
): Binding<T>;
getServer<T extends Server>(target: Constructor<T> | string): Promise<T>;
// Controller registration
controller<T>(
controllerCtor: ControllerClass<T>,
nameOrOptions?: string | BindingFromClassOptions
): Binding<T>;
// Service registration
service<S>(
cls: ServiceOrProviderClass<S>,
nameOrOptions?: string | ServiceOptions
): Binding<S>;
// Lifecycle management
init(): Promise<void>;
start(): Promise<void>;
stop(): Promise<void>;
readonly state: string;
}
interface ApplicationConfig {
name?: string;
shutdown?: ShutdownOptions;
[prop: string]: any;
}Modular architecture for packaging and contributing functionality to applications. Components can declare controllers, providers, servers, and other resources.
interface Component {
controllers?: ControllerClass[];
providers?: ProviderMap;
classes?: ClassMap;
servers?: { [name: string]: Constructor<Server> };
lifeCycleObservers?: Constructor<LifeCycleObserver>[];
services?: ServiceOrProviderClass[];
bindings?: Binding[];
components?: Constructor<Component>[];
}
function mountComponent(app: Application, component: Component): void;Coordinated lifecycle management for application startup and shutdown sequences. Provides hooks for initialization, start, and stop phases.
interface LifeCycleObserver {
init?(...injectedArgs: unknown[]): ValueOrPromise<void>;
start?(...injectedArgs: unknown[]): ValueOrPromise<void>;
stop?(...injectedArgs: unknown[]): ValueOrPromise<void>;
}
function lifeCycleObserver(group?: string, ...specs: BindingSpec[]): ClassDecorator;
class LifeCycleObserverRegistry implements LifeCycleObserver {
getObserverGroupsByOrder(): LifeCycleObserverGroup[];
init(): Promise<void>;
start(): Promise<void>;
stop(): Promise<void>;
}Service discovery and dependency injection system for registering and consuming services throughout the application.
function service(
serviceInterface?: ServiceInterface,
metadata?: InjectionMetadata
): PropertyDecorator & ParameterDecorator;
function createServiceBinding<S>(
cls: ServiceOrProviderClass<S>,
options?: ServiceOptions
): Binding<S>;
type ServiceInterface = string | symbol | Function;Plugin architecture enabling applications to define extension points and register extensions dynamically.
function extensionPoint(name: string, ...specs: BindingSpec[]): ClassDecorator;
function extensions(
extensionPointName?: string,
metadata?: InjectionMetadata
): PropertyDecorator & ParameterDecorator;
function addExtension(
context: Context,
extensionPointName: string,
extensionClass: Constructor<unknown>,
options?: BindingFromClassOptions
): Binding<unknown>;Complete IoC container system with hierarchical contexts, binding management, and type-safe dependency injection.
class Context {
bind<ValueType = BoundValue>(key: BindingAddress<ValueType>): Binding<ValueType>;
get<ValueType>(keyWithPath: BindingAddress<ValueType>): Promise<ValueType>;
getSync<ValueType>(keyWithPath: BindingAddress<ValueType>): ValueType;
}
function inject(
bindingSelector?: BindingSelector<unknown>,
metadata?: InjectionMetadata
): PropertyDecorator & ParameterDecorator;
function injectable<T>(
...specs: BindingSpec[]
): ClassDecorator;Context & Dependency Injection
Core type definitions used throughout the LoopBack Core system:
// Application types
interface ApplicationMetadata extends JSONObject {
name: string;
version: string;
description: string;
}
interface ShutdownOptions {
signals?: NodeJS.Signals[];
gracePeriod?: number;
}
// Component types
interface ProviderMap {
[key: string]: Constructor<Provider<BoundValue>>;
}
interface ClassMap {
[key: string]: Constructor<BoundValue>;
}
// Server types
interface Server extends LifeCycleObserver {
readonly listening: boolean;
}
// Controller and service types
type ControllerClass<T = any> = Constructor<T>;
type ServiceOrProviderClass<T = any> =
| Constructor<T | Provider<T>>
| DynamicValueProviderClass<T>;
// Mixin utility
type MixinTarget<T extends object> = Constructor<{
[P in keyof T]: T[P];
}>;
// Lifecycle types
interface LifeCycleObserverGroup {
group: string;
bindings: Readonly<Binding<LifeCycleObserver>>[];
}
interface LifeCycleObserverOptions {
orderedGroups: string[];
disabledGroups?: string[];
parallel?: boolean;
}
// Core binding keys and tags
namespace CoreBindings {
const APPLICATION_INSTANCE: BindingKey<Application>;
const APPLICATION_CONFIG: BindingKey<ApplicationConfig>;
const APPLICATION_METADATA: BindingKey<ApplicationMetadata>;
const SERVERS: string;
const COMPONENTS: string;
const CONTROLLERS: string;
const CONTROLLER_CLASS: BindingKey<ControllerClass>;
const CONTROLLER_METHOD_NAME: BindingKey<string>;
const CONTROLLER_METHOD_META: string;
const CONTROLLER_CURRENT: BindingKey<any>;
const LIFE_CYCLE_OBSERVERS: string;
const LIFE_CYCLE_OBSERVER_REGISTRY: BindingKey<LifeCycleObserverRegistry>;
const LIFE_CYCLE_OBSERVER_OPTIONS: BindingKey<LifeCycleObserverOptions>;
}
namespace CoreTags {
const COMPONENT: string;
const SERVER: string;
const CONTROLLER: string;
const SERVICE: string;
const SERVICE_INTERFACE: string;
const LIFE_CYCLE_OBSERVER: string;
const LIFE_CYCLE_OBSERVER_GROUP: string;
const EXTENSION_FOR: string;
const EXTENSION_POINT: string;
}