### Get Complete Configuration Tree with Config.all() Source: https://context7.com/adonisjs/config/llms.txt The `all` method retrieves the entire configuration structure as a JavaScript object. This is invaluable for debugging purposes or when a comprehensive view of all loaded configurations is required. It returns the complete configuration tree, allowing for inspection of all settings. ```typescript import { Config } from '@adonisjs/config' const config = new Config({ app: { appKey: 'secret', env: 'development', }, database: { host: 'localhost', port: 5432, }, cache: { driver: 'redis', }, }) // Get entire configuration tree const allConfig = config.all() console.log(allConfig) // Result: // { // app: { appKey: 'secret', env: 'development' }, // database: { host: 'localhost', port: 5432 }, // cache: { driver: 'redis' } // } // Useful for debugging console.log(JSON.stringify(config.all(), null, 2)) // Check configuration keys const configKeys = Object.keys(config.all()) // Result: ['app', 'database', 'cache'] ``` -------------------------------- ### Load Configuration Files with ConfigLoader Source: https://context7.com/adonisjs/config/llms.txt The ConfigLoader class automatically imports all configuration files (e.g., .js, .ts, .json) from a specified directory. It returns their exports merged into a single object tree. If the directory is missing or empty, it returns an empty object. ```typescript import { ConfigLoader } from '@adonisjs/config' // Using a URL path const loader = new ConfigLoader(new URL('./config', import.meta.url)) // Or using a string path const loaderFromString = new ConfigLoader('/path/to/config') // Load all configuration files const configTree = await loader.load() // Given config directory structure: // config/ // app.ts -> export default { appKey: 'secret', env: 'development' } // database.ts -> export default { host: 'localhost', port: 5432 } // server.ts -> export const port = 3333; export const host = '0.0.0.0' // Result: // { // app: { appKey: 'secret', env: 'development' }, // database: { host: 'localhost', port: 5432 }, // server: { port: 3333, host: '0.0.0.0' } // } // Returns empty object if directory is missing or empty const emptyConfig = await new ConfigLoader('./missing-dir').load() // Result: {} ``` -------------------------------- ### Retrieve Configuration Values with Config.get() Source: https://context7.com/adonisjs/config/llms.txt The Config.get() method retrieves configuration values using dot notation for nested properties. It accepts an optional default value to return if the key is not found. Generics can be used for type-safe retrieval. ```typescript import { Config } from '@adonisjs/config' const config = new Config({ app: { appKey: 'my-secret-key', logger: { driver: 'file', level: 'info', }, }, database: { mysql: { host: '127.0.0.1', port: 3306, }, }, }) // Access top-level config const appConfig = config.get('app') // Result: { appKey: 'my-secret-key', logger: { driver: 'file', level: 'info' } } // Access nested values using dot notation const logDriver = config.get('app.logger.driver') // Result: 'file' const dbHost = config.get('database.mysql.host') // Result: '127.0.0.1' // Get with default value for missing keys const cacheDriver = config.get('cache.driver', 'memory') // Result: 'memory' (key doesn't exist) // Returns undefined for missing keys without default const missing = config.get('nonexistent.key') // Result: undefined // Type-safe retrieval with generics interface LoggerConfig { driver: string level: string } const loggerConfig = config.get('app.logger') // Result: { driver: 'file', level: 'info' } with proper typing ``` -------------------------------- ### Check Configuration Key Existence with Config.has() Source: https://context7.com/adonisjs/config/llms.txt The `has` method checks if a configuration key exists, returning true even for falsy values like `false`, `null`, or empty strings. It supports nested keys using dot notation and is useful for conditional logic in configuration. ```typescript import { Config } from '@adonisjs/config' const config = new Config({ app: { debug: false, name: null, version: '', }, database: { mysql: { host: 'localhost', }, }, }) // Check existing keys (including falsy values) console.log(config.has('app.debug')) // Result: true (value is false) console.log(config.has('app.name')) // Result: true (value is null) console.log(config.has('app.version')) // Result: true (value is '') // Check nested keys console.log(config.has('database.mysql')) // Result: true console.log(config.has('database.mysql.host')) // Result: true // Check non-existent keys console.log(config.has('cache')) // Result: false console.log(config.has('database.postgres')) // Result: false console.log(config.has('app.nonexistent')) // Result: false // Useful for conditional configuration if (config.has('mail.smtp')) { // Configure SMTP transport } else { // Use default transport } ``` -------------------------------- ### Set Default Configuration Values with Config.defaults() Source: https://context7.com/adonisjs/config/llms.txt The `defaults` method establishes default values for configuration keys. These defaults are merged with existing configurations, prioritizing user-defined values. This is particularly useful for package authors to provide sensible defaults that can be easily overridden by application developers. ```typescript import { Config } from '@adonisjs/config' const config = new Config({ app: { logger: { driver: 'file', }, }, }) // Set defaults - user values override defaults config.defaults('app.logger', { driver: 'console', // This will be overridden by existing 'file' level: 'info', // This will be added as default filePath: '/logs', // This will be added as default }) console.log(config.get('app.logger')) // Result: { driver: 'file', level: 'info', filePath: '/logs' } // Note: 'driver' remains 'file' (user value), other defaults are merged // Set defaults for missing configuration config.defaults('cache', { driver: 'memory', ttl: 3600, }) console.log(config.get('cache')) // Result: { driver: 'memory', ttl: 3600 } // Useful for package authors providing sensible defaults config.defaults('myPackage', { enabled: true, retries: 3, timeout: 5000, }) // Users can override any of these in their config files ``` -------------------------------- ### Update Configuration Values with Config.set() Source: https://context7.com/adonisjs/config/llms.txt The Config.set() method updates configuration values in memory using dot notation. These changes are not persisted to disk and only affect the runtime configuration. It can update existing values, replace entire objects, or create new nested keys. ```typescript import { Config } from '@adonisjs/config' const config = new Config({ app: { logger: { driver: 'file', level: 'info', }, }, database: { host: 'localhost', }, }) // Update existing nested value config.set('app.logger.driver', 'console') console.log(config.get('app.logger.driver')) // Result: 'console' // Replace entire nested object config.set('app.logger', { driver: 'memory', level: 'debug' }) console.log(config.get('app.logger')) // Result: { driver: 'memory', level: 'debug' } // Set new keys that don't exist config.set('cache.driver', 'redis') config.set('cache.ttl', 3600) console.log(config.get('cache')) // Result: { driver: 'redis', ttl: 3600 } // Update deeply nested paths (creates intermediate objects) config.set('services.email.smtp.host', 'smtp.example.com') console.log(config.get('services')) // Result: { email: { smtp: { host: 'smtp.example.com' } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.