Application
Application class serves as the central utility for managing the lifecycle of a WP-Node application. It provides a unified way to configure environments, initialize runtime contexts, manage hooks, and terminate resources.
Description
This static-only class handles:
- Context Initialization: Initializes and returns a
Contextobject using the appropriate environment configuration and hooks. - Hook Registration: Lets you register hook classes per environment using the
@hookdecorator. - Configuration Management: Stores global and environment-specific configuration used at runtime.
- Graceful Shutdown: Cleans up database connections when the app exits.
This class cannot be instantiated and is intended to be used statically.
Properties
-
installing: boolean
Indicates whether the application is currently in an installation process. -
configs: Configs
Global map of environment-specific configuration objects. -
config: Config
A single fallback configuration object used when environment-specific ones are not provided. -
hooks: Map<string, Hooks>(getter)
Returns the internal map of environment-specificHooksinstances.
Methods
registerHooks(clazzes: Constructor[], env?: string): void
Registers one or more hook classes for a given environment.
clazzes: Array of hook class constructors decorated with@hook.env: Optional. Defaults to"default". Specifies the environment the hooks apply to.- Throws: If any class lacks a
__nameproperty (set by@hook).
getContext(env?: string): Promise<Context>
Initializes and returns the runtime Context object for the specified environment.
env: Optional. Defaults to"default".- Returns: A Promise that resolves to a
Contextinstance with configuration and hooks initialized. - Throws: If configuration is missing or invalid.
Behavior:
- Loads default hooks using
DefaultHooks.load(env). - Merges default hooks into environment-specific ones.
- Creates and caches the
Hooksinstance if not already initialized. - Passes relevant flags (e.g.,
installing) to theContext.
terminate(): void
Closes all active database connections gracefully.
- Internally calls
Database.closeAll().
Usage
await Application.getContext(); // Retrieves app context with hooks and config
Application.registerHooks([MyHookClass]); // Registers hooks for default env
Application.terminate(); // Closes DB connections on exit