Configuration
Configuration is the foundation of every application. From database connections and authentication providers to cache drivers and external services, nearly every part of your application relies on configuration values. Bejibun provides a centralized configuration system that allows you to:- Organize application settings
- Separate configuration from business logic
- Manage environment-specific values
- Access settings consistently throughout your application
- Keep sensitive information out of source code
Configuration Philosophy
Configuration should be predictable, maintainable, and environment-aware. A common mistake is hardcoding values directly into application code. Avoid:The Config Directory
All application configuration files are stored inside theconfig directory.
Example:
Creating Configuration Files
A configuration file exports a configuration object. Example:Accessing Configuration Values
Use the Config service to retrieve values.Configuration Structure
A typical configuration file might look like:Environment Variables
Configuration files often depend on environment variables. Example:| Environment | Purpose |
|---|---|
| Development | Local development |
| Testing | Automated tests |
| Staging | Pre-production validation |
| Production | Live applications |
Default Values
Always provide sensible defaults when appropriate. Example:Application Configuration
The application configuration file contains general settings. Example:- Application name
- Environment
- Debug mode
- Timezone
- Locale
Database Configuration
Database connections are typically defined insideconfig/database.ts.
Example:
Authentication Configuration
Authentication settings belong in a dedicated configuration file. Example:Cache Configuration
Cache drivers and related settings can be configured separately. Example:Storage Configuration
Storage configuration defines where files are stored. Example:Service Configuration
External integrations should have their own configuration file. Example:Environment-Based Configuration
Different environments often require different values. Development:Typed Configuration
One of the advantages of TypeScript is the ability to create strongly typed configuration. Example:- Autocomplete
- Compile-time validation
- Better editor support
- Safer refactoring
Organizing Large Applications
As applications grow, configuration files can become extensive. Instead of:Avoiding Common Mistakes
Hardcoding Secrets
Avoid:Accessing Environment Variables Everywhere
Avoid:Mixing Business Logic and Configuration
Avoid:Configuration Caching
As applications grow, repeatedly loading configuration can become inefficient. Future framework versions may provide configuration caching. Example:- Faster startup
- Reduced file access
- Improved production performance
Recommended Practices
Follow these guidelines when working with configuration:- Keep configuration centralized
- Use environment variables for secrets
- Provide sensible defaults
- Create dedicated configuration files
- Prefer type-safe configuration
- Avoid hardcoded values
- Separate configuration from business logic
Example Configuration Flow
The following diagram illustrates how configuration values are loaded.What’s Next?
Now that you understand how configuration works, the next step is learning how environment variables are loaded and managed. Continue to:- Environment Variables
- Request Lifecycle
