-
Notifications
You must be signed in to change notification settings - Fork 2
Configuration
Configuring a middleware, a model or a controller, is the same process, and here we will use the generic term "resource" to talk about such objects.
Configurations are written in JSON or YAML in a file passed to the config member of the argument of the Rig constructor.
The structure of the object mirrors the dot-notation used for the names when registering corresponding resource.
E.g.
/** server.js */
var Rig = require('rig'),
rig = new Rig({ config: 'config.json' });
rig.register('models.myModel', require('models/myModel'));# config.json
{
"models": {
"myModel": { "foo": "bar" }
}
}In this example the function exported by the myModel node module will be called with { "foo": "bar" } and the result of this call will be stored in the registry.
WARNING: Always use 4 spaces to indent your configuration files (we use node's
requireand it's doesn't like tabs too much when applied to a json or yaml file).
##Non Configurable Resources
If you call rig.register with a name that has no associated configuration/corresponding key in the config file and a function as the second argument, the resource will be skipped and no registration will take place. If your resource takes no configuration, you must still list its name in the config file and pass it null. This is useful in cases where you register resources in bulk (all models in '/models') but only want to select a few of them (see the API). See Registry for more information on registration.
##Default/Shared configuration You can specify default values for the objects passed to resources. This can be useful to not have to repeat a property that you want all middleware or all modules to have for eg.
# config.json
{
"models": {
"*": {
"hello": "world"
},
"myModel": { "foo": "bar" },
"myOtherModel": {
"sacre": "bleu",
"hello": "real world"
}
}
}In the above example, the configuration for myModel will be
{
"hello": "default world",
"foo": "bar"
}but members of configurations for a specific resource prevail, so the configuration for myOtherModel will be
{
"sacre": "bleu",
"hello": "real world"
}##Runtime Configuration It can happen that you need to pass a runtime object to be able to configure a resource. See Registry.register for more information on that case.