Overview

The config-default plugin loads configuration from YAML files and provides validated, typed configuration to all plugins. All BSB implementations share the same configuration file format.

Configuration Interface

  • this.config - Access validated configuration
  • Schema validation with defaults
  • Environment variable overrides via BSB_ prefix
  • Type-safe access to configuration values

Configuration File

Configuration is loaded from sec-config.yaml in your project root. This format is consistent across all BSB implementations:

sec-config.yaml
plugins:
  service-hello:
    enabled: true
    greeting: "Hello"
    maxRetries: 3

  events-default:
    enabled: true

  logging-default:
    enabled: true
    level: debug

Environment Variables

BSB uses specific environment variables for system-level configuration:

# Deployment Configuration
BSB_PROFILE="production"           # Deployment profile name
BSB_REGION="us-east-1"            # Deployment region
BSB_APP_ID="my-service"           # Application identifier

# File Paths
APP_DIR="/path/to/app"            # Application working directory
BSB_CONFIG_FILE="/etc/bsb.yaml"   # Custom config file location
BSB_PLUGIN_DIR="/usr/local/plugins" # Plugin directory

# Config Plugin Override
BSB_CONFIG_PLUGIN="config-custom"        # Override config plugin
BSB_CONFIG_PLUGIN_PACKAGE="@org/config"  # Override config plugin package

# Node.js Standard
NODE_ENV="production"             # Environment mode (production/development)

Note: These are system-level settings. Plugin-specific configuration is defined in your config files (bsb.yaml, bsb.production.yaml, etc.), not via environment variables.

Plugin Configuration

Define a Zod schema and access configuration via this.config:

import { BSBService, BSBServiceConstructor } from "@bettercorp/service-base";
import { z } from "zod";

// Define your configuration schema
const configSchema = z.object({
  greeting: z.string().default("Hello"),
  maxRetries: z.number().default(3),
  database: z.object({
    host: z.string().default("localhost"),
    port: z.number().default(5432)
  }).optional()
});

export class Config {
  static validationSchema = configSchema;
}

export class Plugin extends BSBService<typeof configSchema> {
  constructor(context: BSBServiceConstructor) {
    super(context);
  }

  async init(): Promise<void> {
    // Type-safe access to configuration
    console.log(this.config.greeting);     // "Hello" or env override
    console.log(this.config.maxRetries);   // 3 or env override

    // Optional nested config
    if (this.config.database) {
      console.log(this.config.database.host);
    }
  }
}

Schema Validation

Zod provides runtime validation with helpful error messages:

const configSchema = z.object({
  // Required string
  apiKey: z.string(),

  // Number with constraints
  timeout: z.number().min(1000).max(30000).default(5000),

  // Enum values
  logLevel: z.enum(["debug", "info", "warn", "error"]).default("info"),

  // Arrays
  allowedOrigins: z.array(z.string()).default([]),

  // Complex nested objects
  redis: z.object({
    host: z.string().default("localhost"),
    port: z.number().default(6379),
    password: z.string().optional()
  }).optional()
});

Go implementation coming soon.

Rust implementation coming soon.

Python implementation coming soon.

When to Replace

Replace config-default when you need:

  • Remote configuration (Consul, etcd, AWS Parameter Store)
  • Dynamic configuration updates without restart
  • Encrypted secrets management
  • Configuration versioning and rollback