Config

The Obsidian framework provides a centralized configuration. You can set this configuration from various place in your application and access it from everywhere.

Configuration Topology

Obsidian configuration is splitted in 3 categories:

  • obsidian: this is the framework configuration itself,
  • app: this is your application global configuration,
  • modules: module-specific configuration.

When you export the obsidian configuration, you will obtain an object that contains those 3 categories:

{
    "obsidian": {},
    "app": {},
    "modules": {
        "module1": {},
        "module2": {}
    }
}

Setting Application Base Configuration

You can set the base configuration of the application by passing it to the start method:

const obsidian = require("@obsidian/obsidian");

const app = obsidian("my-application");
// ... load modules ...
app.start({
    obsidian: {
        debug: true,
    },
    app: {
       lang: "en_US",
    },
});

Defining Module’s Default Configuration

When you declare an Obsidian module, you can define its default configuration:

module.exports = {

    name: "my-module",
    requires: [],

    config: {},  // Module's default config

    load(app) {},
    unload(app) {}

};

Overriding Module’s Configuration

It is possible to override a module’s configuration when you declare it in your application:

const obsidian = require("@obsidian/obsidian");
const hello = require("my-hello-module");

const app = obsidian("my-application");

app.use(hello, {
    config: {         // <- module specific configuration
        foo: "bar",
    }
});

app.start();

Accessing Configuration

You can access the configuration from everywhere in you application via app.config.

For example, to access the debug key in the obsidian part of the configuration, you can use the following code:

app.config.get("@obsidian.debug");

The parameter we gave to the get() method is the path of the configuration. The path can be absolute (when starting with @) or relative.

Absolute Paths

Absolute paths always starts with @ followed with the category of configuration or a module name:

  • @obsidian. to access the framework configuration,
  • @app. to access the application global configuration,
  • @myModule. to access to the configuration of the my-module module.

E.g:

app.config.get("@obsidian.debug");
app.config.get("@app.lang");
app.config.get("@dataStore.foo");

Relative Paths

Relative path can be used inside modules to access to the module’s own configuration. For example, if you are in the my-module module, you can access any configuration of the module like this:

app.config.get("foo");

This is equivalent to:

app.config.get("@myModule.foo");

API Reference

Note

“Base config” is the configuration as it is passed at module initialization-time. “Custom config” represent user-defined configuration (set at runtime).

class Config(rootConfig)

Handle Obsidian application and modules configuration.

Config.get(path="\"\"", default_)

Get config located at given path.

Arguments:
  • path (string) – The path of the wanted config (default "")
  • default_ – The default value returned if the path does not exists (default: undefined)
Returns:

the requested config

Config.set(path, value)

Set config located at given path.

Arguments:
  • path (string) – The path of the wanted config
  • value – The value to set
Config.dump(onlyCustom=false)

Dump the config.

Arguments:
  • onlyCustom (boolean) – Set it to true to only dump custom configuration (configuration setted manualy using the set() method, default = false)
Returns:

Object – The dumped config

Config.load(config, custom=false)

Load the config from the given object.

Arguments:
  • config (Object) – The config to load
  • custom (boolean) – Mark loaded configuration as custom (default = false)
Config.setApp(app)

Define the (sub)application this module will work with.

Arguments:
  • app (Application) – The application or sub-application.