Configuration
You are reading the documentation for version 2 of FoalTS. Instructions for upgrading to this version are available here. The old documentation can be found here.
In FoalTS, configuration refers to any parameter that may vary between deploy environments (production, development, test, etc). It includes sensitive information, such as your database credentials, or simple settings, such as the server port.
The framework encourages a strict separation between configuration and code and allows you to define your configuration in environment variables, in .env
files and in files in the config/
directory.
Config directory structure
#
Configuration FilesConfiguration values are provided using configuration files in the config/
directory. Several formats are supported: YAML, JSON and JS files.
config/default.{yml|json|js}
- YAML
- JSON
- JS
YAML support
The use of YAML for configuration requires the installation of the package
yamljs
.When creating a new project, you can also add the flag
--yaml
to have all the configuration directly generated in YAML.The extension of the YAML files must be
.yml
.
#
Deployment EnvironmentsThe default configuration files are used regardless of the environment, i.e. regardless of the value assigned to the NODE_ENV
environment variable.
Configuration values can also be set or overridden for a specific environment using the filename syntax: config/<environment-name>.{json|yml|js}
. If no value is assigned to NODE_ENV
, the environment considered is development.
#
Reserved ParametersAll parameters under the keyword settings
are reserved for the operation of the framework. You can assign values to those given in the documentation, but you cannot create new ones.
- YAML
- JSON
- JS
#
Accessing Configuration ValuesThe Config
class provides two static methods get
and getOrThrow
for reading configuration values.
Config.get
method#
The This function takes the configuration key as first parameter.
The algorithm below is used to retrieve the configuration value:
- Return the value specified in the environment config file if it exists.
- Return the value specified in the default config file it is exists.
- Return
undefined
otherwise.
#
Specifying a typeThe method also accepts a second optional parameter to define the type of the returned value.
When it is set, Foal checks that the configuration value has the correct type and if it does not, it will try to convert it to the desired type (e.g. "true"
becomes true
). If it does not succeed, a ConfigTypeError
is thrown.
Allowed types |
---|
string |
number |
boolean |
boolean|string |
number|string |
any |
#
Specifying a default valueThe third optional parameter of the method allows you to define a default value if none is found in the configuration.
Config.getOrThrow
method#
The This method has the same behavior as Config.get
except that it does not accept a default value. If no value is found, the method will throw a ConfigNotFoundError
.
.env
Files#
Environment Variables and Configuration files in the config/
directory are usually committed and therefore should not contain sensitive information (such as database credentials).
The recommended approach to provide sensitive information to the application is to use environment variables and .env
files which are not committed. Then, in the configuration files, the values are retrieved.
.env
- YAML
- JSON
- JS
If the same variable is provided both as environment variable and in the .env
file, then the value of the environment variable is used.
#
Deployment EnvironmentsJust like the configuration files in the config/
directory, the .env
files can be used for several environments: .env.production
, .env.test
, etc.