Skip to main content

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 Context object using the appropriate environment configuration and hooks.
  • Hook Registration: Lets you register hook classes per environment using the @hook decorator.
  • 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-specific Hooks instances.


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 __name property (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 Context instance 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 Hooks instance if not already initialized.
  • Passes relevant flags (e.g., installing) to the Context.

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